KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Marker.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: Marker.java,v 1.5 2003/08/07 17:56:06 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.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 public final class Marker implements Constants
28 {
29     private Buffer buffer;
30     private Position pos;
31     private final File file;
32     private int lineNumber;
33     private int offset;
34
35     public Marker(Buffer buffer, Position pos )
36     {
37         this.buffer = buffer;
38         this.pos = new Position(pos);
39         file = buffer.getFile();
40         lineNumber = pos.lineNumber();
41         offset = pos.getOffset();
42     }
43
44     public Buffer getBuffer()
45     {
46         return buffer;
47     }
48
49     // Returns an alias, not a copy.
50
public Position getPosition()
51     {
52         return pos;
53     }
54
55     public void setPosition(Position pos)
56     {
57         this.pos = new Position(pos);
58     }
59
60     public Line getLine()
61     {
62         return pos != null ? pos.getLine() : null;
63     }
64
65     public void invalidate()
66     {
67         if (pos != null) {
68             lineNumber = pos.lineNumber();
69             offset = pos.getOffset();
70         }
71         pos = null;
72         buffer = null;
73     }
74
75     public void gotoMarker(Editor editor)
76     {
77         if (file == null)
78             return;
79         if (file.equals(editor.getBuffer().getFile())) {
80             // Marker is in current buffer.
81
editor.addUndo(SimpleEdit.MOVE);
82             editor.unmark();
83             editor.updateDotLine();
84             if (pos != null && editor.getBuffer().contains(pos.getLine())) {
85                 editor.getDot().moveTo(pos);
86             } else {
87                 editor.gotoline(lineNumber);
88                 editor.getDot().setOffset(offset);
89             }
90             if (editor.getDotOffset() > editor.getDotLine().length())
91                 editor.getDot().setOffset(editor.getDotLine().length());
92             editor.moveCaretToDotCol();
93             editor.updateDotLine();
94             editor.setUpdateFlag(REFRAME);
95         } else {
96             // Marker is not in current buffer.
97
Buffer buf = Editor.getBufferList().findBuffer(file);
98             if (buf != null) {
99                 editor.makeNext(buf);
100                 editor.activate(buf);
101                 editor.addUndo(SimpleEdit.MOVE);
102                 editor.updateDotLine();
103                 if (pos != null && buf.contains(pos.getLine())) {
104                     editor.getDot().moveTo(pos);
105                 } else {
106                     editor.gotoline(lineNumber);
107                     editor.getDot().setOffset(offset);
108                 }
109                 if (editor.getDotOffset() > editor.getDotLine().length())
110                     editor.getDot().setOffset(editor.getDotLine().length());
111                 editor.moveCaretToDotCol();
112                 editor.updateDotLine();
113             } else {
114                 buf = Buffer.createBuffer(file);
115                 editor.makeNext(buf);
116                 editor.activate(buf);
117                 editor.gotoline(lineNumber);
118                 editor.getDot().setOffset(offset);
119                 if (editor.getDotOffset() > editor.getDotLine().length())
120                     editor.getDot().setOffset(editor.getDotLine().length());
121                 editor.moveCaretToDotCol();
122             }
123         }
124         pos = new Position(editor.getDot());
125         buffer = editor.getBuffer();
126     }
127
128     public static void selectToMarker()
129     {
130         selectToMarker(InputDialog.showInputDialog(Editor.currentEditor(),
131                                                    "Marker:",
132                                                    "Select To Marker"));
133     }
134
135     public static void selectToTemporaryMarker()
136     {
137         selectToMarker("10");
138     }
139
140     public static void selectToMarker(String JavaDoc s)
141     {
142         if (s == null)
143             return;
144         s = s.trim();
145         if (s.length() == 0)
146             return;
147         final Editor editor = Editor.currentEditor();
148         Marker m = null;
149         try {
150             final int index = Integer.parseInt(s);
151             final Marker[] bookmarks = Editor.getBookmarks();
152             if (index >= 0 && index < bookmarks.length)
153                 m = bookmarks[index];
154         }
155         catch (NumberFormatException JavaDoc e) {}
156         if (m == null) {
157             MessageDialog.showMessageDialog(editor, "No such marker",
158                                             "Select To Marker");
159             return;
160         }
161         m.selectToMarker(editor);
162     }
163
164     private void selectToMarker(Editor editor)
165     {
166         if (file == null)
167             return;
168         if (file.equals(editor.getBuffer().getFile())) {
169             // Marker is in current buffer.
170
editor.addUndo(SimpleEdit.MOVE);
171             editor.unmark();
172             editor.setMarkAtDot();
173             editor.updateDotLine();
174             if (pos != null && editor.getBuffer().contains(pos.getLine())) {
175                 editor.getDot().moveTo(pos);
176             } else {
177                 editor.gotoline(lineNumber);
178                 editor.getDot().setOffset(offset);
179             }
180             if (editor.getDotOffset() > editor.getDotLine().length())
181                 editor.getDot().setOffset(editor.getDotLine().length());
182             editor.moveCaretToDotCol();
183             editor.updateDotLine();
184             editor.setUpdateFlag(REFRAME | REPAINT);
185         } else {
186             // Marker is not in current buffer.
187
MessageDialog.showMessageDialog(editor,
188                                             "Marker is not in this buffer",
189                                             "Select To Marker");
190         }
191     }
192
193     public boolean equals(Object JavaDoc object)
194     {
195         if (this == object)
196             return true;
197         if (object instanceof Marker) {
198             Marker m = (Marker) object;
199             if (buffer != null && buffer == m.buffer)
200                 if (pos != null && pos.equals(m.pos))
201                     return true;
202             if (file != null && file.equals(m.file))
203                 if (lineNumber == m.lineNumber && offset == m.offset)
204                     return true;
205         }
206         return false;
207     }
208
209     public static void invalidateAllMarkers()
210     {
211         List JavaDoc markers = getAllMarkers();
212         for (int i = markers.size(); i-- > 0;) {
213             Marker m = (Marker) markers.get(i);
214             if (m != null)
215                 m.invalidate();
216         }
217     }
218
219     public static void invalidateMarkers(Buffer buf)
220     {
221         List JavaDoc markers = getAllMarkers();
222         for (int i = markers.size(); i-- > 0;) {
223             Marker m = (Marker) markers.get(i);
224             if (m != null && m.getBuffer() == buf)
225                 m.invalidate();
226         }
227     }
228
229     public static List JavaDoc getAllMarkers()
230     {
231         Marker[] bookmarks = Editor.getBookmarks();
232         List JavaDoc positionStack = Editor.getPositionStack();
233         ArrayList JavaDoc list = new ArrayList JavaDoc(bookmarks.length + positionStack.size());
234         for (int i = bookmarks.length; i-- > 0;) {
235             Marker m = bookmarks[i];
236             if (m != null)
237                 list.add(m);
238         }
239         for (int i = positionStack.size(); i-- > 0;)
240             list.add(positionStack.get(i));
241         return list;
242     }
243 }
244
Popular Tags