KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ListOccurrences.java
3  *
4  * Copyright (C) 2000-2004 Peter Graves
5  * $Id: ListOccurrences.java,v 1.8 2004/04/26 19:51:03 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.awt.AWTEvent JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26
27 public class ListOccurrences extends Buffer
28 {
29     protected final Search search;
30     private final Buffer sourceBuffer;
31
32     protected ListOccurrences(Search search)
33     {
34         this.search = search;
35         sourceBuffer = null;
36         init();
37     }
38
39     private ListOccurrences(Search search, Buffer sourceBuffer)
40     {
41         this.search = search;
42         this.sourceBuffer = sourceBuffer;
43         init();
44         if (sourceBuffer.getFile() != null)
45             title = sourceBuffer.getFile().getName() + " \"" + search.getPattern() + "\"";
46         else
47             title = sourceBuffer.getTitle() + " \"" + search.getPattern() + "\"";
48         setTabWidth(sourceBuffer.getTabWidth());
49         if (sourceBuffer.getFile() != null)
50             appendLine("File: " + sourceBuffer.getFile().netPath());
51         else
52             appendLine("Buffer: " + sourceBuffer.getTitle());
53     }
54
55     private void init()
56     {
57         initializeUndo();
58         type = TYPE_LIST_OCCURRENCES;
59         mode = ListOccurrencesMode.getMode();
60         formatter = mode.getFormatter(this);
61         readOnly = true;
62         setTransient(true);
63         try {
64             lockWrite();
65         }
66         catch (InterruptedException JavaDoc e) {
67             Log.debug(e);
68             return;
69         }
70         try {
71             if (search.isRegularExpression())
72                 appendLine("Regular expression: \"" + search.getPattern() + '"');
73             else
74                 appendLine("Pattern: \"" + search.getPattern() + '"');
75             if (search instanceof Replacement) {
76                 String JavaDoc replaceWith = ((Replacement)search).getReplaceWith();
77                 if (replaceWith != null)
78                     appendLine("Replace with: \"" + replaceWith + '"');
79             }
80             appendLine("Options: " + getOptions());
81         }
82         finally {
83             unlockWrite();
84         }
85         setInitialized(true);
86     }
87
88     public Mode getParentMode()
89     {
90         if (search instanceof FindInFiles)
91             return ((FindInFiles)search).getMode();
92         if (sourceBuffer != null)
93             return sourceBuffer.getMode();
94         return null;
95     }
96
97     public static ListOccurrences findBuffer(Buffer sourceBuffer, Search search)
98     {
99         for (BufferIterator it = new BufferIterator(); it.hasNext();) {
100             Buffer buf = it.nextBuffer();
101             if (buf instanceof ListOccurrences) {
102                 ListOccurrences lo = (ListOccurrences) buf;
103                 if (lo.search.equals(search) && lo.sourceBuffer == sourceBuffer)
104                     return lo;
105             }
106         }
107         return null;
108     }
109
110     private static ListOccurrences createBuffer(Buffer sourceBuffer, Search search)
111     {
112         ListOccurrences listOccurrences = null;
113         Position pos = new Position(sourceBuffer.getFirstLine(), 0);
114         while ((pos = search.find(sourceBuffer.getMode(), pos)) != null) {
115             if (listOccurrences == null)
116                 listOccurrences = new ListOccurrences(search, sourceBuffer);
117             listOccurrences.appendOccurrenceLine(pos.getLine());
118             Line next = pos.getNextLine();
119             if (next != null)
120                 pos.moveTo(next, 0);
121             else
122                 break;
123         }
124         if (listOccurrences != null) {
125             listOccurrences.renumber();
126             listOccurrences.setLoaded(true);
127         }
128         return listOccurrences;
129     }
130
131     public static ListOccurrences getBuffer(Buffer sourceBuffer, Search search)
132     {
133         ListOccurrences lo = findBuffer(sourceBuffer, search);
134         if (lo != null)
135             return lo;
136         else
137             return createBuffer(sourceBuffer, search);
138     }
139
140     public final Buffer getSourceBuffer()
141     {
142         return sourceBuffer;
143     }
144
145     public File getCurrentDirectory()
146     {
147         if (sourceBuffer != null)
148             return sourceBuffer.getCurrentDirectory();
149         else
150             return Directories.getUserHomeDirectory();
151     }
152
153     public void findOccurrenceAtDot(Editor editor, boolean killList)
154     {
155         Position pos = editor.getDotCopy();
156         if (pos == null)
157             return;
158         final Line line = pos.getLine();
159         if (!(line instanceof OccurrenceLine))
160             return;
161         Buffer buf = null;
162         for (BufferIterator it = new BufferIterator(); it.hasNext();) {
163             Buffer b = it.nextBuffer();
164             if (b == sourceBuffer) {
165                 buf = b;
166                 break;
167             }
168         }
169         if (buf == null) {
170             sourceBuffer.relink();
171             buf = sourceBuffer;
172         }
173         if (!buf.isLoaded()) {
174             Log.debug("findOccurrence reloading source buffer");
175             buf.load();
176             if (!buf.isLoaded()) {
177                 editor.status("Unable to load buffer");
178                 return;
179             }
180         }
181         final Line sourceLine = ((OccurrenceLine)line).getSourceLine();
182         Debug.assertTrue(sourceLine != null);
183         Line target;
184         if (buf.contains(sourceLine))
185             target = sourceLine;
186         else
187             target = buf.getLine(sourceLine.lineNumber());
188         if (target != null)
189             gotoSource(editor, buf, target, killList);
190     }
191
192     protected void gotoSource(Editor editor, Buffer buf, Line target,
193                               boolean killList)
194     {
195         if (target != null) {
196             Editor ed;
197             if (editor.getFrame().getEditorCount() > 1) {
198                 editor.getOtherEditor().makeNext(buf);
199                 ed = editor.activateInOtherWindow(buf);
200             } else {
201                 ed = editor;
202                 ed.makeNext(buf);
203                 ed.activate(buf);
204             }
205             ed.setLastSearch(search);
206             ed.addUndo(SimpleEdit.MOVE);
207             ed.unmark();
208             ed.update(ed.getDotLine());
209             ed.setDot(target, 0);
210             Position found = search.find(buf.getMode(), ed.getDot());
211             if (found != null) {
212                 ed.setDot(found);
213                 ed.markFoundPattern(search);
214             } else {
215                 ed.moveCaretToDotCol();
216             }
217             ed.update(ed.getDotLine());
218             ed.updateDisplay();
219
220             if (killList) {
221                 if (ed.getFrame().getEditorCount() > 1) {
222                     Editor otherEditor = ed.getOtherEditor();
223                     if (otherEditor != null)
224                         ed.getFrame().closeEditor(otherEditor);
225                     kill();
226                 }
227             }
228         }
229     }
230
231     public final void appendOccurrenceLine(Line sourceLine)
232     {
233         appendLine(new OccurrenceLine(sourceLine));
234     }
235
236     public final void appendOccurrenceLine(String JavaDoc s, int lineNumber)
237     {
238         appendLine(new OccurrenceLine(s, lineNumber));
239     }
240
241     protected String JavaDoc getOptions()
242     {
243         FastStringBuffer sb = new FastStringBuffer(search.ignoreCase() ? "ignore case" : "case sensitive");
244         if (search.wholeWordsOnly())
245             sb.append(", whole words only");
246         return sb.toString();
247     }
248
249     public final Search getSearch()
250     {
251         return search;
252     }
253
254     public Position getInitialDotPos()
255     {
256         for (Line line = getFirstLine(); line != null; line = line.next()) {
257             if (line instanceof OccurrenceLine)
258                 return new Position(line, 0);
259         }
260         return new Position(getFirstLine(), 0);
261     }
262
263     public Position getInitialDotPos(Line sourceLine, int sourceOffs)
264     {
265         for (Line line = getFirstLine(); line != null; line = line.next()) {
266             if (line instanceof OccurrenceLine) {
267                 if (((OccurrenceLine)line).getSourceLine() == sourceLine) {
268                     int index = line.getText().indexOf(':');
269                     if (index >= 0)
270                         return new Position(line, index + 1 + sourceOffs);
271                     else
272                         return new Position(line, sourceOffs);
273                 }
274             }
275         }
276         return getInitialDotPos();
277     }
278
279     public String JavaDoc getFileNameForDisplay()
280     {
281         if (sourceBuffer == null || sourceBuffer.getFile() == null)
282             return "";
283         else
284             return sourceBuffer.getFile().getName();
285     }
286
287     public static final void listOccurrences()
288     {
289         listOccurrences(Editor.currentEditor());
290     }
291
292     public static void listOccurrences(Editor editor)
293     {
294         final Search search = editor.getLastSearch();
295         if (search != null) {
296             editor.setWaitCursor();
297             ListOccurrences buf = getBuffer(editor.getBuffer(), search);
298             editor.setDefaultCursor();
299             if (buf != null) {
300                 editor.makeNext(buf);
301                 Editor otherEditor = editor.getOtherEditor();
302                 if (otherEditor != null) {
303                     buf.setUnsplitOnClose(otherEditor.getBuffer().unsplitOnClose());
304                     otherEditor.makeNext(buf);
305                 } else
306                     buf.setUnsplitOnClose(true);
307                 Editor ed = editor.activateInOtherWindow(buf);
308
309                 ed.setDot(buf.getInitialDotPos());
310                 ed.moveCaretToDotCol();
311                 ed.updateDisplay();
312             } else
313                 search.notFound(editor);
314         }
315     }
316
317     public static void listOccurrencesOfPatternAtDot()
318     {
319         final Editor editor = Editor.currentEditor();
320         final Search search = editor.getSearchAtDot();
321         if (search != null) {
322             editor.setLastSearch(search);
323             editor.setWaitCursor();
324             ListOccurrences buf = getBuffer(editor.getBuffer(), search);
325             editor.setDefaultCursor();
326             if (buf != null) {
327                 editor.makeNext(buf);
328                 Editor otherEditor = editor.getOtherEditor();
329                 if (otherEditor != null) {
330                     buf.setUnsplitOnClose(otherEditor.getBuffer().unsplitOnClose());
331                     otherEditor.makeNext(buf);
332                 } else
333                     buf.setUnsplitOnClose(true);
334                 Editor ed = editor.activateInOtherWindow(buf);
335
336                 ed.setDot(buf.getInitialDotPos(editor.getDotLine(),
337                                                editor.getDotOffset()));
338                 ed.moveCaretToDotCol();
339                 ed.updateDisplay();
340             } else
341                 search.notFound(editor);
342         }
343     }
344
345     public static void findOccurrenceAtDot()
346     {
347         final Editor editor = Editor.currentEditor();
348         final Buffer buffer = editor.getBuffer();
349         if (buffer instanceof ListOccurrences)
350             ((ListOccurrences)buffer).findOccurrenceAtDot(editor, false);
351     }
352
353     public static void findOccurrenceAtDotAndKillList()
354     {
355         final Editor editor = Editor.currentEditor();
356         final Buffer buffer = editor.getBuffer();
357         if (buffer instanceof ListOccurrences)
358             ((ListOccurrences)buffer).findOccurrenceAtDot(editor, true);
359     }
360
361     public static void mouseFindOccurrence()
362     {
363         final Editor editor = Editor.currentEditor();
364         final Buffer buffer = editor.getBuffer();
365         if (buffer instanceof ListOccurrences) {
366             AWTEvent JavaDoc e = editor.getDispatcher().getLastEvent();
367             if (e instanceof MouseEvent JavaDoc) {
368                 editor.mouseMoveDotToPoint((MouseEvent JavaDoc)e);
369                 ((ListOccurrences)buffer).findOccurrenceAtDot(editor, false);
370             }
371         }
372     }
373 }
374
Popular Tags