VCS: asynch load of committed changes when do "Browse changes"
[fedora-idea.git] / platform / lang-impl / src / com / intellij / openapi / vcs / changes / ui / ChangesModuleGroupingPolicy.java
blob7cde5c9b92b1f8d78de60901619163e0f160da3a
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.
17 package com.intellij.openapi.vcs.changes.ui;
19 import com.intellij.openapi.module.Module;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.roots.ProjectFileIndex;
22 import com.intellij.openapi.roots.ProjectRootManager;
23 import com.intellij.openapi.vcs.FilePath;
24 import com.intellij.openapi.vfs.VirtualFile;
25 import org.jetbrains.annotations.Nullable;
27 import javax.swing.tree.DefaultTreeModel;
28 import java.util.HashMap;
30 /**
31 * @author yole
33 public class ChangesModuleGroupingPolicy implements ChangesGroupingPolicy {
34 private final Project myProject;
35 private final DefaultTreeModel myModel;
36 private final HashMap<Module, ChangesBrowserNode> myModuleCache = new HashMap<Module, ChangesBrowserNode>();
38 public static final String PROJECT_ROOT_TAG = "<Project Root>";
40 public ChangesModuleGroupingPolicy(final Project project, final DefaultTreeModel model) {
41 myProject = project;
42 myModel = model;
45 @Nullable
46 public ChangesBrowserNode getParentNodeFor(final ChangesBrowserNode node, final ChangesBrowserNode rootNode) {
47 ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex();
48 final FilePath path = TreeModelBuilder.getPathForObject(node.getUserObject());
49 VirtualFile vFile = path.getVirtualFile();
50 if (vFile != null && vFile == index.getContentRootForFile(vFile)) {
51 Module module = index.getModuleForFile(vFile);
52 return getNodeForModule(module, rootNode);
54 return null;
57 public void clear() {
58 myModuleCache.clear();
61 private ChangesBrowserNode getNodeForModule(Module module, ChangesBrowserNode root) {
62 ChangesBrowserNode node = myModuleCache.get(module);
63 if (node == null) {
64 if (module == null) {
65 node = ChangesBrowserNode.create(myProject, PROJECT_ROOT_TAG);
67 else {
68 node = new ChangesBrowserModuleNode(module);
70 myModel.insertNodeInto(node, root, root.getChildCount());
71 myModuleCache.put(module, node);
73 return node;