View Javadoc

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