377710b488c43daf41893c2d5a250a22c8957750
[phpeclipse.git] /
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.preferences;
12
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;
23
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.PlatformUI;
40 import org.eclipse.ui.dialogs.PropertyPage;
41
42 /**
43  * Property page used to configure project specific compiler settings
44  */
45 public class CompilerPropertyPage extends PropertyPage {
46
47         private CompilerConfigurationBlock fConfigurationBlock;
48
49         private Control fConfigurationBlockControl;
50
51         private ControlEnableState fBlockEnableState;
52
53         private SelectionButtonDialogField fUseWorkspaceSettings;
54
55         private SelectionButtonDialogField fChangeWorkspaceSettings;
56
57         private SelectionButtonDialogField fUseProjectSettings;
58
59         private IStatus fBlockStatus;
60
61         public CompilerPropertyPage() {
62                 fBlockStatus = new StatusInfo();
63                 fBlockEnableState = null;
64
65                 IDialogFieldListener listener = new IDialogFieldListener() {
66                         public void dialogFieldChanged(DialogField field) {
67                                 doDialogFieldChanged(field);
68                         }
69                 };
70
71                 fUseWorkspaceSettings = new SelectionButtonDialogField(SWT.RADIO);
72                 fUseWorkspaceSettings.setDialogFieldListener(listener);
73                 fUseWorkspaceSettings.setLabelText(PreferencesMessages
74                                 .getString("CompilerPropertyPage.useworkspacesettings.label")); //$NON-NLS-1$
75
76                 fChangeWorkspaceSettings = new SelectionButtonDialogField(SWT.PUSH);
77                 fChangeWorkspaceSettings.setLabelText(PreferencesMessages
78                                 .getString("CompilerPropertyPage.useworkspacesettings.change")); //$NON-NLS-1$
79                 fChangeWorkspaceSettings.setDialogFieldListener(listener);
80
81                 fUseWorkspaceSettings.attachDialogField(fChangeWorkspaceSettings);
82
83                 fUseProjectSettings = new SelectionButtonDialogField(SWT.RADIO);
84                 fUseProjectSettings.setDialogFieldListener(listener);
85                 fUseProjectSettings.setLabelText(PreferencesMessages
86                                 .getString("CompilerPropertyPage.useprojectsettings.label")); //$NON-NLS-1$
87         }
88
89         /*
90          * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
91          */
92         public void createControl(Composite parent) {
93                 super.createControl(parent);
94                 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
95                                 IJavaHelpContextIds.COMPILER_PROPERTY_PAGE);
96         }
97
98         /*
99          * @see org.eclipse.jface.preference.IPreferencePage#createContents(Composite)
100          */
101         protected Control createContents(Composite parent) {
102                 IStatusChangeListener listener = new IStatusChangeListener() {
103                         public void statusChanged(IStatus status) {
104                                 fBlockStatus = status;
105                                 doStatusChanged();
106                         }
107                 };
108                 fConfigurationBlock = new CompilerConfigurationBlock(listener,
109                                 getProject());
110
111                 Composite composite = new Composite(parent, SWT.NONE);
112                 GridLayout layout = new GridLayout();
113                 layout.marginHeight = 0;
114                 layout.marginWidth = 0;
115                 layout.numColumns = 2;
116                 composite.setLayout(layout);
117
118                 fUseWorkspaceSettings.doFillIntoGrid(composite, 1);
119                 LayoutUtil.setHorizontalGrabbing(fUseWorkspaceSettings
120                                 .getSelectionButton(null));
121
122                 fChangeWorkspaceSettings.doFillIntoGrid(composite, 1);
123
124                 fUseProjectSettings.doFillIntoGrid(composite, 2);
125
126                 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
127                                 | GridData.VERTICAL_ALIGN_FILL);
128                 data.horizontalSpan = 2;
129
130                 fConfigurationBlockControl = fConfigurationBlock
131                                 .createContents(composite);
132                 fConfigurationBlockControl.setLayoutData(data);
133
134                 boolean useProjectSettings = fConfigurationBlock
135                                 .hasProjectSpecificOptions();
136
137                 fUseProjectSettings.setSelection(useProjectSettings);
138                 fUseWorkspaceSettings.setSelection(!useProjectSettings);
139
140                 updateEnableState();
141                 Dialog.applyDialogFont(composite);
142                 return composite;
143         }
144
145         private boolean useProjectSettings() {
146                 return fUseProjectSettings.isSelected();
147         }
148
149         private void doDialogFieldChanged(DialogField field) {
150                 if (field == fChangeWorkspaceSettings) {
151                         String id = "net.sourceforge.phpdt.ui.preferences.CompilerPreferencePage"; //$NON-NLS-1$
152                         CompilerPreferencePage page = new CompilerPreferencePage();
153                         showPreferencePage(id, page);
154                 } else {
155                         updateEnableState();
156                         doStatusChanged();
157                 }
158         }
159
160         /**
161          * Method statusChanged.
162          */
163         private void doStatusChanged() {
164                 updateStatus(useProjectSettings() ? fBlockStatus : new StatusInfo());
165         }
166
167         /**
168          * Method getProject.
169          */
170         private IJavaProject getProject() {
171                 return (IJavaProject) getElement().getAdapter(IJavaElement.class);
172         }
173
174         private void updateEnableState() {
175                 if (useProjectSettings()) {
176                         if (fBlockEnableState != null) {
177                                 fBlockEnableState.restore();
178                                 fBlockEnableState = null;
179                         }
180                 } else {
181                         if (fBlockEnableState == null) {
182                                 fBlockEnableState = ControlEnableState
183                                                 .disable(fConfigurationBlockControl);
184                         }
185                 }
186         }
187
188         /*
189          * @see org.eclipse.jface.preference.IPreferencePage#performDefaults()
190          */
191         protected void performDefaults() {
192                 if (useProjectSettings()) {
193                         fUseProjectSettings.setSelection(false);
194                         fUseWorkspaceSettings.setSelection(true);
195                         fConfigurationBlock.performDefaults();
196                 }
197                 super.performDefaults();
198         }
199
200         /*
201          * @see org.eclipse.jface.preference.IPreferencePage#performOk()
202          */
203         public boolean performOk() {
204                 return fConfigurationBlock.performOk(useProjectSettings());
205         }
206
207         private void updateStatus(IStatus status) {
208                 setValid(!status.matches(IStatus.ERROR));
209                 StatusUtil.applyToStatusLine(this, status);
210         }
211
212         private boolean showPreferencePage(String id, IPreferencePage page) {
213                 final IPreferenceNode targetNode = new PreferenceNode(id, page);
214
215                 PreferenceManager manager = new PreferenceManager();
216                 manager.addToRoot(targetNode);
217                 final PreferenceDialog dialog = new PreferenceDialog(getControl()
218                                 .getShell(), manager);
219                 final boolean[] result = new boolean[] { false };
220                 BusyIndicator.showWhile(getControl().getDisplay(), new Runnable() {
221                         public void run() {
222                                 dialog.create();
223                                 dialog.setMessage(targetNode.getLabelText());
224                                 result[0] = (dialog.open() == Window.OK);
225                         }
226                 });
227                 return result[0];
228         }
229
230 }