| 1 package org.antlr.works.plugin.intellij; 2 3 import com.intellij.codeHighlighting.BackgroundEditorHighlighter; 4 import com.intellij.ide.structureView.StructureViewBuilder; 5 import com.intellij.openapi.editor.Document; 6 import com.intellij.openapi.fileEditor.*; 7 import com.intellij.openapi.project.Project; 8 import com.intellij.openapi.util.Key; 9 import com.intellij.openapi.vfs.ReadonlyStatusHandler; 10 import com.intellij.openapi.vfs.VfsUtil; 11 import com.intellij.openapi.vfs.VirtualFile; 12 import org.antlr.works.components.grammar.CEditorGrammarDefaultDelegate; 13 import org.antlr.works.plugin.container.PluginContainer; 14 import org.antlr.works.plugin.container.PluginContainerDelegate; 15 import org.jetbrains.annotations.NotNull; 16 17 import javax.swing.*; 18 import java.awt.*; 19 import java.awt.event.ComponentAdapter ; 20 import java.awt.event.ComponentEvent ; 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 26 56 57 public class PIEditor implements FileEditor, PluginContainerDelegate { 58 59 protected PluginContainer container; 60 protected Project project; 61 protected VirtualFile virtualFile; 62 protected Document document; 63 64 protected JSplitPane vertical; 65 protected boolean layout = false; 66 protected boolean synchronizedDoc = false; 67 68 protected MyFileDocumentManagerAdapter fileDocumentAdapter; 69 protected java.util.List <PropertyChangeListener > listeners = new ArrayList <PropertyChangeListener >(); 70 71 protected static final List <PIEditor> editors = new ArrayList <PIEditor>(); 72 73 public PIEditor(Project project, VirtualFile file) { 74 this.project = project; 75 this.virtualFile = file; 76 this.document = FileDocumentManager.getInstance().getDocument(this.virtualFile); 77 78 container = new PluginContainer(); 79 container.load(VfsUtil.virtualToIoFile(virtualFile).getPath()); 80 container.setDelegate(this); 81 82 assemble(); 83 register(); 84 } 85 86 public void close() { 87 save(); 88 unregister(); 89 } 90 91 public static void saveAll() { 92 synchronized(editors) { 93 for(PIEditor e : editors) { 94 e.save(); 95 } 96 } 97 } 98 99 public void assemble() { 100 vertical = new JSplitPane(JSplitPane.VERTICAL_SPLIT); 101 vertical.setTopComponent(container.getEditorComponent()); 102 vertical.setBottomComponent(container.getTabbedComponent()); 103 vertical.setBorder(null); 104 vertical.setContinuousLayout(true); 105 vertical.setOneTouchExpandable(true); 106 107 JPanel upperPanel = new JPanel(new BorderLayout()); 108 upperPanel.add(container.getMenubarComponent(), BorderLayout.NORTH); 109 upperPanel.add(container.getToolbarComponent(), BorderLayout.CENTER); 110 111 JPanel panel = new JPanel(new BorderLayout()); 112 panel.add(upperPanel, BorderLayout.NORTH); 113 panel.add(vertical, BorderLayout.CENTER); 114 panel.add(container.getStatusComponent(), BorderLayout.SOUTH); 115 116 container.setEditorGrammarDelegate(new CEditorGrammarDefaultDelegate(vertical)); 117 container.getContentPane().add(panel); 118 } 119 120 public void register() { 121 container.getContentPane().addComponentListener(new ComponentAdapter () { 122 @Override  123 public void componentResized(ComponentEvent e) { 124 layout(); 125 } 126 }); 127 128 fileDocumentAdapter = new MyFileDocumentManagerAdapter(); 129 FileDocumentManager.getInstance().addFileDocumentManagerListener(fileDocumentAdapter); 130 synchronized(editors) { 131 editors.add(this); 132 } 133 } 134 135 public void unregister() { 136 synchronized(editors) { 137 editors.remove(this); 138 } 139 FileDocumentManager.getInstance().removeFileDocumentManagerListener(fileDocumentAdapter); 140 } 141 142 public void load(String file) { 143 container.load(file); 144 } 145 146 public void save() { 147 container.getDocument().performAutoSave(); 148 notifyFileModified(); 149 } 150 151 154 public void layout() { 155 if(!layout) { 156 vertical.setDividerLocation((int)(container.getContentPane().getHeight()*0.5)); 157 container.becomingVisibleForTheFirstTime(); 158 layout = true; 159 } 160 } 161 162 public void pluginDocumentDidChange() { 163 if(!document.isWritable()) { 164 ReadonlyStatusHandler.OperationStatus op = ReadonlyStatusHandler.getInstance(project).ensureFilesWritable(new VirtualFile[] { virtualFile } ); 165 if(!document.isWritable()) { 166 System.err.println(op.getReadonlyFilesMessage()); 167 return; 168 } 169 } 170 171 notifyFileModified(); 172 173 188 } 189 190 private void notifyFileModified() { 191 for (PropertyChangeListener propertyChangeListener : listeners) { 192 propertyChangeListener.propertyChange(new PropertyChangeEvent (this, FileEditor.PROP_MODIFIED, null, null)); 193 } 194 } 195 196 @NotNull 197 public JComponent getComponent() { 198 return container.getRootPane(); 199 } 200 201 public JComponent getPreferredFocusedComponent() { 202 return container.getEditorComponent(); 203 } 204 205 @NotNull 206 public String getName() { 207 return virtualFile.getName(); 208 } 209 210 @NotNull 211 public FileEditorState getState(@NotNull FileEditorStateLevel level) { 212 return new FileEditorState() { 213 public boolean canBeMergedWith(FileEditorState otherState, FileEditorStateLevel level) { 214 return false; 215 } 216 }; 217 } 218 219 public void setState(@NotNull FileEditorState state) { 220 } 221 222 public boolean isModified() { 223 return container.getDocument().isDirty(); 224 } 225 226 public boolean isValid() { 227 return true; 228 } 229 230 public void selectNotify() { 231 container.activate(); 232 } 233 234 public void deselectNotify() { 235 container.deactivate(); 236 save(); 237 } 238 239 public void addPropertyChangeListener(@NotNull PropertyChangeListener listener) { 240 listeners.add(listener); 241 } 242 243 public void removePropertyChangeListener(@NotNull PropertyChangeListener listener) { 244 listeners.remove(listener); 245 } 246 247 public BackgroundEditorHighlighter getBackgroundHighlighter() { 248 return null; 249 } 250 251 public FileEditorLocation getCurrentLocation() { 252 return null; 253 } 254 255 public StructureViewBuilder getStructureViewBuilder() { 256 return new PIStructureViewBuilder(container); 257 } 258 259 public Key getUserData(Key key) { 260 return null; 261 } 262 263 public void putUserData(Key key, Object value) { 264 } 265 266 private class MyFileDocumentManagerAdapter extends FileDocumentManagerAdapter { 267 268 @Override  269 public void fileContentReloaded(VirtualFile file, Document document) { 270 if(file.equals(PIEditor.this.virtualFile)) { 271 load(VfsUtil.virtualToIoFile(virtualFile).getPath()); 272 } 273 } 274 275 @Override  276 public void fileContentLoaded(VirtualFile file, Document document) { 277 if(file.equals(PIEditor.this.virtualFile)) { 278 load(VfsUtil.virtualToIoFile(virtualFile).getPath()); 279 } 280 } 281 282 @Override  283 public void beforeDocumentSaving(Document document) throws VetoDocumentSavingException { 284 Document doc = FileDocumentManager.getInstance().getDocument(PIEditor.this.virtualFile); 285 if(doc == document) { 286 save(); 287 } 288 } 289 290 } 291 292 293 } 294 | Popular Tags |