absolute paths for external resources -> relative paths + dispose library editors...
[fedora-idea.git] / platform / platform-impl / src / com / intellij / openapi / components / impl / ProjectPathMacroManager.java
blob225de01201a94c0719b769fd4babcf4e610f9536
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.openapi.components.impl;
18 import com.intellij.application.options.PathMacrosImpl;
19 import com.intellij.application.options.ReplacePathToMacroMap;
20 import com.intellij.openapi.components.ExpandMacroToPathMap;
21 import com.intellij.openapi.components.PathMacroMap;
22 import com.intellij.openapi.project.ex.ProjectEx;
23 import com.intellij.openapi.util.text.StringUtil;
24 import com.intellij.openapi.vfs.VirtualFile;
25 import org.jetbrains.annotations.NonNls;
26 import org.jetbrains.annotations.Nullable;
28 import java.io.File;
30 public class ProjectPathMacroManager extends BasePathMacroManager {
31 private final ProjectEx myProject;
34 public ProjectPathMacroManager(final ProjectEx project) {
35 myProject = project;
38 public ExpandMacroToPathMap getExpandMacroMap() {
39 ExpandMacroToPathMap result = super.getExpandMacroMap();
40 getExpandProjectHomeReplacements(result);
41 return result;
44 public ReplacePathToMacroMap getReplacePathMap() {
45 ReplacePathToMacroMap result = new ReplacePathToMacroMap();
46 result.putAll(super.getReplacePathMap());
47 getProjectHomeReplacements(result, true);
48 return result;
51 private void getProjectHomeReplacements(@NonNls ReplacePathToMacroMap result, final boolean savePathsRelative) {
52 String projectDir = getProjectDir();
53 if (projectDir == null) return;
55 File f = new File(projectDir.replace('/', File.separatorChar));
56 //LOG.assertTrue(f.exists());
58 String macro = "$" + PathMacrosImpl.PROJECT_DIR_MACRO_NAME + "$";
59 boolean check = false;
60 while (f != null) {
61 String path = PathMacroMap.quotePath(f.getAbsolutePath());
62 String s = macro;
64 if (StringUtil.endsWithChar(path, '/')) s += "/";
65 if (path.equals("/")) break;
67 putIfAbsent(result, "file://" + path, "file://" + s, check);
68 putIfAbsent(result, "file:/" + path, "file:/" + s, check);
69 putIfAbsent(result, "file:" + path, "file:" + s, check);
70 putIfAbsent(result, "jar://" + path, "jar://" + s, check);
71 putIfAbsent(result, "jar:/" + path, "jar:/" + s, check);
72 putIfAbsent(result, "jar:" + path, "jar:" + s, check);
73 //noinspection HardCodedStringLiteral
74 if (!path.equalsIgnoreCase("e:/") && !path.equalsIgnoreCase("r:/") && !path.equalsIgnoreCase("p:/")) {
75 putIfAbsent(result, path, s, check);
78 if (!savePathsRelative) break;
79 check = true;
80 macro += "/..";
81 f = f.getParentFile();
85 private void getExpandProjectHomeReplacements(ExpandMacroToPathMap result) {
86 String projectDir = getProjectDir();
87 if (projectDir == null) return;
89 File f = new File(projectDir.replace('/', File.separatorChar));
91 getExpandProjectHomeReplacements(result, f, "$" + PathMacrosImpl.PROJECT_DIR_MACRO_NAME + "$");
94 private static void getExpandProjectHomeReplacements(ExpandMacroToPathMap result, File f, String macro) {
95 if (f == null) return;
97 getExpandProjectHomeReplacements(result, f.getParentFile(), macro + "/..");
98 String path = PathMacroMap.quotePath(f.getAbsolutePath());
99 String s = macro;
101 if (StringUtil.endsWithChar(path, '/')) s += "/";
103 result.put(s, path);
106 @Nullable
107 private String getProjectDir() {
108 final VirtualFile baseDir = myProject.getBaseDir();
109 return baseDir != null ? baseDir.getPath() : null;