KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > fix > CleanUpPreferenceUtil


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.corext.fix;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.preferences.DefaultScope;
21 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
22 import org.eclipse.core.runtime.preferences.IScopeContext;
23 import org.eclipse.core.runtime.preferences.InstanceScope;
24
25 import org.eclipse.core.resources.ProjectScope;
26
27 import org.eclipse.jdt.ui.JavaUI;
28
29 import org.eclipse.jdt.internal.ui.JavaPlugin;
30 import org.eclipse.jdt.internal.ui.preferences.cleanup.CleanUpMessages;
31 import org.eclipse.jdt.internal.ui.preferences.cleanup.CleanUpProfileManager;
32 import org.eclipse.jdt.internal.ui.preferences.cleanup.CleanUpProfileVersioner;
33 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager;
34 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileStore;
35 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.BuiltInProfile;
36 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile;
37 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.KeySet;
38 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile;
39
40 public class CleanUpPreferenceUtil {
41     
42     public static final String JavaDoc SAVE_PARTICIPANT_KEY_PREFIX= "sp_"; //$NON-NLS-1$
43

44     public static Map JavaDoc loadOptions(IScopeContext context) {
45         return loadOptions(context, CleanUpConstants.CLEANUP_PROFILE, CleanUpConstants.DEFAULT_PROFILE);
46     }
47
48     private static Map JavaDoc loadOptions(IScopeContext context, String JavaDoc profileIdKey, String JavaDoc defaultProfileId) {
49         IEclipsePreferences contextNode= context.getNode(JavaUI.ID_PLUGIN);
50         String JavaDoc id= contextNode.get(profileIdKey, null);
51         
52         if (id != null && ProjectScope.SCOPE.equals(context.getName())) {
53             return loadFromProject(context);
54         }
55         
56         InstanceScope instanceScope= new InstanceScope();
57         if (id == null) {
58             if (ProjectScope.SCOPE.equals(context.getName())) {
59                 id= instanceScope.getNode(JavaUI.ID_PLUGIN).get(profileIdKey, null);
60             }
61             if (id == null) {
62                 id= new DefaultScope().getNode(JavaUI.ID_PLUGIN).get(profileIdKey, defaultProfileId);
63             }
64         }
65         
66         List JavaDoc builtInProfiles= getBuiltInProfiles();
67         for (Iterator JavaDoc iterator= builtInProfiles.iterator(); iterator.hasNext();) {
68             Profile profile= (Profile)iterator.next();
69             if (id.equals(profile.getID()))
70                 return profile.getSettings();
71         }
72         
73         if (id.equals(CleanUpConstants.SAVE_PARTICIPANT_PROFILE))
74             return CleanUpConstants.getSaveParticipantSettings();
75         
76         CleanUpProfileVersioner versioner= new CleanUpProfileVersioner();
77         ProfileStore profileStore= new ProfileStore(CleanUpConstants.CLEANUP_PROFILES, versioner);
78         
79         List JavaDoc list= null;
80         try {
81             list= profileStore.readProfiles(instanceScope);
82         } catch (CoreException e1) {
83             JavaPlugin.log(e1);
84         }
85         if (list == null)
86             return null;
87         
88         for (Iterator JavaDoc iterator= list.iterator(); iterator.hasNext();) {
89             Profile profile= (Profile)iterator.next();
90             if (id.equals(profile.getID()))
91                 return profile.getSettings();
92         }
93         
94         return null;
95     }
96     
97     private static Map JavaDoc loadFromProject(IScopeContext context) {
98         final Map JavaDoc profileOptions= new HashMap JavaDoc();
99         IEclipsePreferences uiPrefs= context.getNode(JavaUI.ID_PLUGIN);
100         
101         CleanUpProfileVersioner versioner= new CleanUpProfileVersioner();
102         
103         Map JavaDoc defaultSettings= CleanUpConstants.getEclipseDefaultSettings();
104         KeySet[] keySets= CleanUpProfileManager.KEY_SETS;
105         
106         boolean hasValues= false;
107         for (int i= 0; i < keySets.length; i++) {
108             KeySet keySet= keySets[i];
109             IEclipsePreferences preferences= context.getNode(keySet.getNodeName());
110             for (final Iterator JavaDoc keyIter = keySet.getKeys().iterator(); keyIter.hasNext(); ) {
111                 final String JavaDoc key= (String JavaDoc) keyIter.next();
112                 Object JavaDoc val= preferences.get(key, null);
113                 if (val != null) {
114                     hasValues= true;
115                 } else {
116                     val= defaultSettings.get(key);
117                 }
118                 profileOptions.put(key, val);
119             }
120         }
121         
122         if (!hasValues)
123             return null;
124                 
125         int version= uiPrefs.getInt(CleanUpConstants.CLEANUP_SETTINGS_VERSION_KEY, versioner.getFirstVersion());
126         if (version == versioner.getCurrentVersion())
127             return profileOptions;
128         
129         CustomProfile profile= new CustomProfile("tmp", profileOptions, version, versioner.getProfileKind()); //$NON-NLS-1$
130
versioner.update(profile);
131         return profile.getSettings();
132     }
133
134     public static Map JavaDoc loadSaveParticipantOptions(IScopeContext context) {
135         IEclipsePreferences node;
136         if (hasSettingsInScope(context)) {
137             node= context.getNode(JavaUI.ID_PLUGIN);
138         } else {
139             IScopeContext instanceScope= new InstanceScope();
140             if (hasSettingsInScope(instanceScope)) {
141                 node= instanceScope.getNode(JavaUI.ID_PLUGIN);
142             } else {
143                 return CleanUpConstants.getSaveParticipantSettings();
144             }
145         }
146         
147         Map JavaDoc result= new HashMap JavaDoc();
148         Map JavaDoc defaultSettings= CleanUpConstants.getSaveParticipantSettings();
149         for (Iterator JavaDoc iterator= defaultSettings.keySet().iterator(); iterator.hasNext();) {
150             String JavaDoc key= (String JavaDoc)iterator.next();
151             result.put(key, node.get(SAVE_PARTICIPANT_KEY_PREFIX + key, CleanUpConstants.FALSE));
152         }
153         
154         return result;
155     }
156     
157     public static void saveSaveParticipantOptions(IScopeContext context, Map JavaDoc settings) {
158         IEclipsePreferences node= context.getNode(JavaUI.ID_PLUGIN);
159         for (Iterator JavaDoc iterator= settings.keySet().iterator(); iterator.hasNext();) {
160             String JavaDoc key= (String JavaDoc)iterator.next();
161             node.put(SAVE_PARTICIPANT_KEY_PREFIX + key, (String JavaDoc)settings.get(key));
162         }
163     }
164
165     private static boolean hasSettingsInScope(IScopeContext context) {
166         IEclipsePreferences node= context.getNode(JavaUI.ID_PLUGIN);
167         
168         Map JavaDoc defaultSettings= CleanUpConstants.getSaveParticipantSettings();
169         for (Iterator JavaDoc iterator= defaultSettings.keySet().iterator(); iterator.hasNext();) {
170             String JavaDoc key= (String JavaDoc)iterator.next();
171             if (node.get(SAVE_PARTICIPANT_KEY_PREFIX + key, null) != null)
172                 return true;
173         }
174         
175         return false;
176     }
177
178     /**
179      * Returns a list of {@link ProfileManager.Profile} stored in the <code>scope</code>
180      * including the built in profiles.
181      * @param scope the context from which to retrieve the profiles
182      * @return list of profiles, not null
183      * @since 3.3
184      */

185     public static List JavaDoc loadProfiles(IScopeContext scope) {
186         
187         CleanUpProfileVersioner versioner= new CleanUpProfileVersioner();
188         ProfileStore profileStore= new ProfileStore(CleanUpConstants.CLEANUP_PROFILES, versioner);
189         
190         List JavaDoc list= null;
191         try {
192             list= profileStore.readProfiles(scope);
193         } catch (CoreException e1) {
194             JavaPlugin.log(e1);
195         }
196         if (list == null) {
197             list= getBuiltInProfiles();
198         } else {
199             list.addAll(getBuiltInProfiles());
200         }
201         
202         return list;
203     }
204
205     /**
206      * Returns a list of built in clean up profiles
207      * @return the list of built in profiles, not null
208      * @since 3.3
209      */

210     public static List JavaDoc getBuiltInProfiles() {
211         ArrayList JavaDoc result= new ArrayList JavaDoc();
212         
213         final Profile eclipseProfile= new BuiltInProfile(CleanUpConstants.ECLIPSE_PROFILE, CleanUpMessages.CleanUpProfileManager_ProfileName_EclipseBuildIn, CleanUpConstants.getEclipseDefaultSettings(), 2, CleanUpProfileVersioner.CURRENT_VERSION, CleanUpProfileVersioner.PROFILE_KIND);
214         result.add(eclipseProfile);
215         
216         return result;
217     }
218
219 }
220
Popular Tags