001: /*
002: * Marker.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: Marker.java,v 1.5 2003/08/07 17:56:06 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.util.ArrayList;
025: import java.util.List;
026:
027: public final class Marker implements Constants {
028: private Buffer buffer;
029: private Position pos;
030: private final File file;
031: private int lineNumber;
032: private int offset;
033:
034: public Marker(Buffer buffer, Position pos) {
035: this .buffer = buffer;
036: this .pos = new Position(pos);
037: file = buffer.getFile();
038: lineNumber = pos.lineNumber();
039: offset = pos.getOffset();
040: }
041:
042: public Buffer getBuffer() {
043: return buffer;
044: }
045:
046: // Returns an alias, not a copy.
047: public Position getPosition() {
048: return pos;
049: }
050:
051: public void setPosition(Position pos) {
052: this .pos = new Position(pos);
053: }
054:
055: public Line getLine() {
056: return pos != null ? pos.getLine() : null;
057: }
058:
059: public void invalidate() {
060: if (pos != null) {
061: lineNumber = pos.lineNumber();
062: offset = pos.getOffset();
063: }
064: pos = null;
065: buffer = null;
066: }
067:
068: public void gotoMarker(Editor editor) {
069: if (file == null)
070: return;
071: if (file.equals(editor.getBuffer().getFile())) {
072: // Marker is in current buffer.
073: editor.addUndo(SimpleEdit.MOVE);
074: editor.unmark();
075: editor.updateDotLine();
076: if (pos != null
077: && editor.getBuffer().contains(pos.getLine())) {
078: editor.getDot().moveTo(pos);
079: } else {
080: editor.gotoline(lineNumber);
081: editor.getDot().setOffset(offset);
082: }
083: if (editor.getDotOffset() > editor.getDotLine().length())
084: editor.getDot().setOffset(editor.getDotLine().length());
085: editor.moveCaretToDotCol();
086: editor.updateDotLine();
087: editor.setUpdateFlag(REFRAME);
088: } else {
089: // Marker is not in current buffer.
090: Buffer buf = Editor.getBufferList().findBuffer(file);
091: if (buf != null) {
092: editor.makeNext(buf);
093: editor.activate(buf);
094: editor.addUndo(SimpleEdit.MOVE);
095: editor.updateDotLine();
096: if (pos != null && buf.contains(pos.getLine())) {
097: editor.getDot().moveTo(pos);
098: } else {
099: editor.gotoline(lineNumber);
100: editor.getDot().setOffset(offset);
101: }
102: if (editor.getDotOffset() > editor.getDotLine()
103: .length())
104: editor.getDot().setOffset(
105: editor.getDotLine().length());
106: editor.moveCaretToDotCol();
107: editor.updateDotLine();
108: } else {
109: buf = Buffer.createBuffer(file);
110: editor.makeNext(buf);
111: editor.activate(buf);
112: editor.gotoline(lineNumber);
113: editor.getDot().setOffset(offset);
114: if (editor.getDotOffset() > editor.getDotLine()
115: .length())
116: editor.getDot().setOffset(
117: editor.getDotLine().length());
118: editor.moveCaretToDotCol();
119: }
120: }
121: pos = new Position(editor.getDot());
122: buffer = editor.getBuffer();
123: }
124:
125: public static void selectToMarker() {
126: selectToMarker(InputDialog.showInputDialog(Editor
127: .currentEditor(), "Marker:", "Select To Marker"));
128: }
129:
130: public static void selectToTemporaryMarker() {
131: selectToMarker("10");
132: }
133:
134: public static void selectToMarker(String s) {
135: if (s == null)
136: return;
137: s = s.trim();
138: if (s.length() == 0)
139: return;
140: final Editor editor = Editor.currentEditor();
141: Marker m = null;
142: try {
143: final int index = Integer.parseInt(s);
144: final Marker[] bookmarks = Editor.getBookmarks();
145: if (index >= 0 && index < bookmarks.length)
146: m = bookmarks[index];
147: } catch (NumberFormatException e) {
148: }
149: if (m == null) {
150: MessageDialog.showMessageDialog(editor, "No such marker",
151: "Select To Marker");
152: return;
153: }
154: m.selectToMarker(editor);
155: }
156:
157: private void selectToMarker(Editor editor) {
158: if (file == null)
159: return;
160: if (file.equals(editor.getBuffer().getFile())) {
161: // Marker is in current buffer.
162: editor.addUndo(SimpleEdit.MOVE);
163: editor.unmark();
164: editor.setMarkAtDot();
165: editor.updateDotLine();
166: if (pos != null
167: && editor.getBuffer().contains(pos.getLine())) {
168: editor.getDot().moveTo(pos);
169: } else {
170: editor.gotoline(lineNumber);
171: editor.getDot().setOffset(offset);
172: }
173: if (editor.getDotOffset() > editor.getDotLine().length())
174: editor.getDot().setOffset(editor.getDotLine().length());
175: editor.moveCaretToDotCol();
176: editor.updateDotLine();
177: editor.setUpdateFlag(REFRAME | REPAINT);
178: } else {
179: // Marker is not in current buffer.
180: MessageDialog.showMessageDialog(editor,
181: "Marker is not in this buffer", "Select To Marker");
182: }
183: }
184:
185: public boolean equals(Object object) {
186: if (this == object)
187: return true;
188: if (object instanceof Marker) {
189: Marker m = (Marker) object;
190: if (buffer != null && buffer == m.buffer)
191: if (pos != null && pos.equals(m.pos))
192: return true;
193: if (file != null && file.equals(m.file))
194: if (lineNumber == m.lineNumber && offset == m.offset)
195: return true;
196: }
197: return false;
198: }
199:
200: public static void invalidateAllMarkers() {
201: List markers = getAllMarkers();
202: for (int i = markers.size(); i-- > 0;) {
203: Marker m = (Marker) markers.get(i);
204: if (m != null)
205: m.invalidate();
206: }
207: }
208:
209: public static void invalidateMarkers(Buffer buf) {
210: List markers = getAllMarkers();
211: for (int i = markers.size(); i-- > 0;) {
212: Marker m = (Marker) markers.get(i);
213: if (m != null && m.getBuffer() == buf)
214: m.invalidate();
215: }
216: }
217:
218: public static List getAllMarkers() {
219: Marker[] bookmarks = Editor.getBookmarks();
220: List positionStack = Editor.getPositionStack();
221: ArrayList list = new ArrayList(bookmarks.length
222: + positionStack.size());
223: for (int i = bookmarks.length; i-- > 0;) {
224: Marker m = bookmarks[i];
225: if (m != null)
226: list.add(m);
227: }
228: for (int i = positionStack.size(); i-- > 0;)
229: list.add(positionStack.get(i));
230: return list;
231: }
232: }
|