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