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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.preferences;
13 import org.eclipse.core.runtime.IStatus;
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;
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;
31 import org.eclipse.ui.dialogs.PropertyPage;
32 import org.eclipse.ui.help.WorkbenchHelp;
34 import net.sourceforge.phpdt.core.IJavaElement;
35 import net.sourceforge.phpdt.core.IJavaProject;
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;
47 * Property page used to configure project specific task tags settings
49 public class TodoTaskPropertyPage extends PropertyPage {
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;
60 public TodoTaskPropertyPage() {
61 fBlockStatus= new StatusInfo();
62 fBlockEnableState= null;
64 IDialogFieldListener listener= new IDialogFieldListener() {
65 public void dialogFieldChanged(DialogField field) {
66 doDialogFieldChanged(field);
70 fUseWorkspaceSettings= new SelectionButtonDialogField(SWT.RADIO);
71 fUseWorkspaceSettings.setDialogFieldListener(listener);
72 fUseWorkspaceSettings.setLabelText(PreferencesMessages.getString("TodoTaskPropertyPage.useworkspacesettings.label")); //$NON-NLS-1$
74 fChangeWorkspaceSettings= new SelectionButtonDialogField(SWT.PUSH);
75 fChangeWorkspaceSettings.setLabelText(PreferencesMessages.getString("TodoTaskPropertyPage.useworkspacesettings.change")); //$NON-NLS-1$
76 fChangeWorkspaceSettings.setDialogFieldListener(listener);
78 fUseWorkspaceSettings.attachDialogField(fChangeWorkspaceSettings);
80 fUseProjectSettings= new SelectionButtonDialogField(SWT.RADIO);
81 fUseProjectSettings.setDialogFieldListener(listener);
82 fUseProjectSettings.setLabelText(PreferencesMessages.getString("TodoTaskPropertyPage.useprojectsettings.label")); //$NON-NLS-1$
86 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
88 public void createControl(Composite parent) {
89 super.createControl(parent);
90 WorkbenchHelp.setHelp(getControl(), IJavaHelpContextIds.TODOTASK_PROPERTY_PAGE);
94 * @see org.eclipse.jface.preference.IPreferencePage#createContents(Composite)
96 protected Control createContents(Composite parent) {
97 IStatusChangeListener listener= new IStatusChangeListener() {
98 public void statusChanged(IStatus status) {
103 fConfigurationBlock= new TodoTaskConfigurationBlock(listener, getProject());
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);
112 fUseWorkspaceSettings.doFillIntoGrid(composite, 1);
113 LayoutUtil.setHorizontalGrabbing(fUseWorkspaceSettings.getSelectionButton(null));
115 fChangeWorkspaceSettings.doFillIntoGrid(composite, 1);
116 GridData data= (GridData) fChangeWorkspaceSettings.getSelectionButton(null).getLayoutData();
117 data.horizontalIndent= convertWidthInCharsToPixels(3);
118 data.horizontalAlignment= GridData.BEGINNING;
120 fUseProjectSettings.doFillIntoGrid(composite, 1);
122 data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL );
123 data.horizontalSpan= 1;
124 data.horizontalIndent= convertWidthInCharsToPixels(2);
126 fConfigurationBlockControl= fConfigurationBlock.createContents(composite);
127 fConfigurationBlockControl.setLayoutData(data);
129 boolean useProjectSettings= fConfigurationBlock.hasProjectSpecificOptions();
131 fUseProjectSettings.setSelection(useProjectSettings);
132 fUseWorkspaceSettings.setSelection(!useProjectSettings);
135 Dialog.applyDialogFont(composite);
139 private boolean useProjectSettings() {
140 return fUseProjectSettings.isSelected();
143 private void doDialogFieldChanged(DialogField field) {
144 if (field == fChangeWorkspaceSettings) {
145 TodoTaskPreferencePage page= new TodoTaskPreferencePage();
146 showPreferencePage(TodoTaskPreferencePage.ID, page);
153 * Method statusChanged.
155 private void doStatusChanged() {
156 updateStatus(useProjectSettings() ? fBlockStatus : new StatusInfo());
162 private IJavaProject getProject() {
163 return (IJavaProject) getElement().getAdapter(IJavaElement.class);
166 private void updateEnableState() {
167 if (useProjectSettings()) {
168 if (fBlockEnableState != null) {
169 fBlockEnableState.restore();
170 fBlockEnableState= null;
173 if (fBlockEnableState == null) {
174 fBlockEnableState= ControlEnableState.disable(fConfigurationBlockControl);
180 * @see org.eclipse.jface.preference.IPreferencePage#performDefaults()
182 protected void performDefaults() {
183 if (useProjectSettings()) {
184 fUseProjectSettings.setSelection(false);
185 fUseWorkspaceSettings.setSelection(true);
186 fConfigurationBlock.performDefaults();
188 super.performDefaults();
192 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
194 public boolean performOk() {
195 return fConfigurationBlock.performOk(useProjectSettings());
198 private void updateStatus(IStatus status) {
199 setValid(!status.matches(IStatus.ERROR));
200 StatusUtil.applyToStatusLine(this, status);
203 private boolean showPreferencePage(String id, IPreferencePage page) {
204 final IPreferenceNode targetNode = new PreferenceNode(id, page);
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() {
213 dialog.setMessage(targetNode.getLabelText());
214 result[0]= (dialog.open() == Window.OK);