initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / preferences / FieldEditorOverlayPage.java
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
7  * 
8  * Contributors:
9  *     Berthold Daum
10  *******************************************************************************/
11 package net.sourceforge.phpeclipse.wiki.preferences;
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
15
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;
39
40 /**
41  */
42 public abstract class FieldEditorOverlayPage extends FieldEditorPreferencePage
43     implements
44       IWorkbenchPropertyPage {
45   /**
46    * * Name of resource property for the selection of workbench or project
47    * settings **
48    */
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,
59       configureButton;
60   // Overlay preference store for property pages
61   private IPreferenceStore overlayStore;
62   // The image descriptor of this pages title image
63   private ImageDescriptor image;
64   // Cache for page id
65   private String pageId;
66   /**
67    * Constructor
68    * 
69    * @param style -
70    *            layout style
71    */
72   public FieldEditorOverlayPage(int style) {
73     super(style);
74   }
75   /**
76    * Constructor
77    * 
78    * @param style -
79    *            layout style
80    */
81   public FieldEditorOverlayPage(int style, boolean isFileSettings) {
82     super(style);
83     fUseFileSettings = isFileSettings;
84   }
85   /**
86    * Constructor
87    * 
88    * @param title -
89    *            title string
90    * @param style -
91    *            layout style
92    */
93   public FieldEditorOverlayPage(String title, int style) {
94     super(title, style);
95   }
96   public FieldEditorOverlayPage(String title, int style, boolean isFileSettings) {
97     super(title, style);
98     fUseFileSettings = isFileSettings;
99   }
100   /**
101    * Constructor
102    * 
103    * @param title -
104    *            title string
105    * @param image -
106    *            title image
107    * @param style -
108    *            layout style
109    */
110   public FieldEditorOverlayPage(String title, ImageDescriptor image, int style) {
111     super(title, image, style);
112     this.image = image;
113   }
114   /**
115    * Returns the id of the current preference page as defined in plugin.xml
116    * Subclasses must implement.
117    * 
118    * @return - the qualifier
119    */
120   protected abstract String getPageId();
121   /**
122    * Receives the object that owns the properties shown in this property page.
123    * 
124    * @see org.eclipse.ui.IWorkbenchPropertyPage#setElement(org.eclipse.core.runtime.IAdaptable)
125    */
126   public void setElement(IAdaptable element) {
127     this.element = element;
128   }
129   /**
130    * Delivers the object that owns the properties shown in this property page.
131    * 
132    * @see org.eclipse.ui.IWorkbenchPropertyPage#getElement()
133    */
134   public IAdaptable getElement() {
135     return element;
136   }
137   /**
138    * Returns true if this instance represents a property page
139    * 
140    * @return - true for property pages, false for preference pages
141    */
142   public boolean isPropertyPage() {
143     return getElement() != null;
144   }
145   /**
146    * We override the addField method. This allows us to store each field editor
147    * added by subclasses in a list for later processing.
148    * 
149    * @see org.eclipse.jface.preference.FieldEditorPreferencePage#addField(org.eclipse.jface.preference.FieldEditor)
150    */
151   protected void addField(FieldEditor editor) {
152     editors.add(editor);
153     super.addField(editor);
154   }
155   /**
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.
159    * 
160    * @see org.eclipse.jface.preference.PreferencePage#createControl()
161    */
162   public void createControl(Composite parent) {
163     // Special treatment for property pages
164     if (isPropertyPage()) {
165       // Cache the page id
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
171     }
172     super.createControl(parent);
173     // Update state of all subclass controls
174     if (isPropertyPage())
175       updateFieldEditors();
176   }
177   /**
178    * We override the createContents method. In case of property pages we insert
179    * two radio buttons at the top of the page.
180    * 
181    * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
182    */
183   protected Control createContents(Composite parent) {
184     if (isPropertyPage())
185       createSelectionGroup(parent);
186     return super.createContents(parent);
187   }
188   /**
189    * Creates and initializes a selection group with two choice buttons and one
190    * push button.
191    * 
192    * @param parent -
193    *            the parent composite
194    */
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$
210     } else {
211       useProjectSettingsButton = createRadioButton(radioGroup, Messages
212           .getString("OverlayPage.Use_Project_Settings")); //$NON-NLS-1$
213     }
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();
220       }
221     });
222     // Set workspace/project radio buttons
223     try {
224       String use = ((IResource) getElement())
225           .getPersistentProperty(new QualifiedName(pageId, USEPROJECTSETTINGS));
226       if (TRUE.equals(use)) {
227         useProjectSettingsButton.setSelection(true);
228         configureButton.setEnabled(false);
229       } else
230         useWorkspaceSettingsButton.setSelection(true);
231     } catch (CoreException e) {
232       useWorkspaceSettingsButton.setSelection(true);
233     }
234   }
235   /**
236    * Convenience method creating a radio button
237    * 
238    * @param parent -
239    *            the parent composite
240    * @param label -
241    *            the button label
242    * @return - the new button
243    */
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();
251       }
252     });
253     return button;
254   }
255   /**
256    * Returns in case of property pages the overlay store, in case of preference
257    * pages the standard preference store
258    * 
259    * @see org.eclipse.jface.preference.PreferencePage#getPreferenceStore()
260    */
261   public IPreferenceStore getPreferenceStore() {
262     if (isPropertyPage())
263       return overlayStore;
264     return super.getPreferenceStore();
265   }
266   /*
267    * Enables or disables the field editors and buttons of this page
268    */
269   private void updateFieldEditors() {
270     // We iterate through all field editors
271     boolean enabled = useProjectSettingsButton.getSelection();
272     updateFieldEditors(enabled);
273   }
274   /**
275    * Enables or disables the field editors and buttons of this page Subclasses
276    * may override.
277    * 
278    * @param enabled -
279    *            true if enabled
280    */
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);
287     }
288   }
289   /**
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.
293    * 
294    * @see org.eclipse.jface.preference.IPreferencePage#performOk()
295    */
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();
301       try {
302         String value = (useProjectSettingsButton.getSelection()) ? TRUE : FALSE;
303         resource.setPersistentProperty(new QualifiedName(pageId,
304             USEPROJECTSETTINGS), value);
305       } catch (CoreException e) {
306       }
307     }
308     return result;
309   }
310   /**
311    * We override the performDefaults method. In case of property pages we
312    * switch back to the workspace settings and disable the field editors.
313    * 
314    * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
315    */
316   protected void performDefaults() {
317     if (isPropertyPage()) {
318       useWorkspaceSettingsButton.setSelection(true);
319       useProjectSettingsButton.setSelection(false);
320       configureButton.setEnabled(true);
321       updateFieldEditors();
322     }
323     super.performDefaults();
324   }
325   /**
326    * Creates a new preferences page and opens it
327    * 
328    * @see com.bdaum.SpellChecker.preferences.SpellCheckerPreferencePage#configureWorkspaceSettings()
329    */
330   protected void configureWorkspaceSettings() {
331     try {
332       // create a new instance of the current class
333       IPreferencePage page = (IPreferencePage) this.getClass().newInstance();
334       page.setTitle(getTitle());
335       page.setImageDescriptor(image);
336       // and show it
337       showPreferencePage(pageId, page);
338     } catch (InstantiationException e) {
339       e.printStackTrace();
340     } catch (IllegalAccessException e) {
341       e.printStackTrace();
342     }
343   }
344   /**
345    * Show a single preference pages
346    * 
347    * @param id -
348    *            the preference page identification
349    * @param page -
350    *            the preference page
351    */
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() {
359       public void run() {
360         dialog.create();
361         dialog.setMessage(targetNode.getLabelText());
362         dialog.open();
363       }
364     });
365   }
366 }