]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/preferences/PropertyAndPreferencePage.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / preferences / PropertyAndPreferencePage.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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;
14 import java.util.HashSet;
15 import java.util.Map;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.SelectionListener;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.swt.widgets.Link;
26
27 import org.eclipse.core.runtime.IAdaptable;
28 import org.eclipse.core.runtime.IStatus;
29
30 import org.eclipse.core.resources.IProject;
31 import org.eclipse.core.resources.IResource;
32 import org.eclipse.core.resources.ResourcesPlugin;
33
34 import org.eclipse.jface.dialogs.ControlEnableState;
35 import org.eclipse.jface.dialogs.Dialog;
36 import org.eclipse.jface.preference.PreferencePage;
37
38 import org.eclipse.ui.IWorkbench;
39 import org.eclipse.ui.IWorkbenchPreferencePage;
40 import org.eclipse.ui.IWorkbenchPropertyPage;
41 import org.eclipse.ui.dialogs.PreferencesUtil;
42
43 import org.eclipse.jdt.core.IJavaProject;
44 import org.eclipse.jdt.core.JavaCore;
45 import org.eclipse.jdt.core.JavaModelException;
46
47 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
48 import org.eclipse.jdt.internal.ui.dialogs.StatusUtil;
49 import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
50 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
51 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
52 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
53 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
54
55 /**
56  * Base for project property and preference pages
57  */
58 public abstract class PropertyAndPreferencePage extends PreferencePage implements IWorkbenchPreferencePage, IWorkbenchPropertyPage {
59
60         private Control fConfigurationBlockControl;
61         private ControlEnableState fBlockEnableState;
62         private Link fChangeWorkspaceSettings;
63         public SelectionButtonDialogField fUseProjectSettings;
64         private IStatus fBlockStatus;
65         private Composite fParentComposite;
66
67         private IProject fProject; // project or null
68         private Map<String, Object> fData; // page data
69
70         public static final String DATA_NO_LINK= "PropertyAndPreferencePage.nolink"; //$NON-NLS-1$
71
72         public PropertyAndPreferencePage() {
73                 fBlockStatus= new StatusInfo();
74                 fBlockEnableState= null;
75                 fProject= null;
76                 fData= null;
77         }
78
79         protected abstract Control createPreferenceContent(Composite composite);
80         protected abstract boolean hasProjectSpecificOptions(IProject project);
81
82         protected abstract String getPreferencePageID();
83         protected abstract String getPropertyPageID();
84
85         protected boolean supportsProjectSpecificOptions() {
86                 return getPropertyPageID() != null;
87         }
88
89         protected boolean offerLink() {
90                 return fData == null || !Boolean.TRUE.equals(fData.get(DATA_NO_LINK));
91         }
92
93     @Override
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                                         boolean enabled= ((SelectionButtonDialogField) field).isSelected();
109                                         enableProjectSpecificSettings(enabled);
110
111                                         if (enabled && getData() != null) {
112                                                 applyData(getData());
113                                         }
114                                 }
115                         };
116
117                         fUseProjectSettings= new SelectionButtonDialogField(SWT.CHECK);
118                         fUseProjectSettings.generated_3808165026892968926(composite, listener);
119
120                         if (offerLink()) {
121                                 fChangeWorkspaceSettings= createLink(composite, PreferencesMessages.PropertyAndPreferencePage_useworkspacesettings_change);
122                                 fChangeWorkspaceSettings.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
123                         } else {
124                                 LayoutUtil.setHorizontalSpan(fUseProjectSettings.getSelectionButton(null), 2);
125                         }
126
127                         Label horizontalLine= new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
128                         horizontalLine.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, 2, 1));
129                         horizontalLine.setFont(composite.getFont());
130                 } else if (supportsProjectSpecificOptions() && offerLink()) {
131                         fChangeWorkspaceSettings= createLink(parent, PreferencesMessages.PropertyAndPreferencePage_showprojectspecificsettings_label);
132                         fChangeWorkspaceSettings.setLayoutData(new GridData(SWT.END, SWT.CENTER, true, false));
133                 }
134
135                 return super.createDescriptionLabel(parent);
136     }
137
138         /*
139          * @see org.eclipse.jface.preference.IPreferencePage#createContents(Composite)
140          */
141         @Override
142         protected Control createContents(Composite parent) {
143                 Composite composite= new Composite(parent, SWT.NONE);
144                 GridLayout layout= new GridLayout();
145                 layout.marginHeight= 0;
146                 layout.marginWidth= 0;
147                 composite.setLayout(layout);
148                 composite.setFont(parent.getFont());
149
150                 GridData data= new GridData(GridData.FILL, GridData.FILL, true, true);
151
152                 fConfigurationBlockControl= createPreferenceContent(composite);
153                 fConfigurationBlockControl.setLayoutData(data);
154
155                 if (isProjectPreferencePage()) {
156                         boolean useProjectSettings= hasProjectSpecificOptions(getProject());
157                         enableProjectSpecificSettings(useProjectSettings);
158                 }
159
160                 Dialog.applyDialogFont(composite);
161                 return composite;
162         }
163
164         private Link createLink(Composite composite, String text) {
165                 Link link= new Link(composite, SWT.NONE);
166                 link.setFont(composite.getFont());
167                 link.setText("<A>" + text + "</A>");  //$NON-NLS-1$//$NON-NLS-2$
168                 link.addSelectionListener(new SelectionListener() {
169                         public void widgetSelected(SelectionEvent e) {
170                                 doLinkActivated((Link) e.widget);
171                         }
172
173                         public void widgetDefaultSelected(SelectionEvent e) {
174                                 doLinkActivated((Link) e.widget);
175                         }
176                 });
177                 return link;
178         }
179
180         protected boolean useProjectSettings() {
181                 return isProjectPreferencePage() && fUseProjectSettings != null && fUseProjectSettings.isSelected();
182         }
183
184         protected boolean isProjectPreferencePage() {
185                 return fProject != null;
186         }
187
188         protected IProject getProject() {
189                 return fProject;
190         }
191
192         /**
193          * Handle link activation.
194          *
195          * @param link the link
196          */
197         final void doLinkActivated(Link link) {
198                 Map<String, Object> data= getData();
199                 if (data == null)
200                         data= new HashMap<String, Object>();
201                 data.put(DATA_NO_LINK, Boolean.TRUE);
202
203                 if (isProjectPreferencePage()) {
204                         openWorkspacePreferences(data);
205                 } else {
206                         HashSet<IJavaProject> projectsWithSpecifics= new HashSet<IJavaProject>();
207                         try {
208                                 IJavaProject[] projects= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
209                                 for (int i= 0; i < projects.length; i++) {
210                                         IJavaProject curr= projects[i];
211                                         if (hasProjectSpecificOptions(curr.getProject())) {
212                                                 projectsWithSpecifics.add(curr);
213                                         }
214                                 }
215                         } catch (JavaModelException e) {
216                                 // ignore
217                         }
218                         ProjectSelectionDialog dialog= new ProjectSelectionDialog(getShell(), projectsWithSpecifics);
219                         dialog.generated_2705229931928849964(data, this);
220                 }
221         }
222
223         protected final void openWorkspacePreferences(Object data) {
224                 String id= getPreferencePageID();
225                 PreferencesUtil.createPreferenceDialogOn(getShell(), id, new String[] { id }, data).open();
226         }
227
228         protected final void openProjectProperties(IProject project, Object data) {
229                 String id= getPropertyPageID();
230                 if (id != null) {
231                         PreferencesUtil.createPropertyDialogOn(getShell(), project, id, new String[] { id }, data).open();
232                 }
233         }
234
235
236         protected void enableProjectSpecificSettings(boolean useProjectSpecificSettings) {
237                 fUseProjectSettings.setSelection(useProjectSpecificSettings);
238                 enablePreferenceContent(useProjectSpecificSettings);
239                 updateLinkVisibility();
240                 doStatusChanged();
241         }
242
243         private void updateLinkVisibility() {
244                 if (fChangeWorkspaceSettings == null || fChangeWorkspaceSettings.isDisposed()) {
245                         return;
246                 }
247
248                 if (isProjectPreferencePage()) {
249                         fChangeWorkspaceSettings.setEnabled(!useProjectSettings());
250                 }
251         }
252
253
254         protected void setPreferenceContentStatus(IStatus status) {
255                 fBlockStatus= status;
256                 doStatusChanged();
257         }
258
259         /**
260          * Returns a new status change listener that calls {@link #setPreferenceContentStatus(IStatus)}
261          * when the status has changed
262          * @return The new listener
263          */
264         protected IStatusChangeListener getNewStatusChangedListener() {
265                 return new IStatusChangeListener() {
266                         public void statusChanged(IStatus status) {
267                                 setPreferenceContentStatus(status);
268                         }
269                 };
270         }
271
272         protected IStatus getPreferenceContentStatus() {
273                 return fBlockStatus;
274         }
275
276         protected void doStatusChanged() {
277                 if (!isProjectPreferencePage() || useProjectSettings()) {
278                         updateStatus(fBlockStatus);
279                 } else {
280                         updateStatus(new StatusInfo());
281                 }
282         }
283
284         protected void enablePreferenceContent(boolean enable) {
285                 if (enable) {
286                         if (fBlockEnableState != null) {
287                                 fBlockEnableState.restore();
288                                 fBlockEnableState= null;
289                         }
290                 } else {
291                         if (fBlockEnableState == null) {
292                                 fBlockEnableState= ControlEnableState.disable(fConfigurationBlockControl);
293                         }
294                 }
295         }
296
297         /*
298          * @see org.eclipse.jface.preference.IPreferencePage#performDefaults()
299          */
300         @Override
301         protected void performDefaults() {
302                 if (useProjectSettings()) {
303                         enableProjectSpecificSettings(false);
304                 }
305                 super.performDefaults();
306         }
307
308         private void updateStatus(IStatus status) {
309                 setValid(!status.matches(IStatus.ERROR));
310                 StatusUtil.applyToStatusLine(this, status);
311         }
312
313         /* (non-Javadoc)
314          * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
315          */
316         public void init(IWorkbench workbench) {
317         }
318
319         /* (non-Javadoc)
320          * @see org.eclipse.ui.IWorkbenchPropertyPage#getElement()
321          */
322         public IAdaptable getElement() {
323                 return fProject;
324         }
325
326         /* (non-Javadoc)
327          * @see org.eclipse.ui.IWorkbenchPropertyPage#setElement(org.eclipse.core.runtime.IAdaptable)
328          */
329         public void setElement(IAdaptable element) {
330                 fProject= (IProject) element.getAdapter(IResource.class);
331         }
332
333
334         /* (non-Javadoc)
335          * @see org.eclipse.jface.preference.PreferencePage#applyData(java.lang.Object)
336          */
337         @SuppressWarnings("unchecked")
338         @Override
339         public void applyData(Object data) {
340                 if (data instanceof Map) {
341                         fData= (Map<String, Object>) data;
342                 }
343                 if (fChangeWorkspaceSettings != null) {
344                         if (!offerLink()) {
345                                 fChangeWorkspaceSettings.dispose();
346                                 fParentComposite.layout(true, true);
347                         }
348                 }
349         }
350
351         protected Map<String, Object> getData() {
352                 return fData;
353         }
354
355 }