1 11 package org.eclipse.jdt.internal.corext.fix; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 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 SAVE_PARTICIPANT_KEY_PREFIX= "sp_"; 44 public static Map loadOptions(IScopeContext context) { 45 return loadOptions(context, CleanUpConstants.CLEANUP_PROFILE, CleanUpConstants.DEFAULT_PROFILE); 46 } 47 48 private static Map loadOptions(IScopeContext context, String profileIdKey, String defaultProfileId) { 49 IEclipsePreferences contextNode= context.getNode(JavaUI.ID_PLUGIN); 50 String 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 builtInProfiles= getBuiltInProfiles(); 67 for (Iterator 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 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 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 loadFromProject(IScopeContext context) { 98 final Map profileOptions= new HashMap (); 99 IEclipsePreferences uiPrefs= context.getNode(JavaUI.ID_PLUGIN); 100 101 CleanUpProfileVersioner versioner= new CleanUpProfileVersioner(); 102 103 Map 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 keyIter = keySet.getKeys().iterator(); keyIter.hasNext(); ) { 111 final String key= (String ) keyIter.next(); 112 Object 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()); versioner.update(profile); 131 return profile.getSettings(); 132 } 133 134 public static Map 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 result= new HashMap (); 148 Map defaultSettings= CleanUpConstants.getSaveParticipantSettings(); 149 for (Iterator iterator= defaultSettings.keySet().iterator(); iterator.hasNext();) { 150 String key= (String )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 settings) { 158 IEclipsePreferences node= context.getNode(JavaUI.ID_PLUGIN); 159 for (Iterator iterator= settings.keySet().iterator(); iterator.hasNext();) { 160 String key= (String )iterator.next(); 161 node.put(SAVE_PARTICIPANT_KEY_PREFIX + key, (String )settings.get(key)); 162 } 163 } 164 165 private static boolean hasSettingsInScope(IScopeContext context) { 166 IEclipsePreferences node= context.getNode(JavaUI.ID_PLUGIN); 167 168 Map defaultSettings= CleanUpConstants.getSaveParticipantSettings(); 169 for (Iterator iterator= defaultSettings.keySet().iterator(); iterator.hasNext();) { 170 String key= (String )iterator.next(); 171 if (node.get(SAVE_PARTICIPANT_KEY_PREFIX + key, null) != null) 172 return true; 173 } 174 175 return false; 176 } 177 178 185 public static List loadProfiles(IScopeContext scope) { 186 187 CleanUpProfileVersioner versioner= new CleanUpProfileVersioner(); 188 ProfileStore profileStore= new ProfileStore(CleanUpConstants.CLEANUP_PROFILES, versioner); 189 190 List 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 210 public static List getBuiltInProfiles() { 211 ArrayList result= new ArrayList (); 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 |