update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / usages / impl / rules / PackageGroupingRule.java
blobfb181d4a767d5c07a9cd2a5258bb3cf141e6f0d7
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.usages.impl.rules;
18 import com.intellij.openapi.actionSystem.DataKey;
19 import com.intellij.openapi.actionSystem.DataSink;
20 import com.intellij.openapi.actionSystem.LangDataKeys;
21 import com.intellij.openapi.actionSystem.TypeSafeDataProvider;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.openapi.util.Iconable;
24 import com.intellij.openapi.vcs.FileStatus;
25 import com.intellij.openapi.vcs.FileStatusManager;
26 import com.intellij.openapi.vfs.VirtualFile;
27 import com.intellij.psi.JavaDirectoryService;
28 import com.intellij.psi.PsiDirectory;
29 import com.intellij.psi.PsiManager;
30 import com.intellij.psi.PsiPackage;
31 import com.intellij.usages.UsageGroup;
32 import com.intellij.usages.UsageView;
33 import org.jetbrains.annotations.NotNull;
35 import javax.swing.*;
37 /**
38 * @author max
40 public class PackageGroupingRule extends DirectoryGroupingRule {
41 public PackageGroupingRule(Project project) {
42 super(project);
45 protected UsageGroup getGroupForFile(final VirtualFile dir) {
46 PsiDirectory psiDirectory = PsiManager.getInstance(myProject).findDirectory(dir);
47 if (psiDirectory != null) {
48 PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory);
49 if (aPackage != null) return new PackageGroup(aPackage);
51 return super.getGroupForFile(dir);
54 private class PackageGroup implements UsageGroup, TypeSafeDataProvider {
55 private final PsiPackage myPackage;
56 private Icon myOpenIcon;
57 private Icon myClosedIcon;
59 private PackageGroup(PsiPackage aPackage) {
60 myPackage = aPackage;
61 update();
64 public void update() {
65 if (isValid()) {
66 myOpenIcon = myPackage.getIcon(Iconable.ICON_FLAG_OPEN);
67 myClosedIcon = myPackage.getIcon(Iconable.ICON_FLAG_CLOSED);
71 public Icon getIcon(boolean isOpen) {
72 return isOpen? myOpenIcon: myClosedIcon;
75 @NotNull
76 public String getText(UsageView view) {
77 return myPackage.getQualifiedName();
80 public FileStatus getFileStatus() {
81 if (!isValid()) return null;
82 PsiDirectory[] dirs = myPackage.getDirectories();
83 return dirs.length == 1 ? FileStatusManager.getInstance(myProject).getStatus(dirs[0].getVirtualFile()) : null;
86 public boolean isValid() {
87 return myPackage.isValid();
90 public void navigate(boolean focus) throws UnsupportedOperationException {
91 myPackage.navigate(focus);
94 public boolean canNavigate() {
95 return myPackage.canNavigate();
98 public boolean canNavigateToSource() {
99 return false;
102 public int compareTo(UsageGroup usageGroup) {
103 return getText(null).compareTo(usageGroup.getText(null));
106 public boolean equals(Object o) {
107 if (this == o) return true;
108 if (!(o instanceof PackageGroup)) return false;
110 return myPackage.equals(((PackageGroup)o).myPackage);
113 public int hashCode() {
114 return myPackage.hashCode();
117 public void calcData(final DataKey key, final DataSink sink) {
118 if (!isValid()) return;
119 if (LangDataKeys.PSI_ELEMENT == key) {
120 sink.put(LangDataKeys.PSI_ELEMENT, myPackage);