1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
39
40
41
42 public class MemoryVirtualFile extends DeprecatedVirtualFile {
43
44
45
46 private final String name;
47
48 private final String nameWithoutExtension;
49
50
51
52
53 private String content;
54
55
56
57
58 private final boolean isDirectory;
59
60
61
62
63 private final Map<String, MemoryVirtualFile> children = new HashMap<String, MemoryVirtualFile>();
64
65
66
67
68
69 @Nullable
70 private VirtualFile parent;
71
72
73
74
75 private boolean writable = true;
76
77
78
79
80
81
82
83 public MemoryVirtualFile(@NotNull String name, String content) {
84 this(name, content, false);
85 }
86
87
88
89
90
91
92 public MemoryVirtualFile(@NotNull String name) {
93 this(name, null, true);
94 }
95
96
97
98
99
100
101
102
103
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
114
115 @NotNull
116 @NonNls
117 public String getName() {
118 return name;
119 }
120
121
122
123
124 @NotNull
125 public VirtualFileSystem getFileSystem() {
126 return MemoryVirtualFileSystem.getInstance();
127 }
128
129
130
131
132 public String getPath() {
133 VirtualFile myParent = getParent();
134 return myParent == null ? name : myParent.getPath() + '/' + name;
135 }
136
137
138
139
140
141
142 public void setWritable(boolean writable) {
143 this.writable = writable;
144 }
145
146
147
148
149 public boolean isWritable() {
150 return writable;
151 }
152
153
154
155
156 public boolean isDirectory() {
157 return isDirectory;
158 }
159
160
161
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
173
174
175
176 public void setParent(@Nullable VirtualFile parent) {
177 this.parent = parent;
178 }
179
180
181
182
183 @Nullable
184 public VirtualFile getParent() {
185 return parent;
186 }
187
188
189
190
191
192
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
206
207 public VirtualFile[] getChildren() {
208 return children.values().toArray(new VirtualFile[children.size()]);
209 }
210
211
212
213
214 public OutputStream getOutputStream(Object object, long l, long l1) throws IOException {
215 return new ByteArrayOutputStream();
216 }
217
218
219
220
221
222 public byte[] contentsToByteArray() throws IOException {
223 return content.getBytes();
224 }
225
226
227
228
229 public long getTimeStamp() {
230 return 0L;
231 }
232
233
234
235
236 public long getLength() {
237 return content.getBytes().length;
238 }
239
240
241
242
243 public void refresh(boolean b,
244 boolean b1,
245 Runnable runnable) {
246 }
247
248
249
250
251 public InputStream getInputStream() throws IOException {
252 return new ByteArrayInputStream(content.getBytes());
253 }
254
255
256
257
258
259
260 public void setContent(@NotNull String content) {
261 this.content = content;
262 }
263
264
265
266
267
268
269 @NotNull
270 public String getContent() {
271 return content;
272 }
273
274
275
276
277
278
279
280 @Nullable
281 public MemoryVirtualFile getChild(String filename) {
282 return children.get(filename);
283 }
284
285
286
287
288 public long getModificationStamp() {
289 return 0L;
290 }
291
292
293
294
295 @NotNull
296 public String getUrl() {
297 return Constants.JIRAISSUE_SCHEMA + getPath();
298 }
299
300
301
302
303
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 }