01: /*
02: * BinaryMode.java
03: *
04: * Copyright (C) 1998-2002 Peter Graves
05: * $Id: BinaryMode.java,v 1.1.1.1 2002/09/24 16:08:29 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: import java.awt.event.KeyEvent;
25:
26: public final class BinaryMode extends AbstractMode implements
27: Constants, Mode {
28: private static final BinaryMode mode = new BinaryMode();
29:
30: private BinaryMode() {
31: super (BINARY_MODE, BINARY_MODE_NAME);
32: setProperty(Property.VERTICAL_RULE, 0);
33: setProperty(Property.SHOW_LINE_NUMBERS, false);
34: }
35:
36: public static final BinaryMode getMode() {
37: return mode;
38: }
39:
40: protected final void setKeyMapDefaults(KeyMap km) {
41: km.mapKey(KeyEvent.VK_B, CTRL_MASK | ALT_MASK, "defaultMode");
42: }
43:
44: // For the status bar.
45: public String getContextString(Editor editor, boolean verbose /*ignored*/) {
46: if (editor.getMode() instanceof BinaryMode) {
47: final Line dotLine = editor.getDotLine();
48: int col = editor.getDisplay().getCaretCol();
49: int offset = -1;
50: int limit = editor.getDotLine().length() - 58;
51: if (col >= 10 && col < 58)
52: offset = (col - 10) / 3;
53: else if (col >= 58 && col < dotLine.length())
54: offset = col - 58;
55: if (offset >= 0 && offset < limit) {
56: FastStringBuffer sb = new FastStringBuffer("0x");
57: sb.append(dotLine.substring(0, 7));
58: sb.append(Integer.toHexString(offset));
59: int index = 10 + offset * 3;
60: String hex = dotLine.substring(index, index + 2);
61: sb.append(" 0x");
62: sb.append(hex);
63: char c = (char) Integer.parseInt(hex, 16);
64: if (c >= ' ' && c < 0x7f) {
65: sb.append(" '");
66: if (c == '\'')
67: sb.append('\\');
68: sb.append(c);
69: sb.append('\'');
70: }
71: return sb.toString();
72: }
73: }
74: return null;
75: }
76:
77: public static void binaryMode() {
78: final Editor editor = Editor.currentEditor();
79: final Buffer buffer = editor.getBuffer();
80: if (buffer.isModified()) {
81: String prompt = "Buffer will be reloaded in binary mode; discard changes?";
82: if (!editor.confirm(buffer.getFile().getName(), prompt))
83: return;
84: }
85: editor.setWaitCursor();
86: buffer.changeMode(mode);
87: editor.setDefaultCursor();
88: }
89: }
|