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 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;
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;
43 * Property page used to configure project specific task tags settings
45 public class TodoTaskPropertyPage extends PropertyPage {
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;
56 public TodoTaskPropertyPage() {
57 fBlockStatus= new StatusInfo();
58 fBlockEnableState= null;
60 IDialogFieldListener listener= new IDialogFieldListener() {
61 public void dialogFieldChanged(DialogField field) {
62 doDialogFieldChanged(field);
66 fUseWorkspaceSettings= new SelectionButtonDialogField(SWT.RADIO);
67 fUseWorkspaceSettings.setDialogFieldListener(listener);
68 fUseWorkspaceSettings.setLabelText(PreferencesMessages.getString("TodoTaskPropertyPage.useworkspacesettings.label")); //$NON-NLS-1$
70 fChangeWorkspaceSettings= new SelectionButtonDialogField(SWT.PUSH);
71 fChangeWorkspaceSettings.setLabelText(PreferencesMessages.getString("TodoTaskPropertyPage.useworkspacesettings.change")); //$NON-NLS-1$
72 fChangeWorkspaceSettings.setDialogFieldListener(listener);
74 fUseWorkspaceSettings.attachDialogField(fChangeWorkspaceSettings);
76 fUseProjectSettings= new SelectionButtonDialogField(SWT.RADIO);
77 fUseProjectSettings.setDialogFieldListener(listener);
78 fUseProjectSettings.setLabelText(PreferencesMessages.getString("TodoTaskPropertyPage.useprojectsettings.label")); //$NON-NLS-1$
82 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
84 public void createControl(Composite parent) {
85 super.createControl(parent);
86 WorkbenchHelp.setHelp(getControl(), IJavaHelpContextIds.TODOTASK_PROPERTY_PAGE);
90 * @see org.eclipse.jface.preference.IPreferencePage#createContents(Composite)
92 protected Control createContents(Composite parent) {
93 IStatusChangeListener listener= new IStatusChangeListener() {
94 public void statusChanged(IStatus status) {
99 fConfigurationBlock= new TodoTaskConfigurationBlock(listener, getProject());
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);
108 fUseWorkspaceSettings.doFillIntoGrid(composite, 1);
109 LayoutUtil.setHorizontalGrabbing(fUseWorkspaceSettings.getSelectionButton(null));
111 fChangeWorkspaceSettings.doFillIntoGrid(composite, 1);
112 GridData data= (GridData) fChangeWorkspaceSettings.getSelectionButton(null).getLayoutData();
113 data.horizontalIndent= convertWidthInCharsToPixels(3);
114 data.horizontalAlignment= GridData.BEGINNING;
116 fUseProjectSettings.doFillIntoGrid(composite, 1);
118 data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL );
119 data.horizontalSpan= 1;
120 data.horizontalIndent= convertWidthInCharsToPixels(2);
122 fConfigurationBlockControl= fConfigurationBlock.createContents(composite);
123 fConfigurationBlockControl.setLayoutData(data);
125 boolean useProjectSettings= fConfigurationBlock.hasProjectSpecificOptions();
127 fUseProjectSettings.setSelection(useProjectSettings);
128 fUseWorkspaceSettings.setSelection(!useProjectSettings);
131 Dialog.applyDialogFont(composite);
135 private boolean useProjectSettings() {
136 return fUseProjectSettings.isSelected();
139 private void doDialogFieldChanged(DialogField field) {
140 if (field == fChangeWorkspaceSettings) {
141 TodoTaskPreferencePage page= new TodoTaskPreferencePage();
142 showPreferencePage(TodoTaskPreferencePage.ID, page);
149 * Method statusChanged.
151 private void doStatusChanged() {
152 updateStatus(useProjectSettings() ? fBlockStatus : new StatusInfo());
158 private IJavaProject getProject() {
159 return (IJavaProject) getElement().getAdapter(IJavaElement.class);
162 private void updateEnableState() {
163 if (useProjectSettings()) {
164 if (fBlockEnableState != null) {
165 fBlockEnableState.restore();
166 fBlockEnableState= null;
169 if (fBlockEnableState == null) {
170 fBlockEnableState= ControlEnableState.disable(fConfigurationBlockControl);
176 * @see org.eclipse.jface.preference.IPreferencePage#performDefaults()
178 protected void performDefaults() {
179 if (useProjectSettings()) {
180 fUseProjectSettings.setSelection(false);
181 fUseWorkspaceSettings.setSelection(true);
182 fConfigurationBlock.performDefaults();
184 super.performDefaults();
188 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
190 public boolean performOk() {
191 return fConfigurationBlock.performOk(useProjectSettings());
194 private void updateStatus(IStatus status) {
195 setValid(!status.matches(IStatus.ERROR));
196 StatusUtil.applyToStatusLine(this, status);
199 private boolean showPreferencePage(String id, IPreferencePage page) {
200 final IPreferenceNode targetNode = new PreferenceNode(id, page);
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() {
209 dialog.setMessage(targetNode.getLabelText());
210 result[0]= (dialog.open() == Window.OK);