001: /*
002: * ListOccurrencesInFiles.java
003: *
004: * Copyright (C) 2000-2004 Peter Graves
005: * $Id: ListOccurrencesInFiles.java,v 1.7 2004/04/02 03:29:05 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: public final class ListOccurrencesInFiles extends ListOccurrences {
025: private Position lastDotPos;
026:
027: // Find in files.
028: public ListOccurrencesInFiles(Search search) {
029: super (search);
030: if (search instanceof FindInFiles) {
031: appendLine("Files: " + ((FindInFiles) search).getFiles());
032: if (search.wholeWordsOnly())
033: appendLine("Mode: "
034: + ((FindInFiles) search).getMode()
035: .getDisplayName());
036: renumber();
037: }
038: title = "\"" + search.getPattern() + "\"";
039: setLoaded(true);
040: }
041:
042: private final void setLastDotPos(Position pos) {
043: lastDotPos = pos.copy();
044: }
045:
046: public final Position getLastDotPos() {
047: return lastDotPos != null ? lastDotPos : getInitialDotPos();
048: }
049:
050: public final File getCurrentDirectory() {
051: return Directories.getUserHomeDirectory();
052: }
053:
054: public void findOccurrenceAtDot(Editor editor, boolean killList) {
055: Position pos = editor.getDotCopy();
056: if (pos == null)
057: return;
058: Line sourceLine = null;
059: int sourceLineNumber = 0;
060: Buffer buf = null;
061: String canonicalPath = null;
062: setLastDotPos(pos);
063: final Line line = pos.getLine();
064: if ((line instanceof OccurrenceLine)) {
065: sourceLine = ((OccurrenceLine) line).getSourceLine();
066: Line ln = line;
067: if (!ln.getText().startsWith("File: "))
068: sourceLineNumber = Utilities.parseInt(ln.getText()) - 1;
069: while (ln != null) {
070: if (ln.getText().startsWith("File: ")) {
071: canonicalPath = ln.getText().substring(6);
072: break;
073: }
074: ln = ln.previous();
075: }
076: } else if (line instanceof FileLine)
077: canonicalPath = ((FileLine) line).getCanonicalPath();
078: if (buf == null && canonicalPath != null)
079: buf = Editor.getBuffer(File.getInstance(canonicalPath));
080: if (buf != null) {
081: if (!buf.isLoaded()) {
082: if (!buf.initialized())
083: buf.initialize();
084: buf.load();
085: if (!buf.isLoaded()) {
086: editor.status("Unable to load buffer");
087: return;
088: }
089: }
090: if (line instanceof FileLine) {
091: // Mark file visited.
092: ((FileLine) line).markVisited();
093: // Advance dot to next line.
094: Position dot = editor.getDot();
095: if (dot.equals(pos) && dot.getNextLine() != null) {
096: dot.moveTo(dot.getNextLine(), 0);
097: editor.moveCaretToDotCol();
098: }
099: }
100: Line target;
101: if (sourceLine != null) {
102: if (buf.contains(sourceLine))
103: target = sourceLine;
104: else
105: target = buf.getLine(sourceLine.lineNumber());
106: } else
107: target = buf.getLine(sourceLineNumber);
108: gotoSource(editor, buf, target, killList);
109: }
110: }
111:
112: public void findNextOccurrence(Editor editor) {
113: Line line;
114: for (line = editor.getDotLine().next(); line != null; line = line
115: .next()) {
116: if (line instanceof OccurrenceLine)
117: break;
118: }
119: if (line instanceof OccurrenceLine)
120: findOccurrence(editor, line);
121: }
122:
123: public void findPreviousOccurrence(Editor editor) {
124: Line line;
125: for (line = editor.getDotLine().previous(); line != null; line = line
126: .previous()) {
127: if (line instanceof OccurrenceLine)
128: break;
129: }
130: if (line instanceof OccurrenceLine)
131: findOccurrence(editor, line);
132: }
133:
134: // Move dot to line (in all editors) and call findOccurrenceAtDot().
135: private void findOccurrence(Editor editor, Line line) {
136: if (line instanceof OccurrenceLine) {
137: for (EditorIterator it = new EditorIterator(); it.hasNext();) {
138: Editor ed = it.nextEditor();
139: if (ed.getBuffer() == this ) {
140: ed.moveDotTo(line, 0);
141: ed.updateDisplay();
142: }
143: }
144: findOccurrenceAtDot(editor, false);
145: }
146: }
147:
148: public final void appendStatusLine(String s) {
149: appendLine(new ListOccurrencesStatusLine(s));
150: }
151:
152: public final void appendFileLine(File file,
153: boolean listEachOccurrence) {
154: appendLine(new FileLine(file, listEachOccurrence));
155: }
156:
157: protected String getOptions() {
158: String s = super .getOptions();
159: if (search instanceof FindInFiles)
160: if (((FindInFiles) search).getIncludeSubdirs())
161: s = s.concat(", include subdirectories");
162: return s;
163: }
164:
165: public Position getInitialDotPos() {
166: final boolean listEachOccurrence;
167: if (search instanceof FindInFiles)
168: listEachOccurrence = ((FindInFiles) search)
169: .getListEachOccurrence();
170: else
171: listEachOccurrence = true;
172: for (Line line = getFirstLine(); line != null; line = line
173: .next()) {
174: if (line instanceof OccurrenceLine)
175: return new Position(line, 0);
176: if (!listEachOccurrence && line instanceof FileLine)
177: return new Position(line, 0);
178: }
179: return new Position(getFirstLine(), 0);
180: }
181:
182: public void follow(File sourceFile, Line sourceLine) {
183: Line line = findLineForOccurrence(sourceFile, sourceLine);
184: if (line == null)
185: return;
186: for (EditorIterator it = new EditorIterator(); it.hasNext();) {
187: Editor ed = it.nextEditor();
188: if (ed.getBuffer() == this ) {
189: ed.moveDotTo(line, 0);
190: ed.updateDisplay();
191: }
192: }
193: }
194:
195: private Line findLineForOccurrence(File sourceFile, Line sourceLine) {
196: if (sourceFile == null)
197: return null;
198: if (sourceLine == null)
199: return null;
200: final String cp = sourceFile.canonicalPath();
201: Line line;
202: for (line = getFirstLine(); line != null; line = line.next()) {
203: if (line instanceof FileLine) {
204: if (((FileLine) line).getCanonicalPath().equals(cp))
205: break;
206: }
207: }
208: if (line == null)
209: return null;
210: Debug.assertTrue(line instanceof FileLine);
211: Debug.assertTrue(((FileLine) line).getCanonicalPath()
212: .equals(cp));
213: int target = sourceLine.lineNumber() + 1;
214: Line best = null;
215: for (line = line.next(); line instanceof OccurrenceLine; line = line
216: .next()) {
217: int candidate = ((OccurrenceLine) line)
218: .getSourceLineNumber();
219: if (candidate == target)
220: return line;
221: if (best != null && candidate > target)
222: return best;
223: best = line;
224: }
225: return best;
226: }
227:
228: public String getFileNameForDisplay() {
229: return "";
230: }
231: }
|