Source Code Cross Referenced for ProfileManager.java in  » IDE-Eclipse » jdt » org » eclipse » jdt » internal » ui » preferences » formatter » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Eclipse » jdt » org.eclipse.jdt.internal.ui.preferences.formatter 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Copyright (c) 2000, 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.ui.preferences.formatter;
011:
012:        import java.util.ArrayList;
013:        import java.util.Collections;
014:        import java.util.HashMap;
015:        import java.util.Iterator;
016:        import java.util.List;
017:        import java.util.Map;
018:        import java.util.Observable;
019:
020:        import org.eclipse.core.runtime.preferences.DefaultScope;
021:        import org.eclipse.core.runtime.preferences.IEclipsePreferences;
022:        import org.eclipse.core.runtime.preferences.IScopeContext;
023:        import org.eclipse.core.runtime.preferences.InstanceScope;
024:
025:        import org.eclipse.core.resources.IProject;
026:        import org.eclipse.core.resources.ProjectScope;
027:        import org.eclipse.core.resources.ResourcesPlugin;
028:
029:        import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
030:        import org.eclipse.jdt.internal.corext.util.Messages;
031:
032:        import org.eclipse.jdt.ui.JavaUI;
033:
034:        import org.eclipse.jdt.internal.ui.preferences.PreferencesAccess;
035:
036:        import org.osgi.service.prefs.BackingStoreException;
037:
038:        /**
039:         * The model for the set of profiles which are available in the workbench.
040:         */
041:        public abstract class ProfileManager extends Observable {
042:
043:            public static final class KeySet {
044:
045:                private final List fKeys;
046:                private final String fNodeName;
047:
048:                public KeySet(String nodeName, List keys) {
049:                    fNodeName = nodeName;
050:                    fKeys = keys;
051:                }
052:
053:                public String getNodeName() {
054:                    return fNodeName;
055:                }
056:
057:                public List getKeys() {
058:                    return fKeys;
059:                }
060:            }
061:
062:            /**
063:             * A prefix which is prepended to every ID of a user-defined profile, in order
064:             * to differentiate it from a built-in profile.
065:             */
066:            public final static String ID_PREFIX = "_"; //$NON-NLS-1$
067:
068:            /**
069:             * Represents a profile with a unique ID, a name and a map 
070:             * containing the code formatter settings.
071:             */
072:            public static abstract class Profile implements  Comparable {
073:
074:                public abstract String getName();
075:
076:                public abstract Profile rename(String name,
077:                        ProfileManager manager);
078:
079:                public abstract Map getSettings();
080:
081:                public abstract void setSettings(Map settings);
082:
083:                public abstract int getVersion();
084:
085:                public boolean hasEqualSettings(Map otherMap, List allKeys) {
086:                    Map settings = getSettings();
087:                    for (Iterator iter = allKeys.iterator(); iter.hasNext();) {
088:                        String key = (String) iter.next();
089:                        Object other = otherMap.get(key);
090:                        Object curr = settings.get(key);
091:                        if (other == null) {
092:                            if (curr != null) {
093:                                return false;
094:                            }
095:                        } else if (!other.equals(curr)) {
096:                            return false;
097:                        }
098:                    }
099:                    return true;
100:                }
101:
102:                public abstract boolean isProfileToSave();
103:
104:                public abstract String getID();
105:
106:                public boolean isSharedProfile() {
107:                    return false;
108:                }
109:
110:                public boolean isBuiltInProfile() {
111:                    return false;
112:                }
113:            }
114:
115:            /**
116:             * Represents a built-in profile. The state of a built-in profile 
117:             * cannot be changed after instantiation.
118:             */
119:            public static final class BuiltInProfile extends Profile {
120:                private final String fName;
121:                private final String fID;
122:                private final Map fSettings;
123:                private final int fOrder;
124:                private final int fCurrentVersion;
125:                private final String fProfileKind;
126:
127:                public BuiltInProfile(String ID, String name, Map settings,
128:                        int order, int currentVersion, String profileKind) {
129:                    fName = name;
130:                    fID = ID;
131:                    fSettings = settings;
132:                    fOrder = order;
133:                    fCurrentVersion = currentVersion;
134:                    fProfileKind = profileKind;
135:                }
136:
137:                public String getName() {
138:                    return fName;
139:                }
140:
141:                public Profile rename(String name, ProfileManager manager) {
142:                    final String trimmed = name.trim();
143:                    CustomProfile newProfile = new CustomProfile(trimmed,
144:                            fSettings, fCurrentVersion, fProfileKind);
145:                    manager.addProfile(newProfile);
146:                    return newProfile;
147:                }
148:
149:                public Map getSettings() {
150:                    return fSettings;
151:                }
152:
153:                public void setSettings(Map settings) {
154:                }
155:
156:                public String getID() {
157:                    return fID;
158:                }
159:
160:                public final int compareTo(Object o) {
161:                    if (o instanceof  BuiltInProfile) {
162:                        return fOrder - ((BuiltInProfile) o).fOrder;
163:                    }
164:                    return -1;
165:                }
166:
167:                public boolean isProfileToSave() {
168:                    return false;
169:                }
170:
171:                public boolean isBuiltInProfile() {
172:                    return true;
173:                }
174:
175:                public int getVersion() {
176:                    return fCurrentVersion;
177:                }
178:
179:            }
180:
181:            /**
182:             * Represents a user-defined profile. A custom profile can be modified after instantiation.
183:             */
184:            public static class CustomProfile extends Profile {
185:                private String fName;
186:                private Map fSettings;
187:                protected ProfileManager fManager;
188:                private int fVersion;
189:                private final String fKind;
190:
191:                public CustomProfile(String name, Map settings, int version,
192:                        String kind) {
193:                    fName = name;
194:                    fSettings = settings;
195:                    fVersion = version;
196:                    fKind = kind;
197:                }
198:
199:                public String getName() {
200:                    return fName;
201:                }
202:
203:                public Profile rename(String name, ProfileManager manager) {
204:                    final String trimmed = name.trim();
205:                    if (trimmed.equals(getName()))
206:                        return this ;
207:
208:                    String oldID = getID(); // remember old id before changing name
209:                    fName = trimmed;
210:
211:                    manager.profileRenamed(this , oldID);
212:                    return this ;
213:                }
214:
215:                public Map getSettings() {
216:                    return fSettings;
217:                }
218:
219:                public void setSettings(Map settings) {
220:                    if (settings == null)
221:                        throw new IllegalArgumentException();
222:                    fSettings = settings;
223:                    if (fManager != null) {
224:                        fManager.profileChanged(this );
225:                    }
226:                }
227:
228:                public String getID() {
229:                    return ID_PREFIX + fName;
230:                }
231:
232:                public void setManager(ProfileManager profileManager) {
233:                    fManager = profileManager;
234:                }
235:
236:                public ProfileManager getManager() {
237:                    return fManager;
238:                }
239:
240:                public int getVersion() {
241:                    return fVersion;
242:                }
243:
244:                public void setVersion(int version) {
245:                    fVersion = version;
246:                }
247:
248:                public int compareTo(Object o) {
249:                    if (o instanceof  SharedProfile) {
250:                        return -1;
251:                    }
252:                    if (o instanceof  CustomProfile) {
253:                        return getName().compareToIgnoreCase(
254:                                ((Profile) o).getName());
255:                    }
256:                    return 1;
257:                }
258:
259:                public boolean isProfileToSave() {
260:                    return true;
261:                }
262:
263:                public String getKind() {
264:                    return fKind;
265:                }
266:
267:            }
268:
269:            public final class SharedProfile extends CustomProfile {
270:
271:                public SharedProfile(String oldName, Map options, int version,
272:                        String profileKind) {
273:                    super (oldName, options, version, profileKind);
274:                }
275:
276:                public Profile rename(String name, ProfileManager manager) {
277:                    CustomProfile profile = new CustomProfile(name.trim(),
278:                            getSettings(), getVersion(), getKind());
279:
280:                    manager.profileReplaced(this , profile);
281:                    return profile;
282:                }
283:
284:                public String getID() {
285:                    return SHARED_PROFILE;
286:                }
287:
288:                public final int compareTo(Object o) {
289:                    return 1;
290:                }
291:
292:                public boolean isProfileToSave() {
293:                    return false;
294:                }
295:
296:                public boolean isSharedProfile() {
297:                    return true;
298:                }
299:            }
300:
301:            /**
302:             * The possible events for observers listening to this class.
303:             */
304:            public final static int SELECTION_CHANGED_EVENT = 1;
305:            public final static int PROFILE_DELETED_EVENT = 2;
306:            public final static int PROFILE_RENAMED_EVENT = 3;
307:            public final static int PROFILE_CREATED_EVENT = 4;
308:            public final static int SETTINGS_CHANGED_EVENT = 5;
309:
310:            /**
311:             * The key of the preference where the selected profile is stored.
312:             */
313:            private final String fProfileKey;
314:
315:            /**
316:             * The key of the preference where the version of the current settings is stored
317:             */
318:            private final String fProfileVersionKey;
319:
320:            private final static String SHARED_PROFILE = "org.eclipse.jdt.ui.default.shared"; //$NON-NLS-1$
321:
322:            /**
323:             * A map containing the available profiles, using the IDs as keys.
324:             */
325:            private final Map fProfiles;
326:
327:            /**
328:             * The available profiles, sorted by name.
329:             */
330:            private final List fProfilesByName;
331:
332:            /**
333:             * The currently selected profile. 
334:             */
335:            private Profile fSelected;
336:
337:            /**
338:             * The keys of the options to be saved with each profile
339:             */
340:            private final KeySet[] fKeySets;
341:
342:            private final PreferencesAccess fPreferencesAccess;
343:            private final IProfileVersioner fProfileVersioner;
344:
345:            /**
346:             * Create and initialize a new profile manager.
347:             * @param profiles Initial custom profiles (List of type <code>CustomProfile</code>)
348:             * @param profileVersioner 
349:             */
350:            public ProfileManager(List profiles, IScopeContext context,
351:                    PreferencesAccess preferencesAccess,
352:                    IProfileVersioner profileVersioner, KeySet[] keySets,
353:                    String profileKey, String profileVersionKey) {
354:
355:                fPreferencesAccess = preferencesAccess;
356:                fProfileVersioner = profileVersioner;
357:                fKeySets = keySets;
358:                fProfileKey = profileKey;
359:                fProfileVersionKey = profileVersionKey;
360:
361:                fProfiles = new HashMap();
362:                fProfilesByName = new ArrayList();
363:
364:                for (final Iterator iter = profiles.iterator(); iter.hasNext();) {
365:                    final Profile profile = (Profile) iter.next();
366:                    if (profile instanceof  CustomProfile) {
367:                        ((CustomProfile) profile).setManager(this );
368:                    }
369:                    fProfiles.put(profile.getID(), profile);
370:                    fProfilesByName.add(profile);
371:                }
372:
373:                Collections.sort(fProfilesByName);
374:
375:                String profileId = getSelectedProfileId(fPreferencesAccess
376:                        .getInstanceScope());
377:
378:                Profile profile = (Profile) fProfiles.get(profileId);
379:                if (profile == null) {
380:                    profile = getDefaultProfile();
381:                }
382:                fSelected = profile;
383:
384:                if (context.getName() == ProjectScope.SCOPE
385:                        && hasProjectSpecificSettings(context)) {
386:                    Map map = readFromPreferenceStore(context, profile);
387:                    if (map != null) {
388:
389:                        List allKeys = new ArrayList();
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 projProfileId = context
398:                                .getNode(JavaUI.ID_PLUGIN).get(fProfileKey,
399:                                        null);
400:                        if (projProfileId != null) {
401:                            Profile curr = (Profile) fProfiles
402:                                    .get(projProfileId);
403:                            if (curr != null
404:                                    && (curr.isBuiltInProfile() || curr
405:                                            .hasEqualSettings(map, allKeys))) {
406:                                matching = curr;
407:                            }
408:                        } else {
409:                            // old version: look for similar
410:                            for (final Iterator iter = fProfilesByName
411:                                    .iterator(); iter.hasNext();) {
412:                                Profile curr = (Profile) iter.next();
413:                                if (curr.hasEqualSettings(map, allKeys)) {
414:                                    matching = curr;
415:                                    break;
416:                                }
417:                            }
418:                        }
419:                        if (matching == null) {
420:                            String name;
421:                            if (projProfileId != null
422:                                    && !fProfiles.containsKey(projProfileId)) {
423:                                name = Messages
424:                                        .format(
425:                                                FormatterMessages.ProfileManager_unmanaged_profile_with_name,
426:                                                projProfileId
427:                                                        .substring(ID_PREFIX
428:                                                                .length()));
429:                            } else {
430:                                name = FormatterMessages.ProfileManager_unmanaged_profile;
431:                            }
432:                            // current settings do not correspond to any profile -> create a 'team' profile
433:                            SharedProfile shared = new SharedProfile(name, map,
434:                                    fProfileVersioner.getCurrentVersion(),
435:                                    fProfileVersioner.getProfileKind());
436:                            shared.setManager(this );
437:                            fProfiles.put(shared.getID(), shared);
438:                            fProfilesByName.add(shared); // add last
439:                            matching = shared;
440:                        }
441:                        fSelected = matching;
442:                    }
443:                }
444:            }
445:
446:            protected String getSelectedProfileId(IScopeContext instanceScope) {
447:                String profileId = instanceScope.getNode(JavaUI.ID_PLUGIN).get(
448:                        fProfileKey, null);
449:                if (profileId == null) {
450:                    // request from bug 129427
451:                    profileId = new DefaultScope().getNode(JavaUI.ID_PLUGIN)
452:                            .get(fProfileKey, null);
453:                }
454:                return profileId;
455:            }
456:
457:            /**
458:             * Notify observers with a message. The message must be one of the following:
459:             * @param message Message to send out
460:             * 
461:             * @see #SELECTION_CHANGED_EVENT
462:             * @see #PROFILE_DELETED_EVENT
463:             * @see #PROFILE_RENAMED_EVENT
464:             * @see #PROFILE_CREATED_EVENT
465:             * @see #SETTINGS_CHANGED_EVENT
466:             */
467:            protected void notifyObservers(int message) {
468:                setChanged();
469:                notifyObservers(new Integer(message));
470:            }
471:
472:            public static boolean hasProjectSpecificSettings(
473:                    IScopeContext context, KeySet[] keySets) {
474:                for (int i = 0; i < keySets.length; i++) {
475:                    KeySet keySet = keySets[i];
476:                    IEclipsePreferences preferences = context.getNode(keySet
477:                            .getNodeName());
478:                    for (final Iterator keyIter = keySet.getKeys().iterator(); keyIter
479:                            .hasNext();) {
480:                        final String key = (String) keyIter.next();
481:                        Object val = preferences.get(key, null);
482:                        if (val != null) {
483:                            return true;
484:                        }
485:                    }
486:                }
487:                return false;
488:            }
489:
490:            public boolean hasProjectSpecificSettings(IScopeContext context) {
491:                return hasProjectSpecificSettings(context, fKeySets);
492:            }
493:
494:            /**
495:             * Only to read project specific settings to find out to what profile it matches.
496:             * @param context The project context
497:             */
498:            private Map readFromPreferenceStore(IScopeContext context,
499:                    Profile workspaceProfile) {
500:                final Map profileOptions = new HashMap();
501:                IEclipsePreferences uiPrefs = context.getNode(JavaUI.ID_PLUGIN);
502:
503:                int version = uiPrefs.getInt(fProfileVersionKey,
504:                        fProfileVersioner.getFirstVersion());
505:                if (version != fProfileVersioner.getCurrentVersion()) {
506:                    Map allOptions = new HashMap();
507:                    for (int i = 0; i < fKeySets.length; i++) {
508:                        addAll(context.getNode(fKeySets[i].getNodeName()),
509:                                allOptions);
510:                    }
511:                    CustomProfile profile = new CustomProfile(
512:                            "tmp", allOptions, version, fProfileVersioner.getProfileKind()); //$NON-NLS-1$
513:                    fProfileVersioner.update(profile);
514:                    return profile.getSettings();
515:                }
516:
517:                boolean hasValues = false;
518:                for (int i = 0; i < fKeySets.length; i++) {
519:                    KeySet keySet = fKeySets[i];
520:                    IEclipsePreferences preferences = context.getNode(keySet
521:                            .getNodeName());
522:                    for (final Iterator keyIter = keySet.getKeys().iterator(); keyIter
523:                            .hasNext();) {
524:                        final String key = (String) keyIter.next();
525:                        Object val = preferences.get(key, null);
526:                        if (val != null) {
527:                            hasValues = true;
528:                        } else {
529:                            val = workspaceProfile.getSettings().get(key);
530:                        }
531:                        profileOptions.put(key, val);
532:                    }
533:                }
534:
535:                if (!hasValues) {
536:                    return null;
537:                }
538:
539:                setLatestCompliance(profileOptions);
540:                return profileOptions;
541:            }
542:
543:            /**
544:             * @param uiPrefs
545:             * @param allOptions
546:             */
547:            private void addAll(IEclipsePreferences uiPrefs, Map allOptions) {
548:                try {
549:                    String[] keys = uiPrefs.keys();
550:                    for (int i = 0; i < keys.length; i++) {
551:                        String key = keys[i];
552:                        String val = uiPrefs.get(key, null);
553:                        if (val != null) {
554:                            allOptions.put(key, val);
555:                        }
556:                    }
557:                } catch (BackingStoreException e) {
558:                    // ignore
559:                }
560:
561:            }
562:
563:            private boolean updatePreferences(IEclipsePreferences prefs,
564:                    List keys, Map profileOptions) {
565:                boolean hasChanges = false;
566:                for (final Iterator keyIter = keys.iterator(); keyIter
567:                        .hasNext();) {
568:                    final String key = (String) keyIter.next();
569:                    final String oldVal = prefs.get(key, null);
570:                    final String val = (String) profileOptions.get(key);
571:                    if (val == null) {
572:                        if (oldVal != null) {
573:                            prefs.remove(key);
574:                            hasChanges = true;
575:                        }
576:                    } else if (!val.equals(oldVal)) {
577:                        prefs.put(key, val);
578:                        hasChanges = true;
579:                    }
580:                }
581:                return hasChanges;
582:            }
583:
584:            /**
585:             * Update all formatter settings with the settings of the specified profile. 
586:             * @param profile The profile to write to the preference store
587:             */
588:            private void writeToPreferenceStore(Profile profile,
589:                    IScopeContext context) {
590:                final Map profileOptions = profile.getSettings();
591:
592:                for (int i = 0; i < fKeySets.length; i++) {
593:                    updatePreferences(context
594:                            .getNode(fKeySets[i].getNodeName()), fKeySets[i]
595:                            .getKeys(), profileOptions);
596:                }
597:
598:                final IEclipsePreferences uiPrefs = context
599:                        .getNode(JavaUI.ID_PLUGIN);
600:                if (uiPrefs.getInt(fProfileVersionKey, 0) != fProfileVersioner
601:                        .getCurrentVersion()) {
602:                    uiPrefs.putInt(fProfileVersionKey, fProfileVersioner
603:                            .getCurrentVersion());
604:                }
605:
606:                if (context.getName() == InstanceScope.SCOPE) {
607:                    uiPrefs.put(fProfileKey, profile.getID());
608:                } else if (context.getName() == ProjectScope.SCOPE
609:                        && !profile.isSharedProfile()) {
610:                    uiPrefs.put(fProfileKey, profile.getID());
611:                }
612:            }
613:
614:            /** 
615:             * Get an immutable list as view on all profiles, sorted alphabetically. Unless the set 
616:             * of profiles has been modified between the two calls, the sequence is guaranteed to 
617:             * correspond to the one returned by <code>getSortedNames</code>.
618:             * @return a list of elements of type <code>Profile</code>
619:             * 
620:             * @see #getSortedDisplayNames()
621:             */
622:            public List getSortedProfiles() {
623:                return Collections.unmodifiableList(fProfilesByName);
624:            }
625:
626:            /**
627:             * Get the names of all profiles stored in this profile manager, sorted alphabetically. Unless the set of 
628:             * profiles has been modified between the two calls, the sequence is guaranteed to correspond to the one 
629:             * returned by <code>getSortedProfiles</code>.
630:             * @return All names, sorted alphabetically
631:             * @see #getSortedProfiles()  
632:             */
633:            public String[] getSortedDisplayNames() {
634:                final String[] sortedNames = new String[fProfilesByName.size()];
635:                int i = 0;
636:                for (final Iterator iter = fProfilesByName.iterator(); iter
637:                        .hasNext();) {
638:                    Profile curr = (Profile) iter.next();
639:                    sortedNames[i++] = curr.getName();
640:                }
641:                return sortedNames;
642:            }
643:
644:            /**
645:             * Get the profile for this profile id.
646:             * @param ID The profile ID
647:             * @return The profile with the given ID or <code>null</code> 
648:             */
649:            public Profile getProfile(String ID) {
650:                return (Profile) fProfiles.get(ID);
651:            }
652:
653:            /**
654:             * Activate the selected profile, update all necessary options in
655:             * preferences and save profiles to disk.
656:             */
657:            public void commitChanges(IScopeContext scopeContext) {
658:                if (fSelected != null) {
659:                    writeToPreferenceStore(fSelected, scopeContext);
660:                }
661:            }
662:
663:            public void clearAllSettings(IScopeContext context) {
664:                for (int i = 0; i < fKeySets.length; i++) {
665:                    updatePreferences(context
666:                            .getNode(fKeySets[i].getNodeName()), fKeySets[i]
667:                            .getKeys(), Collections.EMPTY_MAP);
668:                }
669:
670:                final IEclipsePreferences uiPrefs = context
671:                        .getNode(JavaUI.ID_PLUGIN);
672:                uiPrefs.remove(fProfileKey);
673:            }
674:
675:            /**
676:             * Get the currently selected profile.
677:             * @return The currently selected profile.
678:             */
679:            public Profile getSelected() {
680:                return fSelected;
681:            }
682:
683:            /**
684:             * Set the selected profile. The profile must already be contained in this profile manager.
685:             * @param profile The profile to select
686:             */
687:            public void setSelected(Profile profile) {
688:                final Profile newSelected = (Profile) fProfiles.get(profile
689:                        .getID());
690:                if (newSelected != null && !newSelected.equals(fSelected)) {
691:                    fSelected = newSelected;
692:                    notifyObservers(SELECTION_CHANGED_EVENT);
693:                }
694:            }
695:
696:            /**
697:             * Check whether a user-defined profile in this profile manager
698:             * already has this name.
699:             * @param name The name to test for
700:             * @return Returns <code>true</code> if a profile with the given name exists
701:             */
702:            public boolean containsName(String name) {
703:                for (final Iterator iter = fProfilesByName.iterator(); iter
704:                        .hasNext();) {
705:                    Profile curr = (Profile) iter.next();
706:                    if (name.equals(curr.getName())) {
707:                        return true;
708:                    }
709:                }
710:                return false;
711:            }
712:
713:            /**
714:             * Add a new custom profile to this profile manager.
715:             * @param profile The profile to add
716:             */
717:            public void addProfile(CustomProfile profile) {
718:                profile.setManager(this );
719:                final CustomProfile oldProfile = (CustomProfile) fProfiles
720:                        .get(profile.getID());
721:                if (oldProfile != null) {
722:                    fProfiles.remove(oldProfile.getID());
723:                    fProfilesByName.remove(oldProfile);
724:                    oldProfile.setManager(null);
725:                }
726:                fProfiles.put(profile.getID(), profile);
727:                fProfilesByName.add(profile);
728:                Collections.sort(fProfilesByName);
729:                fSelected = profile;
730:                notifyObservers(PROFILE_CREATED_EVENT);
731:            }
732:
733:            /**
734:             * Delete the currently selected profile from this profile manager. The next profile
735:             * in the list is selected.
736:             * @return true if the profile has been successfully removed, false otherwise.
737:             */
738:            public boolean deleteSelected() {
739:                if (!(fSelected instanceof  CustomProfile))
740:                    return false;
741:
742:                return deleteProfile((CustomProfile) fSelected);
743:            }
744:
745:            public boolean deleteProfile(CustomProfile profile) {
746:                int index = fProfilesByName.indexOf(profile);
747:
748:                fProfiles.remove(profile.getID());
749:                fProfilesByName.remove(profile);
750:
751:                profile.setManager(null);
752:
753:                if (index >= fProfilesByName.size())
754:                    index--;
755:                fSelected = (Profile) fProfilesByName.get(index);
756:
757:                if (!profile.isSharedProfile()) {
758:                    updateProfilesWithName(profile.getID(), null, false);
759:                }
760:
761:                notifyObservers(PROFILE_DELETED_EVENT);
762:                return true;
763:            }
764:
765:            public void profileRenamed(CustomProfile profile, String oldID) {
766:                fProfiles.remove(oldID);
767:                fProfiles.put(profile.getID(), profile);
768:
769:                if (!profile.isSharedProfile()) {
770:                    updateProfilesWithName(oldID, profile, false);
771:                }
772:
773:                Collections.sort(fProfilesByName);
774:                notifyObservers(PROFILE_RENAMED_EVENT);
775:            }
776:
777:            public void profileReplaced(CustomProfile oldProfile,
778:                    CustomProfile newProfile) {
779:                fProfiles.remove(oldProfile.getID());
780:                fProfiles.put(newProfile.getID(), newProfile);
781:                fProfilesByName.remove(oldProfile);
782:                fProfilesByName.add(newProfile);
783:                Collections.sort(fProfilesByName);
784:
785:                if (!oldProfile.isSharedProfile()) {
786:                    updateProfilesWithName(oldProfile.getID(), null, false);
787:                }
788:
789:                setSelected(newProfile);
790:                notifyObservers(PROFILE_CREATED_EVENT);
791:                notifyObservers(SELECTION_CHANGED_EVENT);
792:            }
793:
794:            public void profileChanged(CustomProfile profile) {
795:                if (!profile.isSharedProfile()) {
796:                    updateProfilesWithName(profile.getID(), profile, true);
797:                }
798:
799:                notifyObservers(SETTINGS_CHANGED_EVENT);
800:            }
801:
802:            protected void updateProfilesWithName(String oldName,
803:                    Profile newProfile, boolean applySettings) {
804:                IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
805:                        .getProjects();
806:                for (int i = 0; i < projects.length; i++) {
807:                    IScopeContext projectScope = fPreferencesAccess
808:                            .getProjectScope(projects[i]);
809:                    IEclipsePreferences node = projectScope
810:                            .getNode(JavaUI.ID_PLUGIN);
811:                    String profileId = node.get(fProfileKey, null);
812:                    if (oldName.equals(profileId)) {
813:                        if (newProfile == null) {
814:                            node.remove(fProfileKey);
815:                        } else {
816:                            if (applySettings) {
817:                                writeToPreferenceStore(newProfile, projectScope);
818:                            } else {
819:                                node.put(fProfileKey, newProfile.getID());
820:                            }
821:                        }
822:                    }
823:                }
824:
825:                IScopeContext instanceScope = fPreferencesAccess
826:                        .getInstanceScope();
827:                final IEclipsePreferences uiPrefs = instanceScope
828:                        .getNode(JavaUI.ID_PLUGIN);
829:                if (newProfile != null
830:                        && oldName.equals(uiPrefs.get(fProfileKey, null))) {
831:                    writeToPreferenceStore(newProfile, instanceScope);
832:                }
833:            }
834:
835:            private static void setLatestCompliance(Map map) {
836:                JavaModelUtil.set50CompilanceOptions(map);
837:            }
838:
839:            public abstract Profile getDefaultProfile();
840:
841:            public IProfileVersioner getProfileVersioner() {
842:                return fProfileVersioner;
843:            }
844:        }
w__ww.__ja___va__2___s___.__c__o___m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.