KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > PropertyAndPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.internal.ui.preferences;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.core.runtime.IStatus;
19
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.ResourcesPlugin;
23
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.SelectionListener;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Link;
33
34 import org.eclipse.jface.dialogs.ControlEnableState;
35 import org.eclipse.jface.dialogs.Dialog;
36 import org.eclipse.jface.preference.PreferencePage;
37 import org.eclipse.jface.window.Window;
38
39 import org.eclipse.ui.IWorkbench;
40 import org.eclipse.ui.IWorkbenchPreferencePage;
41 import org.eclipse.ui.IWorkbenchPropertyPage;
42 import org.eclipse.ui.dialogs.PreferencesUtil;
43
44 import org.eclipse.jdt.core.IJavaProject;
45 import org.eclipse.jdt.core.JavaCore;
46 import org.eclipse.jdt.core.JavaModelException;
47
48 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
49 import org.eclipse.jdt.internal.ui.dialogs.StatusUtil;
50 import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
51 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
52 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
53 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
54 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
55
56 /**
57  * Base for project property and preference pages
58  */

59 public abstract class PropertyAndPreferencePage extends PreferencePage implements IWorkbenchPreferencePage, IWorkbenchPropertyPage {
60     
61     private Control fConfigurationBlockControl;
62     private ControlEnableState fBlockEnableState;
63     private Link fChangeWorkspaceSettings;
64     private SelectionButtonDialogField fUseProjectSettings;
65     private IStatus fBlockStatus;
66     private Composite fParentComposite;
67     
68     private IProject fProject; // project or null
69
private Map JavaDoc fData; // page data
70

71     public static final String JavaDoc DATA_NO_LINK= "PropertyAndPreferencePage.nolink"; //$NON-NLS-1$
72

73     public PropertyAndPreferencePage() {
74         fBlockStatus= new StatusInfo();
75         fBlockEnableState= null;
76         fProject= null;
77         fData= null;
78     }
79
80     protected abstract Control createPreferenceContent(Composite composite);
81     protected abstract boolean hasProjectSpecificOptions(IProject project);
82     
83     protected abstract String JavaDoc getPreferencePageID();
84     protected abstract String JavaDoc getPropertyPageID();
85     
86     protected boolean supportsProjectSpecificOptions() {
87         return getPropertyPageID() != null;
88     }
89     
90     protected boolean offerLink() {
91         return fData == null || !Boolean.TRUE.equals(fData.get(DATA_NO_LINK));
92     }
93     
94     protected Label createDescriptionLabel(Composite parent) {
95         fParentComposite= parent;
96         if (isProjectPreferencePage()) {
97             Composite composite= new Composite(parent, SWT.NONE);
98             composite.setFont(parent.getFont());
99             GridLayout layout= new GridLayout();
100             layout.marginHeight= 0;
101             layout.marginWidth= 0;
102             layout.numColumns= 2;
103             composite.setLayout(layout);
104             composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
105             
106             IDialogFieldListener listener= new IDialogFieldListener() {
107                 public void dialogFieldChanged(DialogField field) {
108                     enableProjectSpecificSettings(((SelectionButtonDialogField)field).isSelected());
109                 }
110             };
111             
112             fUseProjectSettings= new SelectionButtonDialogField(SWT.CHECK);
113             fUseProjectSettings.setDialogFieldListener(listener);
114             fUseProjectSettings.setLabelText(PreferencesMessages.PropertyAndPreferencePage_useprojectsettings_label);
115             fUseProjectSettings.doFillIntoGrid(composite, 1);
116             LayoutUtil.setHorizontalGrabbing(fUseProjectSettings.getSelectionButton(null));
117             
118             if (offerLink()) {
119                 fChangeWorkspaceSettings= createLink(composite, PreferencesMessages.PropertyAndPreferencePage_useworkspacesettings_change);
120                 fChangeWorkspaceSettings.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
121             } else {
122                 LayoutUtil.setHorizontalSpan(fUseProjectSettings.getSelectionButton(null), 2);
123             }
124             
125             Label horizontalLine= new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
126             horizontalLine.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, 2, 1));
127             horizontalLine.setFont(composite.getFont());
128         } else if (supportsProjectSpecificOptions() && offerLink()) {
129             fChangeWorkspaceSettings= createLink(parent, PreferencesMessages.PropertyAndPreferencePage_showprojectspecificsettings_label);
130             fChangeWorkspaceSettings.setLayoutData(new GridData(SWT.END, SWT.CENTER, true, false));
131         }
132
133         return super.createDescriptionLabel(parent);
134     }
135     
136     /*
137      * @see org.eclipse.jface.preference.IPreferencePage#createContents(Composite)
138      */

