summaryrefslogtreecommitdiff
path: root/platform/projectModel-impl/src/com/intellij/openapi/projectRoots/impl/ProjectRootContainerImpl.java
blob: 22800f9a4e1a8771bd117bc2915f9df47f300bdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.intellij.openapi.projectRoots.impl;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.projectRoots.ProjectRootListener;
import com.intellij.openapi.projectRoots.ex.ProjectRoot;
import com.intellij.openapi.projectRoots.ex.ProjectRootContainer;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.PersistentOrderRootType;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.openapi.vfs.*;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.HashMap;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;

/**
 * @author mike
 */
public class ProjectRootContainerImpl implements JDOMExternalizable, ProjectRootContainer {
  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.projectRoots.impl.ProjectRootContainerImpl");
  private final Map<OrderRootType, CompositeProjectRoot> myRoots = new HashMap<OrderRootType, CompositeProjectRoot>();

  private Map<OrderRootType, VirtualFile[]> myFiles = new HashMap<OrderRootType, VirtualFile[]>();

  private boolean myInsideChange = false;
  private final List<ProjectRootListener> myListeners = ContainerUtil.createLockFreeCopyOnWriteList();

  private boolean myNoCopyJars = false;

  public ProjectRootContainerImpl(boolean noCopyJars) {
    myNoCopyJars = noCopyJars;

    for (OrderRootType rootType : OrderRootType.getAllTypes()) {
      myRoots.put(rootType, new CompositeProjectRoot());
      myFiles.put(rootType, VirtualFile.EMPTY_ARRAY);
    }
  }

  @Override
  @NotNull
  public VirtualFile[] getRootFiles(@NotNull OrderRootType type) {
    return myFiles.get(type);
  }

  @Override
  @NotNull
  public ProjectRoot[] getRoots(@NotNull OrderRootType type) {
    return myRoots.get(type).getProjectRoots();
  }

  @Override
  public void startChange() {
    LOG.assertTrue(!myInsideChange);

    myInsideChange = true;
  }

  @Override
  public void finishChange() {
    LOG.assertTrue(myInsideChange);
    HashMap<OrderRootType, VirtualFile[]> oldRoots = new HashMap<OrderRootType, VirtualFile[]>(myFiles);

    for (OrderRootType orderRootType : OrderRootType.getAllTypes()) {
      final VirtualFile[] roots = myRoots.get(orderRootType).getVirtualFiles();
      final boolean same = Comparing.equal(roots, oldRoots.get(orderRootType));

      myFiles.put(orderRootType, myRoots.get(orderRootType).getVirtualFiles());

      if (!same) {
        fireRootsChanged();
      }
    }

    myInsideChange = false;
  }

  public void addProjectRootContainerListener(ProjectRootListener listener) {
    myListeners.add(listener);
  }

  public void removeProjectRootContainerListener(ProjectRootListener listener) {
    myListeners.remove(listener);
  }

  private void fireRootsChanged() {
    /*
    ApplicationManager.getApplication().runReadAction(new Runnable() {
      public void run() {
        LOG.info("roots changed: type='" + type + "'\n    oldRoots='" + Arrays.asList(oldRoots) + "'\n    newRoots='" + Arrays.asList(newRoots) + "' ");
      }
    });
    */
    for (final ProjectRootListener listener : myListeners) {
      listener.rootsChanged();
    }
  }


  @Override
  public void removeRoot(@NotNull ProjectRoot root, @NotNull OrderRootType type) {
    LOG.assertTrue(myInsideChange);
    myRoots.get(type).remove(root);
  }

  @Override
  @NotNull
  public ProjectRoot addRoot(@NotNull VirtualFile virtualFile, @NotNull OrderRootType type) {
    LOG.assertTrue(myInsideChange);
    return myRoots.get(type).add(virtualFile);
  }

  @Override
  public void addRoot(@NotNull ProjectRoot root, @NotNull OrderRootType type) {
    LOG.assertTrue(myInsideChange);
    myRoots.get(type).add(root);
  }

  @Override
  public void removeAllRoots(@NotNull OrderRootType type) {
    LOG.assertTrue(myInsideChange);
    myRoots.get(type).clear();
  }

  @Override
  public void removeRoot(@NotNull VirtualFile root, @NotNull OrderRootType type) {
    LOG.assertTrue(myInsideChange);
    myRoots.get(type).remove(root);
  }

