1 21 22 package org.armedbear.j; 23 24 import java.awt.event.KeyEvent ; 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 public String getContextString(Editor editor, boolean verbose ) 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 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 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 |