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
;
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
) {
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
);
57 private ChangesBrowserNode
getNodeForModule(Module module
, ChangesBrowserNode root
) {
58 ChangesBrowserNode node
= myModuleCache
.get(module
);
61 node
= ChangesBrowserNode
.create(myProject
, PROJECT_ROOT_TAG
);
64 node
= new ChangesBrowserModuleNode(module
);
66 myModel
.insertNodeInto(node
, root
, root
.getChildCount());
67 myModuleCache
.put(module
, node
);