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
24
25
26
27 public class MemoryVirtualFile extends DeprecatedVirtualFile
28 {
29
30
31
32 private final String name;
33
34 private final String nameWithoutExtension;
35
36
37
38
39 private String content;
40
41
42
43
44 private final boolean isDirectory;
45
46
47
48
49 private final Map<String, MemoryVirtualFile> children = new HashMap<String, MemoryVirtualFile>();
50
51
52
53
54
55 @Nullable
56 private VirtualFile parent;
57
58
59
60
61 private boolean writable = true;
62
63
64
65
66
67
68
69 public MemoryVirtualFile(@NotNull String name,
70 String content)
71 {
72 this(name,
73 content,
74 false);
75 }
76
77
78
79
80
81
82 public MemoryVirtualFile(@NotNull String name)
83 {
84 this(name,
85 null,
86 true);
87 }
88
89
90
91
92
93
94
95
96
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
109 @NotNull
110 @NonNls
111 public String getName()
112 {
113 return name;
114 }
115
116
117 @NotNull
118 public VirtualFileSystem getFileSystem()
119 {
120 return MemoryVirtualFileSystem.getInstance();
121 }
122
123
124 public String getPath()
125 {
126 VirtualFile parent = getParent();
127 return parent == null ? name : parent.getPath() + '/' + name;
128 }
129
130
131
132
133
134
135 public void setWritable(boolean writable)
136 {
137 this.writable = writable;
138 }
139
140
141 public boolean isWritable()
142 {
143 return writable;
144 }
145
146
147 public boolean isDirectory()
148 {
149 return isDirectory;
150 }
151
152
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
164
165
166
167 public void setParent(@Nullable VirtualFile parent)
168 {
169 this.parent = parent;
170 }
171
172
173 @Nullable
174 public VirtualFile getParent()
175 {
176 return parent;
177 }
178
179
180
181
182
183
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
200 public VirtualFile[] getChildren()
201 {
202 return children.values().toArray(new VirtualFile[children.size()]);
203 }
204
205
206 public OutputStream getOutputStream(Object object,
207 long l,
208 long l1) throws IOException
209 {
210 return new ByteArrayOutputStream();
211 }
212
213
214
215 public byte[] contentsToByteArray() throws IOException
216 {
217 return content.getBytes();
218 }
219
220
221 public long getTimeStamp()
222 {
223 return 0L;
224 }
225
226
227 public long getLength()
228 {
229 return content.getBytes().length;
230 }
231
232
233 public void refresh(boolean b,
234 boolean b1,
235 Runnable runnable)
236 {
237 }
238
239
240 public InputStream getInputStream() throws IOException
241 {
242 return new ByteArrayInputStream(content.getBytes());
243 }
244
245
246
247
248
249
250 public void setContent(@NotNull String content)
251 {
252 this.content = content;
253 }
254
255
256
257
258
259
260 @NotNull
261 public String getContent()
262 {
263 return content;
264 }
265
266
267
268
269
270
271
272 @Nullable
273 public MemoryVirtualFile getChild(String name)
274 {
275 return children.get(name);
276 }
277
278
279 public long getModificationStamp()
280 {
281 return 0L;
282 }
283
284
285 @NotNull
286 public String getUrl() {
287 return Constants.JIRAISSUE_SCHEMA + getPath();
288 }
289
290
291
292
293
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 }