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