KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > project > ProjectFileItem


1 package org.antlr.works.project;
2
3 import org.antlr.xjlib.foundation.XJUtils;
4 import org.antlr.works.components.ComponentContainer;
5 import org.antlr.works.components.project.CContainerProject;
6
7 import javax.swing.*;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10 /*
11
12 [The "BSD licence"]
13 Copyright (c) 2005-2006 Jean Bovet
14 All rights reserved.
15
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions
18 are met:
19
20 1. Redistributions of source code must retain the above copyright
21 notice, this list of conditions and the following disclaimer.
22 2. Redistributions in binary form must reproduce the above copyright
23 notice, this list of conditions and the following disclaimer in the
24 documentation and/or other materials provided with the distribution.
25 3. The name of the author may not be used to endorse or promote products
26 derived from this software without specific prior written permission.
27
28 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39 */

40
41 public class ProjectFileItem implements Comparable JavaDoc {
42
43     public static final String JavaDoc FILE_GRAMMAR_EXTENSION = ".g";
44     public static final String JavaDoc FILE_JAVA_EXTENSION = ".java";
45     public static final String JavaDoc FILE_STG_EXTENSION = ".stg";
46     public static final String JavaDoc FILE_ST_EXTENSION = ".st";
47     public static final String JavaDoc FILE_MANTRA_EXTENSION = ".om";
48     public static final String JavaDoc FILE_TEXT_EXTENSION = ".txt";
49
50     public static final String JavaDoc FILE_TYPE_GRAMMAR = "FILE_TYPE_GRAMMAR";
51     public static final String JavaDoc FILE_TYPE_JAVA = "FILE_TYPE_JAVA";
52     public static final String JavaDoc FILE_TYPE_STG = "FILE_TYPE_STG";
53     public static final String JavaDoc FILE_TYPE_ST = "FILE_TYPE_ST";
54     public static final String JavaDoc FILE_TYPE_MANTRA = "FILE_TYPE_MANTRA";
55     public static final String JavaDoc FILE_TYPE_TEXT = "FILE_TYPE_TEXT";
56     public static final String JavaDoc FILE_TYPE_UNKNOWN = "FILE_TYPE_UNKNOWN";
57
58     protected CContainerProject project;
59     protected ComponentContainer container;
60
61     protected String JavaDoc fileName;
62     protected String JavaDoc fileType;
63     protected boolean opened;
64     protected int tabIndex;
65     protected Map JavaDoc containerData;
66
67     public ProjectFileItem(CContainerProject project, String JavaDoc name) {
68         setFileName(name);
69         this.project = project;
70     }
71
72     public static String JavaDoc getFileType(String JavaDoc filePath) {
73         if(filePath.endsWith(FILE_GRAMMAR_EXTENSION))
74             return FILE_TYPE_GRAMMAR;
75         if(filePath.endsWith(FILE_STG_EXTENSION))
76             return FILE_TYPE_STG;
77         if(filePath.endsWith(FILE_ST_EXTENSION))
78             return FILE_TYPE_ST;
79         if(filePath.endsWith(FILE_JAVA_EXTENSION))
80             return FILE_TYPE_JAVA;
81         if(filePath.endsWith(FILE_MANTRA_EXTENSION))
82             return FILE_TYPE_MANTRA;
83         if(filePath.endsWith(FILE_TEXT_EXTENSION))
84             return FILE_TYPE_TEXT;
85
86         return FILE_TYPE_UNKNOWN;
87     }
88
89     public static String JavaDoc getFileTypeName(String JavaDoc type) {
90         if(type.equals(FILE_TYPE_GRAMMAR))
91             return "Grammar";
92         if(type.equals(FILE_TYPE_STG))
93             return "ST Group";
94         if(type.equals(FILE_TYPE_ST))
95             return "ST";
96         if(type.equals(FILE_TYPE_JAVA))
97             return "Java";
98         if(type.equals(FILE_TYPE_MANTRA))
99             return "Mantra";
100         if(type.equals(FILE_TYPE_TEXT))
101             return "Text";
102
103         return "Unkown";
104     }
105
106     public void setOpened(boolean flag) {
107         opened = flag;
108     }
109
110     public boolean isOpened() {
111         return opened;
112     }
113
114     public void setTabIndex(int index) {
115         this.tabIndex = index;
116     }
117
118     public int getTabIndex() {
119         return tabIndex;
120     }
121
122     public boolean isDirty() {
123         if(container != null)
124             return container.getDocument().isDirty();
125         else
126             return false;
127     }
128
129     public boolean save() {
130         if(container != null && container.getDocument().isDirty())
131             return container.getDocument().performSave(false);
132         else
133             return false;
134     }
135
136     public void close() {
137         if(container != null)
138             container.close();
139     }
140
141     public void setComponentContainer(ComponentContainer container) {
142         this.container = container;
143         this.container.setPersistentData(containerData);
144     }
145
146     public ComponentContainer getComponentContainer() {
147         return container;
148     }
149
150     public void setContainerPersistentData(Map JavaDoc data) {
151         this.containerData = data;
152     }
153
154     public Map JavaDoc getContainerPersistentData() {
155         if(container == null)
156             return null;
157         else
158             return container.getPersistentData();
159     }
160
161     public JPanel getEditorPanel() {
162         if(container == null)
163             return null;
164         else
165             return container.getEditor().getPanel();
166     }
167
168     public void setFileName(String JavaDoc fileName) {
169         this.fileName = fileName;
170         this.fileType = getFileType(fileName);
171     }
172
173     public String JavaDoc getFilePath() {
174         return XJUtils.concatPath(project.getSourcePath(), fileName);
175     }
176
177     public String JavaDoc getFileName() {
178         return fileName;
179     }
180
181     public String JavaDoc getFileType() {
182         return fileType;
183     }
184
185     public void windowActivated() {
186         if(container != null)
187             container.getEditor().componentActivated();
188     }
189
190     public boolean handleExternalModification() {
191         if(container == null)
192             return false;
193
194         if(container.getDocument().isModifiedOnDisk()) {
195             container.getEditor().componentDocumentContentChanged();
196             container.getDocument().synchronizeLastModifiedDate();
197             return true;
198         } else
199             return false;
200     }
201
202     protected static final String JavaDoc KEY_FILE_NAME = "KEY_FILE_NAME";
203     protected static final String JavaDoc KEY_FILE_OPENED = "KEY_FILE_OPENED";
204     protected static final String JavaDoc KEY_TAB_INDEX = "KEY_TAB_INDEX";
205     protected static final String JavaDoc KEY_CONTAINER_DATA = "KEY_CONTAINER_DATA";
206
207     public void setPersistentData(Map JavaDoc data) {
208         setFileName((String JavaDoc)data.get(KEY_FILE_NAME));
209         setOpened((Boolean JavaDoc) data.get(KEY_FILE_OPENED));
210         setTabIndex((Integer JavaDoc) data.get(KEY_TAB_INDEX));
211         setContainerPersistentData((Map JavaDoc)data.get(KEY_CONTAINER_DATA));
212     }
213
214     public Map JavaDoc<String JavaDoc,Object JavaDoc> getPersistentData() {
215         Map JavaDoc<String JavaDoc,Object JavaDoc> data = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
216         data.put(KEY_FILE_NAME, fileName);
217         data.put(KEY_FILE_OPENED, opened);
218         data.put(KEY_TAB_INDEX, tabIndex);
219
220         Map JavaDoc d = getContainerPersistentData();
221         if(d != null)
222             data.put(KEY_CONTAINER_DATA, d);
223
224         return data;
225     }
226
227     public int hashCode() {
228         return fileName.hashCode();
229     }
230
231     public boolean equals(Object JavaDoc o) {
232         if(o instanceof ProjectFileItem) {
233             ProjectFileItem otherItem = (ProjectFileItem)o;
234             return fileName.equals(otherItem.fileName);
235         } else {
236             return false;
237         }
238     }
239
240     public int compareTo(Object JavaDoc o) {
241         if(o instanceof ProjectFileItem) {
242             ProjectFileItem otherItem = (ProjectFileItem)o;
243             return fileName.compareTo(otherItem.fileName);
244         } else {
245             return 1;
246         }
247     }
248
249     /** Called by the XJTree to display the cell content. Use only the last path component
250      * (that is the name of file) only.
251      */

252
253     public String JavaDoc toString() {
254         return getFileName();
255     }
256
257 }
258
Popular Tags