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