032f3253b6a3d3bf16db094318e63ef62f68dbfa
[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.dialogs.PropertyPage;
40 import org.eclipse.ui.help.WorkbenchHelp;
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         private Control fConfigurationBlockControl;
49         private ControlEnableState fBlockEnableState;
50         private SelectionButtonDialogField fUseWorkspaceSettings;
51         private SelectionButtonDialogField fChangeWorkspaceSettings;
52         private SelectionButtonDialogField fUseProjectSettings;
53         private IStatus fBlockStatus;
54         
55
56         public CompilerPropertyPage() {
57                 fBlockStatus= new StatusInfo();
58                 fBlockEnableState= null;
59                 
60                 IDialogFieldListener listener= new IDialogFieldListener() {
61                         public void dialogFieldChanged(DialogField field) {
62                                 doDialogFieldChanged(field);
63                         }
64                 };
65                 
66                 fUseWorkspaceSettings= new SelectionButtonDialogField(SWT.RADIO);
67                 fUseWorkspaceSettings.setDialogFieldListener(listener);
68                 fUseWorkspaceSettings.setLabelText(PreferencesMessages.getString("CompilerPropertyPage.useworkspacesettings.label")); //$NON-NLS-1$
69
70                 fChangeWorkspaceSettings= new SelectionButtonDialogField(SWT.PUSH);
71                 fChangeWorkspaceSettings.setLabelText(PreferencesMessages.getString("CompilerPropertyPage.useworkspacesettings.change")); //$NON-NLS-1$
72                 fChangeWorkspaceSettings.setDialogFieldListener(listener);
73         
74                 fUseWorkspaceSettings.attachDialogField(fChangeWorkspaceSettings);
75
76                 fUseProjectSettings= new SelectionButtonDialogField(SWT.RADIO);
77                 fUseProjectSettings.setDialogFieldListener(listener);
78                 fUseProjectSettings.setLabelText(PreferencesMessages.getString("CompilerPropertyPage.useprojectsettings.label")); //$NON-NLS-1$
79         }
80
81         /*
82          * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
83          */
84         public void createControl(Composite parent) {
85                 super.createControl(parent);
86                 WorkbenchHelp.setHelp(getControl(), IJavaHelpContextIds.COMPILER_PROPERTY_PAGE);
87         }
88
89         /*
90          * @see org.eclipse.jface.preference.IPreferencePage#createContents(Composite)
91          */
92         protected Control createContents(Composite parent) {
93                 IStatusChangeListener listener= new IStatusChangeListener() {
94                         public void statusChanged(IStatus status) {
95                                 fBlockStatus= status;
96                                 doStatusChanged();
97                         }
98                 };              
99                 fConfigurationBlock= new CompilerConfigurationBlock(listener, getProject());
100                 
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);
107                 
108                 fUseWorkspaceSettings.doFillIntoGrid(composite, 1);
109                 LayoutUtil.setHorizontalGrabbing(fUseWorkspaceSettings.getSelectionButton(null));
110                 
111                 fChangeWorkspaceSettings.doFillIntoGrid(composite, 1);
112                 
113                 fUseProjectSettings.doFillIntoGrid(composite, 2);
114                 
115                 GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL );
116                 data.horizontalSpan= 2;
117                 
118                 fConfigurationBlockControl= fConfigurationBlock.createContents(composite);
119                 fConfigurationBlockControl.setLayoutData(data);
120                 
121                 boolean useProjectSettings= fConfigurationBlock.hasProjectSpecificOptions();
122                 
123                 fUseProjectSettings.setSelection(useProjectSettings);
124                 fUseWorkspaceSettings.setSelection(!useProjectSettings);
125                 
126                 updateEnableState();
127                 Dialog.applyDialogFont(composite);
128                 return composite;
129         }
130         
131         private boolean useProjectSettings() {
132                 return fUseProjectSettings.isSelected();
133         }
134         
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);
140                 } else {
141                         updateEnableState();
142                         doStatusChanged();
143                 }
144         }       
145         /**
146          * Method statusChanged.
147          */
148         private void doStatusChanged() {
149                 updateStatus(useProjectSettings() ? fBlockStatus : new StatusInfo());
150         }
151         
152         /**
153          * Method getProject.
154          */
155         private IJavaProject getProject() {
156                 return (IJavaProject) getElement().getAdapter(IJavaElement.class);              
157         }
158         
159         private void updateEnableState() {
160                 if (useProjectSettings()) {
161                         if (fBlockEnableState != null) {
162                                 fBlockEnableState.restore();
163                                 fBlockEnableState= null;
164                         }
165                 } else {
166                         if (fBlockEnableState == null) {
167                                 fBlockEnableState= ControlEnableState.disable(fConfigurationBlockControl);
168                         }
169                 }       
170         }
171         
172         /*
173          * @see org.eclipse.jface.preference.IPreferencePage#performDefaults()
174          */
175         protected void performDefaults() {
176                 if (useProjectSettings()) {
177                         fUseProjectSettings.setSelection(false);
178                         fUseWorkspaceSettings.setSelection(true);
179                         fConfigurationBlock.performDefaults();
180                 }
181                 super.performDefaults();
182         }
183
184         /*
185          * @see org.eclipse.jface.preference.IPreferencePage#performOk()
186          */
187         public boolean performOk() {
188                 return fConfigurationBlock.performOk(useProjectSettings());
189         }
190         
191         private void updateStatus(IStatus status) {
192                 setValid(!status.matches(IStatus.ERROR));
193                 StatusUtil.applyToStatusLine(this, status);
194         }
195         
196         private boolean showPreferencePage(String id, IPreferencePage page) {
197                 final IPreferenceNode targetNode = new PreferenceNode(id, page);
198                 
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() {
204                         public void run() {
205                                 dialog.create();
206                                 dialog.setMessage(targetNode.getLabelText());
207                                 result[0]= (dialog.open() == Window.OK);
208                         }
209                 });
210                 return result[0];
211         }       
212
213 }