1 21 22 package org.armedbear.j; 23 24 import java.awt.event.KeyEvent ; 25 import java.util.StringTokenizer ; 26 27 public final class WebMode extends AbstractMode implements Constants, Mode 28 { 29 private static final WebMode mode = new WebMode(); 30 31 private WebMode() 32 { 33 super(WEB_MODE, WEB_MODE_NAME); 34 setProperty(Property.VERTICAL_RULE, 0); 35 setProperty(Property.SHOW_LINE_NUMBERS, false); 36 setProperty(Property.HIGHLIGHT_MATCHING_BRACKET, false); 37 setProperty(Property.HIGHLIGHT_BRACKETS, false); 38 setProperty(Property.P4_AUTO_EDIT, false); 39 } 40 41 public static final WebMode getMode() 42 { 43 return mode; 44 } 45 46 protected void setKeyMapDefaults(KeyMap km) 47 { 48 km.mapKey(VK_MOUSE_1, 0, "mouseFollowLink"); 49 km.mapKey(KeyEvent.VK_ENTER, 0, "followLink"); 50 km.mapKey(KeyEvent.VK_G, CTRL_MASK | SHIFT_MASK, "followLink"); 51 km.mapKey('s', "viewSource"); 52 km.mapKey(KeyEvent.VK_BACK_SPACE, 0, "webBack"); 53 km.mapKey(KeyEvent.VK_B, 0, "webBack"); 54 km.mapKey(KeyEvent.VK_F, 0, "webForward"); 55 km.mapKey(KeyEvent.VK_R, 0, "webReload"); 56 } 57 58 public void populateMenu(Editor editor, Menu menu) 59 { 60 final String text = menu.getText(); 61 if (text == "File") { 62 menu.add(editor, "New", 'N', "newBuffer"); 63 menu.add(editor, "Open...", 'O', "openFile"); 64 menu.add(editor, "Recent Files...", 'R', "recentFiles"); 65 menu.addSeparator(); 66 menu.add(editor, "Save As...", 'E', "saveAs"); 67 menu.add(editor, "Save a Copy...", 'Y', "saveCopy"); 68 menu.add(editor, "Save All", 'A', "saveAll"); 69 menu.add(editor, "Close", 'C', "killBuffer"); 70 menu.add(editor, "Close All", 'L', "closeAll"); 71 menu.add(editor, "Close Others", 'H', "closeOthers"); 72 menu.addSeparator(); 73 menu.add(editor, "Properties", 'I', "properties"); 74 menu.addSeparator(); 75 menu.add(editor, "Next Buffer", 'T', "nextBuffer"); 76 menu.add(editor, "Previous Buffer", 'R', "prevBuffer"); 77 menu.addSeparator(); 78 menu.add(editor, "New Frame", 'M', "newFrame"); 79 menu.add(editor, "Execute Command...", 'D', "executeCommand"); 80 menu.addSeparator(); 81 menu.add(editor, "Print...", 'P', "print"); 82 menu.addSeparator(); 83 menu.add(editor, "Save All/Exit", '/', "saveAllExit"); 84 menu.add(editor, "Exit", 'X', "quit"); 85 } else if (text == "Edit") { 86 menu.add(editor, "Copy", 'C', "copyRegion"); 87 menu.add(editor, "Copy Append", 'D', "copyAppend"); 88 } else if (text == "Search") { 89 menu.add(editor, "Find...", 'F', "find"); 90 menu.add(editor, "Find Next", 'T', "findNext"); 91 menu.add(editor, "Find Previous", 'R', "findPrev"); 92 } else if (text == "Go") { 93 menu.add(editor, "Go Back", 'B', "webBack"); 94 menu.add(editor, "Go Forward", 'F', "webForward"); 95 menu.addSeparator(); 96 menu.add(editor, "Go To Next Occurrence of Word", 'T', "findNextWord"); 97 menu.add(editor, "Go To Previous Occurrence of Word", 'R', "findPrevWord"); 98 } else 99 super.populateMenu(editor, menu); 100 } 101 102 public final Formatter getFormatter(Buffer buffer) 103 { 104 return new WebFormatter(buffer); 105 } 106 107 protected ToolBar getDefaultToolBar(Frame frame) 108 { 109 return new WebModeToolBar(frame); 110 } 111 112 public final String getContextString(Editor editor, boolean verbose ) 113 { 114 return getContextString(editor.getDot()); 115 } 116 117 public final String getMouseMovedContextString(Editor editor, Position pos) 118 { 119 final String s = getContextString(pos); 122 return s != null ? s : ""; 123 } 124 125 private String getContextString(Position pos) 126 { 127 if (pos != null && pos.getLine() instanceof WebLine) { 128 HtmlLineSegment segment = 129 ((WebLine)pos.getLine()).findSegment(pos.getOffset()); 130 if (segment != null) { 131 Link link = segment.getLink(); 132 if (link != null) 133 return link.getTarget(); 134 } 135 } 136 return null; 137 } 138 139 public static void google() 140 { 141 final Editor editor = Editor.currentEditor(); 142 InputDialog d = new InputDialog(editor, "Search for:", "Google Search", null); 143 d.setHistory(new History("google.search")); 144 editor.centerDialog(d); 145 d.show(); 146 String s = d.getInput(); 147 if (s == null || s.length() == 0) 148 return; 149 editor.repaintNow(); 150 google(s); 151 } 152 153 public static void google(String s) 154 { 155 query("http://www.google.com/search?q=", s); 156 } 157 158 public static void query(String prefix, String s) 159 { 160 s = s.trim(); 161 int length = s.length(); 163 if (length > 1 && 164 ((s.charAt(0) == '"' && s.charAt(length-1) == '"') || 165 (s.charAt(0) == '\'' && s.charAt(length-1) == '\''))) { 166 s = s.substring(1, length-1).trim(); 167 } 168 FastStringBuffer sb = new FastStringBuffer(prefix); 169 StringTokenizer st = new StringTokenizer (s); 170 final int count = st.countTokens(); 171 for (int i = 0; i < count; i++) { 172 if (i != 0) 173 sb.append('+'); 174 sb.append(st.nextToken()); 175 } 176 HttpFile file = HttpFile.getHttpFile(sb.toString()); 177 if (file != null) { 178 Buffer buf = null; 179 for (BufferIterator it = new BufferIterator(); it.hasNext();) { 181 Buffer b = it.nextBuffer(); 182 if (b instanceof WebBuffer && b.getFile().equals(file)) { 183 buf = b; 184 break; 185 } 186 } 187 if (buf == null) 188 buf = WebBuffer.createWebBuffer(file, null, null); 189 if (buf != null) { 190 final Editor editor = Editor.currentEditor(); 191 editor.makeNext(buf); 192 editor.activate(buf); 193 } 194 } 195 } 196 } 197 | Popular Tags |