Source code: org/aspectj/tools/ajde/jbuilder/JBuilderEditorAdapter.java
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.tools.ajde.jbuilder;
27
28 import java.io.*;
29 import javax.swing.*;
30
31 import java.awt.Font;
32 import java.awt.event.*;
33 import java.util.*;
34 import com.borland.primetime.editor.*;
35 import com.borland.primetime.node.*;
36 import com.borland.primetime.ide.*;
37 import com.borland.primetime.util.*;
38 import com.borland.primetime.vfs.*;
39 import com.borland.primetime.viewer.*;
40 import org.aspectj.asm.*;
41 import org.aspectj.ajde.ui.*;
42 import org.aspectj.ajde.*;
43 import org.aspectj.ajde.ui.swing.*;
44
45 /**
46 * @todo make the API consistent
47 * @author Mik Kersten
48 */
49 public class JBuilderEditorAdapter implements EditorAdapter {
50
51 public void showSourceLine(String filePath, int lineNumber, boolean highlight) {
52 if (filePath.replace('\\', '/').equals(getCurrFile())) {
53 highlightLine(lineNumber, highlight);
54 } else {
55 seekToSourceLine(filePath, lineNumber, 0, highlight);
56 }
57 }
58
59 /**
60 * @todo remove redundancy
61 */
62 public void showSourceLine(SourceLocation sourceLocation, boolean highlight) {
63 // System.err.println("!!!!!!!!!");
64 // seekToSourceLine(
65 // sourceLocation.getSourceFilePath(),
66 // sourceLocation.getLineNumber(),
67 // sourceLocation.getColumnNumber(),
68 // highlight);
69
70 com.borland.primetime.node.Project activeProject
71 = Browser.getActiveBrowser().getActiveProject();
72 Browser.getActiveBrowser().doOpen(new Url(new java.io.File(sourceLocation.getSourceFilePath())),
73 Browser.getActiveBrowser().getActiveProject(),
74 false);
75 highlightLine(
76 sourceLocation.getLineNumber()-1,
77 sourceLocation.getColumnNumber(),
78 highlight
79 );
80 }
81
82 public void showSourceLine(int lineNumber, boolean highlight) {
83 highlightLine(lineNumber, highlight);
84 }
85
86 public void saveContents() throws IOException {
87 try {
88 Browser.getActiveBrowser().doSave(Browser.getAllOpenNodes());
89 } catch (VetoException ve) {
90 throw new IOException(ve.getMessage());
91 }
92 }
93
94 public JPanel getPanel() {
95 return null;
96 }
97
98 public String getCurrFile() {
99 if (Browser.getActiveBrowser() != null) {
100 Node currNode = Browser.getActiveBrowser().getActiveNode();
101 if (currNode != null) {
102 return currNode.getLongDisplayName();
103 } else {
104 return null;
105 }
106 } else {
107 return null;
108 }
109 }
110
111 public void showSourceForFile(String filePath) {
112 com.borland.primetime.node.Project activeProject
113 = Browser.getActiveBrowser().getActiveProject();
114 Browser.getActiveBrowser().doOpen(new Url(new java.io.File(filePath)),
115 Browser.getActiveBrowser().getActiveProject(),
116 false);
117 }
118
119 /**
120 * Not implemented.
121 */
122 public void pasteToCaretPos(String text) { }
123
124 /**
125 * Fails silently (in case resources have changed on disk since structure was built).
126 */
127 public void showSourcelineAnnotation(String filePath, int lineNumber, List items) {
128 try {
129 JPopupMenu menu = new JPopupMenu("Inline Annotations");
130 for (Iterator it2 = items.iterator(); it2.hasNext(); ) {
131 ProgramElementNode node = (ProgramElementNode)it2.next();
132 java.util.List relations = node.getRelations();
133 for (Iterator it3 = relations.iterator(); it3.hasNext(); ) {
134 RelationNode relation = (RelationNode)it3.next();
135 JMenu relationMenu = new JMenu(relation.toString());
136 relationMenu.setIcon((Icon)AjdeUIManager.getDefault().getIconRegistry().getRelationIcon(relation.getRelation()).getIconResource());
137 relationMenu.setFont(new Font(AjdeWidgetStyles.DEFAULT_LABEL_FONT.getName(), Font.ITALIC, AjdeWidgetStyles.DEFAULT_LABEL_FONT.getSize()));
138
139 for (Iterator it4 = relation.getChildren().iterator(); it4.hasNext(); ) {
140 LinkNode link = (LinkNode)it4.next();
141 final String linkSourceFile = link.getProgramElementNode().getSourceLocation().getSourceFilePath();
142 final int linkLineNumber = link.getProgramElementNode().getSourceLocation().getLineNumber();
143
144 JMenuItem linkMenuItem = new JMenuItem(link.toString());
145 linkMenuItem.setForeground(AjdeWidgetStyles.LINK_NODE_COLOR);
146 linkMenuItem.setFont(AjdeWidgetStyles.DEFAULT_LABEL_FONT);
147 linkMenuItem.setIcon((Icon)AjdeUIManager.getDefault().getIconRegistry().getStructureIcon(link.getProgramElementNode().getProgramElementKind()).getIconResource());
148
149 if (link.getProgramElementNode().getSourceLocation().getSourceFilePath() == null) {
150 linkMenuItem.setForeground(AjdeWidgetStyles.LINK_NODE_NO_SOURCE_COLOR);
151 }
152
153 linkMenuItem.addActionListener(new InlineAnnotationMenuListener(link));
154 relationMenu.add(linkMenuItem);
155 }
156 menu.add(relationMenu);
157 }
158 }
159
160 Gutter gutter = JBuilderManager.INSTANCE.getCurrGutter();
161 if (gutter != null) {
162 gutter.addMark(lineNumber,
163 new AjdeGutterMark(AjdeUIManager.getDefault().getIconRegistry().getRelationsIcon(), menu));
164 }
165 } catch (IndexOutOfBoundsException iooe) {
166 //Ajde.getDefault().getErrorHandler().handleError("Inline annotation index out of bounds.", iooe);
167 }
168 }
169
170 class InlineAnnotationMenuListener implements java.awt.event.ActionListener {
171 private LinkNode linkNode;
172
173 public InlineAnnotationMenuListener(LinkNode linkNode) {
174 this.linkNode = linkNode;
175 }
176
177 public void actionPerformed(ActionEvent e) {
178 if (linkNode != null) {
179 Ajde.getDefault().getStructureViewManager().fireNavigationAction(linkNode);
180 //Ajde.getDefault().getEditorManager().showSourceLine(linkSourceFile, linkLineNumber, true);
181 }
182 }
183 };
184
185 private void seekToSourceLine(final String file, final int lineNumber, int columnNumber, boolean highlight) {
186 JBuilderManager.INSTANCE.setNavigationSeekPerformed(true);
187
188 try {
189 // Browser.getActiveBrowser().setContentPaneVisible(true);
190 com.borland.primetime.node.Project activeProject
191 = Browser.getActiveBrowser().getActiveProject();
192 Browser.getActiveBrowser().doOpen(new Url(new java.io.File(file)),
193 Browser.getActiveBrowser().getActiveProject(),
194 false);
195 highlightLine(lineNumber, columnNumber, highlight);
196 GoToLineThread gtt = new GoToLineThread(file.replace('\\', '/'), lineNumber, this);
197 gtt.start();
198 do {
199 shortPause();
200 } while (gtt.isFinished());
201 } catch (Exception e) {
202 //Ajde.getDefault().getErrorHandler().handleError("Could not seek to file: " + file + ", line: " + lineNumber);
203 }
204 }
205
206 private void shortPause() {
207 try {
208 Thread.currentThread().sleep(100);
209 } catch (InterruptedException e) {
210 throw new RuntimeException(e.getMessage());
211 }
212 }
213
214 void highlightLine(int lineNumber, boolean highlight) {
215 highlightLine(lineNumber, 0, highlight);
216 }
217
218 /**
219 * Will not try to highlight negative line numbers. Fails siltently.
220 */
221 void highlightLine(int lineNumber, int columnNumber, boolean highlight) {
222 if (lineNumber < 0) return;
223 try {
224 EditorActions ea = new EditorActions();
225 EditorPane ep = ea.ACTION_DeleteBeginLine.getFocusedEditor();
226 if (lineNumber <= 1) highlight = false;
227 if (lineNumber > -1) {
228 ep.gotoPosition(lineNumber+1, columnNumber, highlight);
229 }
230 } catch (Exception e) {
231 //Ajde.getDefault().getErrorHandler().handleError("Could not highlight line: " + lineNumber, e);
232 }
233 }
234 }
235
236 class AjdeGutterMark extends GutterMark {
237 JPopupMenu menu = null;
238
239 public AjdeGutterMark(Icon icon, JPopupMenu menu) {
240 super(icon);
241 this.menu = menu;
242 }
243
244 public JPopupMenu getPopupMenu() {
245 return menu;
246 }
247 }
248
249 // Gutter gutter = null;
250 // try {
251 // textNodeViewerClass = Class.forName("com.borland.primetime.viewer.TextNodeViewer");
252 // } catch (ClassNotFoundException cnfe) {
253 // return null;
254 // }
255 // if (textNodeViewerClass == null) {
256 // log.println("class is null");
257 // return null;
258 // }
259 // Browser b = Browser.getActiveBrowser();
260 // TextNodeViewer viewer =(TextNodeViewer)b.getViewerOfType(fileNode,
261 // textNodeViewerClass);
262 // if (viewer == null) {
263 // log.println("viewer is null");
264 // return null;
265 // }
266 // TextView view = ((TextViewerComponent)viewer.getViewerComponent()).getMainView();
267 // if (view == null) {
268 // log.println("view is null");
269 // return null;
270 // }
271 // gutter = view.getGutter();
272 // return gutter;
273 // }