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.overlaypages;
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.core.runtime.QualifiedName;
19 import org.eclipse.jface.preference.FieldEditor;
20 import org.eclipse.jface.preference.FieldEditorPreferencePage;
21 import org.eclipse.jface.preference.IPreferenceNode;
22 import org.eclipse.jface.preference.IPreferencePage;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.jface.preference.PreferenceDialog;
25 import org.eclipse.jface.preference.PreferenceManager;
26 import org.eclipse.jface.preference.PreferenceNode;
27 import org.eclipse.jface.resource.ImageDescriptor;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.custom.BusyIndicator;
30 import org.eclipse.swt.events.SelectionAdapter;
31 import org.eclipse.swt.events.SelectionEvent;
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.IWorkbenchPropertyPage;
39 * @author Berthold Daum
41 public abstract class FieldEditorOverlayPage extends FieldEditorPreferencePage
43 IWorkbenchPropertyPage {
45 * * Name of resource property for the selection of workbench or project
48 public static final String USEPROJECTSETTINGS = "useProjectSettings"; //$NON-NLS-1$
49 private static final String FALSE = "false"; //$NON-NLS-1$
50 private static final String TRUE = "true"; //$NON-NLS-1$
51 private boolean fUseFileSettings = false;
52 // Stores all created field editors
53 private List editors = new ArrayList();
54 // Stores owning element of properties
55 private IAdaptable element;
56 // Additional buttons for property pages
57 private Button useWorkspaceSettingsButton, useProjectSettingsButton,
59 // Overlay preference store for property pages
60 private IPreferenceStore overlayStore;
61 // The image descriptor of this pages title image
62 private ImageDescriptor image;
64 private String pageId;
71 public FieldEditorOverlayPage(int style) {
80 public FieldEditorOverlayPage(int style, boolean isFileSettings) {
82 fUseFileSettings = isFileSettings;
92 public FieldEditorOverlayPage(String title, int style) {
95 public FieldEditorOverlayPage(String title, int style, boolean isFileSettings) {
97 fUseFileSettings = isFileSettings;
109 public FieldEditorOverlayPage(String title, ImageDescriptor image, int style) {
110 super(title, image, style);
114 * Returns the id of the current preference page as defined in plugin.xml
115 * Subclasses must implement.
117 * @return - the qualifier
119 protected abstract String getPageId();
121 * Receives the object that owns the properties shown in this property page.
123 * @see org.eclipse.ui.IWorkbenchPropertyPage#setElement(org.eclipse.core.runtime.IAdaptable)
125 public void setElement(IAdaptable element) {
126 this.element = element;
129 * Delivers the object that owns the properties shown in this property page.
131 * @see org.eclipse.ui.IWorkbenchPropertyPage#getElement()
133 public IAdaptable getElement() {
137 * Returns true if this instance represents a property page
139 * @return - true for property pages, false for preference pages
141 public boolean isPropertyPage() {
142 return getElement() != null;
145 * We override the addField method. This allows us to store each field editor
146 * added by subclasses in a list for later processing.
148 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#addField(org.eclipse.jface.preference.FieldEditor)
150 protected void addField(FieldEditor editor) {
152 super.addField(editor);
155 * We override the createControl method. In case of property pages we create
156 * a new PropertyStore as local preference store. After all control have been
157 * create, we enable/disable these controls.
159 * @see org.eclipse.jface.preference.PreferencePage#createControl()
161 public void createControl(Composite parent) {
162 // Special treatment for property pages
163 if (isPropertyPage()) {
165 pageId = getPageId();
166 // Create an overlay preference store and fill it with properties
167 overlayStore = new PropertyStore((IResource) getElement(), super
168 .getPreferenceStore(), pageId);
169 // Set overlay store as current preference store
171 super.createControl(parent);
172 // Update state of all subclass controls
173 if (isPropertyPage())
174 updateFieldEditors();
177 * We override the createContents method. In case of property pages we insert
178 * two radio buttons at the top of the page.
180 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
182 protected Control createContents(Composite parent) {
183 if (isPropertyPage())
184 createSelectionGroup(parent);
185 return super.createContents(parent);
188 * Creates and initializes a selection group with two choice buttons and one
192 * the parent composite
194 private void createSelectionGroup(Composite parent) {
195 Composite comp = new Composite(parent, SWT.NONE);
196 GridLayout layout = new GridLayout(2, false);
197 layout.marginHeight = 0;
198 layout.marginWidth = 0;
199 comp.setLayout(layout);
200 comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
201 Composite radioGroup = new Composite(comp, SWT.NONE);
202 radioGroup.setLayout(new GridLayout());
203 radioGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
204 useWorkspaceSettingsButton = createRadioButton(radioGroup, Messages
205 .getString("OverlayPage.Use_Workspace_Settings")); //$NON-NLS-1$
206 if (fUseFileSettings) {
207 useProjectSettingsButton = createRadioButton(radioGroup, Messages
208 .getString("OverlayPage.Use_File_Settings")); //$NON-NLS-1$
210 useProjectSettingsButton = createRadioButton(radioGroup, Messages
211 .getString("OverlayPage.Use_Project_Settings")); //$NON-NLS-1$
213 configureButton = new Button(comp, SWT.PUSH);
214 configureButton.setText(Messages
215 .getString("OverlayPage.Configure_Workspace_Settings")); //$NON-NLS-1$
216 configureButton.addSelectionListener(new SelectionAdapter() {
217 public void widgetSelected(SelectionEvent e) {
218 configureWorkspaceSettings();
221 // Set workspace/project radio buttons
223 String use = ((IResource) getElement())
224 .getPersistentProperty(new QualifiedName(pageId, USEPROJECTSETTINGS));
225 if (TRUE.equals(use)) {
226 useProjectSettingsButton.setSelection(true);
227 configureButton.setEnabled(false);
229 useWorkspaceSettingsButton.setSelection(true);
230 } catch (CoreException e) {
231 useWorkspaceSettingsButton.setSelection(true);
235 * Convenience method creating a radio button
238 * the parent composite
241 * @return - the new button
243 private Button createRadioButton(Composite parent, String label) {
244 final Button button = new Button(parent, SWT.RADIO);
245 button.setText(label);
246 button.addSelectionListener(new SelectionAdapter() {
247 public void widgetSelected(SelectionEvent e) {
248 configureButton.setEnabled(button == useWorkspaceSettingsButton);
249 updateFieldEditors();
255 * Returns in case of property pages the overlay store, in case of preference
256 * pages the standard preference store
258 * @see org.eclipse.jface.preference.PreferencePage#getPreferenceStore()
260 public IPreferenceStore getPreferenceStore() {
261 if (isPropertyPage())
263 return super.getPreferenceStore();
266 * Enables or disables the field editors and buttons of this page
268 private void updateFieldEditors() {
269 // We iterate through all field editors
270 boolean enabled = useProjectSettingsButton.getSelection();
271 updateFieldEditors(enabled);
274 * Enables or disables the field editors and buttons of this page Subclasses
280 protected void updateFieldEditors(boolean enabled) {
281 Composite parent = getFieldEditorParent();
282 Iterator it = editors.iterator();
283 while (it.hasNext()) {
284 FieldEditor editor = (FieldEditor) it.next();
285 editor.setEnabled(enabled, parent);
289 * We override the performOk method. In case of property pages we copy the
290 * values in the overlay store into the property values of the selected
291 * project. We also save the state of the radio buttons.
293 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
295 public boolean performOk() {
296 boolean result = super.performOk();
297 if (result && isPropertyPage()) {
298 // Save state of radiobuttons in project properties
299 IResource resource = (IResource) getElement();
301 String value = (useProjectSettingsButton.getSelection()) ? TRUE : FALSE;
302 resource.setPersistentProperty(new QualifiedName(pageId,
303 USEPROJECTSETTINGS), value);
304 } catch (CoreException e) {
310 * We override the performDefaults method. In case of property pages we
311 * switch back to the workspace settings and disable the field editors.
313 * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
315 protected void performDefaults() {
316 if (isPropertyPage()) {
317 useWorkspaceSettingsButton.setSelection(true);
318 useProjectSettingsButton.setSelection(false);
319 configureButton.setEnabled(true);
320 updateFieldEditors();
322 super.performDefaults();
325 * Creates a new preferences page and opens it
327 * @see com.bdaum.SpellChecker.preferences.SpellCheckerPreferencePage#configureWorkspaceSettings()
329 protected void configureWorkspaceSettings() {
331 // create a new instance of the current class
332 IPreferencePage page = (IPreferencePage) this.getClass().newInstance();
333 page.setTitle(getTitle());
334 page.setImageDescriptor(image);
336 showPreferencePage(pageId, page);
337 } catch (InstantiationException e) {
339 } catch (IllegalAccessException e) {
344 * Show a single preference pages
347 * the preference page identification
349 * the preference page
351 protected void showPreferencePage(String id, IPreferencePage page) {
352 final IPreferenceNode targetNode = new PreferenceNode(id, page);
353 PreferenceManager manager = new PreferenceManager();
354 manager.addToRoot(targetNode);
355 final PreferenceDialog dialog = new PreferenceDialog(getControl()
356 .getShell(), manager);
357 BusyIndicator.showWhile(getControl().getDisplay(), new Runnable() {
360 dialog.setMessage(targetNode.getLabelText());