KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * IList.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: IList.java,v 1.2 2002/10/08 02:48:56 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.io.BufferedReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Stack JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30
31 public final class IList implements BackgroundProcess, Constants
32 {
33     private final HashSet JavaDoc searchedFiles = new HashSet JavaDoc(256);
34     private final Stack JavaDoc stack = new Stack JavaDoc();
35     private final Editor editor;
36     private final Buffer sourceBuffer;
37     private final Search search;
38     private final boolean verbose;
39     private final String JavaDoc path;
40     private final File currentDirectory;
41
42     private ListOccurrencesInFiles outputBuffer;
43     private boolean cancelled;
44
45     public IList(Editor editor, Search search, boolean verbose)
46     {
47         this.editor = editor;
48         sourceBuffer = editor.getBuffer();
49         this.search = search;
50         this.verbose = verbose;
51         path = sourceBuffer.getStringProperty(Property.INCLUDE_PATH);
52         currentDirectory = editor.getCurrentDirectory();
53     }
54
55     private Buffer getSourceBuffer()
56     {
57         return sourceBuffer;
58     }
59
60     private ListOccurrences getOutputBuffer()
61     {
62         return outputBuffer;
63     }
64
65     private ListOccurrencesInFiles createOutputBuffer()
66     {
67         ListOccurrencesInFiles buf = new ListOccurrencesInFiles(search);
68         FastStringBuffer sb = new FastStringBuffer(sourceBuffer.getFile().getName());
69         sb.append(" \"");
70         sb.append(search.getPattern());
71         sb.append('"');
72         buf.setTitle(sb.toString());
73         return buf;
74     }
75
76     public void run()
77     {
78         if (SwingUtilities.isEventDispatchThread())
79             Debug.bug();
80         try {
81             final Mode mode = sourceBuffer.getMode();
82             for (Line line = sourceBuffer.getFirstLine(); line != null; line = line.next()) {
83                 Position pos = new Position(line, 0);
84                 if ((pos = search.findInLine(mode, pos)) != null)
85                     found(sourceBuffer.getFile(), line.getText(), line.lineNumber()+1);
86                 String JavaDoc s = Utilities.extractInclude(line.getText());
87                 if (s != null)
88                     searchFile(s, search);
89                 if (cancelled)
90                     break;
91             }
92             if (outputBuffer != null) {
93                 if (cancelled)
94                     outputBuffer.appendStatusLine("Search cancelled by user");
95                 outputBuffer.renumber();
96                 outputBuffer.setLoaded(true);
97             }
98         }
99         finally {
100             Log.debug("calling sourceBuffer.unlockRead");
101             sourceBuffer.unlockRead();
102             sourceBuffer.setBusy(false);
103             SwingUtilities.invokeLater(completionRunnable);
104         }
105     }
106
107     public void cancel()
108     {
109         cancelled = true;
110     }
111
112     private void searchFile(final String JavaDoc s, Search search)
113     {
114         File file = Utilities.findInclude(s, path, currentDirectory);
115         if (file == null) {
116             // Not found.
117
if (verbose) {
118                 if (outputBuffer == null)
119                     outputBuffer = createOutputBuffer();
120                 outputBuffer.appendLine(s.concat(" not found"));
121             }
122             return;
123         }
124         if (searchedFiles.contains(file)) {
125             // Already searched.
126
if (verbose) {
127                 if (outputBuffer == null)
128                     outputBuffer = createOutputBuffer();
129                 outputBuffer.appendLine(s.concat(" already searched"));
130             }
131             return;
132         }
133         searchedFiles.add(file);
134         if (verbose) {
135             if (outputBuffer == null)
136                 outputBuffer = createOutputBuffer();
137             outputBuffer.appendLine(s.concat(": searching ".concat(file.toString())));
138         }
139         try {
140             BufferedReader JavaDoc reader =
141                 new BufferedReader JavaDoc(new InputStreamReader JavaDoc(file.getInputStream()));
142             String JavaDoc line;
143             int lineNumber = 0;
144             while ((line = reader.readLine()) != null) {
145                 ++lineNumber;
146                 if (search.find(line)) {
147                     found(file, line, lineNumber);
148                 }
149                 String JavaDoc name = Utilities.extractInclude(line);
150                 if (name != null) {
151                     // Recurse!
152
stack.push(file);
153                     searchFile(name, search);
154                     stack.pop();
155                 }
156             }
157             reader.close();
158         }
159         catch (IOException JavaDoc e) {
160             Log.error(e);
161         }
162     }
163
164     private File currentFile;
165
166     private void found(File file, String JavaDoc s, int lineNumber)
167     {
168         if (outputBuffer == null)
169             outputBuffer = createOutputBuffer();
170         if (!file.equals(currentFile)) {
171             outputBuffer.appendFileLine(file, true);
172             currentFile = file;
173         }
174         outputBuffer.appendOccurrenceLine(s, lineNumber);
175     }
176
177     public static void iList()
178     {
179         iList(false);
180     }
181
182     public static void iList(String JavaDoc arg)
183     {
184         iList(arg != null && arg.trim().equals("-v"));
185     }
186
187     private static void iList(boolean verbose)
188     {
189         final Editor editor = Editor.currentEditor();
190         int modeId = editor.getModeId();
191         if (modeId == C_MODE || modeId == CPP_MODE) {
192             final Search search = editor.getSearchAtDot();
193             if (search != null) {
194                 editor.setLastSearch(search);
195                 editor.setWaitCursor();
196                 IList ilist = new IList(editor, search, verbose);
197                 Buffer buffer = ilist.getSourceBuffer();
198                 try {
199                     buffer.lockRead();
200                 }
201                 catch (InterruptedException JavaDoc e) {
202                     Log.error(e);
203                     return;
204                 }
205                 buffer.setBusy(true);
206                 new Thread JavaDoc(ilist).start();
207             }
208         }
209     }
210
211     private Runnable JavaDoc completionRunnable = new Runnable JavaDoc() {
212         public void run()
213         {
214             Log.debug("completionRunnable.run");
215             editor.setDefaultCursor();
216             Buffer buf = getOutputBuffer();
217             if (buf != null) {
218                 editor.makeNext(buf);
219                 Editor ed = editor.activateInOtherWindow(buf);
220                 ed.setDot(buf.getInitialDotPos());
221                 ed.moveCaretToDotCol();
222                 ed.updateDisplay();
223             } else if (!cancelled)
224                 search.notFound(editor);
225             if (cancelled)
226                 editor.status("Search cancelled");
227         }
228     };
229 }
230
Popular Tags