KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > cleanup > CleanUpConfigurationBlock


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  * Aaron Luchko, aluchko@redhat.com - 105926 [Formatter] Exporting Unnamed profile fails silently
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.ui.preferences.cleanup;
13
14 import java.util.Hashtable JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Observable JavaDoc;
19 import java.util.Observer JavaDoc;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.preferences.DefaultScope;
23 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
24 import org.eclipse.core.runtime.preferences.IScopeContext;
25 import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
26
27 import org.eclipse.core.resources.IProject;
28
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Shell;
34
35 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
36 import org.eclipse.jdt.internal.corext.fix.CleanUpPreferenceUtil;
37 import org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring;
38
39 import org.eclipse.jdt.ui.JavaUI;
40
41 import org.eclipse.jdt.internal.ui.JavaPlugin;
42 import org.eclipse.jdt.internal.ui.fix.ICleanUp;
43 import org.eclipse.jdt.internal.ui.preferences.BulletListBlock;
44 import org.eclipse.jdt.internal.ui.preferences.PreferencesAccess;
45 import org.eclipse.jdt.internal.ui.preferences.formatter.IProfileVersioner;
46 import org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialog;
47 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileConfigurationBlock;
48 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager;
49 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileStore;
50 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile;
51 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile;
52 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
53 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
54 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
55
56
57 /**
58  * The clean up configuration block for the clean up preference page.
59  */

