KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > formatter > ProfileManager


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.preferences.formatter;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Observable JavaDoc;
20
21 import org.eclipse.core.runtime.preferences.DefaultScope;
22 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
23 import org.eclipse.core.runtime.preferences.IScopeContext;
24 import org.eclipse.core.runtime.preferences.InstanceScope;
25
26 import org.eclipse.core.resources.IProject;
27 import org.eclipse.core.resources.ProjectScope;
28 import org.eclipse.core.resources.ResourcesPlugin;
29
30 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
31 import org.eclipse.jdt.internal.corext.util.Messages;
32
33 import org.eclipse.jdt.ui.JavaUI;
34
35 import org.eclipse.jdt.internal.ui.preferences.PreferencesAccess;
36
37 import org.osgi.service.prefs.BackingStoreException;
38
39 /**
40  * The model for the set of profiles which are available in the workbench.
41  */

42 public abstract class ProfileManager extends Observable JavaDoc {
43     
44     public static final class KeySet {
45
46         private final List JavaDoc fKeys;
47         private final String JavaDoc fNodeName;
48
49         public KeySet(String JavaDoc nodeName, List JavaDoc keys) {
50             fNodeName= nodeName;
51             fKeys= keys;
52         }
53
54         public String JavaDoc getNodeName() {
55             return fNodeName;
56         }
57
58         public List JavaDoc getKeys() {
59             return fKeys;
60         }
61     }
62
63     /**
64      * A prefix which is prepended to every ID of a user-defined profile, in order
65      * to differentiate it from a built-in profile.
66      */

67     public final static String JavaDoc ID_PREFIX= "_"; //$NON-NLS-1$
68

69     /**
70      * Represents a profile with a unique ID, a name and a map
71      * containing the code formatter settings.
72      */

73     public static abstract class Profile implements Comparable JavaDoc {
74         
75         public abstract String JavaDoc getName();
76         public abstract Profile rename(String JavaDoc name, ProfileManager manager);
77         
78         public abstract Map JavaDoc getSettings();
79         public abstract void setSettings(Map JavaDoc settings);
80         
81         public abstract int getVersion();
82         
83         public boolean hasEqualSettings(Map JavaDoc otherMap, List JavaDoc allKeys) {
84             Map JavaDoc settings= getSettings();
85             for (Iterator JavaDoc iter= allKeys.iterator(); iter.hasNext(); ){
86                 String JavaDoc key= (String JavaDoc) iter.next();
87                 Object JavaDoc other= otherMap.get(key);
88                 Object JavaDoc curr= settings.get(key);
89                 if (other == null) {
90                     if (curr != null) {
91                         return false;
92                     }
93                 } else if (!other.equals(curr)) {
94                     return false;
95                 }
96             }
97             return true;
98         }
99         
100         public abstract boolean isProfileToSave();
101         
102         public abstract String JavaDoc getID();
103         
104         public boolean isSharedProfile() {
105             return false;
106         }
107         
108         public boolean isBuiltInProfile() {
109             return false;
110         }
111     }
112     
113     /**
114      * Represents a built-in profile. The state of a built-in profile
115      * cannot be changed after instantiation.
116      */

117     public static final class BuiltInProfile extends Profile {
118         private final String JavaDoc fName;
119         private final String JavaDoc fID;
120         private final Map JavaDoc fSettings;
121         private final int fOrder;
122         private final int fCurrentVersion;
123         private final String JavaDoc fProfileKind;
124         
125         public BuiltInProfile(String JavaDoc ID, String JavaDoc name, Map JavaDoc settings, int order, int currentVersion, String JavaDoc profileKind) {
126             fName= name;
127             fID= ID;
128             fSettings= settings;
129             fOrder= order;
130             fCurrentVersion= currentVersion;
131             fProfileKind= profileKind;
132         }
133         
134         public String JavaDoc getName() {
135             return fName;
136         }
137         
138         public Profile rename(String JavaDoc name, ProfileManager manager) {
139             final String JavaDoc trimmed= name.trim();
140             CustomProfile newProfile= new CustomProfile(trimmed, fSettings, fCurrentVersion, fProfileKind);
141             manager.addProfile(newProfile);
142             return newProfile;
143         }
144         
145         public Map JavaDoc getSettings() {
146             return fSettings;
147         }
148     
149         public void setSettings(Map JavaDoc settings) {
150         }
151     
152         public String JavaDoc getID() {
153             return fID;
154         }
155         
156         public final int compareTo(Object JavaDoc o) {
157             if (o instanceof BuiltInProfile) {
158                 return fOrder - ((BuiltInProfile)o).fOrder;
159             }
160             return -1;
161         }
162
163         public boolean isProfileToSave() {
164             return false;
165         }
166         
167         public boolean isBuiltInProfile() {
168             return true;
169         }
170
171         public int getVersion() {
172             return fCurrentVersion;
173         }
174     
175     }
176
177     /**
178      * Represents a user-defined profile. A custom profile can be modified after instantiation.
179      */

180     public static class CustomProfile extends Profile {
181         private String JavaDoc fName;
182         private Map JavaDoc fSettings;
183         protected ProfileManager fManager;
184         private int fVersion;
185         private final String JavaDoc fKind;
186
187         public CustomProfile(String JavaDoc name, Map JavaDoc settings, int version, String JavaDoc kind) {
188             fName= name;
189             fSettings= settings;
190             fVersion= version;
191             fKind= kind;
192         }
193         
194         public String JavaDoc getName() {
195             return fName;
196         }
197         
198         public Profile rename(String JavaDoc name, ProfileManager manager) {
199             final String JavaDoc trimmed= name.trim();
200             if (trimmed.equals(getName()))
201                 return this;
202             
203             String JavaDoc oldID= getID(); // remember old id before changing name
204
fName= trimmed;
205             
206             manager.profileRenamed(this, oldID);
207             return this;
208         }
209
210         public Map JavaDoc getSettings() {
211             return fSettings;
212         }
213         
214         public void setSettings(Map JavaDoc settings) {
215             if (settings == null)
216                 throw new IllegalArgumentException JavaDoc();
217             fSettings= settings;
218             if (fManager != null) {
219                 fManager.profileChanged(this);
220             }
221         }
222         
223         public String JavaDoc getID() {
224             return ID_PREFIX + fName;
225         }
226         
227         public void setManager(ProfileManager profileManager) {
228             fManager= profileManager;
229         }
230         
231         public ProfileManager getManager() {
232             return fManager;
233         }
234
235         public int getVersion() {
236             return fVersion;
237         }
238         
239         public void setVersion(int version) {
240             fVersion= version;
241         }
242         
243         public int compareTo(Object JavaDoc o) {
244             if (o instanceof SharedProfile) {
245                 return -1;
246             }
247             if (o instanceof CustomProfile) {
248                 return getName().compareToIgnoreCase(((Profile)o).getName());
249             }
250             return 1;
251         }
252         
253         public boolean isProfileToSave() {
254             return true;
255         }
256
257         public String JavaDoc getKind() {
258             return fKind;
259         }
260
261     }
262     
263     public final class SharedProfile extends CustomProfile {
264         
265         public SharedProfile(String JavaDoc oldName, Map JavaDoc options, int version, String JavaDoc profileKind) {
266             super(oldName, options, version, profileKind);
267         }
268         
269         public Profile rename(String JavaDoc name, ProfileManager manager) {
270             CustomProfile profile= new CustomProfile(name.trim(), getSettings(), getVersion(), getKind());
271
272             manager.profileReplaced(this, profile);
273             return profile;
274         }
275                 
276         public String JavaDoc getID() {
277             return SHARED_PROFILE;
278         }
279         
280         public final int compareTo(Object JavaDoc o) {
281             return 1;
282         }
283         
284         public boolean isProfileToSave() {
285             return false;
286         }
287         
288         public boolean isSharedProfile() {
289             return true;
290         }
291     }
292     
293
294     /**
295      * The possible events for observers listening to this class.
296      */

297     public final static int SELECTION_CHANGED_EVENT= 1;
298     public final static int PROFILE_DELETED_EVENT= 2;
299     public final static int PROFILE_RENAMED_EVENT= 3;
300     public final static int PROFILE_CREATED_EVENT= 4;
301     public final static int SETTINGS_CHANGED_EVENT= 5;
302     
303     
304     /**
305      * The key of the preference where the selected profile is stored.
306      */

307     private final String JavaDoc fProfileKey;
308     
309     /**
310      * The key of the preference where the version of the current settings is stored
311      */

312     private final String JavaDoc fProfileVersionKey;
313
314     
315     private final static String JavaDoc SHARED_PROFILE= "org.eclipse.jdt.ui.default.shared"; //$NON-NLS-1$
316

317     
318     /**
319      * A map containing the available profiles, using the IDs as keys.
320      */

321     private final Map JavaDoc fProfiles;
322     
323     /**
324      * The available profiles, sorted by name.
325      */

326     private final List JavaDoc fProfilesByName;
327     
328
329     /**
330      * The currently selected profile.
331      */

332     private Profile fSelected;
333     
334
335     /**
336      * The keys of the options to be saved with each profile
337      */

338     private final KeySet[] fKeySets;
339
340     private final PreferencesAccess fPreferencesAccess;
341     private final IProfileVersioner fProfileVersioner;
342
343     /**
344      * Create and initialize a new profile manager.
345      * @param profiles Initial custom profiles (List of type <code>CustomProfile</code>)
346      * @param profileVersioner
347      */

348     public ProfileManager(
349             List JavaDoc profiles,
350             IScopeContext context,
351             PreferencesAccess preferencesAccess,
352             IProfileVersioner profileVersioner,
353             KeySet[] keySets,
354             String JavaDoc profileKey,
355             String JavaDoc profileVersionKey) {
356         
357         fPreferencesAccess= preferencesAccess;
358         fProfileVersioner= profileVersioner;
359         fKeySets= keySets;
360         fProfileKey= profileKey;
361         fProfileVersionKey= profileVersionKey;
362         
363         fProfiles= new HashMap JavaDoc();
364         fProfilesByName= new ArrayList JavaDoc();
365     
366         for (final Iterator JavaDoc iter = profiles.iterator(); iter.hasNext();) {
367             final Profile profile= (Profile) iter.next();
368             if (profile instanceof CustomProfile) {
369                 ((CustomProfile)profile).setManager(this);
370             }
371             fProfiles.put(profile.getID(), profile);
372             fProfilesByName.add(profile);
373         }
374         
375         Collections.sort(fProfilesByName);
376         
377         String JavaDoc profileId= getSelectedProfileId(fPreferencesAccess.getInstanceScope());
378         
379         Profile profile= (Profile) fProfiles.get(profileId);
380         if (profile == null) {
381             profile= getDefaultProfile();
382         }
383         fSelected= profile;
384         
385         if (context.getName() == ProjectScope.SCOPE && hasProjectSpecificSettings(context)) {
386             Map JavaDoc map= readFromPreferenceStore(context, profile);
387             if (map != null) {
388                 
389                 List JavaDoc allKeys= new ArrayList JavaDoc();
390                 for (int i= 0; i < fKeySets.length; i++) {
391                     allKeys.addAll(fKeySets[i].getKeys());
392                 }
393                 Collections.sort(allKeys);
394                 
395                 Profile matching= null;
396             
397                 String JavaDoc projProfileId= context.getNode(JavaUI.ID_PLUGIN).get(fProfileKey, null);
398                 if (projProfileId != null) {
399                     Profile curr= (Profile) fProfiles.get(projProfileId);
400                     if (curr != null && (curr.isBuiltInProfile() || curr.hasEqualSettings(map, allKeys))) {
401                         matching= curr;
402                     }
403                 } else {
404                     // old version: look for similar
405
for (final Iterator JavaDoc iter = fProfilesByName.iterator(); iter.hasNext();) {
406                         Profile curr= (Profile) iter.next();
407                         if (curr.hasEqualSettings(map, allKeys)) {
408                             matching= curr;
409                             break;
410                         }
411                     }
412                 }
413                 if (matching == null) {
414                     String JavaDoc name;
415                     if (projProfileId != null && !fProfiles.containsKey(projProfileId)) {
416                         name= Messages.format(FormatterMessages.ProfileManager_unmanaged_profile_with_name, projProfileId.substring(ID_PREFIX.length()));
417                     } else {
418                         name= FormatterMessages.ProfileManager_unmanaged_profile;
419                     }
420                     // current settings do not correspond to any profile -> create a 'team' profile
421
SharedProfile shared= new SharedProfile(name, map, fProfileVersioner.getCurrentVersion(), fProfileVersioner.getProfileKind());
422                     shared.setManager(this);
423                     fProfiles.put(shared.getID(), shared);
424                     fProfilesByName.add(shared); // add last
425
matching= shared;
426                 }
427                 fSelected= matching;
428             }
429         }
430     }
431
432     protected String JavaDoc getSelectedProfileId(IScopeContext instanceScope) {
433         String JavaDoc profileId= instanceScope.getNode(JavaUI.ID_PLUGIN).get(fProfileKey, null);
434         if (profileId == null) {
435             // request from bug 129427
436
profileId= new DefaultScope().getNode(JavaUI.ID_PLUGIN).get(fProfileKey, null);
437         }
438         return profileId;
439     }
440
441     /**
442      * Notify observers with a message. The message must be one of the following:
443      * @param message Message to send out
444      *
445      * @see #SELECTION_CHANGED_EVENT
446      * @see #PROFILE_DELETED_EVENT
447      * @see #PROFILE_RENAMED_EVENT
448      * @see #PROFILE_CREATED_EVENT
449      * @see #SETTINGS_CHANGED_EVENT
450      */

451     protected void notifyObservers(int message) {
452         setChanged();
453         notifyObservers(new Integer JavaDoc(message));
454     }
455     
456     public static boolean hasProjectSpecificSettings(IScopeContext context, KeySet[] keySets) {
457         for (int i= 0; i < keySets.length; i++) {
458             KeySet keySet= keySets[i];
459             IEclipsePreferences preferences= context.getNode(keySet.getNodeName());
460             for (final Iterator JavaDoc keyIter= keySet.getKeys().iterator(); keyIter.hasNext();) {
461                 final String JavaDoc key= (String JavaDoc)keyIter.next();
462                 Object JavaDoc val= preferences.get(key, null);
463                 if (val != null) {
464                     return true;
465                 }
466             }
467         }
468         return false;
469     }
470     
471     public boolean hasProjectSpecificSettings(IScopeContext context) {
472         return hasProjectSpecificSettings(context, fKeySets);
473     }
474
475     /**
476      * Only to read project specific settings to find out to what profile it matches.
477      * @param context The project context
478      */

479     private Map JavaDoc readFromPreferenceStore(IScopeContext context, Profile workspaceProfile) {
480         final Map JavaDoc profileOptions= new HashMap JavaDoc();
481         IEclipsePreferences uiPrefs= context.getNode(JavaUI.ID_PLUGIN);
482                 
483         int version= uiPrefs.getInt(fProfileVersionKey, fProfileVersioner.getFirstVersion());
484         if (version != fProfileVersioner.getCurrentVersion()) {
485             Map JavaDoc allOptions= new HashMap JavaDoc();
486             for (int i= 0; i < fKeySets.length; i++) {
487                 addAll(context.getNode(fKeySets[i].getNodeName()), allOptions);
488             }
489             CustomProfile profile= new CustomProfile("tmp", allOptions, version, fProfileVersioner.getProfileKind()); //$NON-NLS-1$
490
fProfileVersioner.update(profile);
491             return profile.getSettings();
492         }
493         
494         boolean hasValues= false;
495         for (int i= 0; i < fKeySets.length; i++) {
496             KeySet keySet= fKeySets[i];
497             IEclipsePreferences preferences= context.getNode(keySet.getNodeName());
498             for (final Iterator JavaDoc keyIter = keySet.getKeys().iterator(); keyIter.hasNext(); ) {
499                 final String JavaDoc key= (String JavaDoc) keyIter.next();
500                 Object JavaDoc val= preferences.get(key, null);
501                 if (val != null) {
502                     hasValues= true;
503                 } else {
504                     val= workspaceProfile.getSettings().get(key);
505                 }
506                 profileOptions.put(key, val);
507             }
508         }
509         
510         if (!hasValues) {
511             return null;
512         }
513
514         setLatestCompliance(profileOptions);
515         return profileOptions;
516     }
517     
518     
519     /**
520      * @param uiPrefs
521      * @param allOptions
522      */

523     private void addAll(IEclipsePreferences uiPrefs, Map JavaDoc allOptions) {
524         try {
525             String JavaDoc[] keys= uiPrefs.keys();
526             for (int i= 0; i < keys.length; i++) {
527                 String JavaDoc key= keys[i];
528                 String JavaDoc val= uiPrefs.get(key, null);
529                 if (val != null) {
530                     allOptions.put(key, val);
531                 }
532             }
533         } catch (BackingStoreException e) {
534             // ignore
535
}
536         
537     }
538
539     private boolean updatePreferences(IEclipsePreferences prefs, List JavaDoc keys, Map JavaDoc profileOptions) {
540         boolean hasChanges= false;
541         for (final Iterator JavaDoc keyIter = keys.iterator(); keyIter.hasNext(); ) {
542             final String JavaDoc key= (String JavaDoc) keyIter.next();
543             final String JavaDoc oldVal= prefs.get(key, null);
544             final String JavaDoc val= (String JavaDoc) profileOptions.get(key);
545             if (val == null) {
546                 if (oldVal != null) {
547                     prefs.remove(key);
548                     hasChanges= true;
549                 }
550             } else if (!val.equals(oldVal)) {
551                 prefs.put(key, val);
552                 hasChanges= true;
553             }
554         }
555         return hasChanges;
556     }
557     
558     
559     /**
560      * Update all formatter settings with the settings of the specified profile.
561      * @param profile The profile to write to the preference store
562      */

563     private void writeToPreferenceStore(Profile profile, IScopeContext context) {
564         final Map JavaDoc profileOptions= profile.getSettings();
565         
566         for (int i= 0; i < fKeySets.length; i++) {
567             updatePreferences(context.getNode(fKeySets[i].getNodeName()), fKeySets[i].getKeys(), profileOptions);
568         }
569         
570         final IEclipsePreferences uiPrefs= context.getNode(JavaUI.ID_PLUGIN);
571         if (uiPrefs.getInt(fProfileVersionKey, 0) != fProfileVersioner.getCurrentVersion()) {
572             uiPrefs.putInt(fProfileVersionKey, fProfileVersioner.getCurrentVersion());
573         }
574         
575         if (context.getName() == InstanceScope.SCOPE) {
576             uiPrefs.put(fProfileKey, profile.getID());
577         } else if (context.getName() == ProjectScope.SCOPE && !profile.isSharedProfile()) {
578             uiPrefs.put(fProfileKey, profile.getID());
579         }
580     }
581     
582         
583     /**
584      * Get an immutable list as view on all profiles, sorted alphabetically. Unless the set
585      * of profiles has been modified between the two calls, the sequence is guaranteed to
586      * correspond to the one returned by <code>getSortedNames</code>.
587      * @return a list of elements of type <code>Profile</code>
588      *
589      * @see #getSortedDisplayNames()
590      */

591     public List JavaDoc getSortedProfiles() {
592         return Collections.unmodifiableList(fProfilesByName);
593     }
594
595     /**
596      * Get the names of all profiles stored in this profile manager, sorted alphabetically. Unless the set of
597      * profiles has been modified between the two calls, the sequence is guaranteed to correspond to the one
598      * returned by <code>getSortedProfiles</code>.
599      * @return All names, sorted alphabetically
600      * @see #getSortedProfiles()
601      */

602     public String JavaDoc[] getSortedDisplayNames() {
603         final String JavaDoc[] sortedNames= new String JavaDoc[fProfilesByName.size()];
604         int i= 0;
605         for (final Iterator JavaDoc iter = fProfilesByName.iterator(); iter.hasNext();) {
606             Profile curr= (Profile) iter.next();
607             sortedNames[i++]= curr.getName();
608         }
609         return sortedNames;
610     }
611     
612     /**
613      * Get the profile for this profile id.
614      * @param ID The profile ID
615      * @return The profile with the given ID or <code>null</code>
616      */

617     public Profile getProfile(String JavaDoc ID) {
618         return (Profile)fProfiles.get(ID);
619     }
620     
621     /**
622      * Activate the selected profile, update all necessary options in
623      * preferences and save profiles to disk.
624      */

625     public void commitChanges(IScopeContext scopeContext) {
626         if (fSelected != null) {
627             writeToPreferenceStore(fSelected, scopeContext);
628         }
629     }
630     
631     public void clearAllSettings(IScopeContext context) {
632         for (int i= 0; i < fKeySets.length; i++) {
633             updatePreferences(context.getNode(fKeySets[i].getNodeName()), fKeySets[i].getKeys(), Collections.EMPTY_MAP);
634         }
635         
636         final IEclipsePreferences uiPrefs= context.getNode(JavaUI.ID_PLUGIN);
637         uiPrefs.remove(fProfileKey);
638     }
639     
640     /**
641      * Get the currently selected profile.
642      * @return The currently selected profile.
643      */

644     public Profile getSelected() {
645         return fSelected;
646     }
647
648     /**
649      * Set the selected profile. The profile must already be contained in this profile manager.
650      * @param profile The profile to select
651      */

652     public void setSelected(Profile profile) {
653         final Profile newSelected= (Profile)fProfiles.get(profile.getID());
654         if (newSelected != null && !newSelected.equals(fSelected)) {
655             fSelected= newSelected;
656             notifyObservers(SELECTION_CHANGED_EVENT);
657         }
658     }
659
660     /**
661      * Check whether a user-defined profile in this profile manager
662      * already has this name.
663      * @param name The name to test for
664      * @return Returns <code>true</code> if a profile with the given name exists
665      */

666     public boolean containsName(String JavaDoc name) {
667         for (final Iterator JavaDoc iter = fProfilesByName.iterator(); iter.hasNext();) {
668             Profile curr= (Profile) iter.next();
669             if (name.equals(curr.getName())) {
670                 return true;
671             }
672         }
673         return false;
674     }
675     
676     /**
677      * Add a new custom profile to this profile manager.
678      * @param profile The profile to add
679      */

680     public void addProfile(CustomProfile profile) {
681         profile.setManager(this);
682         final CustomProfile oldProfile= (CustomProfile)fProfiles.get(profile.getID());
683         if (oldProfile != null) {
684             fProfiles.remove(oldProfile.getID());
685             fProfilesByName.remove(oldProfile);
686             oldProfile.setManager(null);
687         }
688         fProfiles.put(profile.getID(), profile);
689         fProfilesByName.add(profile);
690         Collections.sort(fProfilesByName);
691         fSelected= profile;
692         notifyObservers(PROFILE_CREATED_EVENT);
693     }
694     
695     /**
696      * Delete the currently selected profile from this profile manager. The next profile
697      * in the list is selected.
698      * @return true if the profile has been successfully removed, false otherwise.
699      */

700     public boolean deleteSelected() {
701         if (!(fSelected instanceof CustomProfile))
702             return false;
703         
704         return deleteProfile((CustomProfile)fSelected);
705     }
706
707     public boolean deleteProfile(CustomProfile profile) {
708         int index= fProfilesByName.indexOf(profile);
709         
710         fProfiles.remove(profile.getID());
711         fProfilesByName.remove(profile);
712         
713         profile.setManager(null);
714         
715         if (index >= fProfilesByName.size())
716             index--;
717         fSelected= (Profile) fProfilesByName.get(index);
718
719         if (!profile.isSharedProfile()) {
720             updateProfilesWithName(profile.getID(), null, false);
721         }
722         
723         notifyObservers(PROFILE_DELETED_EVENT);
724         return true;
725     }
726
727     
728     public void profileRenamed(CustomProfile profile, String JavaDoc oldID) {
729         fProfiles.remove(oldID);
730         fProfiles.put(profile.getID(), profile);
731
732         if (!profile.isSharedProfile()) {
733             updateProfilesWithName(oldID, profile, false);
734         }
735         
736         Collections.sort(fProfilesByName);
737         notifyObservers(PROFILE_RENAMED_EVENT);
738     }
739     
740     public void profileReplaced(CustomProfile oldProfile, CustomProfile newProfile) {
741         fProfiles.remove(oldProfile.getID());
742         fProfiles.put(newProfile.getID(), newProfile);
743         fProfilesByName.remove(oldProfile);
744         fProfilesByName.add(newProfile);
745         Collections.sort(fProfilesByName);
746         
747         if (!oldProfile.isSharedProfile()) {
748             updateProfilesWithName(oldProfile.getID(), null, false);
749         }
750         
751         setSelected(newProfile);
752         notifyObservers(PROFILE_CREATED_EVENT);
753         notifyObservers(SELECTION_CHANGED_EVENT);
754     }
755     
756     public void profileChanged(CustomProfile profile) {
757         if (!profile.isSharedProfile()) {
758             updateProfilesWithName(profile.getID(), profile, true);
759         }
760         
761         notifyObservers(SETTINGS_CHANGED_EVENT);
762     }
763     
764     
765     protected void updateProfilesWithName(String JavaDoc oldName, Profile newProfile, boolean applySettings) {
766         IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects();
767         for (int i= 0; i < projects.length; i++) {
768             IScopeContext projectScope= fPreferencesAccess.getProjectScope(projects[i]);
769             IEclipsePreferences node= projectScope.getNode(JavaUI.ID_PLUGIN);
770             String JavaDoc profileId= node.get(fProfileKey, null);
771             if (oldName.equals(profileId)) {
772                 if (newProfile == null) {
773                     node.remove(fProfileKey);
774                 } else {
775                     if (applySettings) {
776                         writeToPreferenceStore(newProfile, projectScope);
777                     } else {
778                         node.put(fProfileKey, newProfile.getID());
779                     }
780                 }
781             }
782         }
783         
784         IScopeContext instanceScope= fPreferencesAccess.getInstanceScope();
785         final IEclipsePreferences uiPrefs= instanceScope.getNode(JavaUI.ID_PLUGIN);
786         if (newProfile != null && oldName.equals(uiPrefs.get(fProfileKey, null))) {
787             writeToPreferenceStore(newProfile, instanceScope);
788         }
789     }
790     
791     private static void setLatestCompliance(Map JavaDoc map) {
792         JavaModelUtil.set50CompilanceOptions(map);
793     }
794
795     public abstract Profile getDefaultProfile();
796
797     public IProfileVersioner getProfileVersioner() {
798         return fProfileVersioner;
799     }
800 }
801
Popular Tags