a6ecaab6c67fc010c4c9ca0ec022a2b6a9873b30
[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 task tags settings
48  */
49 public class TodoTaskPropertyPage extends PropertyPage {
50
51         private TodoTaskConfigurationBlock 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 TodoTaskPropertyPage() {
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("TodoTaskPropertyPage.useworkspacesettings.label")); //$NON-NLS-1$
73
74                 fChangeWorkspaceSettings= new SelectionButtonDialogField(SWT.PUSH);
75                 fChangeWorkspaceSettings.setLabelText(PreferencesMessages.getString("TodoTaskPropertyPage.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("TodoTaskPropertyPage.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.TODOTASK_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 TodoTaskConfigurationBlock(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= 1;
110                 composite.setLayout(layout);
111                 
112                 fUseWorkspaceSettings.doFillIntoGrid(composite, 1);
113                 LayoutUtil.setHorizontalGrabbing(fUseWorkspaceSettings.getSelectionButton(null));
114                 
115                 fChangeWorkspaceSettings.doFillIntoGrid(composite, 1);
116                 GridData data= (GridData) fChangeWorkspaceSettings.getSelectionButton(null).getLayoutData();
117                 data.horizontalIndent= convertWidthInCharsToPixels(3);
118                 data.horizontalAlignment= GridData.BEGINNING;
119                 
120                 fUseProjectSettings.doFillIntoGrid(composite, 1);
121                 
122                 data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL );
123                 data.horizontalSpan= 1;
124                 data.horizontalIndent= convertWidthInCharsToPixels(2);
125                 
126                 fConfigurationBlockControl= fConfigurationBlock.createContents(composite);
127                 fConfigurationBlockControl.setLayoutData(data);
128                 
129                 boolean useProjectSettings= fConfigurationBlock.hasProjectSpecificOptions();
130                 
131                 fUseProjectSettings.setSelection(useProjectSettings);
132                 fUseWorkspaceSettings.setSelection(!useProjectSettings);
133                 
134                 updateEnableState();
135                 Dialog.applyDialogFont(composite);
136                 return composite;
137         }
138         
139         private boolean useProjectSettings() {
140                 return fUseProjectSettings.isSelected();
141         }
142         
143         private void doDialogFieldChanged(DialogField field) {
144                 if (field == fChangeWorkspaceSettings) {
145                         TodoTaskPreferencePage page= new TodoTaskPreferencePage();
146                         showPreferencePage(TodoTaskPreferencePage.ID, page);
147                 } else {
148                         updateEnableState();
149                         doStatusChanged();
150                 }
151         }       
152         /**
153          * Method statusChanged.
154          */
155         private void doStatusChanged() {
156                 updateStatus(useProjectSettings() ? fBlockStatus : new StatusInfo());
157         }
158         
159         /**
160          * Method getProject.
161          */
162         private IJavaProject getProject() {
163                 return (IJavaProject) getElement().getAdapter(IJavaElement.class);              
164         }
165         
166         private void updateEnableState() {
167                 if (useProjectSettings()) {
168                         if (fBlockEnableState != null) {
169                                 fBlockEnableState.restore();
170                                 fBlockEnableState= null;
171                         }
172                 } else {
173                         if (fBlockEnableState == null) {
174                                 fBlockEnableState= ControlEnableState.disable(fConfigurationBlockControl);
175                         }
176                 }       
177         }
178         
179         /*
180          * @see org.eclipse.jface.preference.IPreferencePage#performDefaults()
181          */
182         protected void performDefaults() {
183                 if (useProjectSettings()) {
184                         fUseProjectSettings.setSelection(false);
185                         fUseWorkspaceSettings.setSelection(true);
186                         fConfigurationBlock.performDefaults();
187                 }
188                 super.performDefaults();
189         }
190
191         /*
192          * @see org.eclipse.jface.preference.IPreferencePage#performOk()
193          */
194         public boolean performOk() {
195                 return fConfigurationBlock.performOk(useProjectSettings());
196         }
197         
198         private void updateStatus(IStatus status) {
199                 setValid(!status.matches(IStatus.ERROR));
200                 StatusUtil.applyToStatusLine(this, status);
201         }
202         
203         private boolean showPreferencePage(String id, IPreferencePage page) {
204                 final IPreferenceNode targetNode = new PreferenceNode(id, page);
205                 
206                 PreferenceManager manager = new PreferenceManager();
207                 manager.addToRoot(targetNode);
208                 final PreferenceDialog dialog = new PreferenceDialog(getShell(), manager);
209                 final boolean [] result = new boolean[] { false };
210                 BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() {
211                         public void run() {
212                                 dialog.create();
213                                 dialog.setMessage(targetNode.getLabelText());
214                                 result[0]= (dialog.open() == Window.OK);
215                         }
216                 });
217                 return result[0];
218         }       
219
220 }