60 public class CleanUpConfigurationBlock extends ProfileConfigurationBlock {
61     
62     private static final String JavaDoc CLEANUP_PAGE_SETTINGS_KEY= "cleanup_page"; //$NON-NLS-1$
63
private static final String JavaDoc DIALOGSTORE_LASTSAVELOADPATH= JavaUI.ID_PLUGIN + ".cleanup"; //$NON-NLS-1$
64

65     private final IScopeContext fCurrContext;
66     private SelectionButtonDialogField fShowCleanUpWizardDialogField;
67     private CleanUpProfileManager fProfileManager;
68     private ProfileStore fProfileStore;
69     private BulletListBlock fBrowserBlock;
70     
71     public CleanUpConfigurationBlock(IProject project, PreferencesAccess access) {
72         super(project, access, DIALOGSTORE_LASTSAVELOADPATH);
73         
74         if (project != null) {
75             fCurrContext= null;
76         } else {
77             fCurrContext= access.getInstanceScope();
78         }
79     }
80
81     protected IProfileVersioner createProfileVersioner() {
82         return new CleanUpProfileVersioner();
83     }
84     
85     protected ProfileStore createProfileStore(IProfileVersioner versioner) {
86         fProfileStore= new ProfileStore(CleanUpConstants.CLEANUP_PROFILES, versioner);
87         return fProfileStore;
88     }
89     
90     protected ProfileManager createProfileManager(List JavaDoc profiles, IScopeContext context, PreferencesAccess access, IProfileVersioner profileVersioner) {
91         profiles.addAll(CleanUpPreferenceUtil.getBuiltInProfiles());
92         fProfileManager= new CleanUpProfileManager(profiles, context, access, profileVersioner);
93         return fProfileManager;
94     }
95     
96     /**
97      * {@inheritDoc}
98      */

99     protected void configurePreview(Composite composite, int numColumns, final ProfileManager profileManager) {
100         Map JavaDoc settings= profileManager.getSelected().getSettings();
101         final Map JavaDoc sharedSettings= new Hashtable JavaDoc();
102         fill(settings, sharedSettings);
103         
104         final ICleanUp[] cleanUps= CleanUpRefactoring.createCleanUps(sharedSettings);
105         
106         createLabel(composite, CleanUpMessages.CleanUpConfigurationBlock_SelectedCleanUps_label, numColumns);
107         
108         fBrowserBlock= new BulletListBlock();
109         Control control= fBrowserBlock.createControl(composite);
110         ((GridData)control.getLayoutData()).horizontalSpan= numColumns;
111         fBrowserBlock.setText(getSelectedCleanUpsInfo(cleanUps));
112         
113         profileManager.addObserver(new Observer JavaDoc() {
114
115             public void update(Observable JavaDoc o, Object JavaDoc arg) {
116                 final int value= ((Integer JavaDoc)arg).intValue();
117                 switch (value) {
118                 case ProfileManager.PROFILE_CREATED_EVENT:
119                 case ProfileManager.PROFILE_DELETED_EVENT:
120                 case ProfileManager.SELECTION_CHANGED_EVENT:
121                 case ProfileManager.SETTINGS_CHANGED_EVENT:
122                     fill(profileManager.getSelected().getSettings(), sharedSettings);
123                     fBrowserBlock.setText(getSelectedCleanUpsInfo(cleanUps));
124                 }
125             }
126             
127         });
128     }
129
130     private String JavaDoc getSelectedCleanUpsInfo(ICleanUp[] cleanUps) {
131         if (cleanUps.length == 0)
132             return ""; //$NON-NLS-1$
133

134         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
135         
136         boolean first= true;
137         for (int i= 0; i < cleanUps.length; i++) {
138             String JavaDoc[] descriptions= cleanUps[i].getDescriptions();
139             if (descriptions != null) {
140                 for (int j= 0; j < descriptions.length; j++) {
141                     if (first) {
142                         first= false;
143                     } else {
144                         buf.append('\n');
145                     }
146                     buf.append(descriptions[j]);
147                 }
148             }
149         }
150         
151         return buf.toString();
152     }
153
154     private void fill(Map JavaDoc settings, Map JavaDoc sharedSettings) {
155         sharedSettings.clear();
156         for (Iterator JavaDoc iterator= settings.keySet().iterator(); iterator.hasNext();) {
157             String JavaDoc key= (String JavaDoc)iterator.next();
158             sharedSettings.put(key, settings.get(key));
159         }
160     }
161
162     protected ModifyDialog createModifyDialog(Shell shell, Profile profile, ProfileManager profileManager, ProfileStore profileStore, boolean newProfile) {
163         return new CleanUpModifyDialog(shell, profile, profileManager, profileStore, newProfile, CLEANUP_PAGE_SETTINGS_KEY, DIALOGSTORE_LASTSAVELOADPATH);
164     }
165     
166     /**
167      * {@inheritDoc}
168      */

169     public Composite createContents(Composite parent) {
170         Composite composite= super.createContents(parent);
171         
172         if (fCurrContext == null)
173             return composite;
174         
175         fShowCleanUpWizardDialogField= new SelectionButtonDialogField(SWT.CHECK);
176         fShowCleanUpWizardDialogField.setLabelText(CleanUpMessages.CleanUpConfigurationBlock_ShowCleanUpWizard_checkBoxLabel);
177         fShowCleanUpWizardDialogField.doFillIntoGrid(composite, 5);
178         
179         IEclipsePreferences node= fCurrContext.getNode(JavaUI.ID_PLUGIN);
180         boolean showWizard;
181         if (node.get(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, null) != null) {
182             showWizard= node.getBoolean(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true);
183         } else {
184             showWizard= new DefaultScope().getNode(JavaUI.ID_PLUGIN).getBoolean(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true);
185         }
186         if (showWizard)
187             fShowCleanUpWizardDialogField.setSelection(true);
188         
189         fShowCleanUpWizardDialogField.setDialogFieldListener(new IDialogFieldListener() {
190             public void dialogFieldChanged(DialogField field) {
191                 doShowCleanUpWizard(fShowCleanUpWizardDialogField.isSelected());
192             }
193         });
194             
195         return composite;
196     }
197
198     private void doShowCleanUpWizard(boolean showWizard) {
199         IEclipsePreferences preferences= fCurrContext.getNode(JavaUI.ID_PLUGIN);
200         if (preferences.get(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, null) != null &&
201                 preferences.getBoolean(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true) == showWizard)
202             return;
203         
204         preferences.putBoolean(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, showWizard);
205     }
206     
207     /**
208      * {@inheritDoc}
209      */

210     public void performDefaults() {
211         super.performDefaults();
212         if (fCurrContext == null)
213             return;
214         
215         fCurrContext.getNode(JavaUI.ID_PLUGIN).remove(CleanUpConstants.SHOW_CLEAN_UP_WIZARD);
216         boolean showWizard= new DefaultScope().getNode(JavaUI.ID_PLUGIN).getBoolean(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true);
217         fShowCleanUpWizardDialogField.setDialogFieldListener(null);
218         fShowCleanUpWizardDialogField.setSelection(showWizard);
219         fShowCleanUpWizardDialogField.setDialogFieldListener(new IDialogFieldListener() {
220             public void dialogFieldChanged(DialogField field) {
221                 doShowCleanUpWizard(fShowCleanUpWizardDialogField.isSelected());
222             }
223         });
224     }
225     
226     /**
227      * {@inheritDoc}
228      */

229     protected void preferenceChanged(PreferenceChangeEvent event) {
230         if (CleanUpConstants.CLEANUP_PROFILES.equals(event.getKey())) {
231             try {
232                 String JavaDoc id= fCurrContext.getNode(JavaUI.ID_PLUGIN).get(CleanUpConstants.CLEANUP_PROFILE, null);
233                 if (id == null)
234                     fProfileManager.getDefaultProfile().getID();
235                 
236                 List JavaDoc oldProfiles= fProfileManager.getSortedProfiles();
237                 Profile[] oldProfilesArray= (Profile[])oldProfiles.toArray(new Profile[oldProfiles.size()]);
238                 for (int i= 0; i < oldProfilesArray.length; i++) {
239                     if (oldProfilesArray[i] instanceof CustomProfile) {
240                         fProfileManager.deleteProfile((CustomProfile)oldProfilesArray[i]);
241                     }
242                 }
243
244                 List JavaDoc newProfiles= fProfileStore.readProfilesFromString((String JavaDoc)event.getNewValue());
245                 for (Iterator JavaDoc iterator= newProfiles.iterator(); iterator.hasNext();) {
246                     CustomProfile profile= (CustomProfile)iterator.next();
247                     fProfileManager.addProfile(profile);
248                 }
249
250                 Profile profile= fProfileManager.getProfile(id);
251                 if (profile != null) {
252                     fProfileManager.setSelected(profile);
253                 } else {
254                     fProfileManager.setSelected(fProfileManager.getDefaultProfile());
255                 }
256             } catch (CoreException e) {
257                 JavaPlugin.log(e);
258             }
259         } else if (CleanUpConstants.CLEANUP_PROFILE.equals(event.getKey())) {
260             if (event.getNewValue() == null) {
261                 fProfileManager.setSelected(fProfileManager.getDefaultProfile());
262             } else {
263                 Profile profile= fProfileManager.getProfile((String JavaDoc)event.getNewValue());
264                 if (profile != null) {
265                     fProfileManager.setSelected(profile);
266                 }
267             }
268         }
269     }
270     
271     public void enableProjectSpecificSettings(boolean useProjectSpecificSettings) {
272         fBrowserBlock.setEnabled(useProjectSpecificSettings);
273     }
274 }
Popular Tags