KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > properties > CompilersPropertyPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.properties;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.jface.dialogs.ControlEnableState;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.preference.IPreferenceNode;
17 import org.eclipse.jface.preference.PreferenceDialog;
18 import org.eclipse.jface.preference.PreferenceManager;
19 import org.eclipse.jface.preference.PreferenceNode;
20 import org.eclipse.jface.window.Window;
21 import org.eclipse.pde.internal.core.builders.CompilerFlags;
22 import org.eclipse.pde.internal.ui.IHelpContextIds;
23 import org.eclipse.pde.internal.ui.PDEUIMessages;
24 import org.eclipse.pde.internal.ui.preferences.CompilersConfigurationTab;
25 import org.eclipse.pde.internal.ui.preferences.CompilersPreferencePage;
26 import org.eclipse.pde.internal.ui.util.SWTUtil;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.custom.BusyIndicator;
29 import org.eclipse.swt.events.SelectionAdapter;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.events.SelectionListener;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.dialogs.PropertyPage;
39
40 public class CompilersPropertyPage extends PropertyPage {
41
42     private ControlEnableState blockEnableState;
43
44     private Button changeWorkspaceSettingsButton;
45
46     private CompilersConfigurationTab configurationBlock;
47
48     private Control configurationBlockControl;
49
50     private Button useProjectButton;
51
52     private Button useWorkspaceButton;
53
54     /*
55      * (non-Javadoc)
56      *
57      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
58      */

59     protected Control createContents(Composite parent) {
60         configurationBlock = new CompilersConfigurationTab(getProject());
61
62         SelectionListener listener = new SelectionAdapter() {
63
64             /*
65              * (non-Javadoc)
66              *
67              * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
68              */

69             public void widgetSelected(SelectionEvent e) {
70                 if (e.getSource() instanceof Button) {
71                     doDialogFieldChanged((Button) e.getSource());
72                 }
73             }
74         };
75
76         Composite composite = new Composite(parent, SWT.NONE);
77         GridLayout layout = new GridLayout();
78         layout.marginHeight = 0;
79         layout.marginWidth = 0;
80         layout.numColumns = 2;
81         composite.setLayout(layout);
82
83         // Use Workspace Settings radio button
84
useWorkspaceButton = new Button(composite, SWT.RADIO);
85         useWorkspaceButton.addSelectionListener(listener);
86         useWorkspaceButton
87                 .setText(PDEUIMessages.CompilersPropertyPage_useworkspacesettings_label);
88         GridData gd = new GridData();
89         gd.horizontalSpan = 1;
90         gd.horizontalAlignment = GridData.FILL;
91         // if (fButtonStyle == SWT.PUSH) {
92
// gd.heightHint = SWTUtil.getButtonHeightHint(button);
93
// gd.widthHint = SWTUtil.getButtonWidthHint(button);
94
// }
95
gd.grabExcessHorizontalSpace = true;
96         useWorkspaceButton.setLayoutData(gd);
97
98         // Change Workspace Settings push button
99
changeWorkspaceSettingsButton = new Button(composite, SWT.PUSH);
100         changeWorkspaceSettingsButton
101                 .setText(PDEUIMessages.CompilersPropertyPage_useworkspacesettings_change);
102         changeWorkspaceSettingsButton.addSelectionListener(listener);
103         gd = new GridData();
104         gd.horizontalSpan = 1;
105         gd.horizontalAlignment = GridData.FILL;
106         useWorkspaceButton.setLayoutData(gd);
107         SWTUtil.setButtonDimensionHint(changeWorkspaceSettingsButton);
108
109         // Use Project Settings radio button
110
useProjectButton = new Button(composite, SWT.RADIO);
111         useProjectButton.addSelectionListener(listener);
112         useProjectButton
113                 .setText(PDEUIMessages.CompilersPropertyPage_useprojectsettings_label);
114         gd = new GridData();
115         gd.horizontalSpan = 1;
116         gd.horizontalAlignment = GridData.FILL;
117         gd.grabExcessHorizontalSpace = true;
118         useProjectButton.setLayoutData(gd);
119
120         //
121
GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
122                 | GridData.VERTICAL_ALIGN_FILL);
123         data.horizontalSpan = 2;
124
125         configurationBlockControl = configurationBlock
126                 .createContents(composite);
127         configurationBlockControl.setLayoutData(data);
128
129         boolean useProjectSettings = CompilerFlags.getBoolean(getProject(),
130                 CompilerFlags.USE_PROJECT_PREF);
131
132         useProjectButton.setSelection(useProjectSettings);
133         useWorkspaceButton.setSelection(!useProjectSettings);
134         changeWorkspaceSettingsButton.setEnabled(!useProjectSettings);
135
136         updateEnableState();
137         Dialog.applyDialogFont(composite);
138         return composite;
139     }
140
141     /*
142      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
143      */

144     public void createControl(Composite parent) {
145         super.createControl(parent);
146         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
147                 IHelpContextIds.COMPILERS_PROPERTY_PAGE);
148     }
149
150     private void doDialogFieldChanged(Button button) {
151         if (button == changeWorkspaceSettingsButton) {
152             showPreferencePage();
153         } else {
154             updateEnableState();
155         }
156     }
157
158     private IProject getProject() {
159         return (IProject) getElement().getAdapter(IProject.class);
160     }
161
162     /*
163      * @see org.eclipse.jface.preference.IPreferencePage#performDefaults()
164      */

165     protected void performDefaults() {
166         if (useProjectSettings()) {
167             useProjectButton.setSelection(false);
168             useWorkspaceButton.setSelection(true);
169             updateEnableState();
170             configurationBlock.performDefaults();
171         }
172         super.performDefaults();
173     }
174
175     /*
176      * @see org.eclipse.jface.preference.IPreferencePage#performOk()
177      */

178     public boolean performOk() {
179         if (!configurationBlock.performOk(useProjectSettings())) {
180             getContainer().updateButtons();
181             return false;
182         }
183         return super.performOk();
184     }
185
186     private boolean showPreferencePage() {
187         final IPreferenceNode targetNode = new PreferenceNode(
188                 "org.eclipse.pde.ui.CompilersPreferencePage", //$NON-NLS-1$
189
new CompilersPreferencePage());
190
191         PreferenceManager manager = new PreferenceManager();
192         manager.addToRoot(targetNode);
193         final PreferenceDialog dialog = new PreferenceDialog(getShell(),
194                 manager);
195         final boolean[] result = new boolean[] { false };
196         BusyIndicator.showWhile(getShell().getDisplay(), new Runnable JavaDoc() {
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 }
226
Popular Tags