001: /*
002: * ListOccurrences.java
003: *
004: * Copyright (C) 2000-2004 Peter Graves
005: * $Id: ListOccurrences.java,v 1.8 2004/04/26 19:51:03 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.awt.AWTEvent;
025: import java.awt.event.MouseEvent;
026:
027: public class ListOccurrences extends Buffer {
028: protected final Search search;
029: private final Buffer sourceBuffer;
030:
031: protected ListOccurrences(Search search) {
032: this .search = search;
033: sourceBuffer = null;
034: init();
035: }
036:
037: private ListOccurrences(Search search, Buffer sourceBuffer) {
038: this .search = search;
039: this .sourceBuffer = sourceBuffer;
040: init();
041: if (sourceBuffer.getFile() != null)
042: title = sourceBuffer.getFile().getName() + " \""
043: + search.getPattern() + "\"";
044: else
045: title = sourceBuffer.getTitle() + " \""
046: + search.getPattern() + "\"";
047: setTabWidth(sourceBuffer.getTabWidth());
048: if (sourceBuffer.getFile() != null)
049: appendLine("File: " + sourceBuffer.getFile().netPath());
050: else
051: appendLine("Buffer: " + sourceBuffer.getTitle());
052: }
053:
054: private void init() {
055: initializeUndo();
056: type = TYPE_LIST_OCCURRENCES;
057: mode = ListOccurrencesMode.getMode();
058: formatter = mode.getFormatter(this );
059: readOnly = true;
060: setTransient(true);
061: try {
062: lockWrite();
063: } catch (InterruptedException e) {
064: Log.debug(e);
065: return;
066: }
067: try {
068: if (search.isRegularExpression())
069: appendLine("Regular expression: \""
070: + search.getPattern() + '"');
071: else
072: appendLine("Pattern: \"" + search.getPattern() + '"');
073: if (search instanceof Replacement) {
074: String replaceWith = ((Replacement) search)
075: .getReplaceWith();
076: if (replaceWith != null)
077: appendLine("Replace with: \"" + replaceWith + '"');
078: }
079: appendLine("Options: " + getOptions());
080: } finally {
081: unlockWrite();
082: }
083: setInitialized(true);
084: }
085:
086: public Mode getParentMode() {
087: if (search instanceof FindInFiles)
088: return ((FindInFiles) search).getMode();
089: if (sourceBuffer != null)
090: return sourceBuffer.getMode();
091: return null;
092: }
093:
094: public static ListOccurrences findBuffer(Buffer sourceBuffer,
095: Search search) {
096: for (BufferIterator it = new BufferIterator(); it.hasNext();) {
097: Buffer buf = it.nextBuffer();
098: if (buf instanceof ListOccurrences) {
099: ListOccurrences lo = (ListOccurrences) buf;
100: if (lo.search.equals(search)
101: && lo.sourceBuffer == sourceBuffer)
102: return lo;
103: }
104: }
105: return null;
106: }
107:
108: private static ListOccurrences createBuffer(Buffer sourceBuffer,
109: Search search) {
110: ListOccurrences listOccurrences = null;
111: Position pos = new Position(sourceBuffer.getFirstLine(), 0);
112: while ((pos = search.find(sourceBuffer.getMode(), pos)) != null) {
113: if (listOccurrences == null)
114: listOccurrences = new ListOccurrences(search,
115: sourceBuffer);
116: listOccurrences.appendOccurrenceLine(pos.getLine());
117: Line next = pos.getNextLine();
118: if (next != null)
119: pos.moveTo(next, 0);
120: else
121: break;
122: }
123: if (listOccurrences != null) {
124: listOccurrences.renumber();
125: listOccurrences.setLoaded(true);
126: }
127: return listOccurrences;
128: }
129:
130: public static ListOccurrences getBuffer(Buffer sourceBuffer,
131: Search search) {
132: ListOccurrences lo = findBuffer(sourceBuffer, search);
133: if (lo != null)
134: return lo;
135: else
136: return createBuffer(sourceBuffer, search);
137: }
138:
139: public final Buffer getSourceBuffer() {
140: return sourceBuffer;
141: }
142:
143: public File getCurrentDirectory() {
144: if (sourceBuffer != null)
145: return sourceBuffer.getCurrentDirectory();
146: else
147: return Directories.getUserHomeDirectory();
148: }
149:
150: public void findOccurrenceAtDot(Editor editor, boolean killList) {
151: Position pos = editor.getDotCopy();
152: if (pos == null)
153: return;
154: final Line line = pos.getLine();
155: if (!(line instanceof OccurrenceLine))
156: return;
157: Buffer buf = null;
158: for (BufferIterator it = new BufferIterator(); it.hasNext();) {
159: Buffer b = it.nextBuffer();
160: if (b == sourceBuffer) {
161: buf = b;
162: break;
163: }
164: }
165: if (buf == null) {
166: sourceBuffer.relink();
167: buf = sourceBuffer;
168: }
169: if (!buf.isLoaded()) {
170: Log.debug("findOccurrence reloading source buffer");
171: buf.load();
172: if (!buf.isLoaded()) {
173: editor.status("Unable to load buffer");
174: return;
175: }
176: }
177: final Line sourceLine = ((OccurrenceLine) line).getSourceLine();
178: Debug.assertTrue(sourceLine != null);
179: Line target;
180: if (buf.contains(sourceLine))
181: target = sourceLine;
182: else
183: target = buf.getLine(sourceLine.lineNumber());
184: if (target != null)
185: gotoSource(editor, buf, target, killList);
186: }
187:
188: protected void gotoSource(Editor editor, Buffer buf, Line target,
189: boolean killList) {
190: if (target != null) {
191: Editor ed;
192: if (editor.getFrame().getEditorCount() > 1) {
193: editor.getOtherEditor().makeNext(buf);
194: ed = editor.activateInOtherWindow(buf);
195: } else {
196: ed = editor;
197: ed.makeNext(buf);
198: ed.activate(buf);
199: }
200: ed.setLastSearch(search);
201: ed.addUndo(SimpleEdit.MOVE);
202: ed.unmark();
203: ed.update(ed.getDotLine());
204: ed.setDot(target, 0);
205: Position found = search.find(buf.getMode(), ed.getDot());
206: if (found != null) {
207: ed.setDot(found);
208: ed.markFoundPattern(search);
209: } else {
210: ed.moveCaretToDotCol();
211: }
212: ed.update(ed.getDotLine());
213: ed.updateDisplay();
214:
215: if (killList) {
216: if (ed.getFrame().getEditorCount() > 1) {
217: Editor otherEditor = ed.getOtherEditor();
218: if (otherEditor != null)
219: ed.getFrame().closeEditor(otherEditor);
220: kill();
221: }
222: }
223: }
224: }
225:
226: public final void appendOccurrenceLine(Line sourceLine) {
227: appendLine(new OccurrenceLine(sourceLine));
228: }
229:
230: public final void appendOccurrenceLine(String s, int lineNumber) {
231: appendLine(new OccurrenceLine(s, lineNumber));
232: }
233:
234: protected String getOptions() {
235: FastStringBuffer sb = new FastStringBuffer(
236: search.ignoreCase() ? "ignore case" : "case sensitive");
237: if (search.wholeWordsOnly())
238: sb.append(", whole words only");
239: return sb.toString();
240: }
241:
242: public final Search getSearch() {
243: return search;
244: }
245:
246: public Position getInitialDotPos() {
247: for (Line line = getFirstLine(); line != null; line = line
248: .next()) {
249: if (line instanceof OccurrenceLine)
250: return new Position(line, 0);
251: }
252: return new Position(getFirstLine(), 0);
253: }
254:
255: public Position getInitialDotPos(Line sourceLine, int sourceOffs) {
256: for (Line line = getFirstLine(); line != null; line = line
257: .next()) {
258: if (line instanceof OccurrenceLine) {
259: if (((OccurrenceLine) line).getSourceLine() == sourceLine) {
260: int index = line.getText().indexOf(':');
261: if (index >= 0)
262: return new Position(line, index + 1
263: + sourceOffs);
264: else
265: return new Position(line, sourceOffs);
266: }
267: }
268: }
269: return getInitialDotPos();
270: }
271:
272: public String getFileNameForDisplay() {
273: if (sourceBuffer == null || sourceBuffer.getFile() == null)
274: return "";
275: else
276: return sourceBuffer.getFile().getName();
277: }
278:
279: public static final void listOccurrences() {
280: listOccurrences(Editor.currentEditor());
281: }
282:
283: public static void listOccurrences(Editor editor) {
284: final Search search = editor.getLastSearch();
285: if (search != null) {
286: editor.setWaitCursor();
287: ListOccurrences buf = getBuffer(editor.getBuffer(), search);
288: editor.setDefaultCursor();
289: if (buf != null) {
290: editor.makeNext(buf);
291: Editor otherEditor = editor.getOtherEditor();
292: if (otherEditor != null) {
293: buf.setUnsplitOnClose(otherEditor.getBuffer()
294: .unsplitOnClose());
295: otherEditor.makeNext(buf);
296: } else
297: buf.setUnsplitOnClose(true);
298: Editor ed = editor.activateInOtherWindow(buf);
299:
300: ed.setDot(buf.getInitialDotPos());
301: ed.moveCaretToDotCol();
302: ed.updateDisplay();
303: } else
304: search.notFound(editor);
305: }
306: }
307:
308: public static void listOccurrencesOfPatternAtDot() {
309: final Editor editor = Editor.currentEditor();
310: final Search search = editor.getSearchAtDot();
311: if (search != null) {
312: editor.setLastSearch(search);
313: editor.setWaitCursor();
314: ListOccurrences buf = getBuffer(editor.getBuffer(), search);
315: editor.setDefaultCursor();
316: if (buf != null) {
317: editor.makeNext(buf);
318: Editor otherEditor = editor.getOtherEditor();
319: if (otherEditor != null) {
320: buf.setUnsplitOnClose(otherEditor.getBuffer()
321: .unsplitOnClose());
322: otherEditor.makeNext(buf);
323: } else
324: buf.setUnsplitOnClose(true);
325: Editor ed = editor.activateInOtherWindow(buf);
326:
327: ed.setDot(buf.getInitialDotPos(editor.getDotLine(),
328: editor.getDotOffset()));
329: ed.moveCaretToDotCol();
330: ed.updateDisplay();
331: } else
332: search.notFound(editor);
333: }
334: }
335:
336: public static void findOccurrenceAtDot() {
337: final Editor editor = Editor.currentEditor();
338: final Buffer buffer = editor.getBuffer();
339: if (buffer instanceof ListOccurrences)
340: ((ListOccurrences) buffer).findOccurrenceAtDot(editor,
341: false);
342: }
343:
344: public static void findOccurrenceAtDotAndKillList() {
345: final Editor editor = Editor.currentEditor();
346: final Buffer buffer = editor.getBuffer();
347: if (buffer instanceof ListOccurrences)
348: ((ListOccurrences) buffer)
349: .findOccurrenceAtDot(editor, true);
350: }
351:
352: public static void mouseFindOccurrence() {
353: final Editor editor = Editor.currentEditor();
354: final Buffer buffer = editor.getBuffer();
355: if (buffer instanceof ListOccurrences) {
356: AWTEvent e = editor.getDispatcher().getLastEvent();
357: if (e instanceof MouseEvent) {
358: editor.mouseMoveDotToPoint((MouseEvent) e);
359: ((ListOccurrences) buffer).findOccurrenceAtDot(editor,
360: false);
361: }
362: }
363: }
364: }
|