/**
  * Add a new custom profile to this profile manager.
  *
  * @param profile The profile to add
  */
 public void addProfile(CustomProfile profile) {
   profile.setManager(this);
   final CustomProfile oldProfile = (CustomProfile) fProfiles.get(profile.getID());
   if (oldProfile != null) {
     fProfiles.remove(oldProfile.getID());
     fProfilesByName.remove(oldProfile);
     oldProfile.setManager(null);
   }
   fProfiles.put(profile.getID(), profile);
   fProfilesByName.add(profile);
   Collections.sort(fProfilesByName);
   fSelected = profile;
   notifyObservers(PROFILE_CREATED_EVENT);
 }
 @Override
 public Profile rename(String name, ProfileManager manager) {
   CustomProfile profile =
       new CustomProfile(name.trim(), getSettings(), getVersion(), getKind());
   profile.setManager(manager);
   manager.profileReplaced(this, profile);
   return profile;
 }
  public boolean deleteProfile(CustomProfile profile) {
    int index = fProfilesByName.indexOf(profile);

    fProfiles.remove(profile.getID());
    fProfilesByName.remove(profile);

    profile.setManager(null);

    if (index >= fProfilesByName.size()) index--;
    fSelected = fProfilesByName.get(index);

    if (!profile.isSharedProfile()) {
      updateProfilesWithName(profile.getID(), null, false);
    }

    notifyObservers(PROFILE_DELETED_EVENT);
    return true;
  }
  /**
   * Create and initialize a new profile manager.
   *
   * @param profiles Initial custom profiles (List of type <code>CustomProfile</code>)
   * @param profileVersioner
   */
  public ProfileManager(
      List<Profile> profiles,
      IScopeContext context,
      PreferencesAccess preferencesAccess,
      IProfileVersioner profileVersioner,
      KeySet[] keySets,
      String profileKey,
      String profileVersionKey) {

    fPreferencesAccess = preferencesAccess;
    fProfileVersioner = profileVersioner;
    fKeySets = keySets;
    fProfileKey = profileKey;
    fProfileVersionKey = profileVersionKey;

    fProfiles = new HashMap<String, Profile>();
    fProfilesByName = new ArrayList<Profile>();

    for (final Iterator<Profile> iter = profiles.iterator(); iter.hasNext(); ) {
      final Profile profile = iter.next();
      if (profile instanceof CustomProfile) {
        ((CustomProfile) profile).setManager(this);
      }
      fProfiles.put(profile.getID(), profile);
      fProfilesByName.add(profile);
    }

    Collections.sort(fProfilesByName);

    String profileId = getSelectedProfileId(fPreferencesAccess.getInstanceScope());

    Profile profile = fProfiles.get(profileId);
    if (profile == null) {
      profile = getDefaultProfile();
    }
    fSelected = profile;

    if (context.getName() == ProjectScope.SCOPE && hasProjectSpecificSettings(context)) {
      Map<String, String> map = readFromPreferenceStore(context, profile);
      if (map != null) {

        List<String> allKeys = new ArrayList<String>();
        for (int i = 0; i < fKeySets.length; i++) {
          allKeys.addAll(fKeySets[i].getKeys());
        }
        Collections.sort(allKeys);

        Profile matching = null;

        String projProfileId = context.getNode(JavaUI.ID_PLUGIN).get(fProfileKey, null);
        if (projProfileId != null) {
          Profile curr = fProfiles.get(projProfileId);
          if (curr != null && (curr.isBuiltInProfile() || curr.hasEqualSettings(map, allKeys))) {
            matching = curr;
          }
        } else {
          // old version: look for similar
          for (final Iterator<Profile> iter = fProfilesByName.iterator(); iter.hasNext(); ) {
            Profile curr = iter.next();
            if (curr.hasEqualSettings(map, allKeys)) {
              matching = curr;
              break;
            }
          }
        }
        if (matching == null) {
          String name;
          if (projProfileId != null && !fProfiles.containsKey(projProfileId)) {
            name =
                Messages.format(
                    FormatterMessages.ProfileManager_unmanaged_profile_with_name,
                    projProfileId.substring(ID_PREFIX.length()));
          } else {
            name = FormatterMessages.ProfileManager_unmanaged_profile;
          }
          // current settings do not correspond to any profile -> create a 'team' profile
          SharedProfile shared =
              new SharedProfile(
                  name,
                  map,
                  fProfileVersioner.getCurrentVersion(),
                  fProfileVersioner.getProfileKind());
          shared.setManager(this);
          fProfiles.put(shared.getID(), shared);
          fProfilesByName.add(shared); // add last
          matching = shared;
        }
        fSelected = matching;
      }
    }
  }