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 compiler settings
45 public class CompilerPropertyPage extends PropertyPage {
47 private CompilerConfigurationBlock 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 CompilerPropertyPage() {
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("CompilerPropertyPage.useworkspacesettings.label")); //$NON-NLS-1$
76 fChangeWorkspaceSettings = new SelectionButtonDialogField(SWT.PUSH);
77 fChangeWorkspaceSettings.setLabelText(PreferencesMessages
78 .getString("CompilerPropertyPage.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("CompilerPropertyPage.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.COMPILER_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 CompilerConfigurationBlock(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 = 2;
116 composite.setLayout(layout);
118 fUseWorkspaceSettings.doFillIntoGrid(composite, 1);
119 LayoutUtil.setHorizontalGrabbing(fUseWorkspaceSettings
120 .getSelectionButton(null));
122 fChangeWorkspaceSettings.doFillIntoGrid(composite, 1);
124 fUseProjectSettings.doFillIntoGrid(composite, 2);
126 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
127 | GridData.VERTICAL_ALIGN_FILL);
128 data.horizontalSpan = 2;
130 fConfigurationBlockControl = fConfigurationBlock
131 .createContents(composite);
132 fConfigurationBlockControl.setLayoutData(data);
134 boolean useProjectSettings = fConfigurationBlock
135 .hasProjectSpecificOptions();
137 fUseProjectSettings.setSelection(useProjectSettings);
138 fUseWorkspaceSettings.setSelection(!useProjectSettings);
141 Dialog.applyDialogFont(composite);
145 private boolean useProjectSettings() {
146 return fUseProjectSettings.isSelected();
149 private void doDialogFieldChanged(DialogField field) {
150 if (field == fChangeWorkspaceSettings) {
151 String id = "net.sourceforge.phpdt.ui.preferences.CompilerPreferencePage"; //$NON-NLS-1$
152 CompilerPreferencePage page = new CompilerPreferencePage();
153 showPreferencePage(id, page);
161 * Method statusChanged.
163 private void doStatusChanged() {
164 updateStatus(useProjectSettings() ? fBlockStatus : new StatusInfo());
170 private IJavaProject getProject() {
171 return (IJavaProject) getElement().getAdapter(IJavaElement.class);
174 private void updateEnableState() {
175 if (useProjectSettings()) {
176 if (fBlockEnableState != null) {
177 fBlockEnableState.restore();
178 fBlockEnableState = null;
181 if (fBlockEnableState == null) {
182 fBlockEnableState = ControlEnableState
183 .disable(fConfigurationBlockControl);
189 * @see org.eclipse.jface.preference.IPreferencePage#performDefaults()
191 protected void performDefaults() {
192 if (useProjectSettings()) {
193 fUseProjectSettings.setSelection(false);
194 fUseWorkspaceSettings.setSelection(true);
195 fConfigurationBlock.performDefaults();
197 super.performDefaults();
201 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
203 public boolean performOk() {
204 return fConfigurationBlock.performOk(useProjectSettings());
207 private void updateStatus(IStatus status) {
208 setValid(!status.matches(IStatus.ERROR));
209 StatusUtil.applyToStatusLine(this, status);
212 private boolean showPreferencePage(String id, IPreferencePage page) {
213 final IPreferenceNode targetNode = new PreferenceNode(id, page);
215 PreferenceManager manager = new PreferenceManager();
216 manager.addToRoot(targetNode);
217 final PreferenceDialog dialog = new PreferenceDialog(getControl()
218 .getShell(), manager);
219 final boolean[] result = new boolean[] { false };
220 BusyIndicator.showWhile(getControl().getDisplay(), new Runnable() {
223 dialog.setMessage(targetNode.getLabelText());
224 result[0] = (dialog.open() == Window.OK);