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 org
.jetbrains
.idea
.svn
;
18 import com
.intellij
.openapi
.application
.ApplicationManager
;
19 import com
.intellij
.openapi
.application
.ModalityState
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.openapi
.vcs
.changes
.VcsDirtyScopeManager
;
22 import com
.intellij
.openapi
.vfs
.VirtualFile
;
23 import com
.intellij
.openapi
.vfs
.VirtualFileAdapter
;
24 import com
.intellij
.openapi
.vfs
.VirtualFileEvent
;
26 import java
.util
.ArrayList
;
27 import java
.util
.Collection
;
28 import java
.util
.Collections
;
29 import java
.util
.List
;
31 public class SvnEntriesFileListener
extends VirtualFileAdapter
{
32 private final Project myProject
;
33 private final Collection
<SvnEntriesListener
> myListeners
= new ArrayList
<SvnEntriesListener
>();
35 public SvnEntriesFileListener(final Project project
) {
39 public void fileCreated(VirtualFileEvent event
) {
40 if (!event
.isFromRefresh()) {
43 final VirtualFile file
= event
.getFile();
44 if (file
!= null && SvnUtil
.SVN_ADMIN_DIR_NAME
.equals(file
.getName())) {
45 if (event
.getParent() != null) {
46 VirtualFile parent
= event
.getParent();
47 fireFileStatusesChanged(parent
);
48 fileEntriesChanged(parent
);
53 public void contentsChanged(VirtualFileEvent event
) {
54 if (!event
.isFromRefresh()) {
57 final VirtualFile file
= event
.getFile();
58 if (file
!= null && isEntriesFile(file
) && file
.getParent() != null) {
59 VirtualFile parent
= file
.getParent();
61 VirtualFile grandParent
= parent
.getParent();
62 if (grandParent
!= null) {
63 fireFileStatusesChanged(grandParent
);
64 fileEntriesChanged(grandParent
);
70 public void fileDeleted(VirtualFileEvent event
) {
71 if (!event
.isFromRefresh()) {
74 final VirtualFile file
= event
.getFile();
75 if (file
!= null && SvnUtil
.SVN_ADMIN_DIR_NAME
.equals(file
.getName())) {
76 if (event
.getParent() != null) {
77 VirtualFile parent
= event
.getParent();
78 fireFileStatusesChanged(parent
);
79 fileEntriesChanged(parent
);
84 private void fileEntriesChanged(final VirtualFile parent
) {
85 final SvnEntriesListener
[] listeners
= myListeners
.toArray(new SvnEntriesListener
[myListeners
.size()]);
86 ApplicationManager
.getApplication().invokeLater(new Runnable() {
88 for (int i
= 0; i
< listeners
.length
; i
++) {
89 SvnEntriesListener listener
= listeners
[i
];
90 listener
.onEntriesChanged(parent
);
93 }, ModalityState
.NON_MODAL
);
96 public void addListener(SvnEntriesListener listener
) {
97 myListeners
.add(listener
);
100 public void removeListener(SvnEntriesListener listener
) {
101 myListeners
.remove(listener
);
104 private void fireFileStatusesChanged(VirtualFile parent
) {
105 final VirtualFile
[] children
= parent
.getChildren();
106 final List
<VirtualFile
> files
= new ArrayList
<VirtualFile
>(children
.length
+ 1);
108 Collections
.addAll(files
, children
);
109 VcsDirtyScopeManager
.getInstance(myProject
).filesDirty(files
, null);
111 final FileStatusManager fileStatusManager = FileStatusManager.getInstance(myProject);
112 final VirtualFile[] children = parent.getChildren();
113 ApplicationManager.getApplication().invokeLater(new Runnable() {
115 for (int i = 0; i < children.length; i++) {
116 VirtualFile child = children[i];
117 fileStatusManager.fileStatusChanged(child);
120 }, ModalityState.NON_MMODAL);
124 private static boolean isEntriesFile(final VirtualFile file
) {
125 VirtualFile parent
= file
.getParent();
126 return !file
.isDirectory() && SvnUtil
.ENTRIES_FILE_NAME
.equals(file
.getName()) && parent
!= null && SvnUtil
.SVN_ADMIN_DIR_NAME
.equals(parent
.getName());