139     protected Control createContents(Composite parent) {
140         Composite composite= new Composite(parent, SWT.NONE);
141         GridLayout layout= new GridLayout();
142         layout.marginHeight= 0;
143         layout.marginWidth= 0;
144         composite.setLayout(layout);
145         composite.setFont(parent.getFont());
146             
147         GridData data= new GridData(GridData.FILL, GridData.FILL, true, true);
148         
149         fConfigurationBlockControl= createPreferenceContent(composite);
150         fConfigurationBlockControl.setLayoutData(data);
151
152         if (isProjectPreferencePage()) {
153             boolean useProjectSettings= hasProjectSpecificOptions(getProject());
154             enableProjectSpecificSettings(useProjectSettings);
155         }
156
157         Dialog.applyDialogFont(composite);
158         return composite;
159     }
160
161     private Link createLink(Composite composite, String JavaDoc text) {
162         Link link= new Link(composite, SWT.NONE);
163         link.setFont(composite.getFont());
164         link.setText("<A>" + text + "</A>"); //$NON-NLS-1$//$NON-NLS-2$
165
link.addSelectionListener(new SelectionListener() {
166             public void widgetSelected(SelectionEvent e) {
167                 doLinkActivated((Link) e.widget);
168             }
169
170             public void widgetDefaultSelected(SelectionEvent e) {
171                 doLinkActivated((Link) e.widget);
172             }
173         });
174         return link;
175     }
176     
177     protected boolean useProjectSettings() {
178         return isProjectPreferencePage() && fUseProjectSettings != null && fUseProjectSettings.isSelected();
179     }
180     
181     protected boolean isProjectPreferencePage() {
182         return fProject != null;
183     }
184     
185     protected IProject getProject() {
186         return fProject;
187     }
188     
189     final void doLinkActivated(Link link) {
190         Map JavaDoc data= new HashMap JavaDoc();
191         data.put(DATA_NO_LINK, Boolean.TRUE);
192         
193         if (isProjectPreferencePage()) {
194             openWorkspacePreferences(data);
195         } else {
196             HashSet JavaDoc projectsWithSpecifics= new HashSet JavaDoc();
197             try {
198                 IJavaProject[] projects= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
199                 for (int i= 0; i < projects.length; i++) {
200                     IJavaProject curr= projects[i];
201                     if (hasProjectSpecificOptions(curr.getProject())) {
202                         projectsWithSpecifics.add(curr);
203                     }
204                 }
205             } catch (JavaModelException e) {
206                 // ignore
207
}
208             ProjectSelectionDialog dialog= new ProjectSelectionDialog(getShell(), projectsWithSpecifics);
209             if (dialog.open() == Window.OK) {
210                 IJavaProject res= (IJavaProject) dialog.getFirstResult();
211                 openProjectProperties(res.getProject(), data);
212             }
213         }
214     }
215     
216     protected final void openWorkspacePreferences(Object JavaDoc data) {
217         String JavaDoc id= getPreferencePageID();
218         PreferencesUtil.createPreferenceDialogOn(getShell(), id, new String JavaDoc[] { id }, data).open();
219     }
220     
221     protected final void openProjectProperties(IProject project, Object JavaDoc data) {
222         String JavaDoc id= getPropertyPageID();
223         if (id != null) {
224             PreferencesUtil.createPropertyDialogOn(getShell(), project, id, new String JavaDoc[] { id }, data).open();
225         }
226     }
227     
228     
229     protected void enableProjectSpecificSettings(boolean useProjectSpecificSettings) {
230         fUseProjectSettings.setSelection(useProjectSpecificSettings);
231         enablePreferenceContent(useProjectSpecificSettings);
232         updateLinkVisibility();
233         doStatusChanged();
234     }
235     
236     private void updateLinkVisibility() {
237         if (fChangeWorkspaceSettings == null || fChangeWorkspaceSettings.isDisposed()) {
238             return;
239         }
240         
241         if (isProjectPreferencePage()) {
242             fChangeWorkspaceSettings.setEnabled(!useProjectSettings());
243         }
244     }
245     
246
247     protected void setPreferenceContentStatus(IStatus status) {
248         fBlockStatus= status;
249         doStatusChanged();
250     }
251     
252     /**
253      * Returns a new status change listener that calls {@link #setPreferenceContentStatus(IStatus)}
254      * when the status has changed
255      * @return The new listener
256      */

