KickJava   Java API By Example, From Geeks To Geeks.

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


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

20
21 package org.armedbear.j;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 public final class FindTagTextFieldHandler extends DefaultTextFieldHandler
28 {
29     public FindTagTextFieldHandler(Editor editor, HistoryTextField textField)
30     {
31         super(editor, textField);
32     }
33
34     public boolean wantTab()
35     {
36         return true;
37     }
38
39     public void enter()
40     {
41         String JavaDoc pattern = textField.getText();
42         if (pattern == null)
43             return;
44         pattern = pattern.trim();
45         if (pattern.length() == 0)
46             return;
47         // Save history.
48
History history = textField.getHistory();
49         if (history != null) {
50             history.append(pattern);
51             history.save();
52         }
53         editor.ensureActive();
54         editor.setFocusToDisplay();
55         editor.updateLocation();
56         findTag(pattern);
57         editor.getDispatcher().eventHandled();
58     }
59
60     private void findTag(String JavaDoc pattern)
61     {
62         final Buffer buffer = editor.getBuffer();
63         List JavaDoc tags = findMatchingTags(buffer, pattern);
64         if (tags != null) {
65             if (tags.size() > 1) {
66                 // Can we get a unique match if we just consider defuns etc.
67
// and explicit tags?
68
ArrayList JavaDoc shortList = new ArrayList JavaDoc();
69                 for (Iterator JavaDoc it = tags.iterator(); it.hasNext();) {
70                     Tag tag = (Tag) it.next();
71                     if (tag instanceof LocalTag) {
72                         int type = ((LocalTag)tag).getType();
73                         // Java etc.
74
if (type == TAG_METHOD || type == TAG_EXPLICIT)
75                             shortList.add(tag);
76                         // Lisp.
77
if (type == TAG_DEFUN || type == TAG_GENERIC_FUNCTION ||
78                             type == TAG_MACRO)
79                             shortList.add(tag);
80                     }
81                 }
82                 if (shortList.size() == 1)
83                     tags = shortList;
84             }
85             if (tags.size() > 1) {
86                 editor.setDefaultCursor();
87                 ListTagsBuffer buf =
88                     new ListTagsBuffer(editor, "findTag", pattern, tags);
89                 editor.makeNext(buf);
90                 Editor ed = editor.activateInOtherWindow(buf);
91                 ed.setDot(buf.getInitialDotPos());
92                 ed.moveCaretToDotCol();
93                 ed.updateDisplay();
94             } else if (tags.size() == 1) {
95                 // Exactly one match.
96
Tag tag = (Tag) tags.get(0);
97                 editor.pushPosition();
98                 if (tag instanceof LocalTag)
99                     TagCommands.gotoLocalTag(editor, (LocalTag)tag, false);
100                 else if (tag instanceof GlobalTag)
101                     TagCommands.gotoGlobalTag(editor, (GlobalTag)tag, false);
102                 else
103                     Debug.bug();
104             }
105         } else
106             editor.status("Tag \"".concat(pattern).concat("\" not found"));
107     }
108
109     private static List JavaDoc findMatchingTags(Buffer buffer, String JavaDoc pattern)
110     {
111         boolean ignoreCase = Utilities.isLowerCase(pattern);
112         final Mode mode = buffer.getMode();
113         // We'll start by looking in the current buffer. If we find an exact
114
// match there, we're done.
115
List JavaDoc list = findMatchingTagsInBuffer(buffer, pattern, ignoreCase);
116         if (list == null) {
117             // No exact match in the current buffer. Look in the current
118
// directory.
119
list =
120                 TagCommands.findMatchingTagsInDirectory(pattern,
121                     buffer.getCurrentDirectory(), mode, -1, ignoreCase);
122             if (list == null) {
123                 // Look at all the directories in the buffer's tag path.
124
List JavaDoc dirs = TagCommands.getDirectoriesInTagPath(buffer);
125                 if (dirs != null) {
126                     for (int i = 0; i < dirs.size(); i++) {
127                         String JavaDoc dir = (String JavaDoc) dirs.get(i);
128                         File directory = File.getInstance(dir);
129                         if (directory.equals(buffer.getCurrentDirectory()))
130                             continue;
131                         List JavaDoc tagsInDir =
132                             TagCommands.findMatchingTagsInDirectory(pattern,
133                                 directory, mode, -1, ignoreCase);
134                         if (tagsInDir != null) {
135                             if (list == null)
136                                 list = new ArrayList JavaDoc();
137                             list.addAll(tagsInDir);
138                         }
139                     }
140                 }
141             }
142         }
143         return (list != null && list.size() > 0) ? list : null;
144     }
145
146     private static List JavaDoc findMatchingTagsInBuffer(Buffer buffer, String JavaDoc pattern,
147         boolean ignoreCase)
148     {
149         if (buffer.getTags() == null) {
150             Tagger tagger = buffer.getMode().getTagger(buffer);
151             if (tagger != null)
152                 tagger.run();
153         }
154         boolean isQualified = buffer.getMode().isQualifiedName(pattern);
155         List JavaDoc list = new ArrayList JavaDoc();
156         final List JavaDoc localTags = buffer.getTags();
157         if (localTags != null) {
158             // Look through all the local tags.
159
Iterator JavaDoc iter = localTags.iterator();
160             while (iter.hasNext()) {
161                 LocalTag localTag = (LocalTag) iter.next();
162                 if (isQualified) {
163                     String JavaDoc tagName = localTag.getName();
164                     if ((ignoreCase && tagName.equalsIgnoreCase(pattern)) || tagName.equals(pattern)) {
165                         list.add(localTag);
166                     }
167                 } else {
168                     // Not qualified.
169
String JavaDoc methodName = localTag.getMethodName();
170                     if (methodName != null) {
171                         if ((ignoreCase && methodName.equalsIgnoreCase(pattern)) || methodName.equals(pattern)) {
172                             list.add(localTag);
173                         }
174                     }
175                 }
176             }
177         }
178         return (list != null && list.size() > 0) ? list : null;
179     }
180
181     public List JavaDoc getCompletions(final String JavaDoc prefix)
182     {
183         List JavaDoc list = getCompletionsInCurrentBuffer(prefix);
184         Mode mode = editor.getMode();
185         List JavaDoc tags =
186             Editor.getTagFileManager().getTags(editor.getCurrentDirectory(),
187                 mode);
188         if (tags != null) {
189             boolean prefixIsQualified = mode.isQualifiedName(prefix);
190             boolean ignoreCase = Utilities.isLowerCase(prefix);
191             int prefixLength = prefix.length();
192             for (int i = 0; i < tags.size(); i++) {
193                 GlobalTag tag = (GlobalTag) tags.get(i);
194                 if (tag.getName().regionMatches(ignoreCase, 0, prefix, 0, prefixLength)) {
195                     String JavaDoc toBeAdded;
196                     if (prefixIsQualified)
197                         toBeAdded = tag.getName();
198                     else {
199                         toBeAdded = tag.getClassName();
200                         if (toBeAdded == null)
201                             toBeAdded = tag.getName();
202                     }
203                     maybeAdd(list, toBeAdded);
204                     continue;
205                 }
206                 if (!prefixIsQualified && mode.hasQualifiedNames()) {
207                     // The name we're looking for does not have a class prefix.
208
// Look for a match on the method name of the tag.
209
String JavaDoc methodName = tag.getMethodName();
210                     if (methodName != null) {
211                         if (methodName.regionMatches(ignoreCase, 0, prefix, 0, prefixLength))
212                             maybeAdd(list, tag.getMethodName());
213                     }
214                 }
215             }
216         }
217         return list;
218     }
219
220     private List JavaDoc getCompletionsInCurrentBuffer(String JavaDoc prefix)
221     {
222         List JavaDoc list = new ArrayList JavaDoc();
223         List JavaDoc tags = editor.getBuffer().getTags();
224         if (tags != null) {
225             boolean ignoreCase = Utilities.isLowerCase(prefix);
226             int prefixLength = prefix.length();
227             for (int i = 0; i < tags.size(); i++) {
228                 Tag tag = (Tag) tags.get(i);
229                 if (tag.getMethodName().regionMatches(ignoreCase, 0, prefix, 0, prefixLength))
230                     maybeAdd(list, tag.getMethodName());
231             }
232         }
233         return list;
234     }
235
236     // Add name if it's not already in the list.
237
private void maybeAdd(List JavaDoc list, String JavaDoc name)
238     {
239         if (name != null) {
240             for (int i = list.size(); i-- > 0;)
241                 if (name.equals(list.get(i)))
242                     return; // It's already there.
243
list.add(name);
244         }
245     }
246 }
247
Popular Tags