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