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.PlatformUI;
40 import org.eclipse.ui.dialogs.PropertyPage;
43 * Property page used to configure project specific task tags settings
45 public class TodoTaskPropertyPage extends PropertyPage {
47 private TodoTaskConfigurationBlock fConfigurationBlock;
49 private Control fConfigurationBlockControl;
51 private ControlEnableState fBlockEnableState;
53 private SelectionButtonDialogField fUseWorkspaceSettings;
55 private SelectionButtonDialogField fChangeWorkspaceSettings;
57 private SelectionButtonDialogField fUseProjectSettings;
59 private IStatus fBlockStatus;
61 public TodoTaskPropertyPage() {
62 fBlockStatus = new StatusInfo();
63 fBlockEnableState = null;
65 IDialogFieldListener listener = new IDialogFieldListener() {
66 public void dialogFieldChanged(DialogField field) {
67 doDialogFieldChanged(field);
71 fUseWorkspaceSettings = new SelectionButtonDialogField(SWT.RADIO);
72 fUseWorkspaceSettings.setDialogFieldListener(listener);
73 fUseWorkspaceSettings.setLabelText(PreferencesMessages
74 .getString("TodoTaskPropertyPage.useworkspacesettings.label")); //$NON-NLS-1$
76 fChangeWorkspaceSettings = new SelectionButtonDialogField(SWT.PUSH);
77 fChangeWorkspaceSettings.setLabelText(PreferencesMessages
78 .getString("TodoTaskPropertyPage.useworkspacesettings.change")); //$NON-NLS-1$
79 fChangeWorkspaceSettings.setDialogFieldListener(listener);
81 fUseWorkspaceSettings.attachDialogField(fChangeWorkspaceSettings);
83 fUseProjectSettings = new SelectionButtonDialogField(SWT.RADIO);
84 fUseProjectSettings.setDialogFieldListener(listener);
85 fUseProjectSettings.setLabelText(PreferencesMessages
86 .getString("TodoTaskPropertyPage.useprojectsettings.label")); //$NON-NLS-1$
90 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
92 public void createControl(Composite parent) {
93 super.createControl(parent);
94 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
95 IJavaHelpContextIds.TODOTASK_PROPERTY_PAGE);
99 * @see org.eclipse.jface.preference.IPreferencePage#createContents(Composite)
101 protected Control createContents(Composite parent) {
102 IStatusChangeListener listener = new IStatusChangeListener() {
103 public void statusChanged(IStatus status) {
104 fBlockStatus = status;
108 fConfigurationBlock = new TodoTaskConfigurationBlock(listener,
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);
118 fUseWorkspaceSettings.doFillIntoGrid(composite, 1);
119 LayoutUtil.setHorizontalGrabbing(fUseWorkspaceSettings
120 .getSelectionButton(null));
122 fChangeWorkspaceSettings.doFillIntoGrid(composite, 1);
123 GridData data = (GridData) fChangeWorkspaceSettings.getSelectionButton(
124 null).getLayoutData();
125 data.horizontalIndent = convertWidthInCharsToPixels(3);
126 data.horizontalAlignment = GridData.BEGINNING;
128 fUseProjectSettings.doFillIntoGrid(composite, 1);
130 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
131 | GridData.VERTICAL_ALIGN_FILL);
132 data.horizontalSpan = 1;
133 data.horizontalIndent = convertWidthInCharsToPixels(2);
135 fConfigurationBlockControl = fConfigurationBlock
136 .createContents(composite);
137 fConfigurationBlockControl.setLayoutData(data);
139 boolean useProjectSettings = fConfigurationBlock
140 .hasProjectSpecificOptions();
142 fUseProjectSettings.setSelection(useProjectSettings);
143 fUseWorkspaceSettings.setSelection(!useProjectSettings);
146 Dialog.applyDialogFont(composite);
150 private boolean useProjectSettings() {
151 return fUseProjectSettings.isSelected();
154 private void doDialogFieldChanged(DialogField field) {
155 if (field == fChangeWorkspaceSettings) {
156 TodoTaskPreferencePage page = new TodoTaskPreferencePage();
157 showPreferencePage(TodoTaskPreferencePage.ID, page);
165 * Method statusChanged.
167 private void doStatusChanged() {
168 updateStatus(useProjectSettings() ? fBlockStatus : new StatusInfo());
174 private IJavaProject getProject() {
175 return (IJavaProject) getElement().getAdapter(IJavaElement.class);
178 private void updateEnableState() {
179 if (useProjectSettings()) {
180 if (fBlockEnableState != null) {
181 fBlockEnableState.restore();
182 fBlockEnableState = null;
185 if (fBlockEnableState == null) {
186 fBlockEnableState = ControlEnableState
187 .disable(fConfigurationBlockControl);
193 * @see org.eclipse.jface.preference.IPreferencePage#performDefaults()
195 protected void performDefaults() {
196 if (useProjectSettings()) {
197 fUseProjectSettings.setSelection(false);
198 fUseWorkspaceSettings.setSelection(true);
199 fConfigurationBlock.performDefaults();
201 super.performDefaults();
205 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
207 public boolean performOk() {
208 return fConfigurationBlock.performOk(useProjectSettings());
211 private void updateStatus(IStatus status) {
212 setValid(!status.matches(IStatus.ERROR));
213 StatusUtil.applyToStatusLine(this, status);
216 private boolean showPreferencePage(String id, IPreferencePage page) {
217 final IPreferenceNode targetNode = new PreferenceNode(id, page);
219 PreferenceManager manager = new PreferenceManager();
220 manager.addToRoot(targetNode);
221 final PreferenceDialog dialog = new PreferenceDialog(getShell(),
223 final boolean[] result = new boolean[] { false };
224 BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() {
227 dialog.setMessage(targetNode.getLabelText());
228 result[0] = (dialog.open() == Window.OK);