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.util.memoryvfs;
18  
19  import com.intellij.openapi.fileTypes.FileType;
20  import com.intellij.openapi.fileTypes.StdFileTypes;
21  import com.intellij.openapi.util.IconLoader;
22  import com.intellij.openapi.util.io.FileUtil;
23  import com.intellij.openapi.vfs.DeprecatedVirtualFile;
24  import com.intellij.openapi.vfs.VirtualFile;
25  import com.intellij.openapi.vfs.VirtualFileSystem;
26  import org.jetbrains.annotations.NonNls;
27  import org.jetbrains.annotations.NotNull;
28  import org.jetbrains.annotations.Nullable;
29  
30  import javax.swing.*;
31  import java.io.ByteArrayInputStream;
32  import java.io.ByteArrayOutputStream;
33  import java.io.IOException;
34  import java.io.InputStream;
35  import java.io.OutputStream;
36  
37  /**
38   * A memory-based file.
39   *
40   * @author Steve Chaloner
41   */
42  public class PlainTextMemoryVirtualFile extends DeprecatedVirtualFile {
43      /**
44       * The name of the file.
45       */
46      private final String name;
47      private final String nameWithoutExtension;
48  
49      /**
50       * The content of the file.
51       */
52      private String content;
53  
54      /**
55       * Immutability flag
56       */
57      private boolean writable = false;
58  
59      public PlainTextMemoryVirtualFile(@NotNull String name) {
60          this.name = name;
61          nameWithoutExtension = FileUtil.getNameWithoutExtension(name);
62      }
63  
64      /**
65       * Initialises a new instance of this class.
66       *
67       * @param name    the name of the file
68       * @param content the content of the file.  This is mutually exclusive with
69       *                <code>isDirectory</code>.
70       */
71      public PlainTextMemoryVirtualFile(@NotNull String name, String content) {
72          this.name = name;
73          nameWithoutExtension = FileUtil.getNameWithoutExtension(name);
74          this.content = content;
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @NotNull
81      @NonNls
82      public String getName() {
83          return name;
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @NotNull
90      public VirtualFileSystem getFileSystem() {
91          return PlainTextMemoryVirtualFileSystem.getInstance();
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      public String getPath() {
98          VirtualFile myParent = getParent();
99          return myParent == null ? name : myParent.getPath() + '/' + name;
100     }
101 
102     @NotNull
103     public FileType getFileType() {
104         return StdFileTypes.PLAIN_TEXT;
105     }
106 
107     /**
108      * Sets the writable status of the file.
109      *
110      * @param writable true if the file is writable
111      */
112     public void setWritable(boolean writable) {
113         this.writable = writable;
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     public boolean isWritable() {
120         return writable;
121     }
122 
123     public boolean isDirectory() {
124         return false;
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     public boolean isValid() {
131         return true;
132     }
133 
134     @Nullable
135     public VirtualFile getParent() {
136         return null;
137     }
138 
139     public VirtualFile[] getChildren() {
140         throw new UnsupportedOperationException("method getChidren");
141     }
142 
143     public Icon getIcon() {
144         return IconLoader.getIcon("/icons/tab_bamboo.png");
145     }
146 
147 
148     /**
149      * {@inheritDoc}
150      */
151     public OutputStream getOutputStream(Object object, long l, long l1) throws IOException {
152         return new ByteArrayOutputStream();
153     }
154 
155 
156     /**
157      * {@inheritDoc}
158      */
159     public byte[] contentsToByteArray() throws IOException {
160         return content.getBytes();
161     }
162 
163     /**
164      * {@inheritDoc}
165      */
166     public long getTimeStamp() {
167         return 0L;
168     }
169 
170     /**
171      * {@inheritDoc}
172      */
173     public long getLength() {
174         return content.getBytes().length;
175     }
176 
177     /**
178      * {@inheritDoc}
179      */
180     public void refresh(boolean b,
181                         boolean b1,
182                         Runnable runnable) {
183     }
184 
185     /**
186      * {@inheritDoc}
187      */
188     public InputStream getInputStream() throws IOException {
189         return new ByteArrayInputStream(content.getBytes());
190     }
191 
192     /**
193      * Sets the content of the file.
194      *
195      * @param content the content
196      */
197     public void setContent(@NotNull String content) {
198         this.content = content;
199     }
200 
201     /**
202      * Gets the content of the file.
203      *
204      * @return the content of the file
205      */
206     @NotNull
207     public String getContent() {
208         return content;
209     }
210 
211     /**
212      * {@inheritDoc}
213      */
214     public long getModificationStamp() {
215         return 0L;
216     }
217 
218     /**
219      * {@inheritDoc}
220      */
221     @NotNull
222     public String getUrl() {
223         return Constants.PLAINTEXT_SCHEMA + getPath();
224     }
225 
226     @NonNls
227     public String toString() {
228         return nameWithoutExtension;
229     }
230 }