| 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 ; 9 import java.util.Map ; 10 40 41 public class ProjectFileItem implements Comparable { 42 43 public static final String FILE_GRAMMAR_EXTENSION = ".g"; 44 public static final String FILE_JAVA_EXTENSION = ".java"; 45 public static final String FILE_STG_EXTENSION = ".stg"; 46 public static final String FILE_ST_EXTENSION = ".st"; 47 public static final String FILE_MANTRA_EXTENSION = ".om"; 48 public static final String FILE_TEXT_EXTENSION = ".txt"; 49 50 public static final String FILE_TYPE_GRAMMAR = "FILE_TYPE_GRAMMAR"; 51 public static final String FILE_TYPE_JAVA = "FILE_TYPE_JAVA"; 52 public static final String FILE_TYPE_STG = "FILE_TYPE_STG"; 53 public static final String FILE_TYPE_ST = "FILE_TYPE_ST"; 54 public static final String FILE_TYPE_MANTRA = "FILE_TYPE_MANTRA"; 55 public static final String FILE_TYPE_TEXT = "FILE_TYPE_TEXT"; 56 public static final String FILE_TYPE_UNKNOWN = "FILE_TYPE_UNKNOWN"; 57 58 protected CContainerProject project; 59 protected ComponentContainer container; 60 61 protected String fileName; 62 protected String fileType; 63 protected boolean opened; 64 protected int tabIndex; 65 protected Map containerData; 66 67 public ProjectFileItem(CContainerProject project, String name) { 68 setFileName(name); 69 this.project = project; 70 } 71 72 public static String getFileType(String 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 getFileTypeName(String 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 data) { 151 this.containerData = data; 152 } 153 154 public Map 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 fileName) { 169 this.fileName = fileName; 170 this.fileType = getFileType(fileName); 171 } 172 173 public String getFilePath() { 174 return XJUtils.concatPath(project.getSourcePath(), fileName); 175 } 176 177 public String getFileName() { 178 return fileName; 179 } 180 181 public String 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 KEY_FILE_NAME = "KEY_FILE_NAME"; 203 protected static final String KEY_FILE_OPENED = "KEY_FILE_OPENED"; 204 protected static final String KEY_TAB_INDEX = "KEY_TAB_INDEX"; 205 protected static final String KEY_CONTAINER_DATA = "KEY_CONTAINER_DATA"; 206 207 public void setPersistentData(Map data) { 208 setFileName((String )data.get(KEY_FILE_NAME)); 209 setOpened((Boolean ) data.get(KEY_FILE_OPENED)); 210 setTabIndex((Integer ) data.get(KEY_TAB_INDEX)); 211 setContainerPersistentData((Map )data.get(KEY_CONTAINER_DATA)); 212 } 213 214 public Map <String ,Object > getPersistentData() { 215 Map <String ,Object > data = new HashMap <String , Object >(); 216 data.put(KEY_FILE_NAME, fileName); 217 data.put(KEY_FILE_OPENED, opened); 218 data.put(KEY_TAB_INDEX, tabIndex); 219 220 Map 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 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 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 252 253 public String toString() { 254 return getFileName(); 255 } 256 257 } 258 | Popular Tags |