KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > plugin > intellij > PIEditor


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 JavaDoc;
20 import java.awt.event.ComponentEvent JavaDoc;
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 /*
27
28 [The "BSD licence"]
29 Copyright (c) 2005-2006 Jean Bovet
30 All rights reserved.
31
32 Redistribution and use in source and binary forms, with or without
33 modification, are permitted provided that the following conditions
34 are met:
35
36 1. Redistributions of source code must retain the above copyright
37 notice, this list of conditions and the following disclaimer.
38 2. Redistributions in binary form must reproduce the above copyright
39 notice, this list of conditions and the following disclaimer in the
40 documentation and/or other materials provided with the distribution.
41 3. The name of the author may not be used to endorse or promote products
42 derived from this software without specific prior written permission.
43
44 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
48 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
53 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54
55 */

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 JavaDoc<PropertyChangeListener JavaDoc> listeners = new ArrayList JavaDoc<PropertyChangeListener JavaDoc>();
70
71     protected static final List JavaDoc<PIEditor> editors = new ArrayList JavaDoc<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 JavaDoc() {
122             @Override JavaDoc
123             public void componentResized(ComponentEvent JavaDoc 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 JavaDoc file) {
143         container.load(file);
144     }
145
146     public void save() {
147         container.getDocument().performAutoSave();
148         notifyFileModified();
149     }
150
151     /** Perform the layout only once, when the component resize for the first time.
152      *
153      */

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         /*CommandProcessor.getInstance().runUndoTransparentAction( new Runnable() {
174             public void run() {
175                 CommandProcessor.getInstance().executeCommand( project, new Runnable() {
176                     public void run() {
177                         ApplicationManager.getApplication().runWriteAction( new Runnable() {
178                             public void run() {
179                                 int offset = document.getTextLength();
180                                 document.insertString( offset, " " );
181                                 document.deleteString( offset, offset + 1 );
182                             }
183                         } );
184                     }
185                 }, "", null, UndoConfirmationPolicy.DO_NOT_REQUEST_CONFIRMATION );
186             }
187         } ); */

188     }
189
190     private void notifyFileModified() {
191         for (PropertyChangeListener JavaDoc propertyChangeListener : listeners) {
192             propertyChangeListener.propertyChange(new PropertyChangeEvent JavaDoc(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 JavaDoc 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 JavaDoc listener) {
240         listeners.add(listener);
241     }
242
243     public void removePropertyChangeListener(@NotNull PropertyChangeListener JavaDoc 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 JavaDoc value) {
264     }
265
266     private class MyFileDocumentManagerAdapter extends FileDocumentManagerAdapter {
267
268         @Override JavaDoc
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 JavaDoc
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 JavaDoc
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