001: /*
002: * IList.java
003: *
004: * Copyright (C) 2002 Peter Graves
005: * $Id: IList.java,v 1.2 2002/10/08 02:48:56 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.io.BufferedReader;
025: import java.io.IOException;
026: import java.io.InputStreamReader;
027: import java.util.HashSet;
028: import java.util.Stack;
029: import javax.swing.SwingUtilities;
030:
031: public final class IList implements BackgroundProcess, Constants {
032: private final HashSet searchedFiles = new HashSet(256);
033: private final Stack stack = new Stack();
034: private final Editor editor;
035: private final Buffer sourceBuffer;
036: private final Search search;
037: private final boolean verbose;
038: private final String path;
039: private final File currentDirectory;
040:
041: private ListOccurrencesInFiles outputBuffer;
042: private boolean cancelled;
043:
044: public IList(Editor editor, Search search, boolean verbose) {
045: this .editor = editor;
046: sourceBuffer = editor.getBuffer();
047: this .search = search;
048: this .verbose = verbose;
049: path = sourceBuffer.getStringProperty(Property.INCLUDE_PATH);
050: currentDirectory = editor.getCurrentDirectory();
051: }
052:
053: private Buffer getSourceBuffer() {
054: return sourceBuffer;
055: }
056:
057: private ListOccurrences getOutputBuffer() {
058: return outputBuffer;
059: }
060:
061: private ListOccurrencesInFiles createOutputBuffer() {
062: ListOccurrencesInFiles buf = new ListOccurrencesInFiles(search);
063: FastStringBuffer sb = new FastStringBuffer(sourceBuffer
064: .getFile().getName());
065: sb.append(" \"");
066: sb.append(search.getPattern());
067: sb.append('"');
068: buf.setTitle(sb.toString());
069: return buf;
070: }
071:
072: public void run() {
073: if (SwingUtilities.isEventDispatchThread())
074: Debug.bug();
075: try {
076: final Mode mode = sourceBuffer.getMode();
077: for (Line line = sourceBuffer.getFirstLine(); line != null; line = line
078: .next()) {
079: Position pos = new Position(line, 0);
080: if ((pos = search.findInLine(mode, pos)) != null)
081: found(sourceBuffer.getFile(), line.getText(), line
082: .lineNumber() + 1);
083: String s = Utilities.extractInclude(line.getText());
084: if (s != null)
085: searchFile(s, search);
086: if (cancelled)
087: break;
088: }
089: if (outputBuffer != null) {
090: if (cancelled)
091: outputBuffer
092: .appendStatusLine("Search cancelled by user");
093: outputBuffer.renumber();
094: outputBuffer.setLoaded(true);
095: }
096: } finally {
097: Log.debug("calling sourceBuffer.unlockRead");
098: sourceBuffer.unlockRead();
099: sourceBuffer.setBusy(false);
100: SwingUtilities.invokeLater(completionRunnable);
101: }
102: }
103:
104: public void cancel() {
105: cancelled = true;
106: }
107:
108: private void searchFile(final String s, Search search) {
109: File file = Utilities.findInclude(s, path, currentDirectory);
110: if (file == null) {
111: // Not found.
112: if (verbose) {
113: if (outputBuffer == null)
114: outputBuffer = createOutputBuffer();
115: outputBuffer.appendLine(s.concat(" not found"));
116: }
117: return;
118: }
119: if (searchedFiles.contains(file)) {
120: // Already searched.
121: if (verbose) {
122: if (outputBuffer == null)
123: outputBuffer = createOutputBuffer();
124: outputBuffer.appendLine(s.concat(" already searched"));
125: }
126: return;
127: }
128: searchedFiles.add(file);
129: if (verbose) {
130: if (outputBuffer == null)
131: outputBuffer = createOutputBuffer();
132: outputBuffer.appendLine(s.concat(": searching ".concat(file
133: .toString())));
134: }
135: try {
136: BufferedReader reader = new BufferedReader(
137: new InputStreamReader(file.getInputStream()));
138: String line;
139: int lineNumber = 0;
140: while ((line = reader.readLine()) != null) {
141: ++lineNumber;
142: if (search.find(line)) {
143: found(file, line, lineNumber);
144: }
145: String name = Utilities.extractInclude(line);
146: if (name != null) {
147: // Recurse!
148: stack.push(file);
149: searchFile(name, search);
150: stack.pop();
151: }
152: }
153: reader.close();
154: } catch (IOException e) {
155: Log.error(e);
156: }
157: }
158:
159: private File currentFile;
160:
161: private void found(File file, String s, int lineNumber) {
162: if (outputBuffer == null)
163: outputBuffer = createOutputBuffer();
164: if (!file.equals(currentFile)) {
165: outputBuffer.appendFileLine(file, true);
166: currentFile = file;
167: }
168: outputBuffer.appendOccurrenceLine(s, lineNumber);
169: }
170:
171: public static void iList() {
172: iList(false);
173: }
174:
175: public static void iList(String arg) {
176: iList(arg != null && arg.trim().equals("-v"));
177: }
178:
179: private static void iList(boolean verbose) {
180: final Editor editor = Editor.currentEditor();
181: int modeId = editor.getModeId();
182: if (modeId == C_MODE || modeId == CPP_MODE) {
183: final Search search = editor.getSearchAtDot();
184: if (search != null) {
185: editor.setLastSearch(search);
186: editor.setWaitCursor();
187: IList ilist = new IList(editor, search, verbose);
188: Buffer buffer = ilist.getSourceBuffer();
189: try {
190: buffer.lockRead();
191: } catch (InterruptedException e) {
192: Log.error(e);
193: return;
194: }
195: buffer.setBusy(true);
196: new Thread(ilist).start();
197: }
198: }
199: }
200:
201: private Runnable completionRunnable = new Runnable() {
202: public void run() {
203: Log.debug("completionRunnable.run");
204: editor.setDefaultCursor();
205: Buffer buf = getOutputBuffer();
206: if (buf != null) {
207: editor.makeNext(buf);
208: Editor ed = editor.activateInOtherWindow(buf);
209: ed.setDot(buf.getInitialDotPos());
210: ed.moveCaretToDotCol();
211: ed.updateDisplay();
212: } else if (!cancelled)
213: search.notFound(editor);
214: if (cancelled)
215: editor.status("Search cancelled");
216: }
217: };
218: }
|