  @Override
  public void removeAllRoots() {
    LOG.assertTrue(myInsideChange);
    for (CompositeProjectRoot myRoot : myRoots.values()) {
      myRoot.clear();
    }
  }

  @Override
  public void update() {
    LOG.assertTrue(myInsideChange);
    for (CompositeProjectRoot myRoot : myRoots.values()) {
      myRoot.update();
    }
  }

  @Override
  public void readExternal(Element element) throws InvalidDataException {
    for (PersistentOrderRootType type : OrderRootType.getAllPersistentTypes()) {
      read(element, type);
    }

    ApplicationManager.getApplication().runReadAction(new Runnable() {
      @Override
      public void run() {
        myFiles = new HashMap<OrderRootType, VirtualFile[]>();
        for (OrderRootType rootType : myRoots.keySet()) {
          CompositeProjectRoot root = myRoots.get(rootType);
          if (myNoCopyJars) {
            setNoCopyJars(root);
          }
          myFiles.put(rootType, root.getVirtualFiles());
        }
      }
    });

    for (OrderRootType type : OrderRootType.getAllTypes()) {
      final VirtualFile[] newRoots = getRootFiles(type);
      final VirtualFile[] oldRoots = VirtualFile.EMPTY_ARRAY;
      if (!Comparing.equal(oldRoots, newRoots)) {
        fireRootsChanged();
      }
    }
  }

  @Override
  public void writeExternal(Element element) throws WriteExternalException {
    List<PersistentOrderRootType> allTypes = OrderRootType.getSortedRootTypes();
    for (PersistentOrderRootType type : allTypes) {
      write(element, type);
    }
  }

  private static void setNoCopyJars(ProjectRoot root) {
    if (root instanceof SimpleProjectRoot) {
      String url = ((SimpleProjectRoot)root).getUrl();
      if (StandardFileSystems.JAR_PROTOCOL.equals(VirtualFileManager.extractProtocol(url))) {
        String path = VirtualFileManager.extractPath(url);
        final VirtualFileSystem fileSystem = StandardFileSystems.jar();
        if (fileSystem instanceof JarCopyingFileSystem) {
          ((JarCopyingFileSystem)fileSystem).setNoCopyJarForPath(path);
        }
      }
    }
    else if (root instanceof CompositeProjectRoot) {
      ProjectRoot[] roots = ((CompositeProjectRoot)root).getProjectRoots();
      for (ProjectRoot root1 : roots) {
        setNoCopyJars(root1);
      }
    }
  }

  private void read(Element element, PersistentOrderRootType type) throws InvalidDataException {
    Element child = element.getChild(type.getSdkRootName());
    if (child == null) {
      myRoots.put(type, new CompositeProjectRoot());
      return;
    }

    List children = child.getChildren();
    LOG.assertTrue(children.size() == 1);
    CompositeProjectRoot root = (CompositeProjectRoot)ProjectRootUtil.read((Element)children.get(0));
    myRoots.put(type, root);
  }

  private void write(Element roots, PersistentOrderRootType type) throws WriteExternalException {
    Element e = new Element(type.getSdkRootName());
    roots.addContent(e);
    final Element root = ProjectRootUtil.write(myRoots.get(type));
    if (root != null) {
      e.addContent(root);
    }
  }


  @SuppressWarnings({"HardCodedStringLiteral"})
  void readOldVersion(Element child) {
    for (final Object o : child.getChildren("root")) {
      Element root = (Element)o;
      String url = root.getAttributeValue("file");
      SimpleProjectRoot projectRoot = new SimpleProjectRoot(url);
      String type = root.getChild("property").getAttributeValue("value");

      for (PersistentOrderRootType rootType : OrderRootType.getAllPersistentTypes()) {
        if (type.equals(rootType.getOldSdkRootName())) {
          addRoot(projectRoot, rootType);
          break;
        }
      }
    }

    myFiles = new HashMap<OrderRootType, VirtualFile[]>();
    for (OrderRootType rootType : myRoots.keySet()) {
      myFiles.put(rootType, myRoots.get(rootType).getVirtualFiles());
    }
    for (OrderRootType type : OrderRootType.getAllTypes()) {
      final VirtualFile[] oldRoots = VirtualFile.EMPTY_ARRAY;
      final VirtualFile[] newRoots = getRootFiles(type);
      if (!Comparing.equal(oldRoots, newRoots)) {
        fireRootsChanged();
      }
    }
  }
}