KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > fix > CleanUpSaveParticipantPreferenceConfiguration


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.fix;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
19 import org.eclipse.core.runtime.preferences.IScopeContext;
20 import org.eclipse.core.runtime.preferences.InstanceScope;
21
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.ProjectScope;
24
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Link;
34 import org.eclipse.swt.widgets.Shell;
35
36 import org.eclipse.jface.preference.IPreferencePageContainer;
37
38 import org.eclipse.ui.dialogs.PreferencesUtil;
39 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
40
41 import org.eclipse.jdt.core.IJavaProject;
42 import org.eclipse.jdt.core.JavaCore;
43
44 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
45 import org.eclipse.jdt.internal.corext.fix.CleanUpPostSaveListener;
46 import org.eclipse.jdt.internal.corext.fix.CleanUpPreferenceUtil;
47 import org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring;
48
49 import org.eclipse.jdt.ui.JavaUI;
50
51 import org.eclipse.jdt.internal.ui.javaeditor.saveparticipant.AbstractSaveParticipantPreferenceConfiguration;
52 import org.eclipse.jdt.internal.ui.preferences.BulletListBlock;
53 import org.eclipse.jdt.internal.ui.preferences.CodeFormatterPreferencePage;
54 import org.eclipse.jdt.internal.ui.preferences.ImportOrganizePreferencePage;
55 import org.eclipse.jdt.internal.ui.util.PixelConverter;
56
57 /**
58  * Preference configuration UI for the clean up save participant.
59  *
60  * @since 3.3
61  */

