protected void updateProfilesWithName(String oldName, Profile newProfile, boolean applySettings) {
    IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
    for (int i = 0; i < projects.length; i++) {
      IScopeContext projectScope = fPreferencesAccess.getProjectScope(projects[i]);
      IEclipsePreferences node = projectScope.getNode(JavaUI.ID_PLUGIN);
      String profileId = node.get(fProfileKey, null);
      if (oldName.equals(profileId)) {
        if (newProfile == null) {
          node.remove(fProfileKey);
        } else {
          if (applySettings) {
            writeToPreferenceStore(newProfile, projectScope);
          } else {
            node.put(fProfileKey, newProfile.getID());
          }
        }
      }
    }

    IScopeContext instanceScope = fPreferencesAccess.getInstanceScope();
    final IEclipsePreferences uiPrefs = instanceScope.getNode(JavaUI.ID_PLUGIN);
    if (newProfile != null && oldName.equals(uiPrefs.get(fProfileKey, null))) {
      writeToPreferenceStore(newProfile, instanceScope);
    }
  }
 /**
  * Set the selected profile. The profile must already be contained in this profile manager.
  *
  * @param profile The profile to select
  */
 public void setSelected(Profile profile) {
   final Profile newSelected = fProfiles.get(profile.getID());
   if (newSelected != null && !newSelected.equals(fSelected)) {
     fSelected = newSelected;
     notifyObservers(SELECTION_CHANGED_EVENT);
   }
 }
  /**
   * Update all formatter settings with the settings of the specified profile.
   *
   * @param profile The profile to write to the preference store
   */
  private void writeToPreferenceStore(Profile profile, IScopeContext context) {
    final Map<String, String> profileOptions = profile.getSettings();

    for (int i = 0; i < fKeySets.length; i++) {
      updatePreferences(
          context.getNode(fKeySets[i].getNodeName()), fKeySets[i].getKeys(), profileOptions);
    }

    final IEclipsePreferences uiPrefs = context.getNode(JavaUI.ID_PLUGIN);
    if (uiPrefs.getInt(fProfileVersionKey, 0) != fProfileVersioner.getCurrentVersion()) {
      uiPrefs.putInt(fProfileVersionKey, fProfileVersioner.getCurrentVersion());
    }

    if (context.getName() == InstanceScope.SCOPE) {
      uiPrefs.put(fProfileKey, profile.getID());
    } else if (context.getName() == ProjectScope.SCOPE && !profile.isSharedProfile()) {
      uiPrefs.put(fProfileKey, profile.getID());
    }
  }
  @Override
  protected void updateProfilesWithName(String oldName, Profile newProfile, boolean applySettings) {
    super.updateProfilesWithName(oldName, newProfile, applySettings);

    IEclipsePreferences node = fPreferencesAccess.getInstanceScope().getNode(JavaUI.ID_PLUGIN);
    String name = node.get(CleanUpConstants.CLEANUP_ON_SAVE_PROFILE, null);
    if (name != null && name.equals(oldName)) {
      if (newProfile == null) {
        node.remove(CleanUpConstants.CLEANUP_ON_SAVE_PROFILE);
      } else {
        node.put(CleanUpConstants.CLEANUP_ON_SAVE_PROFILE, newProfile.getID());
      }
    }
  }
  /**
   * 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;
      }
    }
  }