1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.preferences;
13 import net.sourceforge.phpdt.core.IJavaElement;
14 import net.sourceforge.phpdt.core.IJavaProject;
15 import net.sourceforge.phpdt.internal.ui.IJavaHelpContextIds;
16 import net.sourceforge.phpdt.internal.ui.dialogs.StatusInfo;
17 import net.sourceforge.phpdt.internal.ui.dialogs.StatusUtil;
18 import net.sourceforge.phpdt.internal.ui.wizards.IStatusChangeListener;
19 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.DialogField;
20 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
21 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.LayoutUtil;
22 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.jface.dialogs.ControlEnableState;
26 import org.eclipse.jface.dialogs.Dialog;
27 import org.eclipse.jface.preference.IPreferenceNode;
28 import org.eclipse.jface.preference.IPreferencePage;
29 import org.eclipse.jface.preference.PreferenceDialog;
30 import org.eclipse.jface.preference.PreferenceManager;
31 import org.eclipse.jface.preference.PreferenceNode;
32 import org.eclipse.jface.window.Window;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.custom.BusyIndicator;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.ui.dialogs.PropertyPage;
40 import org.eclipse.ui.help.WorkbenchHelp;
43 * Property page used to configure project specific compiler settings
45 public class CompilerPropertyPage extends PropertyPage {
47 private CompilerConfigurationBlock fConfigurationBlock;
48 private Control fConfigurationBlockControl;
49 private ControlEnableState fBlockEnableState;
50 private SelectionButtonDialogField fUseWorkspaceSettings;
51 private SelectionButtonDialogField fChangeWorkspaceSettings;
52 private SelectionButtonDialogField fUseProjectSettings;
53 private IStatus fBlockStatus;
56 public CompilerPropertyPage() {
57 fBlockStatus= new StatusInfo();
58 fBlockEnableState= null;
60 IDialogFieldListener listener= new IDialogFieldListener() {
61 public void dialogFieldChanged(DialogField field) {
62 doDialogFieldChanged(field);
66 fUseWorkspaceSettings= new SelectionButtonDialogField(SWT.RADIO);
67 fUseWorkspaceSettings.setDialogFieldListener(listener);
68 fUseWorkspaceSettings.setLabelText(PreferencesMessages.getString("CompilerPropertyPage.useworkspacesettings.label")); //$NON-NLS-1$
70 fChangeWorkspaceSettings= new SelectionButtonDialogField(SWT.PUSH);
71 fChangeWorkspaceSettings.setLabelText(PreferencesMessages.getString("CompilerPropertyPage.useworkspacesettings.change")); //$NON-NLS-1$
72 fChangeWorkspaceSettings.setDialogFieldListener(listener);
74 fUseWorkspaceSettings.attachDialogField(fChangeWorkspaceSettings);
76 fUseProjectSettings= new SelectionButtonDialogField(SWT.RADIO);
77 fUseProjectSettings.setDialogFieldListener(listener);
78 fUseProjectSettings.setLabelText(PreferencesMessages.getString("CompilerPropertyPage.useprojectsettings.label")); //$NON-NLS-1$
82 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
84 public void createControl(Composite parent) {
85 super.createControl(parent);
86 WorkbenchHelp.setHelp(getControl(), IJavaHelpContextIds.COMPILER_PROPERTY_PAGE);
90 * @see org.eclipse.jface.preference.IPreferencePage#createContents(Composite)
92 protected Control createContents(Composite parent) {
93 IStatusChangeListener listener= new IStatusChangeListener() {
94 public void statusChanged(IStatus status) {
99 fConfigurationBlock= new CompilerConfigurationBlock(listener, getProject());
101 Composite composite= new Composite(parent, SWT.NONE);
102 GridLayout layout= new GridLayout();
103 layout.marginHeight= 0;
104 layout.marginWidth= 0;
105 layout.numColumns= 2;
106 composite.setLayout(layout);
108 fUseWorkspaceSettings.doFillIntoGrid(composite, 1);
109 LayoutUtil.setHorizontalGrabbing(fUseWorkspaceSettings.getSelectionButton(null));
111 fChangeWorkspaceSettings.doFillIntoGrid(composite, 1);
113 fUseProjectSettings.doFillIntoGrid(composite, 2);
115 GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL );
116 data.horizontalSpan= 2;
118 fConfigurationBlockControl= fConfigurationBlock.createContents(composite);
119 fConfigurationBlockControl.setLayoutData(data);
121 boolean useProjectSettings= fConfigurationBlock.hasProjectSpecificOptions();
123 fUseProjectSettings.setSelection(useProjectSettings);
124 fUseWorkspaceSettings.setSelection(!useProjectSettings);
127 Dialog.applyDialogFont(composite);
131 private boolean useProjectSettings() {
132 return fUseProjectSettings.isSelected();
135 private void doDialogFieldChanged(DialogField field) {
136 if (field == fChangeWorkspaceSettings) {
137 String id= "net.sourceforge.phpdt.ui.preferences.CompilerPreferencePage"; //$NON-NLS-1$
138 CompilerPreferencePage page= new CompilerPreferencePage();
139 showPreferencePage(id, page);
146 * Method statusChanged.
148 private void doStatusChanged() {
149 updateStatus(useProjectSettings() ? fBlockStatus : new StatusInfo());
155 private IJavaProject getProject() {
156 return (IJavaProject) getElement().getAdapter(IJavaElement.class);
159 private void updateEnableState() {
160 if (useProjectSettings()) {
161 if (fBlockEnableState != null) {
162 fBlockEnableState.restore();
163 fBlockEnableState= null;
166 if (fBlockEnableState == null) {
167 fBlockEnableState= ControlEnableState.disable(fConfigurationBlockControl);
173 * @see org.eclipse.jface.preference.IPreferencePage#performDefaults()
175 protected void performDefaults() {
176 if (useProjectSettings()) {
177 fUseProjectSettings.setSelection(false);
178 fUseWorkspaceSettings.setSelection(true);
179 fConfigurationBlock.performDefaults();
181 super.performDefaults();
185 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
187 public boolean performOk() {
188 return fConfigurationBlock.performOk(useProjectSettings());
191 private void updateStatus(IStatus status) {
192 setValid(!status.matches(IStatus.ERROR));
193 StatusUtil.applyToStatusLine(this, status);
196 private boolean showPreferencePage(String id, IPreferencePage page) {
197 final IPreferenceNode targetNode = new PreferenceNode(id, page);
199 PreferenceManager manager = new PreferenceManager();
200 manager.addToRoot(targetNode);
201 final PreferenceDialog dialog = new PreferenceDialog(getControl().getShell(), manager);
202 final boolean [] result = new boolean[] { false };
203 BusyIndicator.showWhile(getControl().getDisplay(), new Runnable() {
206 dialog.setMessage(targetNode.getLabelText());
207 result[0]= (dialog.open() == Window.OK);