1 11 package org.eclipse.jdt.internal.ui.preferences.formatter; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.Observable ; 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 42 public abstract class ProfileManager extends Observable { 43 44 public static final class KeySet { 45 46 private final List fKeys; 47 private final String fNodeName; 48 49 public KeySet(String nodeName, List keys) { 50 fNodeName= nodeName; 51 fKeys= keys; 52 } 53 54 public String getNodeName() { 55 return fNodeName; 56 } 57 58 public List getKeys() { 59 return fKeys; 60 } 61 } 62 63 67 public final static String ID_PREFIX= "_"; 69 73 public static abstract class Profile implements Comparable { 74 75 public abstract String getName(); 76 public abstract Profile rename(String name, ProfileManager manager); 77 78 public abstract Map getSettings(); 79 public abstract void setSettings(Map settings); 80 81 public abstract int getVersion(); 82 83 public boolean hasEqualSettings(Map otherMap, List allKeys) { 84 Map settings= getSettings(); 85 for (Iterator iter= allKeys.iterator(); iter.hasNext(); ){ 86 String key= (String ) iter.next(); 87 Object other= otherMap.get(key); 88 Object 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 getID(); 103 104 public boolean isSharedProfile() { 105 return false; 106 } 107 108 public boolean isBuiltInProfile() { 109 return false; 110 } 111 } 112 113 117 public static final class BuiltInProfile extends Profile { 118 private final String fName; 119 private final String fID; 120 private final Map fSettings; 121 private final int fOrder; 122 private final int fCurrentVersion; 123 private final String fProfileKind; 124 125 public BuiltInProfile(String ID, String name, Map settings, int order, int currentVersion, String profileKind) { 126 fName= name; 127 fID= ID; 128 fSettings= settings; 129 fOrder= order; 130 fCurrentVersion= currentVersion; 131 fProfileKind= profileKind; 132 } 133 134 public String getName() { 135 return fName; 136 } 137 138 public Profile rename(String name, ProfileManager manager) { 139 final String trimmed= name.trim(); 140 CustomProfile newProfile= new CustomProfile(trimmed, fSettings, fCurrentVersion, fProfileKind); 141 manager.addProfile(newProfile); 142 return newProfile; 143 } 144 145 public Map getSettings() { 146 return fSettings; 147 } 148 149 public void setSettings(Map settings) { 150 } 151 152 public String getID() { 153 return fID; 154 } 155 156 public final int compareTo(Object 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 180 public static class CustomProfile extends Profile { 181 private String fName; 182 private Map fSettings; 183 protected ProfileManager fManager; 184 private int fVersion; 185 private final String fKind; 186 187 public CustomProfile(String name, Map settings, int version, String kind) { 188 fName= name; 189 fSettings= settings; 190 fVersion= version; 191 fKind= kind; 192 } 193 194 public String getName() { 195 return fName; 196 } 197 198 public Profile rename(String name, ProfileManager manager) { 199 final String trimmed= name.trim(); 200 if (trimmed.equals(getName())) 201 return this; 202 203 String oldID= getID(); fName= trimmed; 205 206 manager.profileRenamed(this, oldID); 207 return this; 208 } 209 210 public Map getSettings() { 211 return fSettings; 212 } 213 214 public void setSettings(Map settings) { 215 if (settings == null) 216 throw new IllegalArgumentException (); 217 fSettings= settings; 218 if (fManager != null) { 219 fManager.profileChanged(this); 220 } 221 } 222 223 public String 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 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 getKind() { 258 return fKind; 259 } 260 261 } 262 263 public final class SharedProfile extends CustomProfile { 264 265 public SharedProfile(String oldName, Map options, int version, String profileKind) { 266 super(oldName, options, version, profileKind); 267 } 268 269 public Profile rename(String 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 getID() { 277 return SHARED_PROFILE; 278 } 279 280 public final int compareTo(Object 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 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 307 private final String fProfileKey; 308 309 312 private final String fProfileVersionKey; 313 314 315 private final static String SHARED_PROFILE= "org.eclipse.jdt.ui.default.shared"; 317 318 321 private final Map fProfiles; 322 323 326 private final List fProfilesByName; 327 328 329 332 private Profile fSelected; 333 334 335 338 private final KeySet[] fKeySets; 339 340 private final PreferencesAccess fPreferencesAccess; 341 private final IProfileVersioner fProfileVersioner; 342 343 348 public ProfileManager( 349 List profiles, 350 IScopeContext context, 351 PreferencesAccess preferencesAccess, 352 IProfileVersioner profileVersioner, 353 KeySet[] keySets, 354 String profileKey, 355 String profileVersionKey) { 356 357 fPreferencesAccess= preferencesAccess; 358 fProfileVersioner= profileVersioner; 359 fKeySets= keySets; 360 fProfileKey= profileKey; 361 fProfileVersionKey= profileVersionKey; 362 363 fProfiles= new HashMap (); 364 fProfilesByName= new ArrayList (); 365 366 for (final Iterator 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 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 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.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 for (final Iterator 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 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 SharedProfile shared= new SharedProfile(name, map, fProfileVersioner.getCurrentVersion(), fProfileVersioner.getProfileKind()); 422 shared.setManager(this); 423 fProfiles.put(shared.getID(), shared); 424 fProfilesByName.add(shared); matching= shared; 426 } 427 fSelected= matching; 428 } 429 } 430 } 431 432 protected String getSelectedProfileId(IScopeContext instanceScope) { 433 String profileId= instanceScope.getNode(JavaUI.ID_PLUGIN).get(fProfileKey, null); 434 if (profileId == null) { 435 profileId= new DefaultScope().getNode(JavaUI.ID_PLUGIN).get(fProfileKey, null); 437 } 438 return profileId; 439 } 440 441 451 protected void notifyObservers(int message) { 452 setChanged(); 453 notifyObservers(new Integer (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 keyIter= keySet.getKeys().iterator(); keyIter.hasNext();) { 461 final String key= (String )keyIter.next(); 462 Object 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 479 private Map readFromPreferenceStore(IScopeContext context, Profile workspaceProfile) { 480 final Map profileOptions= new HashMap (); 481 IEclipsePreferences uiPrefs= context.getNode(JavaUI.ID_PLUGIN); 482 483 int version= uiPrefs.getInt(fProfileVersionKey, fProfileVersioner.getFirstVersion()); 484 if (version != fProfileVersioner.getCurrentVersion()) { 485 Map allOptions= new HashMap (); 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()); 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 keyIter = keySet.getKeys().iterator(); keyIter.hasNext(); ) { 499 final String key= (String ) keyIter.next(); 500 Object 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 523 private void addAll(IEclipsePreferences uiPrefs, Map allOptions) { 524 try { 525 String [] keys= uiPrefs.keys(); 526 for (int i= 0; i < keys.length; i++) { 527 String key= keys[i]; 528 String val= uiPrefs.get(key, null); 529 if (val != null) { 530 allOptions.put(key, val); 531 } 532 } 533 } catch (BackingStoreException e) { 534 } 536 537 } 538 539 private boolean updatePreferences(IEclipsePreferences prefs, List keys, Map profileOptions) { 540 boolean hasChanges= false; 541 for (final Iterator keyIter = keys.iterator(); keyIter.hasNext(); ) { 542 final String key= (String ) keyIter.next(); 543 final String oldVal= prefs.get(key, null); 544 final String val= (String ) 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 563 private void writeToPreferenceStore(Profile profile, IScopeContext context) { 564 final Map 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 591 public List getSortedProfiles() { 592 return Collections.unmodifiableList(fProfilesByName); 593 } 594 595 602 public String [] getSortedDisplayNames() { 603 final String [] sortedNames= new String [fProfilesByName.size()]; 604 int i= 0; 605 for (final Iterator iter = fProfilesByName.iterator(); iter.hasNext();) { 606 Profile curr= (Profile) iter.next(); 607 sortedNames[i++]= curr.getName(); 608 } 609 return sortedNames; 610 } 611 612 617 public Profile getProfile(String ID) { 618 return (Profile)fProfiles.get(ID); 619 } 620 621 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 644 public Profile getSelected() { 645 return fSelected; 646 } 647 648 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 666 public boolean containsName(String name) { 667 for (final Iterator 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 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 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 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 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 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 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 |