1 21 22 package org.armedbear.j; 23 24 public final class JumpCommands implements Constants 25 { 26 public static void jumpToLine() 27 { 28 final Editor editor = Editor.currentEditor(); 29 String response = InputDialog.showInputDialog(editor, "Line number:", 30 "Jump To Line"); 31 if (response == null || response.length() == 0) 32 return; 33 try { 34 final int here = editor.getDotLineNumber() + 1; 35 int lineNumber = parseNumericInput(response, here) - 1; 36 editor.jumpToLine(lineNumber); 37 } 38 catch (NumberFormatException e) { 39 MessageDialog.showMessageDialog(editor, "Invalid line number", "Error"); 40 } 41 } 42 43 public static void jumpToColumn() 44 { 45 final Editor editor = Editor.currentEditor(); 46 String response = InputDialog.showInputDialog(editor, "Column number:", 47 "Jump To Column"); 48 if (response == null || response.length() == 0) 49 return; 50 try { 51 final int here = editor.getDotCol() + 1; 52 int col = parseNumericInput(response, here) - 1; 53 final Display display = editor.getDisplay(); 54 if (col >= 0 && col != display.getAbsoluteCaretCol()) { 55 editor.addUndo(SimpleEdit.MOVE); 56 editor.unmark(); 57 display.setCaretCol(col - display.getShift()); 58 editor.moveDotToCaretCol(); 59 display.setUpdateFlag(REFRAME); 60 } 61 } 62 catch (NumberFormatException e) { 63 MessageDialog.showMessageDialog(editor, "Invalid column number", "Error"); 64 } 65 } 66 67 public static void jumpToOffset() 68 { 69 final Editor editor = Editor.currentEditor(); 70 String response = InputDialog.showInputDialog(editor, "Offset:", 71 "Jump To Offset"); 72 if (response == null || response.length() == 0) 73 return; 74 try { 75 final Buffer buffer = editor.getBuffer(); 76 int here = buffer.getAbsoluteOffset(editor.getDot()); 78 int offset = parseNumericInput(response, here); 79 Position pos = buffer.getPosition(offset); 80 if (pos != null) { 81 editor.moveDotTo(pos); 82 return; 83 } 84 } 85 catch (NumberFormatException e) { 86 MessageDialog.showMessageDialog(editor, "Invalid offset", "Error"); 87 } 88 } 89 90 private static int parseNumericInput(String s, int here) throws NumberFormatException  91 { 92 s = s.trim(); 93 if (s.length() == 0) 94 throw new NumberFormatException (); 95 char c = s.charAt(0); 96 if (c == '+' || c == '-') { 97 int offset = Integer.parseInt(s.substring(1).trim()); 99 return c == '+' ? here + offset : here - offset; 100 } 101 return Integer.parseInt(s); 102 } 103 } 104 | Popular Tags |