write file even through only project profile name was changed (IDEA-25257)
[fedora-idea.git] / platform / lang-api / src / com / intellij / profile / DefaultProjectProfileManager.java
blobc0a3256a9d459779a5a04fb7aea804a50d24758e
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.profile;
18 import com.intellij.openapi.application.ApplicationNamesInfo;
19 import com.intellij.openapi.components.StateSplitter;
20 import com.intellij.openapi.diagnostic.Logger;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.util.*;
23 import com.intellij.openapi.util.io.FileUtil;
24 import com.intellij.packageDependencies.DependencyValidationManager;
25 import com.intellij.psi.search.scope.packageSet.NamedScopesHolder;
26 import com.intellij.util.ArrayUtil;
27 import com.intellij.util.text.UniqueNameGenerator;
28 import org.jdom.Element;
29 import org.jetbrains.annotations.NonNls;
30 import org.jetbrains.annotations.NotNull;
32 import java.util.*;
34 /**
35 * User: anna
36 * Date: 30-Nov-2005
38 public abstract class DefaultProjectProfileManager extends ProjectProfileManager {
39 protected static final Logger LOG = Logger.getInstance("#com.intellij.profile.DefaultProjectProfileManager");
41 @NonNls protected static final String PROFILES = "profiles";
42 @NonNls public static final String SCOPES = "scopes";
43 @NonNls protected static final String SCOPE = "scope";
44 @NonNls public static final String PROFILE = "profile";
45 @NonNls protected static final String NAME = "name";
48 private static final String VERSION = "1.0";
50 protected final Project myProject;
52 private final String myProfileType;
54 public String PROJECT_PROFILE;
55 public boolean USE_PROJECT_PROFILE = !isPlatform();
57 protected ApplicationProfileManager myApplicationProfileManager;
59 protected Map<String, Profile> myProfiles = new HashMap<String, Profile>();
60 protected final DependencyValidationManager myHolder;
61 protected List<ProfileChangeAdapter> myProfilesListener = new ArrayList<ProfileChangeAdapter>();
62 @NonNls private static final String PROJECT_DEFAULT_PROFILE_NAME = "Project Default";
64 public DefaultProjectProfileManager(final Project project, final String profileType, final DependencyValidationManager holder) {
65 myProject = project;
66 myProfileType = profileType;
67 myHolder = holder;
68 myApplicationProfileManager = ApplicationProfileManager.getProfileManager(profileType);
69 LOG.assertTrue(myApplicationProfileManager != null);
72 public Profile getProfile(@NotNull String name, boolean returnRootProfileIfNamedIsAbsent) {
73 return myProfiles.containsKey(name) ? myProfiles.get(name) : myApplicationProfileManager.getProfile(name, returnRootProfileIfNamedIsAbsent);
76 public void updateProfile(Profile profile) {
77 myProfiles.put(profile.getName(), profile);
78 for (ProfileChangeAdapter profileChangeAdapter : myProfilesListener) {
79 profileChangeAdapter.profileChanged(profile);
85 public void readExternal(Element element) throws InvalidDataException {
86 myProfiles.clear();
87 DefaultJDOMExternalizer.readExternal(this, element);
88 final Element profilesElement = element.getChild(PROFILES);
89 if (profilesElement != null) {
90 for (Object o : profilesElement.getChildren(PROFILE)) {
91 final Profile profile = myApplicationProfileManager.createProfile();
92 profile.setProfileManager(this);
93 profile.readExternal((Element)o);
94 final String name = profile.getName();
95 if (myApplicationProfileManager.getProfile(name) != null) { //override ide profile
96 // myApplicationProfileManager.deleteProfile(name);
98 myProfiles.put(name, profile);
101 if (element.getChild("version") == null || !Comparing.strEqual(element.getChild("version").getAttributeValue("value"), VERSION)) {
102 boolean toConvert = true;
103 for (Object o : element.getChildren("option")) {
104 if (Comparing.strEqual(((Element)o).getAttributeValue("name"), "USE_PROJECT_LEVEL_SETTINGS")) {
105 toConvert = Boolean.parseBoolean(((Element)o).getAttributeValue("value"));
106 break;
109 if (toConvert) {
110 convert(element);
115 protected void convert(Element element) throws InvalidDataException {
118 public void writeExternal(Element element) throws WriteExternalException {
120 final List<String> sortedProfiles = new ArrayList<String>(myProfiles.keySet());
121 Element profiles = null;
122 Collections.sort(sortedProfiles);
123 for (String profile : sortedProfiles) {
124 final Profile projectProfile = myProfiles.get(profile);
125 if (projectProfile != null) {
126 final Element profileElement = new Element(PROFILE);
127 projectProfile.writeExternal(profileElement);
128 boolean hasSmthToSave = sortedProfiles.size() > 1;
129 for (Object child : profileElement.getChildren()) {
130 if (!((Element)child).getName().equals("option")) {
131 hasSmthToSave = true;
132 break;
135 if (!hasSmthToSave) continue;
137 if (profiles == null) {
138 profiles = new Element(PROFILES);
139 element.addContent(profiles);
142 profiles.addContent(profileElement);
146 if (profiles != null || !Comparing.strEqual(PROJECT_PROFILE, PROJECT_DEFAULT_PROFILE_NAME)) {
147 DefaultJDOMExternalizer.writeExternal(this, element);
148 final Element version = new Element("version");
149 version.setAttribute("value", VERSION);
150 element.addContent(version);
154 public NamedScopesHolder getScopesManager() {
155 return DependencyValidationManager.getInstance(myProject);
158 public String getProfileType() {
159 return myProfileType;
162 public Collection<Profile> getProfiles() {
163 getProjectProfileImpl();
164 return myProfiles.values();
167 public String[] getAvailableProfileNames() {
168 return ArrayUtil.toStringArray(myProfiles.keySet());
171 public void deleteProfile(String name) {
172 myProfiles.remove(name);
175 public String getProjectProfile() {
176 return PROJECT_PROFILE;
179 public void setProjectProfile(final String projectProfile) {
180 final String profileName = PROJECT_PROFILE;
181 PROJECT_PROFILE = projectProfile;
182 USE_PROJECT_PROFILE = projectProfile != null;
183 for (ProfileChangeAdapter adapter : myProfilesListener) {
184 adapter.profileActivated(profileName != null ? getProfile(profileName) : null, projectProfile != null ? getProfile(projectProfile) : null);
188 @NotNull
189 public Profile getProjectProfileImpl(){
190 if (isPlatform() || !USE_PROJECT_PROFILE) return myApplicationProfileManager.getRootProfile();
191 if (PROJECT_PROFILE == null || myProfiles.isEmpty()){
192 setProjectProfile(PROJECT_DEFAULT_PROFILE_NAME);
193 final Profile projectProfile = myApplicationProfileManager.createProfile();
194 projectProfile.copyFrom(myApplicationProfileManager.getRootProfile());
195 projectProfile.setLocal(false);
196 projectProfile.setName(PROJECT_DEFAULT_PROFILE_NAME);
197 myProfiles.put(PROJECT_DEFAULT_PROFILE_NAME, projectProfile);
198 } else if (!myProfiles.containsKey(PROJECT_PROFILE)){
199 final String projectProfileAttempt = myProfiles.keySet().iterator().next();
200 setProjectProfile(projectProfileAttempt);
202 final Profile profile = myProfiles.get(PROJECT_PROFILE);
203 profile.setProfileManager(this);
204 return profile;
207 private static boolean isPlatform() {
208 return !ApplicationNamesInfo.getInstance().getLowercaseProductName().equals("Idea");
211 public void addProfilesListener(ProfileChangeAdapter profilesListener) {
212 myProfilesListener.add(profilesListener);
215 public void removeProfilesListener(ProfileChangeAdapter profilesListener) {
216 myProfilesListener.remove(profilesListener);
219 public static class ProfileStateSplitter implements StateSplitter {
221 public List<Pair<Element, String>> splitState(final Element e) {
222 final UniqueNameGenerator generator = new UniqueNameGenerator();
223 List<Pair<Element, String>> result = new ArrayList<Pair<Element, String>>();
225 final Element[] elements = JDOMUtil.getElements(e);
226 for (Element element : elements) {
227 if (element.getName().equals("profiles")) {
228 element.detach();
230 final Element[] profiles = JDOMUtil.getElements(element);
231 for (Element profile : profiles) {
232 String profileName = null;
234 final Element[] options = JDOMUtil.getElements(profile);
235 for (Element option : options) {
236 if (option.getName().equals("option") && option.getAttributeValue("name").equals("myName")) {
237 profileName = option.getAttributeValue("value");
241 assert profileName != null;
243 final String name = generator.generateUniqueName(FileUtil.sanitizeFileName(profileName)) + ".xml";
244 result.add(new Pair<Element, String>(profile, name));
250 if (!e.getContent().isEmpty()) result.add(new Pair<Element, String>(e, generator.generateUniqueName("profiles_settings") + ".xml"));
252 return result;
255 public void mergeStatesInto(final Element target, final Element[] elements) {
256 Element profiles = new Element("profiles");
257 target.addContent(profiles);
259 for (Element element : elements) {
260 if (element.getName().equals("profile")) {
261 element.detach();
262 profiles.addContent(element);
264 else {
265 final Element[] states = JDOMUtil.getElements(element);
266 for (Element state : states) {
267 state.detach();
268 target.addContent(state);