KickJava   Java API By Example, From Geeks To Geeks.

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


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