KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > TagCommands


1 /*
2  * TagCommands.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: TagCommands.java,v 1.7 2002/11/10 01:03:35 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.AWTEvent JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30 import javax.swing.undo.CompoundEdit JavaDoc;
31
32 public final class TagCommands implements Constants
33 {
34     public static void nextTag()
35     {
36         final Editor editor = Editor.currentEditor();
37         final Position dot = editor.getDot();
38         if (dot != null) {
39             final List JavaDoc tags = editor.getBuffer().getTags();
40             if (tags != null) {
41                 // Find the next tag after dot.
42
final int dotLineNumber = dot.lineNumber();
43                 final int limit = tags.size();
44                 for (int i = 0; i < limit; i++) {
45                     LocalTag tag = (LocalTag) tags.get(i);
46                     if (tag.lineNumber() > dotLineNumber) {
47                         editor.moveDotTo(tag.getPosition());
48                         return;
49                     }
50                 }
51             }
52         }
53     }
54
55     public static void previousTag()
56     {
57         final Editor editor = Editor.currentEditor();
58         final Position dot = editor.getDot();
59         if (dot != null) {
60             final List JavaDoc tags = editor.getBuffer().getTags();
61             if (tags != null) {
62                 // Find the last tag before dot.
63
final int dotLineNumber = dot.lineNumber();
64                 for (int i = tags.size()-1; i >= 0; i--) {
65                     LocalTag tag = (LocalTag) tags.get(i);
66                     if (tag.lineNumber() < dotLineNumber) {
67                         editor.moveDotTo(tag.getPosition());
68                         return;
69                     }
70                 }
71             }
72         }
73     }
74
75     public static void findTag()
76     {
77         final Editor editor = Editor.currentEditor();
78         final LocationBar locationBar = editor.getLocationBar();
79         locationBar.setLabelText(LocationBar.PROMPT_TAG);
80         HistoryTextField textField = locationBar.getTextField();
81         textField.setHandler(new FindTagTextFieldHandler(editor, textField));
82         textField.setHistory(new History("findTag.tag"));
83         textField.setText("");
84         if (editor.getDispatcher().getLastEvent().getSource() instanceof MenuItem) {
85             Runnable JavaDoc r = new Runnable JavaDoc() {
86                 public void run()
87                 {
88                     editor.setFocusToTextField();
89                 }
90             };
91             SwingUtilities.invokeLater(r);
92         } else
93             editor.setFocusToTextField();
94     }
95
96     private static boolean findTag(Editor editor, Expression expression,
97         boolean useOtherWindow)
98     {
99         List JavaDoc tags = findMatchingTags(editor.getBuffer(), expression);
100         if (tags == null || tags.size() == 0)
101             return false;
102         if (tags.size() == 1) {
103             // One match.
104
Tag tag = (Tag) tags.get(0);
105             editor.pushPosition();
106             if (tag instanceof LocalTag)
107                 gotoLocalTag(editor, (LocalTag)tag, useOtherWindow);
108             else if (tag instanceof GlobalTag)
109                 gotoGlobalTag(editor, (GlobalTag)tag, useOtherWindow);
110             else
111                 Debug.bug();
112         } else {
113             // More than one match.
114
editor.setDefaultCursor();
115             ListTagsBuffer buf =
116                 new ListTagsBuffer(editor, "findTag", expression.getName(), tags);
117             editor.makeNext(buf);
118             Editor ed = editor.activateInOtherWindow(buf);
119             ed.setDot(buf.getInitialDotPos());
120             ed.moveCaretToDotCol();
121             ed.updateDisplay();
122         }
123         return true;
124     }
125
126     public static List JavaDoc findMatchingTags(Buffer buffer, Expression expression)
127     {
128         final Mode mode = buffer.getMode();
129         if (!mode.isTaggable())
130             return null;
131         // We'll start by looking in the current buffer. If we find an exact
132
// match there, we're done.
133
List JavaDoc list = findMatchingTagsInBuffer(buffer, expression);
134         if (list == null) {
135             // No exact match in the current buffer. Look in the current
136
// directory.
137
final File currentDirectory = buffer.getCurrentDirectory();
138             list = findMatchingTagsInDirectory(expression, currentDirectory,
139                                                mode);
140             if (list == null) {
141                 // Look at all the directories in the buffer's tag path.
142
List JavaDoc dirs = getDirectoriesInTagPath(buffer);
143                 if (dirs != null) {
144                     for (int i = 0; i < dirs.size(); i++) {
145                         String JavaDoc dir = (String JavaDoc) dirs.get(i);
146                         File directory =
147                             File.getInstance(currentDirectory, dir);
148                         if (directory == null)
149                             continue;
150                         if (directory.equals(currentDirectory))
151                             continue;
152                         List JavaDoc tagsInDir =
153                             findMatchingTagsInDirectory(expression, directory,
154                                                         mode);
155                         if (tagsInDir != null) {
156                             if (list == null)
157                                 list = new ArrayList JavaDoc();
158                             list.addAll(tagsInDir);
159                         }
160                     }
161                 }
162             }
163         }
164         return list;
165     }
166
167     private static List JavaDoc findMatchingTagsInBuffer(Buffer buffer,
168         Expression expression)
169     {
170         if (buffer.getTags() == null) {
171             Tagger tagger = buffer.getMode().getTagger(buffer);
172             if (tagger != null)
173                 tagger.run();
174         }
175         List JavaDoc list = null;
176         final List JavaDoc localTags = buffer.getTags();
177         if (localTags != null) {
178             // Look through all the local tags.
179
Iterator JavaDoc iter = localTags.iterator();
180             while (iter.hasNext()) {
181                 LocalTag localTag = (LocalTag) iter.next();
182                 if (expression.matches(localTag)) {
183                     if (list == null)
184                         list = new ArrayList JavaDoc();
185                     list.add(localTag);
186                 }
187             }
188         }
189         return list;
190     }
191
192     public static List JavaDoc findMatchingTagsInDirectory(Expression expression,
193         File directory, Mode mode)
194     {
195         if (!mode.isTaggable())
196             return null;
197         final String JavaDoc name = expression.getName();
198         final int arity = expression.getArity();
199         List JavaDoc tags = Editor.getTagFileManager().getTags(directory, mode);
200         if (tags == null) {
201             if (!directory.isRemote())
202                 Editor.getTagFileManager().addToQueue(directory, mode);
203             return null;
204         }
205         List JavaDoc list = null;
206         Iterator JavaDoc iter = tags.iterator();
207         while (iter.hasNext()) {
208             GlobalTag tag = (GlobalTag) iter.next();
209             String JavaDoc methodName = tag.getMethodName();
210             if (methodName != null && methodName.equals(name)) {
211                 if (arity >= 0) {
212                     int n = Expression.getArity(tag.getCanonicalSignature());
213                     if (n >= 0 && n != arity)
214                         continue;
215                 }
216                 if (list == null)
217                     list = new ArrayList JavaDoc();
218                 list.add(tag);
219             }
220         }
221         return list;
222     }
223
224     public static List JavaDoc findMatchingTagsInDirectory(String JavaDoc name,
225         File directory, Mode mode, int arity, boolean ignoreCase)
226     {
227         if (!mode.isTaggable())
228             return null;
229         List JavaDoc tags = Editor.getTagFileManager().getTags(directory, mode);
230         if (tags == null) {
231             if (!directory.isRemote())
232                 Editor.getTagFileManager().addToQueue(directory, mode);
233             return null;
234         }
235         boolean isQualified = mode.isQualifiedName(name);
236         List JavaDoc list = new ArrayList JavaDoc();
237         Iterator JavaDoc iter = tags.iterator();
238         while (iter.hasNext()) {
239             GlobalTag tag = (GlobalTag) iter.next();
240             String JavaDoc tagName = tag.getName();
241             if ((ignoreCase && tagName.equalsIgnoreCase(name)) || tagName.equals(name)) {
242                 if (arity >= 0) {
243                     int n = Expression.getArity(tag.getCanonicalSignature());
244                     if (n == -1 || n == arity) {
245                         list.add(tag);
246                         continue;
247                     }
248                 } else {
249                     list.add(tag);
250                     continue;
251                 }
252             }
253             if (!isQualified) {
254                 // The name we're looking for does not have a class prefix.
255
String JavaDoc methodName = tag.getMethodName();
256                 if (methodName != null) {
257                     if ((ignoreCase && methodName.equalsIgnoreCase(name)) || methodName.equals(name)) {
258                         if (arity >= 0) {
259                             int n = Expression.getArity(tag.getCanonicalSignature());
260                             if (n < 0 || n == arity)
261                                 list.add(tag);
262                         } else
263                             list.add(tag);
264                     }
265                 }
266             }
267         }
268         return list.size() > 0 ? list : null;
269     }
270
271     public static boolean findClass(Editor editor, String JavaDoc className,
272         boolean useOtherWindow)
273     {
274         editor.setWaitCursor();
275         boolean succeeded = false;
276         File file = JavaSource.findSource(editor.getBuffer(), className,
277             false);
278         if (file != null) {
279             Buffer buf = Editor.getBuffer(file);
280             if (buf != null) {
281                 Editor ed;
282                 if (useOtherWindow)
283                     ed = editor.displayInOtherWindow(buf);
284                 else
285                     ed = editor;
286                 ed.makeNext(buf);
287                 ed.activate(buf);
288                 ed.repaintDisplay();
289                 List JavaDoc localTags = buf.getTags(true);
290                 if (localTags != null) {
291                     Position pos = null;
292                     for (int i = 0; i < localTags.size(); i++) {
293                         JavaTag tag = (JavaTag) localTags.get(i);
294                         int type = tag.getType();
295                         if (type == TAG_CLASS || type == TAG_INTERFACE) {
296                             String JavaDoc name = tag.getMethodName();
297                             if (name.startsWith("class"))
298                                 name = name.substring(5).trim();
299                             else if (name.startsWith("interface"))
300                                 name = name.substring(9).trim();
301                             if (name.equals(className)) {
302                                 pos = tag.getPosition();
303                                 break;
304                             }
305                         }
306                     }
307                     if (pos != null) {
308                         CompoundEdit JavaDoc compoundEdit = editor.beginCompoundEdit();
309                         ed.addUndo(SimpleEdit.FOLD);
310                         ed.unfoldMethod(pos.getLine());
311                         ed.moveDotTo(pos);
312                         centerTag(ed);
313                         ed.endCompoundEdit(compoundEdit);
314                         ed.updateDisplay();
315                         succeeded = true;
316                     }
317                 }
318             }
319         }
320         editor.setDefaultCursor();
321         return succeeded;
322     }
323
324     public static void listMatchingTags()
325     {
326         final Editor editor = Editor.currentEditor();
327         FindTagDialog findTagDialog =
328             new FindTagDialog(editor, "List Matching Tags");
329         editor.centerDialog(findTagDialog);
330         findTagDialog.show();
331         listMatchingTags(editor, findTagDialog.getInput());
332     }
333
334     public static void listMatchingTags(String JavaDoc name)
335     {
336         listMatchingTags(Editor.currentEditor(), name);
337     }
338
339     public static void listMatchingTagsAtDot()
340     {
341         final Editor editor = Editor.currentEditor();
342         listMatchingTags(editor,
343             editor.getMode().getIdentifier(editor.getDot()));
344     }
345
346     private static void listMatchingTags(Editor editor, String JavaDoc name)
347     {
348         if (name != null && name.length() > 0) {
349             editor.repaintNow();
350             editor.setWaitCursor();
351             final Buffer buffer = editor.getBuffer();
352             List JavaDoc tags = findMatchingTagsInDirectory(name,
353                 buffer.getCurrentDirectory(), buffer.getMode(), -1,
354                 Utilities.isLowerCase(name));
355             editor.setDefaultCursor();
356             if (tags != null) {
357                 ListTagsBuffer buf =
358                     new ListTagsBuffer(editor, "listMatchingTags", name, tags);
359                 editor.makeNext(buf);
360                 Editor ed = editor.activateInOtherWindow(buf);
361                 ed.setDot(buf.getInitialDotPos());
362                 ed.moveCaretToDotCol();
363                 ed.updateDisplay();
364             } else
365                 editor.status("Tag \"" + name + "\" not found");
366         }
367     }
368
369     public static void gotoLocalTag(Editor editor, LocalTag localTag,
370         boolean useOtherWindow)
371     {
372         Editor ed;
373         if (useOtherWindow)
374             ed = editor.displayInOtherWindow(editor.getBuffer());
375         else
376             ed = editor;
377         localTag.gotoTag(ed);
378     }
379
380     public static void gotoGlobalTag(Editor editor, GlobalTag globalTag,
381         boolean useOtherWindow)
382     {
383         Buffer buf = editor.getBuffer(File.getInstance(globalTag.getFileName()));
384         Editor ed;
385         if (useOtherWindow)
386             ed = editor.displayInOtherWindow(buf);
387         else
388             ed = editor;
389         globalTag.gotoTag(ed);
390     }
391
392     public static List JavaDoc getDirectoriesInTagPath(Buffer buffer)
393     {
394         String JavaDoc tagPath = buffer.getStringProperty(Property.TAG_PATH);
395         if (tagPath == null)
396             return null;
397         return Utilities.getDirectoriesInPath(tagPath);
398     }
399
400     public static void centerTag()
401     {
402         centerTag(Editor.currentEditor());
403     }
404
405     public static void centerTag(Editor editor)
406     {
407         final Buffer buffer = editor.getBuffer();
408         final List JavaDoc tags = buffer.getTags();
409         if (tags == null)
410             return;
411         final int size = tags.size();
412         int dotLineNumber = editor.getDotLineNumber();
413         Line begin = null;
414         Line end = null;
415         for (int i = 0; i < size; i++) {
416             LocalTag tag = (LocalTag) tags.get(i);
417             if (tag.lineNumber() <= dotLineNumber) {
418                 begin = tag.getLine();
419             } else {
420                 // tag.lineNumber() > dotLineNumber
421
end = tag.getLine();
422                 break;
423             }
424         }
425         if (begin != null)
426             editor.getDisplay().centerRegion(begin, end);
427     }
428
429     public static void makeTagFile()
430     {
431         final Editor editor = Editor.currentEditor();
432         File directory = editor.getCurrentDirectory();
433         if (directory.isRemote()) {
434             MessageDialog.showMessageDialog(
435                 "Tag files are not supported for remote directories",
436                 "Make Tag File");
437             return;
438         }
439         Mode mode = editor.getMode();
440         if (!mode.isTaggable()) {
441             MessageDialog.showMessageDialog(
442                 "Tag files are not supported in " + mode + " mode",
443                 "Make Tag File");
444             return;
445         }
446         editor.repaintNow();
447         editor.setWaitCursor();
448         Editor.getTagFileManager().makeTagFile(directory, mode);
449         editor.setDefaultCursor();
450         MessageDialog.showMessageDialog("Tag file is ready", "Make Tag File");
451     }
452
453     public static void findTagAtDot()
454     {
455         findTagAtDotInternal(Editor.currentEditor(), false, false);
456     }
457
458     public static void findTagAtDotOtherWindow()
459     {
460         findTagAtDotInternal(Editor.currentEditor(), false, true);
461     }
462
463     public static void mouseFindTag()
464     {
465         final Editor editor = Editor.currentEditor();
466         AWTEvent JavaDoc e = editor.getDispatcher().getLastEvent();
467         if (e instanceof MouseEvent JavaDoc) {
468             editor.mouseMoveDotToPoint((MouseEvent JavaDoc)e);
469             findTagAtDotInternal(editor, true, false);
470         }
471     }
472
473     private static void findTagAtDotInternal(Editor editor, boolean exact,
474         boolean useOtherWindow)
475     {
476         editor.setWaitCursor();
477         Expression expr = editor.getMode().getExpressionAtDot(editor, exact);
478         if (expr != null) {
479             boolean succeeded = findTag(editor, expr, useOtherWindow);
480             if (!succeeded && editor.getModeId() == C_MODE) {
481                 // Special case for Emacs source.
482
// If name is "Frun_hooks", look for "run-hooks".
483
String JavaDoc name = expr.getName();
484                 if (name != null && name.length() > 0 && name.charAt(0) == 'F')
485                     name = name.substring(1).replace('_', '-');
486                     succeeded = findTag(editor, new Expression(name),
487                         useOtherWindow);
488             }
489             if (!succeeded)
490                 editor.status("Tag \"" + expr.getName() + "\" not found");
491         }
492         editor.setDefaultCursor();
493     }
494 }
495
Popular Tags