VCS: fix group by structure for Changes | Local
[fedora-idea.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / changes / VirtualFileHolder.java
blobc8293e05030ee1131c018b5a157a9e6fe0963537
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.vcs.changes;
18 import com.intellij.openapi.application.ApplicationManager;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.vcs.FilePath;
21 import com.intellij.openapi.vcs.FilePathImpl;
22 import com.intellij.openapi.vcs.ProjectLevelVcsManager;
23 import com.intellij.openapi.vfs.VirtualFile;
25 import java.util.*;
27 /**
28 * @author max
30 public class VirtualFileHolder implements FileHolder {
31 private final Set<VirtualFile> myFiles = new HashSet<VirtualFile>();
32 private final Project myProject;
33 private final HolderType myType;
35 public VirtualFileHolder(Project project, final HolderType type) {
36 myProject = project;
37 myType = type;
40 public HolderType getType() {
41 return myType;
44 public void cleanAll() {
45 myFiles.clear();
48 static void cleanScope(final Project project, final Collection<VirtualFile> files, final VcsDirtyScope scope) {
49 ApplicationManager.getApplication().runReadAction(new Runnable() {
50 public void run() {
51 // to avoid deadlocks caused by incorrect lock ordering, need to lock on this after taking read action
52 if (project.isDisposed() || files.isEmpty()) return;
53 final List<VirtualFile> currentFiles = new ArrayList<VirtualFile>(files);
54 if (scope.getRecursivelyDirtyDirectories().size() == 0) {
55 final Set<FilePath> dirtyFiles = scope.getDirtyFiles();
56 boolean cleanedDroppedFiles = false;
57 for(FilePath dirtyFile: dirtyFiles) {
58 VirtualFile f = dirtyFile.getVirtualFile();
59 if (f != null) {
60 files.remove(f);
62 else {
63 if (!cleanedDroppedFiles) {
64 cleanedDroppedFiles = true;
65 for(VirtualFile file: currentFiles) {
66 if (fileDropped(project, file)) files.remove(file);
72 else {
73 for (VirtualFile file : currentFiles) {
74 if (fileDropped(project, file) || scope.belongsTo(new FilePathImpl(file))) {
75 files.remove(file);
80 });
83 public void cleanScope(final VcsDirtyScope scope) {
84 cleanScope(myProject, myFiles, scope);
87 private static boolean fileDropped(final Project project, final VirtualFile file) {
88 return !file.isValid() || ProjectLevelVcsManager.getInstance(project).getVcsFor(file) == null;
91 public void addFile(VirtualFile file) {
92 myFiles.add(file);
95 public void removeFile(VirtualFile file) {
96 myFiles.remove(file);
99 public List<VirtualFile> getFiles() {
100 return new ArrayList<VirtualFile>(myFiles);
103 public VirtualFileHolder copy() {
104 final VirtualFileHolder copyHolder = new VirtualFileHolder(myProject, myType);
105 copyHolder.myFiles.addAll(myFiles);
106 return copyHolder;
109 public boolean containsFile(final VirtualFile file) {
110 return myFiles.contains(file);
113 public boolean equals(final Object o) {
114 if (this == o) return true;
115 if (o == null || getClass() != o.getClass()) return false;
117 final VirtualFileHolder that = (VirtualFileHolder)o;
119 if (!myFiles.equals(that.myFiles)) return false;
121 return true;
124 public int hashCode() {
125 return myFiles.hashCode();