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 compiler settings
49 public class CompilerPropertyPage extends PropertyPage {
51 private CompilerConfigurationBlock 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 CompilerPropertyPage() {
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("CompilerPropertyPage.useworkspacesettings.label")); //$NON-NLS-1$
74 fChangeWorkspaceSettings= new SelectionButtonDialogField(SWT.PUSH);
75 fChangeWorkspaceSettings.setLabelText(PreferencesMessages.getString("CompilerPropertyPage.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("CompilerPropertyPage.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.COMPILER_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 CompilerConfigurationBlock(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= 2;
110 composite.setLayout(layout);
112 fUseWorkspaceSettings.doFillIntoGrid(composite, 1);
113 LayoutUtil.setHorizontalGrabbing(fUseWorkspaceSettings.getSelectionButton(null));
115 fChangeWorkspaceSettings.doFillIntoGrid(composite, 1);
117 fUseProjectSettings.doFillIntoGrid(composite, 2);
119 GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL );
120 data.horizontalSpan= 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 String id= "net.sourceforge.phpdt.ui.preferences.CompilerPreferencePage"; //$NON-NLS-1$
142 CompilerPreferencePage page= new CompilerPreferencePage();
143 showPreferencePage(id, page);
150 * Method statusChanged.
152 private void doStatusChanged() {
153 updateStatus(useProjectSettings() ? fBlockStatus : new StatusInfo());
159 private IJavaProject getProject() {
160 return (IJavaProject) getElement().getAdapter(IJavaElement.class);
163 private void updateEnableState() {
164 if (useProjectSettings()) {
165 if (fBlockEnableState != null) {
166 fBlockEnableState.restore();
167 fBlockEnableState= null;
170 if (fBlockEnableState == null) {
171 fBlockEnableState= ControlEnableState.disable(fConfigurationBlockControl);
177 * @see org.eclipse.jface.preference.IPreferencePage#performDefaults()
179 protected void performDefaults() {
180 if (useProjectSettings()) {
181 fUseProjectSettings.setSelection(false);
182 fUseWorkspaceSettings.setSelection(true);
183 fConfigurationBlock.performDefaults();
185 super.performDefaults();
189 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
191 public boolean performOk() {
192 return fConfigurationBlock.performOk(useProjectSettings());
195 private void updateStatus(IStatus status) {
196 setValid(!status.matches(IStatus.ERROR));
197 StatusUtil.applyToStatusLine(this, status);
200 private boolean showPreferencePage(String id, IPreferencePage page) {
201 final IPreferenceNode targetNode = new PreferenceNode(id, page);
203 PreferenceManager manager = new PreferenceManager();
204 manager.addToRoot(targetNode);
205 final PreferenceDialog dialog = new PreferenceDialog(getControl().getShell(), manager);
206 final boolean [] result = new boolean[] { false };
207 BusyIndicator.showWhile(getControl().getDisplay(), new Runnable() {
210 dialog.setMessage(targetNode.getLabelText());
211 result[0]= (dialog.open() == Window.OK);