View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
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  package com.atlassian.theplugin.idea.jira.editor.vfs;
18  
19  import com.intellij.openapi.util.io.FileUtil;
20  import com.intellij.openapi.util.IconLoader;
21  import com.intellij.openapi.vfs.DeprecatedVirtualFile;
22  import com.intellij.openapi.vfs.VirtualFile;
23  import com.intellij.openapi.vfs.VirtualFileSystem;
24  import org.jetbrains.annotations.NonNls;
25  import org.jetbrains.annotations.NotNull;
26  import org.jetbrains.annotations.Nullable;
27  
28  import javax.swing.*;
29  import java.io.ByteArrayInputStream;
30  import java.io.ByteArrayOutputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.OutputStream;
34  import java.util.HashMap;
35  import java.util.Map;
36  
37  /**
38   * A memory-based file.
39   *
40   * @author Steve Chaloner
41   */
42  public class MemoryVirtualFile extends DeprecatedVirtualFile {
43      /**
44       * The name of the file.
45       */
46      private final String name;
47  
48      private final String nameWithoutExtension;
49  
50      /**
51       * The content of the file.
52       */
53      private String content;
54  
55      /**
56       * A flag to indicate if this file represents a directory.
57       */
58      private final boolean isDirectory;
59  
60      /**
61       * The children of this file, if the file is a directory.
62       */
63      private final Map<String, MemoryVirtualFile> children = new HashMap<String, MemoryVirtualFile>();
64  
65      /**
66       * The parent of this file.  If this file is at the root of the file
67       * system, it will not have a parent.
68       */
69      @Nullable
70      private VirtualFile parent;
71  
72      /**
73       * Immutability flag
74       */
75      private boolean writable = true;
76  
77      /**
78       * Initialises a new instance of this class.
79       *
80       * @param name    the name of the file
81       * @param content the content of the file
82       */
83      public MemoryVirtualFile(@NotNull String name, String content) {
84          this(name, content, false);
85      }
86  
87      /**
88       * Initialises a new instance of this class.
89       *
90       * @param name the name of the file
91       */
92      public MemoryVirtualFile(@NotNull String name) {
93          this(name, null, true);
94      }
95  
96      /**
97       * Initialises a new instance of this class.
98       *
99       * @param name        the name of the file
100      * @param content     the content of the file.  This is mutually exclusive with
101      *                    <code>isDirectory</code>.
102      * @param isDirectory true iff this file is a directory.  This is mutually exclusive
103      *                    with <code>content<code>.
104      */
105     private MemoryVirtualFile(@NotNull String name, String content, boolean isDirectory) {
106         this.name = name;
107         nameWithoutExtension = FileUtil.getNameWithoutExtension(name);
108         this.content = content;
109         this.isDirectory = isDirectory;
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @NotNull
116     @NonNls
117     public String getName() {
118         return name;
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     @NotNull
125     public VirtualFileSystem getFileSystem() {
126         return MemoryVirtualFileSystem.getInstance();
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     public String getPath() {
133         VirtualFile myParent = getParent();
134         return myParent == null ? name : myParent.getPath() + '/' + name;
135     }
136 
137     /**
138      * Sets the writable status of the file.
139      *
140      * @param writable true if the file is writable
141      */
142     public void setWritable(boolean writable) {
143         this.writable = writable;
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
149     public boolean isWritable() {
150         return writable;
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     public boolean isDirectory() {
157         return isDirectory;
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     public boolean isValid() {
164         return true;
165     }
166 
167     public Icon getIcon() {
168         return IconLoader.getIcon("/icons/tab_jira.png");
169     }
170 
171     /**
172      * Sets the parent of this file.
173      *
174      * @param parent the parent
175      */
176     public void setParent(@Nullable VirtualFile parent) {
177         this.parent = parent;
178     }
179 
180     /**
181      * {@inheritDoc}
182      */
183     @Nullable
184     public VirtualFile getParent() {
185         return parent;
186     }
187 
188     /**
189      * Add the given file to the child list of this directory.
190      *
191      * @param file the file to add to the list of children
192      * @throws IllegalStateException if this file is not a directory
193      */
194     public void addChild(MemoryVirtualFile file) throws IllegalStateException {
195         if (isDirectory) {
196             file.setParent(this);
197             children.put(file.getName(),
198                     file);
199         } else {
200             throw new IllegalStateException("files can only be added to a directory");
201         }
202     }
203 
204     /**
205      * {@inheritDoc}
206      */
207     public VirtualFile[] getChildren() {
208         return children.values().toArray(new VirtualFile[children.size()]);
209     }
210 
211     /**
212      * {@inheritDoc}
213      */
214     public OutputStream getOutputStream(Object object, long l, long l1) throws IOException {
215         return new ByteArrayOutputStream();
216     }
217 
218 
219     /**
220      * {@inheritDoc}
221      */
222     public byte[] contentsToByteArray() throws IOException {
223         return content.getBytes();
224     }
225 
226     /**
227      * {@inheritDoc}
228      */
229     public long getTimeStamp() {
230         return 0L;
231     }
232 
233     /**
234      * {@inheritDoc}
235      */
236     public long getLength() {
237         return content.getBytes().length;
238     }
239 
240     /**
241      * {@inheritDoc}
242      */
243     public void refresh(boolean b,
244             boolean b1,
245             Runnable runnable) {
246     }
247 
248     /**
249      * {@inheritDoc}
250      */
251     public InputStream getInputStream() throws IOException {
252         return new ByteArrayInputStream(content.getBytes());
253     }
254 
255     /**
256      * Sets the content of the file.
257      *
258      * @param content the content
259      */
260     public void setContent(@NotNull String content) {
261         this.content = content;
262     }
263 
264     /**
265      * Gets the content of the file.
266      *
267      * @return the content of the file
268      */
269     @NotNull
270     public String getContent() {
271         return content;
272     }
273 
274     /**
275      * Gets the file from this directory's children.
276      *
277      * @param filename the name of the child to retrieve
278      * @return the file, or null if it cannot be found
279      */
280     @Nullable
281     public MemoryVirtualFile getChild(String filename) {
282         return children.get(filename);
283     }
284 
285     /**
286      * {@inheritDoc}
287      */
288     public long getModificationStamp() {
289         return 0L;
290     }
291 
292     /**
293      * {@inheritDoc}
294      */
295     @NotNull
296     public String getUrl() {
297         return Constants.JIRAISSUE_SCHEMA + getPath();
298     }
299 
300     /**
301      * Deletes the specified file.
302      *
303      * @param file the file to delete
304      */
305     public void deleteChild(MemoryVirtualFile file) {
306         children.remove(file.getName());
307     }
308 
309     @NonNls
310     public String toString() {
311         return nameWithoutExtension;
312     }
313 }