Merge remote-tracking branch 'origin/master'
[idea/community.git] / platform / core-api / src / com / intellij / openapi / vfs / DeprecatedVirtualFileSystem.java
1 /*
2  * Copyright 2000-2016 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /*
18  * @author max
19  */
20 package com.intellij.openapi.vfs;
21
22 import com.intellij.openapi.application.Application;
23 import com.intellij.openapi.application.ApplicationManager;
24 import com.intellij.openapi.vfs.impl.BulkVirtualFileListenerAdapter;
25 import com.intellij.util.EventDispatcher;
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
28
29 import java.io.IOException;
30
31 public abstract class DeprecatedVirtualFileSystem extends VirtualFileSystem {
32   private final EventDispatcher<VirtualFileListener> myEventDispatcher = EventDispatcher.create(VirtualFileListener.class);
33
34   protected void startEventPropagation() {
35     Application app = ApplicationManager.getApplication();
36     if (app != null) {
37       app.getMessageBus().connect().subscribe(
38         VirtualFileManager.VFS_CHANGES, new BulkVirtualFileListenerAdapter(myEventDispatcher.getMulticaster(), this));
39     }
40   }
41
42   @Override
43   public void addVirtualFileListener(@NotNull VirtualFileListener listener) {
44     myEventDispatcher.addListener(listener);
45   }
46
47   @Override
48   public void removeVirtualFileListener(@NotNull VirtualFileListener listener) {
49     myEventDispatcher.removeListener(listener);
50   }
51
52   protected void firePropertyChanged(Object requestor,
53                                      @NotNull VirtualFile file,
54                                      @NotNull String propertyName,
55                                      Object oldValue,
56                                      Object newValue) {
57     assertWriteAccessAllowed();
58     VirtualFilePropertyEvent event = new VirtualFilePropertyEvent(requestor, file, propertyName, oldValue, newValue);
59     myEventDispatcher.getMulticaster().propertyChanged(event);
60   }
61
62   protected void fireContentsChanged(Object requestor, @NotNull VirtualFile file, long oldModificationStamp) {
63     assertWriteAccessAllowed();
64     VirtualFileEvent event = new VirtualFileEvent(requestor, file, file.getParent(), oldModificationStamp, file.getModificationStamp());
65     myEventDispatcher.getMulticaster().contentsChanged(event);
66   }
67
68   protected void fireFileCreated(@Nullable Object requestor, @NotNull VirtualFile file) {
69     assertWriteAccessAllowed();
70     VirtualFileEvent event = new VirtualFileEvent(requestor, file, file.getName(), file.getParent());
71     myEventDispatcher.getMulticaster().fileCreated(event);
72   }
73
74   protected void fireFileDeleted(Object requestor, @NotNull VirtualFile file, @NotNull String fileName, VirtualFile parent) {
75     assertWriteAccessAllowed();
76     VirtualFileEvent event = new VirtualFileEvent(requestor, file, fileName, parent);
77     myEventDispatcher.getMulticaster().fileDeleted(event);
78   }
79
80   protected void fireFileMoved(Object requestor, @NotNull VirtualFile file, VirtualFile oldParent) {
81     assertWriteAccessAllowed();
82     VirtualFileMoveEvent event = new VirtualFileMoveEvent(requestor, file, oldParent, file.getParent());
83     myEventDispatcher.getMulticaster().fileMoved(event);
84   }
85
86   protected void fireFileCopied(@Nullable Object requestor, @NotNull VirtualFile originalFile, @NotNull VirtualFile createdFile) {
87     assertWriteAccessAllowed();
88     VirtualFileCopyEvent event = new VirtualFileCopyEvent(requestor, originalFile, createdFile);
89     myEventDispatcher.getMulticaster().fileCopied(event);
90   }
91
92   protected void fireBeforePropertyChange(Object requestor,
93                                           @NotNull VirtualFile file,
94                                           @NotNull String propertyName,
95                                           Object oldValue,
96                                           Object newValue) {
97     assertWriteAccessAllowed();
98     VirtualFilePropertyEvent event = new VirtualFilePropertyEvent(requestor, file, propertyName, oldValue, newValue);
99     myEventDispatcher.getMulticaster().beforePropertyChange(event);
100   }
101
102   protected void fireBeforeContentsChange(Object requestor, @NotNull VirtualFile file) {
103     assertWriteAccessAllowed();
104     VirtualFileEvent event = new VirtualFileEvent(requestor, file, file.getName(), file.getParent());
105     myEventDispatcher.getMulticaster().beforeContentsChange(event);
106   }
107
108   protected void fireBeforeFileDeletion(Object requestor, @NotNull VirtualFile file) {
109     assertWriteAccessAllowed();
110     VirtualFileEvent event = new VirtualFileEvent(requestor, file, file.getName(), file.getParent());
111     myEventDispatcher.getMulticaster().beforeFileDeletion(event);
112   }
113
114   protected void fireBeforeFileMovement(Object requestor, @NotNull VirtualFile file, VirtualFile newParent) {
115     assertWriteAccessAllowed();
116     VirtualFileMoveEvent event = new VirtualFileMoveEvent(requestor, file, file.getParent(), newParent);
117     myEventDispatcher.getMulticaster().beforeFileMovement(event);
118   }
119
120   protected void assertWriteAccessAllowed() {
121     ApplicationManager.getApplication().assertWriteAccessAllowed();
122   }
123
124   @Override
125   public boolean isReadOnly() {
126     return true;
127   }
128
129   @Override
130   protected void deleteFile(Object requestor, @NotNull VirtualFile vFile) throws IOException {
131     throw unsupported("deleteFile", vFile);
132   }
133
134   @Override
135   protected void moveFile(Object requestor, @NotNull VirtualFile vFile, @NotNull VirtualFile newParent) throws IOException {
136     throw unsupported("move", vFile);
137   }
138
139   @Override
140   protected void renameFile(Object requestor, @NotNull VirtualFile vFile, @NotNull String newName) throws IOException {
141     throw unsupported("renameFile", vFile);
142   }
143
144   @NotNull
145   @Override
146   public VirtualFile createChildFile(Object requestor, @NotNull VirtualFile vDir, @NotNull String fileName) throws IOException {
147     throw unsupported("createChildFile", vDir);
148   }
149
150   @NotNull
151   @Override
152   public VirtualFile createChildDirectory(Object requestor, @NotNull VirtualFile vDir, @NotNull String dirName) throws IOException {
153     throw unsupported("createChildDirectory", vDir);
154   }
155
156   @NotNull
157   @Override
158   public VirtualFile copyFile(Object requestor, @NotNull VirtualFile vFile, @NotNull VirtualFile newParent, @NotNull String copyName) throws IOException {
159     throw unsupported("copyFile", vFile);
160   }
161
162   private UnsupportedOperationException unsupported(String op, VirtualFile vFile) {
163     return new UnsupportedOperationException(op + '(' + vFile + ") not supported by " + getClass().getName());
164   }
165 }