001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.fix;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.Iterator;
015: import java.util.List;
016: import java.util.Map;
017:
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.preferences.DefaultScope;
020: import org.eclipse.core.runtime.preferences.IEclipsePreferences;
021: import org.eclipse.core.runtime.preferences.IScopeContext;
022: import org.eclipse.core.runtime.preferences.InstanceScope;
023:
024: import org.eclipse.core.resources.ProjectScope;
025:
026: import org.eclipse.jdt.ui.JavaUI;
027:
028: import org.eclipse.jdt.internal.ui.JavaPlugin;
029: import org.eclipse.jdt.internal.ui.preferences.cleanup.CleanUpMessages;
030: import org.eclipse.jdt.internal.ui.preferences.cleanup.CleanUpProfileManager;
031: import org.eclipse.jdt.internal.ui.preferences.cleanup.CleanUpProfileVersioner;
032: import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager;
033: import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileStore;
034: import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.BuiltInProfile;
035: import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile;
036: import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.KeySet;
037: import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile;
038:
039: public class CleanUpPreferenceUtil {
040:
041: public static final String SAVE_PARTICIPANT_KEY_PREFIX = "sp_"; //$NON-NLS-1$
042:
043: public static Map loadOptions(IScopeContext context) {
044: return loadOptions(context, CleanUpConstants.CLEANUP_PROFILE,
045: CleanUpConstants.DEFAULT_PROFILE);
046: }
047:
048: private static Map loadOptions(IScopeContext context,
049: String profileIdKey, String defaultProfileId) {
050: IEclipsePreferences contextNode = context
051: .getNode(JavaUI.ID_PLUGIN);
052: String id = contextNode.get(profileIdKey, null);
053:
054: if (id != null && ProjectScope.SCOPE.equals(context.getName())) {
055: return loadFromProject(context);
056: }
057:
058: InstanceScope instanceScope = new InstanceScope();
059: if (id == null) {
060: if (ProjectScope.SCOPE.equals(context.getName())) {
061: id = instanceScope.getNode(JavaUI.ID_PLUGIN).get(
062: profileIdKey, null);
063: }
064: if (id == null) {
065: id = new DefaultScope().getNode(JavaUI.ID_PLUGIN).get(
066: profileIdKey, defaultProfileId);
067: }
068: }
069:
070: List builtInProfiles = getBuiltInProfiles();
071: for (Iterator iterator = builtInProfiles.iterator(); iterator
072: .hasNext();) {
073: Profile profile = (Profile) iterator.next();
074: if (id.equals(profile.getID()))
075: return profile.getSettings();
076: }
077:
078: if (id.equals(CleanUpConstants.SAVE_PARTICIPANT_PROFILE))
079: return CleanUpConstants.getSaveParticipantSettings();
080:
081: CleanUpProfileVersioner versioner = new CleanUpProfileVersioner();
082: ProfileStore profileStore = new ProfileStore(
083: CleanUpConstants.CLEANUP_PROFILES, versioner);
084:
085: List list = null;
086: try {
087: list = profileStore.readProfiles(instanceScope);
088: } catch (CoreException e1) {
089: JavaPlugin.log(e1);
090: }
091: if (list == null)
092: return null;
093:
094: for (Iterator iterator = list.iterator(); iterator.hasNext();) {
095: Profile profile = (Profile) iterator.next();
096: if (id.equals(profile.getID()))
097: return profile.getSettings();
098: }
099:
100: return null;
101: }
102:
103: private static Map loadFromProject(IScopeContext context) {
104: final Map profileOptions = new HashMap();
105: IEclipsePreferences uiPrefs = context.getNode(JavaUI.ID_PLUGIN);
106:
107: CleanUpProfileVersioner versioner = new CleanUpProfileVersioner();
108:
109: Map defaultSettings = CleanUpConstants
110: .getEclipseDefaultSettings();
111: KeySet[] keySets = CleanUpProfileManager.KEY_SETS;
112:
113: boolean hasValues = false;
114: for (int i = 0; i < keySets.length; i++) {
115: KeySet keySet = keySets[i];
116: IEclipsePreferences preferences = context.getNode(keySet
117: .getNodeName());
118: for (final Iterator keyIter = keySet.getKeys().iterator(); keyIter
119: .hasNext();) {
120: final String key = (String) keyIter.next();
121: Object val = preferences.get(key, null);
122: if (val != null) {
123: hasValues = true;
124: } else {
125: val = defaultSettings.get(key);
126: }
127: profileOptions.put(key, val);
128: }
129: }
130:
131: if (!hasValues)
132: return null;
133:
134: int version = uiPrefs.getInt(
135: CleanUpConstants.CLEANUP_SETTINGS_VERSION_KEY,
136: versioner.getFirstVersion());
137: if (version == versioner.getCurrentVersion())
138: return profileOptions;
139:
140: CustomProfile profile = new CustomProfile(
141: "tmp", profileOptions, version, versioner.getProfileKind()); //$NON-NLS-1$
142: versioner.update(profile);
143: return profile.getSettings();
144: }
145:
146: public static Map loadSaveParticipantOptions(IScopeContext context) {
147: IEclipsePreferences node;
148: if (hasSettingsInScope(context)) {
149: node = context.getNode(JavaUI.ID_PLUGIN);
150: } else {
151: IScopeContext instanceScope = new InstanceScope();
152: if (hasSettingsInScope(instanceScope)) {
153: node = instanceScope.getNode(JavaUI.ID_PLUGIN);
154: } else {
155: return CleanUpConstants.getSaveParticipantSettings();
156: }
157: }
158:
159: Map result = new HashMap();
160: Map defaultSettings = CleanUpConstants
161: .getSaveParticipantSettings();
162: for (Iterator iterator = defaultSettings.keySet().iterator(); iterator
163: .hasNext();) {
164: String key = (String) iterator.next();
165: result.put(key, node.get(SAVE_PARTICIPANT_KEY_PREFIX + key,
166: CleanUpConstants.FALSE));
167: }
168:
169: return result;
170: }
171:
172: public static void saveSaveParticipantOptions(
173: IScopeContext context, Map settings) {
174: IEclipsePreferences node = context.getNode(JavaUI.ID_PLUGIN);
175: for (Iterator iterator = settings.keySet().iterator(); iterator
176: .hasNext();) {
177: String key = (String) iterator.next();
178: node.put(SAVE_PARTICIPANT_KEY_PREFIX + key,
179: (String) settings.get(key));
180: }
181: }
182:
183: private static boolean hasSettingsInScope(IScopeContext context) {
184: IEclipsePreferences node = context.getNode(JavaUI.ID_PLUGIN);
185:
186: Map defaultSettings = CleanUpConstants
187: .getSaveParticipantSettings();
188: for (Iterator iterator = defaultSettings.keySet().iterator(); iterator
189: .hasNext();) {
190: String key = (String) iterator.next();
191: if (node.get(SAVE_PARTICIPANT_KEY_PREFIX + key, null) != null)
192: return true;
193: }
194:
195: return false;
196: }
197:
198: /**
199: * Returns a list of {@link ProfileManager.Profile} stored in the <code>scope</code>
200: * including the built in profiles.
201: * @param scope the context from which to retrieve the profiles
202: * @return list of profiles, not null
203: * @since 3.3
204: */
205: public static List loadProfiles(IScopeContext scope) {
206:
207: CleanUpProfileVersioner versioner = new CleanUpProfileVersioner();
208: ProfileStore profileStore = new ProfileStore(
209: CleanUpConstants.CLEANUP_PROFILES, versioner);
210:
211: List list = null;
212: try {
213: list = profileStore.readProfiles(scope);
214: } catch (CoreException e1) {
215: JavaPlugin.log(e1);
216: }
217: if (list == null) {
218: list = getBuiltInProfiles();
219: } else {
220: list.addAll(getBuiltInProfiles());
221: }
222:
223: return list;
224: }
225:
226: /**
227: * Returns a list of built in clean up profiles
228: * @return the list of built in profiles, not null
229: * @since 3.3
230: */
231: public static List getBuiltInProfiles() {
232: ArrayList result = new ArrayList();
233:
234: final Profile eclipseProfile = new BuiltInProfile(
235: CleanUpConstants.ECLIPSE_PROFILE,
236: CleanUpMessages.CleanUpProfileManager_ProfileName_EclipseBuildIn,
237: CleanUpConstants.getEclipseDefaultSettings(), 2,
238: CleanUpProfileVersioner.CURRENT_VERSION,
239: CleanUpProfileVersioner.PROFILE_KIND);
240: result.add(eclipseProfile);
241:
242: return result;
243: }
244:
245: }
|