KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > ajde > ui > EditorManager


1
2 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3  *
4  * This file is part of the IDE support for the AspectJ(tm)
5  * programming language; see http://aspectj.org
6  *
7  * The contents of this file are subject to the Mozilla Public License
8  * Version 1.1 (the "License"); you may not use this file except in
9  * compliance with the License. You may obtain a copy of the License at
10  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is AspectJ.
18  *
19  * The Initial Developer of the Original Code is Xerox Corporation. Portions
20  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
21  * All Rights Reserved.
22  *
23  * Contributor(s):
24  */

25
26 package org.aspectj.ajde.ui;
27
28 import java.io.*;
29 import javax.swing.*;
30 import java.awt.*;
31 import java.util.*;
32 import org.aspectj.asm.*;
33 import org.aspectj.ajde.*;
34 import org.aspectj.ajde.ui.swing.*;
35
36 /**
37  * Responsible for controlling the editor.
38  *
39  * @todo remove coupling to <CODE>BasicEditor</CODE>
40  * @author Mik Kersten
41  */

42 public class EditorManager {
43
44     private EditorAdapter editor = null;
45     private BasicEditor basicEditor = null;
46     private ArrayList editorListeners = new ArrayList();
47     private Vector editors = new Vector();
48     private JPanel editor_panel = null;
49     private Box editors_box = Box.createVerticalBox();
50
51     public EditorManager(EditorAdapter ajdeEditor) {
52         if (ajdeEditor instanceof BasicEditor) {
53             this.basicEditor = (BasicEditor)ajdeEditor;
54             editors.add(basicEditor);
55             editors_box.add(basicEditor.getPanel());
56             editor_panel = new JPanel(new BorderLayout());
57             editor_panel.add(editors_box, BorderLayout.CENTER);
58         } else {
59             editors.add(ajdeEditor);
60             this.editor = ajdeEditor;
61         }
62     }
63
64     public void addListener(EditorListener editorListener) {
65         editorListeners.add(editorListener);
66     }
67
68     public void removeListener(EditorListener editorListener) {
69         editorListeners.remove(editorListener);
70     }
71
72     public void notifyCurrentFileChanged(String JavaDoc filePath) {
73         for (Iterator it = editorListeners.iterator(); it.hasNext(); ) {
74             ((EditorListener)it.next()).currentFileChanged(filePath);
75         }
76     }
77
78     public void addViewForSourceLine(final String JavaDoc filePath, final int lineNumber) {
79         if (basicEditor == null) return;
80         editors_box.remove(basicEditor.getPanel());
81         final BasicEditor newEditor = new BasicEditor();
82         editors.add(newEditor);
83         
84         Runnable JavaDoc update = new Runnable JavaDoc() {
85             public void run() {
86                 editors_box.add(newEditor.getPanel());
87                 newEditor.showSourceLine(filePath, lineNumber, true);
88                 //AjdeUIManager.getDefault().getIdeUIAdapter().resetGUI();
89
}
90         };
91
92         if (SwingUtilities.isEventDispatchThread()) {
93             update.run();
94         } else {
95             try {
96                 SwingUtilities.invokeAndWait(update);
97             } catch (Exception JavaDoc e) {
98                 Ajde.getDefault().getErrorHandler().handleError("Could not add view for source line.", e);
99             }
100         }
101     }
102
103     public String JavaDoc getCurrFile() {
104         if (basicEditor != null) {
105             return basicEditor.getCurrFile();
106         } else {
107             return editor.getCurrFile();
108         }
109     }
110
111     
112     public void showSourceLine(SourceLocation sourceLocation, boolean highlight) {
113         if (sourceLocation != null) {
114             showSourceLine(
115                 sourceLocation.getSourceFilePath(),
116                 sourceLocation.getLineNumber(),
117                 highlight);
118         }
119     }
120
121     /**
122      * @todo remove "instanceof AjdeManager" hack
123      */

124     public void showSourceLine(String JavaDoc filePath, int lineNumber, boolean highlight) {
125         if (editors.size() > 1) {
126             editors_box.removeAll();
127             editors_box.add(basicEditor.getPanel());
128             //AjdeUIManager.getDefault().getIdeUIAdapter().resetGUI();
129
editors.removeAllElements();
130             editors.add(basicEditor);
131         }
132         
133         if (basicEditor != null) {
134             basicEditor.showSourceLine(filePath, lineNumber, highlight);
135         } else {
136             editor.showSourceLine(filePath, lineNumber, highlight);
137         }
138     }
139
140     public void pasteToCaretPos(String JavaDoc text) {
141         if (basicEditor != null) {
142             basicEditor.pasteToCaretPos(text);
143         } else {
144             editor.pasteToCaretPos(text);
145         }
146     }
147
148     public void showSourcelineAnnotation(String JavaDoc filePath, int lineNumber, java.util.List JavaDoc items) {
149         editor.showSourcelineAnnotation(filePath, lineNumber, items);
150     }
151
152     public void saveContents() {
153         try {
154             for (Iterator it = editors.iterator(); it.hasNext(); ) {
155                 ((EditorAdapter)it.next()).saveContents();
156             }
157         } catch (IOException ioe) {
158             Ajde.getDefault().getErrorHandler().handleError("Editor could not save the current file.", ioe);
159         }
160     }
161
162     public JPanel getEditorPanel() {
163         if (editor_panel != null) {
164             return editor_panel;
165         } else {
166             return basicEditor.getPanel();
167         }
168     }
169 }
170
171
172
Popular Tags