01: /*
02: * JumpCommands.java
03: *
04: * Copyright (C) 1998-2003 Peter Graves
05: * $Id: JumpCommands.java,v 1.2 2003/07/01 12:44:43 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * 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: public final class JumpCommands implements Constants {
25: public static void jumpToLine() {
26: final Editor editor = Editor.currentEditor();
27: String response = InputDialog.showInputDialog(editor,
28: "Line number:", "Jump To Line");
29: if (response == null || response.length() == 0)
30: return;
31: try {
32: final int here = editor.getDotLineNumber() + 1;
33: int lineNumber = parseNumericInput(response, here) - 1;
34: editor.jumpToLine(lineNumber);
35: } catch (NumberFormatException e) {
36: MessageDialog.showMessageDialog(editor,
37: "Invalid line number", "Error");
38: }
39: }
40:
41: public static void jumpToColumn() {
42: final Editor editor = Editor.currentEditor();
43: String response = InputDialog.showInputDialog(editor,
44: "Column number:", "Jump To Column");
45: if (response == null || response.length() == 0)
46: return;
47: try {
48: final int here = editor.getDotCol() + 1;
49: int col = parseNumericInput(response, here) - 1;
50: final Display display = editor.getDisplay();
51: if (col >= 0 && col != display.getAbsoluteCaretCol()) {
52: editor.addUndo(SimpleEdit.MOVE);
53: editor.unmark();
54: display.setCaretCol(col - display.getShift());
55: editor.moveDotToCaretCol();
56: display.setUpdateFlag(REFRAME);
57: }
58: } catch (NumberFormatException e) {
59: MessageDialog.showMessageDialog(editor,
60: "Invalid column number", "Error");
61: }
62: }
63:
64: public static void jumpToOffset() {
65: final Editor editor = Editor.currentEditor();
66: String response = InputDialog.showInputDialog(editor,
67: "Offset:", "Jump To Offset");
68: if (response == null || response.length() == 0)
69: return;
70: try {
71: final Buffer buffer = editor.getBuffer();
72: // Zero-based.
73: int here = buffer.getAbsoluteOffset(editor.getDot());
74: int offset = parseNumericInput(response, here);
75: Position pos = buffer.getPosition(offset);
76: if (pos != null) {
77: editor.moveDotTo(pos);
78: return;
79: }
80: } catch (NumberFormatException e) {
81: MessageDialog.showMessageDialog(editor, "Invalid offset",
82: "Error");
83: }
84: }
85:
86: private static int parseNumericInput(String s, int here)
87: throws NumberFormatException {
88: s = s.trim();
89: if (s.length() == 0)
90: throw new NumberFormatException();
91: char c = s.charAt(0);
92: if (c == '+' || c == '-') {
93: // It's an offset from the current location.
94: int offset = Integer.parseInt(s.substring(1).trim());
95: return c == '+' ? here + offset : here - offset;
96: }
97: return Integer.parseInt(s);
98: }
99: }
|