1 /*******************************************************************************
2 * Copyright (c) 2003 Berthold Daum.
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
10 *******************************************************************************/
11 package net.sourceforge.phpeclipse.ui.overlaypages;
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.QualifiedName;
20 import org.eclipse.jface.preference.FieldEditor;
21 import org.eclipse.jface.preference.FieldEditorPreferencePage;
22 import org.eclipse.jface.preference.IPreferenceNode;
23 import org.eclipse.jface.preference.IPreferencePage;
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.preference.PreferenceDialog;
26 import org.eclipse.jface.preference.PreferenceManager;
27 import org.eclipse.jface.preference.PreferenceNode;
28 import org.eclipse.jface.resource.ImageDescriptor;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.custom.BusyIndicator;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.ui.IWorkbenchPropertyPage;
40 * @author Berthold Daum
42 public abstract class FieldEditorOverlayPage extends FieldEditorPreferencePage
44 IWorkbenchPropertyPage {
46 * * Name of resource property for the selection of workbench or project
49 public static final String USEPROJECTSETTINGS = "useProjectSettings"; //$NON-NLS-1$
50 private static final String FALSE = "false"; //$NON-NLS-1$
51 private static final String TRUE = "true"; //$NON-NLS-1$
52 private boolean fUseFileSettings = false;
53 // Stores all created field editors
54 private List editors = new ArrayList();
55 // Stores owning element of properties
56 private IAdaptable element;
57 // Additional buttons for property pages
58 private Button useWorkspaceSettingsButton, useProjectSettingsButton,
60 // Overlay preference store for property pages
61 private IPreferenceStore overlayStore;
62 // The image descriptor of this pages title image
63 private ImageDescriptor image;
65 private String pageId;
72 public FieldEditorOverlayPage(int style) {
81 public FieldEditorOverlayPage(int style, boolean isFileSettings) {
83 fUseFileSettings = isFileSettings;
93 public FieldEditorOverlayPage(String title, int style) {
96 public FieldEditorOverlayPage(String title, int style, boolean isFileSettings) {
98 fUseFileSettings = isFileSettings;
110 public FieldEditorOverlayPage(String title, ImageDescriptor image, int style) {
111 super(title, image, style);
115 * Returns the id of the current preference page as defined in plugin.xml
116 * Subclasses must implement.
118 * @return - the qualifier
120 protected abstract String getPageId();
122 * Receives the object that owns the properties shown in this property page.
124 * @see org.eclipse.ui.IWorkbenchPropertyPage#setElement(org.eclipse.core.runtime.IAdaptable)
126 public void setElement(IAdaptable element) {
127 this.element = element;
130 * Delivers the object that owns the properties shown in this property page.
132 * @see org.eclipse.ui.IWorkbenchPropertyPage#getElement()
134 public IAdaptable getElement() {
138 * Returns true if this instance represents a property page
140 * @return - true for property pages, false for preference pages
142 public boolean isPropertyPage() {
143 return getElement() != null;
146 * We override the addField method. This allows us to store each field editor
147 * added by subclasses in a list for later processing.
149 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#addField(org.eclipse.jface.preference.FieldEditor)
151 protected void addField(FieldEditor editor) {
153 super.addField(editor);
156 * We override the createControl method. In case of property pages we create
157 * a new PropertyStore as local preference store. After all control have been
158 * create, we enable/disable these controls.
160 * @see org.eclipse.jface.preference.PreferencePage#createControl()
162 public void createControl(Composite parent) {
163 // Special treatment for property pages
164 if (isPropertyPage()) {
166 pageId = getPageId();
167 // Create an overlay preference store and fill it with properties
168 overlayStore = new PropertyStore((IResource) getElement(), super
169 .getPreferenceStore(), pageId);
170 // Set overlay store as current preference store
172 super.createControl(parent);
173 // Update state of all subclass controls
174 if (isPropertyPage())
175 updateFieldEditors();
178 * We override the createContents method. In case of property pages we insert
179 * two radio buttons at the top of the page.
181 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
183 protected Control createContents(Composite parent) {
184 if (isPropertyPage())
185 createSelectionGroup(parent);
186 return super.createContents(parent);
189 * Creates and initializes a selection group with two choice buttons and one
193 * the parent composite
195 private void createSelectionGroup(Composite parent) {
196 Composite comp = new Composite(parent, SWT.NONE);
197 GridLayout layout = new GridLayout(2, false);
198 layout.marginHeight = 0;
199 layout.marginWidth = 0;
200 comp.setLayout(layout);
201 comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
202 Composite radioGroup = new Composite(comp, SWT.NONE);
203 radioGroup.setLayout(new GridLayout());
204 radioGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
205 useWorkspaceSettingsButton = createRadioButton(radioGroup, Messages
206 .getString("OverlayPage.Use_Workspace_Settings")); //$NON-NLS-1$
207 if (fUseFileSettings) {
208 useProjectSettingsButton = createRadioButton(radioGroup, Messages
209 .getString("OverlayPage.Use_File_Settings")); //$NON-NLS-1$
211 useProjectSettingsButton = createRadioButton(radioGroup, Messages
212 .getString("OverlayPage.Use_Project_Settings")); //$NON-NLS-1$
214 configureButton = new Button(comp, SWT.PUSH);
215 configureButton.setText(Messages
216 .getString("OverlayPage.Configure_Workspace_Settings")); //$NON-NLS-1$
217 configureButton.addSelectionListener(new SelectionAdapter() {
218 public void widgetSelected(SelectionEvent e) {
219 configureWorkspaceSettings();
222 // Set workspace/project radio buttons
224 String use = ((IResource) getElement())
225 .getPersistentProperty(new QualifiedName(pageId, USEPROJECTSETTINGS));
226 if (TRUE.equals(use)) {
227 useProjectSettingsButton.setSelection(true);
228 configureButton.setEnabled(false);
230 useWorkspaceSettingsButton.setSelection(true);
231 } catch (CoreException e) {
232 useWorkspaceSettingsButton.setSelection(true);
236 * Convenience method creating a radio button
239 * the parent composite
242 * @return - the new button
244 private Button createRadioButton(Composite parent, String label) {
245 final Button button = new Button(parent, SWT.RADIO);
246 button.setText(label);
247 button.addSelectionListener(new SelectionAdapter() {
248 public void widgetSelected(SelectionEvent e) {
249 configureButton.setEnabled(button == useWorkspaceSettingsButton);
250 updateFieldEditors();
256 * Returns in case of property pages the overlay store, in case of preference
257 * pages the standard preference store
259 * @see org.eclipse.jface.preference.PreferencePage#getPreferenceStore()
261 public IPreferenceStore getPreferenceStore() {
262 if (isPropertyPage())
264 return super.getPreferenceStore();
267 * Enables or disables the field editors and buttons of this page
269 private void updateFieldEditors() {
270 // We iterate through all field editors
271 boolean enabled = useProjectSettingsButton.getSelection();
272 updateFieldEditors(enabled);
275 * Enables or disables the field editors and buttons of this page Subclasses
281 protected void updateFieldEditors(boolean enabled) {
282 Composite parent = getFieldEditorParent();
283 Iterator it = editors.iterator();
284 while (it.hasNext()) {
285 FieldEditor editor = (FieldEditor) it.next();
286 editor.setEnabled(enabled, parent);
290 * We override the performOk method. In case of property pages we copy the
291 * values in the overlay store into the property values of the selected
292 * project. We also save the state of the radio buttons.
294 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
296 public boolean performOk() {
297 boolean result = super.performOk();
298 if (result && isPropertyPage()) {
299 // Save state of radiobuttons in project properties
300 IResource resource = (IResource) getElement();
302 String value = (useProjectSettingsButton.getSelection()) ? TRUE : FALSE;
303 resource.setPersistentProperty(new QualifiedName(pageId,
304 USEPROJECTSETTINGS), value);
305 } catch (CoreException e) {
311 * We override the performDefaults method. In case of property pages we
312 * switch back to the workspace settings and disable the field editors.
314 * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
316 protected void performDefaults() {
317 if (isPropertyPage()) {
318 useWorkspaceSettingsButton.setSelection(true);
319 useProjectSettingsButton.setSelection(false);
320 configureButton.setEnabled(true);
321 updateFieldEditors();
323 super.performDefaults();
326 * Creates a new preferences page and opens it
328 * @see com.bdaum.SpellChecker.preferences.SpellCheckerPreferencePage#configureWorkspaceSettings()
330 protected void configureWorkspaceSettings() {
332 // create a new instance of the current class
333 IPreferencePage page = (IPreferencePage) this.getClass().newInstance();
334 page.setTitle(getTitle());
335 page.setImageDescriptor(image);
337 showPreferencePage(pageId, page);
338 } catch (InstantiationException e) {
340 } catch (IllegalAccessException e) {
345 * Show a single preference pages
348 * the preference page identification
350 * the preference page
352 protected void showPreferencePage(String id, IPreferencePage page) {
353 final IPreferenceNode targetNode = new PreferenceNode(id, page);
354 PreferenceManager manager = new PreferenceManager();
355 manager.addToRoot(targetNode);
356 final PreferenceDialog dialog = new PreferenceDialog(getControl()
357 .getShell(), manager);
358 BusyIndicator.showWhile(getControl().getDisplay(), new Runnable() {
361 dialog.setMessage(targetNode.getLabelText());