62 public class CleanUpSaveParticipantPreferenceConfiguration extends AbstractSaveParticipantPreferenceConfiguration {
63     
64     private static final int INDENT= 10;
65     
66     private IScopeContext fContext;
67     private Map JavaDoc fSettings;
68     private BulletListBlock fSelectedActionsText;
69     private Button fFormatCodeButton;
70     private Button fOrganizeImportsButton;
71     private Shell fShell;
72     private Link fFormatConfigLink;
73     private Link fOrganizeImportsConfigLink;
74     private IPreferencePageContainer fContainer;
75     private Button fAdditionalActionButton;
76     private Button fConfigureButton;
77     
78     /**
79      * {@inheritDoc}
80      */

81     public Control createConfigControl(final Composite parent, IPreferencePageContainer container) {
82         fContainer= container;
83         fShell= parent.getShell();
84         
85         final Composite composite= new Composite(parent, SWT.NONE);
86         GridData gridData= new GridData(SWT.FILL, SWT.FILL, true, true);
87         gridData.horizontalIndent= INDENT;
88         composite.setLayoutData(gridData);
89         GridLayout gridLayout= new GridLayout(1, false);
90         gridLayout.marginHeight= 0;
91         gridLayout.marginWidth= 0;
92         composite.setLayout(gridLayout);
93         
94         fFormatCodeButton= new Button(composite, SWT.CHECK);
95         fFormatCodeButton.setText(SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_SaveActionPreferencePage_FormatSource_Checkbox);
96         fFormatCodeButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
97         fFormatCodeButton.addSelectionListener(new SelectionAdapter() {
98             /**
99              * {@inheritDoc}
100              */

101             public void widgetSelected(SelectionEvent e) {
102                 changeSettingsValue(CleanUpConstants.FORMAT_SOURCE_CODE, fFormatCodeButton.getSelection());
103             }
104         });
105         
106         PixelConverter pixelConverter= new PixelConverter(parent);
107         int heightOneHalf= (int)Math.round(pixelConverter.convertHeightInCharsToPixels(1) * 1.5);
108         
109         fFormatConfigLink= new Link(composite, SWT.NONE);
110         fFormatConfigLink.setText(SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_ConfigureFormatter_Link);
111         GridData gridData2= new GridData(SWT.LEFT, SWT.TOP, false, true);
112         gridData2.horizontalIndent= 20;
113         gridData2.minimumHeight= heightOneHalf;
114         fFormatConfigLink.setLayoutData(gridData2);
115         
116         fOrganizeImportsButton= new Button(composite, SWT.CHECK);
117         fOrganizeImportsButton.setText(SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_SaveActionPreferencePage_OrganizeImports_Checkbox);
118         fOrganizeImportsButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
119         fOrganizeImportsButton.addSelectionListener(new SelectionAdapter() {
120             /**
121              * {@inheritDoc}
122              */

123             public void widgetSelected(SelectionEvent e) {
124                 changeSettingsValue(CleanUpConstants.ORGANIZE_IMPORTS, fOrganizeImportsButton.getSelection());
125             }
126         });
127         
128         fOrganizeImportsConfigLink= new Link(composite, SWT.NONE);
129         fOrganizeImportsConfigLink.setText(SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_ConfigureImports_Link);
130         GridData gridData3= new GridData(SWT.LEFT, SWT.TOP, false, true);
131         gridData3.horizontalIndent= 20;
132         gridData3.minimumHeight= heightOneHalf;
133         fOrganizeImportsConfigLink.setLayoutData(gridData3);
134         
135         fAdditionalActionButton= new Button(composite, SWT.CHECK);
136         fAdditionalActionButton.setText(SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_AdditionalActions_Checkbox);
137         
138         createAdvancedComposite(composite);
139         fAdditionalActionButton.addSelectionListener(new SelectionAdapter() {
140             /**
141              * {@inheritDoc}
142              */

143             public void widgetSelected(SelectionEvent e) {
144                 changeSettingsValue(CleanUpConstants.CLEANUP_ON_SAVE_ADDITIONAL_OPTIONS, fAdditionalActionButton.getSelection());
145             }
146         });
147         
148         return composite;
149     }
150     
151     private Composite createAdvancedComposite(final Composite parent) {
152         Composite composite= new Composite(parent, SWT.NONE);
153         GridData gridData= new GridData(SWT.FILL, SWT.FILL, true, true);
154         gridData.horizontalIndent= INDENT;
155         composite.setLayoutData(gridData);
156         GridLayout layout= new GridLayout(2, false);
157         layout.marginHeight= 0;
158         layout.marginWidth= 0;
159         composite.setLayout(layout);
160         
161         fSelectedActionsText= new BulletListBlock();
162         final GridData data= (GridData)fSelectedActionsText.createControl(composite).getLayoutData();
163         data.heightHint= new PixelConverter(composite).convertHeightInCharsToPixels(8);
164         
165         fConfigureButton= new Button(composite, SWT.NONE);
166         fConfigureButton.setText(SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_Configure_Button);
167         fConfigureButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
168         fConfigureButton.addSelectionListener(new SelectionAdapter() {
169             /**
170              * {@inheritDoc}
171              */

172             public void widgetSelected(SelectionEvent e) {
173                 new CleanUpSaveParticipantConfigurationModifyDialog(parent.getShell(), fSettings, SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_CleanUpSaveParticipantConfiguration_Title).open();
174                 settingsChanged();
175             }
176             
177         });
178         
179         return composite;
180     }
181     
182     /**
183      * {@inheritDoc}
184      */

185     public void initialize(final IScopeContext context, IAdaptable element) {
186         fContext= context;
187         fSettings= CleanUpPreferenceUtil.loadSaveParticipantOptions(context);
188         
189         settingsChanged();
190         
191         IJavaProject javaProject= null;
192         if (element != null) {
193             IProject project= (IProject)element.getAdapter(IProject.class);
194             if (project != null) {
195                 IJavaProject jProject= JavaCore.create(project);
196                 if (jProject != null && jProject.exists()) {
197                     javaProject= jProject;
198                 }
199             }
200         }
201         
202         configurePreferenceLink(fFormatConfigLink, javaProject, CodeFormatterPreferencePage.PREF_ID, CodeFormatterPreferencePage.PROP_ID);
203         configurePreferenceLink(fOrganizeImportsConfigLink, javaProject, ImportOrganizePreferencePage.PREF_ID, ImportOrganizePreferencePage.PROP_ID);
204         
205         super.initialize(context, element);
206     }
207     
208     /**
209      * {@inheritDoc}
210      */

211     public void dispose() {
212         super.dispose();
213     }
214     
215     /**
216      * {@inheritDoc}
217      */

218     public void performDefaults() {
219         fSettings= CleanUpPreferenceUtil.loadSaveParticipantOptions(new InstanceScope());
220         settingsChanged();
221     }
222     
223     /**
224      * {@inheritDoc}
225      */

226     public void performOk() {
227         super.performOk();
228         
229         if (!ProjectScope.SCOPE.equals(fContext.getName()) || hasSettingsInScope(fContext))
230             CleanUpPreferenceUtil.saveSaveParticipantOptions(fContext, fSettings);
231     }
232     
233     /**
234      * {@inheritDoc}
235      */

236     public void enableProjectSettings() {
237         super.enableProjectSettings();
238         
239         CleanUpPreferenceUtil.saveSaveParticipantOptions(fContext, fSettings);
240         
241         updateAdvancedEnableState();
242     }
243     
244     /**
245      * {@inheritDoc}
246      */

247     public void disableProjectSettings() {
248         super.disableProjectSettings();
249         
250         IEclipsePreferences node= fContext.getNode(JavaUI.ID_PLUGIN);
251         
252         Map JavaDoc settings= CleanUpConstants.getSaveParticipantSettings();
253         for (Iterator JavaDoc iterator= settings.keySet().iterator(); iterator.hasNext();) {
254             String JavaDoc key= (String JavaDoc)iterator.next();
255             node.remove(CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + key);
256         }
257         
258         updateAdvancedEnableState();
259     }
260     
261     /**
262      * {@inheritDoc}
263      */

264     protected String JavaDoc getPostSaveListenerId() {
265         return CleanUpPostSaveListener.POSTSAVELISTENER_ID;
266     }
267     
268     /**
269      * {@inheritDoc}
270      */

271     protected String JavaDoc getPostSaveListenerName() {
272         return SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_CleanUpActionsTopNodeName_Checkbox;
273     }
274     
275     /**
276      * {@inheritDoc}
277      */

278     protected void enableConfigControl(boolean isEnabled) {
279         super.enableConfigControl(isEnabled);
280         
281         updateAdvancedEnableState();
282     }
283     
284     private void settingsChanged() {
285         fFormatCodeButton.setSelection(CleanUpConstants.TRUE.equals(fSettings.get(CleanUpConstants.FORMAT_SOURCE_CODE)));
286         fOrganizeImportsButton.setSelection(CleanUpConstants.TRUE.equals(fSettings.get(CleanUpConstants.ORGANIZE_IMPORTS)));
287         fAdditionalActionButton.setSelection(CleanUpConstants.TRUE.equals(fSettings.get(CleanUpConstants.CLEANUP_ON_SAVE_ADDITIONAL_OPTIONS)));
288         
289         updateAdvancedEnableState();
290         
291         Map JavaDoc settings= new HashMap JavaDoc(fSettings);
292         settings.put(CleanUpConstants.FORMAT_SOURCE_CODE, CleanUpConstants.FALSE);
293         settings.put(CleanUpConstants.ORGANIZE_IMPORTS, CleanUpConstants.FALSE);
294         
295         final ICleanUp[] cleanUps= CleanUpRefactoring.createCleanUps(settings);
296         
297         if (cleanUps.length == 0) {
298             fSelectedActionsText.setText(SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_NoActionEnabled_Info);
299         } else {
300             StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
301             
302             boolean first= true;
303             for (int i= 0; i < cleanUps.length; i++) {
304                 String JavaDoc[] descriptions= cleanUps[i].getDescriptions();
305                 if (descriptions != null) {
306                     for (int j= 0; j < descriptions.length; j++) {
307                         if (first) {
308                             first= false;
309                         } else {
310                             buf.append('\n');
311                         }
312                         buf.append(descriptions[j]);
313                     }
314                 }
315             }
316             fSelectedActionsText.setText(buf.toString());
317         }
318     }
319     
320     private void updateAdvancedEnableState() {
321         boolean additionalOptionEnabled= isEnabled(fContext) && CleanUpConstants.TRUE.equals(fSettings.get(CleanUpConstants.CLEANUP_ON_SAVE_ADDITIONAL_OPTIONS));
322         boolean additionalEnabled= additionalOptionEnabled && (!ProjectScope.SCOPE.equals(fContext.getName()) || hasSettingsInScope(fContext));
323         fSelectedActionsText.setEnabled(additionalEnabled);
324         fConfigureButton.setEnabled(additionalEnabled);
325     }
326     
327     private void configurePreferenceLink(Link link, final IJavaProject javaProject, final String JavaDoc preferenceId, final String JavaDoc propertyId) {
328         link.addSelectionListener(new SelectionAdapter() {
329             public void widgetSelected(SelectionEvent e) {
330                 if (fContainer instanceof IWorkbenchPreferenceContainer) {
331                     IWorkbenchPreferenceContainer container= (IWorkbenchPreferenceContainer)fContainer;
332                     if (javaProject != null) {
333                         container.openPage(propertyId, null);
334                     } else {
335                         container.openPage(preferenceId, null);
336                     }
337                 } else {
338                     PreferencesUtil.createPreferenceDialogOn(fShell, preferenceId, null, null);
339                 }
340             }
341         });
342     }
343     
344     private void changeSettingsValue(String JavaDoc key, boolean enabled) {
345         String JavaDoc value;
346         if (enabled) {
347             value= CleanUpConstants.TRUE;
348         } else {
349             value= CleanUpConstants.FALSE;
350         }
351         fSettings.put(key, value);
352         settingsChanged();
353     }
354 }
Popular Tags