001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.properties;
011:
012: import org.eclipse.core.resources.IProject;
013: import org.eclipse.jface.dialogs.ControlEnableState;
014: import org.eclipse.jface.dialogs.Dialog;
015: import org.eclipse.jface.preference.IPreferenceNode;
016: import org.eclipse.jface.preference.PreferenceDialog;
017: import org.eclipse.jface.preference.PreferenceManager;
018: import org.eclipse.jface.preference.PreferenceNode;
019: import org.eclipse.jface.window.Window;
020: import org.eclipse.pde.internal.core.builders.CompilerFlags;
021: import org.eclipse.pde.internal.ui.IHelpContextIds;
022: import org.eclipse.pde.internal.ui.PDEUIMessages;
023: import org.eclipse.pde.internal.ui.preferences.CompilersConfigurationTab;
024: import org.eclipse.pde.internal.ui.preferences.CompilersPreferencePage;
025: import org.eclipse.pde.internal.ui.util.SWTUtil;
026: import org.eclipse.swt.SWT;
027: import org.eclipse.swt.custom.BusyIndicator;
028: import org.eclipse.swt.events.SelectionAdapter;
029: import org.eclipse.swt.events.SelectionEvent;
030: import org.eclipse.swt.events.SelectionListener;
031: import org.eclipse.swt.layout.GridData;
032: import org.eclipse.swt.layout.GridLayout;
033: import org.eclipse.swt.widgets.Button;
034: import org.eclipse.swt.widgets.Composite;
035: import org.eclipse.swt.widgets.Control;
036: import org.eclipse.ui.PlatformUI;
037: import org.eclipse.ui.dialogs.PropertyPage;
038:
039: public class CompilersPropertyPage extends PropertyPage {
040:
041: private ControlEnableState blockEnableState;
042:
043: private Button changeWorkspaceSettingsButton;
044:
045: private CompilersConfigurationTab configurationBlock;
046:
047: private Control configurationBlockControl;
048:
049: private Button useProjectButton;
050:
051: private Button useWorkspaceButton;
052:
053: /*
054: * (non-Javadoc)
055: *
056: * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
057: */
058: protected Control createContents(Composite parent) {
059: configurationBlock = new CompilersConfigurationTab(getProject());
060:
061: SelectionListener listener = new SelectionAdapter() {
062:
063: /*
064: * (non-Javadoc)
065: *
066: * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
067: */
068: public void widgetSelected(SelectionEvent e) {
069: if (e.getSource() instanceof Button) {
070: doDialogFieldChanged((Button) e.getSource());
071: }
072: }
073: };
074:
075: Composite composite = new Composite(parent, SWT.NONE);
076: GridLayout layout = new GridLayout();
077: layout.marginHeight = 0;
078: layout.marginWidth = 0;
079: layout.numColumns = 2;
080: composite.setLayout(layout);
081:
082: // Use Workspace Settings radio button
083: useWorkspaceButton = new Button(composite, SWT.RADIO);
084: useWorkspaceButton.addSelectionListener(listener);
085: useWorkspaceButton
086: .setText(PDEUIMessages.CompilersPropertyPage_useworkspacesettings_label);
087: GridData gd = new GridData();
088: gd.horizontalSpan = 1;
089: gd.horizontalAlignment = GridData.FILL;
090: // if (fButtonStyle == SWT.PUSH) {
091: // gd.heightHint = SWTUtil.getButtonHeightHint(button);
092: // gd.widthHint = SWTUtil.getButtonWidthHint(button);
093: // }
094: gd.grabExcessHorizontalSpace = true;
095: useWorkspaceButton.setLayoutData(gd);
096:
097: // Change Workspace Settings push button
098: changeWorkspaceSettingsButton = new Button(composite, SWT.PUSH);
099: changeWorkspaceSettingsButton
100: .setText(PDEUIMessages.CompilersPropertyPage_useworkspacesettings_change);
101: changeWorkspaceSettingsButton.addSelectionListener(listener);
102: gd = new GridData();
103: gd.horizontalSpan = 1;
104: gd.horizontalAlignment = GridData.FILL;
105: useWorkspaceButton.setLayoutData(gd);
106: SWTUtil.setButtonDimensionHint(changeWorkspaceSettingsButton);
107:
108: // Use Project Settings radio button
109: useProjectButton = new Button(composite, SWT.RADIO);
110: useProjectButton.addSelectionListener(listener);
111: useProjectButton
112: .setText(PDEUIMessages.CompilersPropertyPage_useprojectsettings_label);
113: gd = new GridData();
114: gd.horizontalSpan = 1;
115: gd.horizontalAlignment = GridData.FILL;
116: gd.grabExcessHorizontalSpace = true;
117: useProjectButton.setLayoutData(gd);
118:
119: //
120: GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
121: | GridData.VERTICAL_ALIGN_FILL);
122: data.horizontalSpan = 2;
123:
124: configurationBlockControl = configurationBlock
125: .createContents(composite);
126: configurationBlockControl.setLayoutData(data);
127:
128: boolean useProjectSettings = CompilerFlags.getBoolean(
129: getProject(), CompilerFlags.USE_PROJECT_PREF);
130:
131: useProjectButton.setSelection(useProjectSettings);
132: useWorkspaceButton.setSelection(!useProjectSettings);
133: changeWorkspaceSettingsButton.setEnabled(!useProjectSettings);
134:
135: updateEnableState();
136: Dialog.applyDialogFont(composite);
137: return composite;
138: }
139:
140: /*
141: * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
142: */
143: public void createControl(Composite parent) {
144: super .createControl(parent);
145: PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
146: IHelpContextIds.COMPILERS_PROPERTY_PAGE);
147: }
148:
149: private void doDialogFieldChanged(Button button) {
150: if (button == changeWorkspaceSettingsButton) {
151: showPreferencePage();
152: } else {
153: updateEnableState();
154: }
155: }
156:
157: private IProject getProject() {
158: return (IProject) getElement().getAdapter(IProject.class);
159: }
160:
161: /*
162: * @see org.eclipse.jface.preference.IPreferencePage#performDefaults()
163: */
164: protected void performDefaults() {
165: if (useProjectSettings()) {
166: useProjectButton.setSelection(false);
167: useWorkspaceButton.setSelection(true);
168: updateEnableState();
169: configurationBlock.performDefaults();
170: }
171: super .performDefaults();
172: }
173:
174: /*
175: * @see org.eclipse.jface.preference.IPreferencePage#performOk()
176: */
177: public boolean performOk() {
178: if (!configurationBlock.performOk(useProjectSettings())) {
179: getContainer().updateButtons();
180: return false;
181: }
182: return super .performOk();
183: }
184:
185: private boolean showPreferencePage() {
186: final IPreferenceNode targetNode = new PreferenceNode(
187: "org.eclipse.pde.ui.CompilersPreferencePage", //$NON-NLS-1$
188: new CompilersPreferencePage());
189:
190: PreferenceManager manager = new PreferenceManager();
191: manager.addToRoot(targetNode);
192: final PreferenceDialog dialog = new PreferenceDialog(
193: getShell(), manager);
194: final boolean[] result = new boolean[] { false };
195: BusyIndicator.showWhile(getShell().getDisplay(),
196: new Runnable() {
197: public void run() {
198: dialog.create();
199: dialog.setMessage(targetNode.getLabelText());
200: result[0] = (dialog.open() == Window.OK);
201: }
202: });
203: return result[0];
204: }
205:
206: private void updateEnableState() {
207: if (useProjectSettings()) {
208: if (blockEnableState != null) {
209: changeWorkspaceSettingsButton.setEnabled(false);
210: blockEnableState.restore();
211: blockEnableState = null;
212: }
213: } else {
214: if (blockEnableState == null) {
215: changeWorkspaceSettingsButton.setEnabled(true);
216: blockEnableState = ControlEnableState
217: .disable(configurationBlockControl);
218: }
219: }
220: }
221:
222: private boolean useProjectSettings() {
223: return useProjectButton.getSelection();
224: }
225: }
|