Macroses must be expanded also after settings conversion
[fedora-idea.git] / lang-impl / src / com / intellij / openapi / components / impl / stores / ModuleStoreImpl.java
blob97dca0925435087c6cd10f4c1c47190fa96d3f64
1 package com.intellij.openapi.components.impl.stores;
3 import com.intellij.ide.impl.convert.ProjectConversionHelper;
4 import com.intellij.openapi.application.ApplicationManager;
5 import com.intellij.openapi.components.PathMacroManager;
6 import com.intellij.openapi.components.StateStorage;
7 import com.intellij.openapi.components.impl.ComponentManagerImpl;
8 import com.intellij.openapi.diagnostic.Logger;
9 import com.intellij.openapi.module.Module;
10 import com.intellij.openapi.module.ModuleTypeManager;
11 import com.intellij.openapi.module.impl.ModuleImpl;
12 import com.intellij.openapi.project.ex.ProjectEx;
13 import com.intellij.openapi.vfs.LocalFileSystem;
14 import com.intellij.openapi.vfs.VirtualFile;
15 import org.jdom.Attribute;
16 import org.jdom.Element;
17 import org.jetbrains.annotations.NonNls;
18 import org.jetbrains.annotations.NotNull;
19 import org.jetbrains.annotations.Nullable;
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.TreeMap;
28 public class ModuleStoreImpl extends BaseFileConfigurableStoreImpl implements IModuleStore {
29 private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.components.impl.stores.ModuleStoreImpl");
30 @NonNls private static final String MODULE_FILE_MACRO = "MODULE_FILE";
32 private ModuleImpl myModule;
34 public static final String DEFAULT_STATE_STORAGE = "$" + MODULE_FILE_MACRO + "$";
37 @SuppressWarnings({"UnusedDeclaration"})
38 public ModuleStoreImpl(final ComponentManagerImpl componentManager, final ModuleImpl module) {
39 super(componentManager);
40 myModule = module;
43 protected XmlElementStorage getMainStorage() {
44 final XmlElementStorage storage = (XmlElementStorage)getStateStorageManager().getFileStateStorage(DEFAULT_STATE_STORAGE);
45 assert storage != null;
46 return storage;
51 @Override
52 public void load() throws IOException, StateStorage.StateStorageException {
53 super.load();
55 final ModuleFileData storageData = getMainStorageData();
56 final String moduleTypeId = storageData.myOptions.get(ModuleImpl.ELEMENT_TYPE);
57 myModule.setModuleType(ModuleTypeManager.getInstance().findByID(moduleTypeId));
61 public ModuleFileData getMainStorageData() throws StateStorage.StateStorageException {
62 return (ModuleFileData)super.getMainStorageData();
65 static class ModuleFileData extends BaseStorageData {
66 private final Map<String, String> myOptions;
67 private final Module myModule;
69 public ModuleFileData(final String rootElementName, Module module) {
70 super(rootElementName);
71 myModule = module;
72 myOptions = new TreeMap<String, String>();
75 protected ModuleFileData(final ModuleFileData storageData) {
76 super(storageData);
78 myOptions = new TreeMap<String, String>(storageData.myOptions);
79 myModule = storageData.myModule;
82 protected void load(@NotNull final Element rootElement) throws IOException {
83 final ProjectConversionHelper conversionHelper = getConversionHelper(myModule);
84 if (conversionHelper != null) {
85 conversionHelper.convertModuleRootToNewFormat(rootElement, myModule.getName());
86 PathMacroManager.getInstance(myModule).expandPaths(rootElement);
89 super.load(rootElement);
91 final List attributes = rootElement.getAttributes();
92 for (Object attribute : attributes) {
93 final Attribute attr = (Attribute)attribute;
94 myOptions.put(attr.getName(), attr.getValue());
98 @NotNull
99 protected Element save() {
100 final Element root = super.save();
102 myOptions.put(VERSION_OPTION, Integer.toString(myVersion));
103 Set<String> options = myOptions.keySet();
104 for (String option : options) {
105 root.setAttribute(option, myOptions.get(option));
108 final ProjectConversionHelper conversionHelper = getConversionHelper(myModule);
109 if (conversionHelper != null) {
110 conversionHelper.convertModuleRootToOldFormat(root, myModule.getName());
113 //need be last for compat reasons
114 root.removeAttribute(VERSION_OPTION);
115 root.setAttribute(VERSION_OPTION, Integer.toString(myVersion));
117 return root;
120 public XmlElementStorage.StorageData clone() {
121 return new ModuleFileData(this);
124 protected int computeHash() {
125 return super.computeHash()*31 + myOptions.hashCode();
128 @Nullable
129 public Set<String> getDifference(final XmlElementStorage.StorageData storageData) {
130 final ModuleFileData data = (ModuleFileData)storageData;
131 if (!myOptions.equals(data.myOptions)) return null;
132 return super.getDifference(storageData);
135 public void setOption(final String optionName, final String optionValue) {
136 clearHash();
137 myOptions.put(optionName, optionValue);
140 public void clearOption(final String optionName) {
141 clearHash();
142 myOptions.remove(optionName);
145 public String getOptionValue(final String optionName) {
146 return myOptions.get(optionName);
151 @Nullable
152 private static ProjectConversionHelper getConversionHelper(Module module) {
153 return (ProjectConversionHelper)module.getProject().getPicoContainer().getComponentInstance(ProjectConversionHelper.class);
156 public void setModuleFilePath(@NotNull final String filePath) {
157 final String path = filePath.replace(File.separatorChar, '/');
158 ApplicationManager.getApplication().runWriteAction(new Runnable() {
159 public void run() {
160 LocalFileSystem.getInstance().refreshAndFindFileByPath(path);
163 final StateStorageManager storageManager = getStateStorageManager();
164 storageManager.clearStateStorage(DEFAULT_STATE_STORAGE);
165 storageManager.addMacro(MODULE_FILE_MACRO, path);
168 @Nullable
169 public VirtualFile getModuleFile() {
170 final FileBasedStorage storage = (FileBasedStorage)getStateStorageManager().getFileStateStorage(DEFAULT_STATE_STORAGE);
171 assert storage != null;
172 return storage.getVirtualFile();
175 @NotNull
176 public String getModuleFilePath() {
177 final FileBasedStorage storage = (FileBasedStorage)getStateStorageManager().getFileStateStorage(DEFAULT_STATE_STORAGE);
178 assert storage != null;
179 return storage.getFilePath();
182 @NotNull
183 public String getModuleFileName() {
184 final FileBasedStorage storage = (FileBasedStorage)getStateStorageManager().getFileStateStorage(DEFAULT_STATE_STORAGE);
185 assert storage != null;
186 return storage.getFileName();
189 public void setSavePathsRelative(final boolean b) {
190 super.setSavePathsRelative(b);
191 setOption(RELATIVE_PATHS_OPTION, String.valueOf(b));
194 public void setOption(final String optionName, final String optionValue) {
195 try {
196 getMainStorageData().setOption(optionName, optionValue);
198 catch (StateStorage.StateStorageException e) {
199 LOG.error(e);
203 public void clearOption(final String optionName) {
204 try {
205 getMainStorageData().clearOption(optionName);
207 catch (StateStorage.StateStorageException e) {
208 LOG.error(e);
212 public String getOptionValue(final String optionName) {
213 try {
214 return getMainStorageData().getOptionValue(optionName);
216 catch (StateStorage.StateStorageException e) {
217 LOG.error(e);
218 return null;
222 @Override
223 protected boolean optimizeTestLoading() {
224 return ((ProjectEx)myModule.getProject()).isOptimiseTestLoadSpeed();
227 protected StateStorageManager createStateStorageManager() {
228 return new ModuleStateStorageManager(PathMacroManager.getInstance(getComponentManager()).createTrackingSubstitutor(), myModule);