257     protected IStatusChangeListener getNewStatusChangedListener() {
258         return new IStatusChangeListener() {
259             public void statusChanged(IStatus status) {
260                 setPreferenceContentStatus(status);
261             }
262         };
263     }
264     
265     protected IStatus getPreferenceContentStatus() {
266         return fBlockStatus;
267     }
268
269     protected void doStatusChanged() {
270         if (!isProjectPreferencePage() || useProjectSettings()) {
271             updateStatus(fBlockStatus);
272         } else {
273             updateStatus(new StatusInfo());
274         }
275     }
276         
277     protected void enablePreferenceContent(boolean enable) {
278         if (enable) {
279             if (fBlockEnableState != null) {
280                 fBlockEnableState.restore();
281                 fBlockEnableState= null;
282             }
283         } else {
284             if (fBlockEnableState == null) {
285                 fBlockEnableState= ControlEnableState.disable(fConfigurationBlockControl);
286             }
287         }
288     }
289     
290     /*
291      * @see org.eclipse.jface.preference.IPreferencePage#performDefaults()
292      */

293     protected void performDefaults() {
294         if (useProjectSettings()) {
295             enableProjectSpecificSettings(false);
296         }
297         super.performDefaults();
298     }
299
300     private void updateStatus(IStatus status) {
301         setValid(!status.matches(IStatus.ERROR));
302         StatusUtil.applyToStatusLine(this, status);
303     }
304
305     /* (non-Javadoc)
306      * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
307      */

308     public void init(IWorkbench workbench) {
309     }
310
311     /* (non-Javadoc)
312      * @see org.eclipse.ui.IWorkbenchPropertyPage#getElement()
313      */

314     public IAdaptable getElement() {
315         return fProject;
316     }
317
318     /* (non-Javadoc)
319      * @see org.eclipse.ui.IWorkbenchPropertyPage#setElement(org.eclipse.core.runtime.IAdaptable)
320      */

321     public void setElement(IAdaptable element) {
322         fProject= (IProject) element.getAdapter(IResource.class);
323     }
324     
325     
326     /* (non-Javadoc)
327      * @see org.eclipse.jface.preference.PreferencePage#applyData(java.lang.Object)
328      */

329     public void applyData(Object JavaDoc data) {
330         if (data instanceof Map JavaDoc) {
331             fData= (Map JavaDoc) data;
332         }
333         if (fChangeWorkspaceSettings != null) {
334             if (!offerLink()) {
335                 fChangeWorkspaceSettings.dispose();
336                 fParentComposite.layout(true, true);
337             }
338         }
339     }
340     
341     protected Map JavaDoc getData() {
342         return fData;
343     }
344     
345 }
346
Popular Tags