001: /*
002: * WebMode.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: WebMode.java,v 1.3 2003/01/18 17:46:52 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.event.KeyEvent;
025: import java.util.StringTokenizer;
026:
027: public final class WebMode extends AbstractMode implements Constants,
028: Mode {
029: private static final WebMode mode = new WebMode();
030:
031: private WebMode() {
032: super (WEB_MODE, WEB_MODE_NAME);
033: setProperty(Property.VERTICAL_RULE, 0);
034: setProperty(Property.SHOW_LINE_NUMBERS, false);
035: setProperty(Property.HIGHLIGHT_MATCHING_BRACKET, false);
036: setProperty(Property.HIGHLIGHT_BRACKETS, false);
037: setProperty(Property.P4_AUTO_EDIT, false);
038: }
039:
040: public static final WebMode getMode() {
041: return mode;
042: }
043:
044: protected void setKeyMapDefaults(KeyMap km) {
045: km.mapKey(VK_MOUSE_1, 0, "mouseFollowLink");
046: km.mapKey(KeyEvent.VK_ENTER, 0, "followLink");
047: km.mapKey(KeyEvent.VK_G, CTRL_MASK | SHIFT_MASK, "followLink");
048: km.mapKey('s', "viewSource");
049: km.mapKey(KeyEvent.VK_BACK_SPACE, 0, "webBack");
050: km.mapKey(KeyEvent.VK_B, 0, "webBack");
051: km.mapKey(KeyEvent.VK_F, 0, "webForward");
052: km.mapKey(KeyEvent.VK_R, 0, "webReload");
053: }
054:
055: public void populateMenu(Editor editor, Menu menu) {
056: final String text = menu.getText();
057: if (text == "File") {
058: menu.add(editor, "New", 'N', "newBuffer");
059: menu.add(editor, "Open...", 'O', "openFile");
060: menu.add(editor, "Recent Files...", 'R', "recentFiles");
061: menu.addSeparator();
062: menu.add(editor, "Save As...", 'E', "saveAs");
063: menu.add(editor, "Save a Copy...", 'Y', "saveCopy");
064: menu.add(editor, "Save All", 'A', "saveAll");
065: menu.add(editor, "Close", 'C', "killBuffer");
066: menu.add(editor, "Close All", 'L', "closeAll");
067: menu.add(editor, "Close Others", 'H', "closeOthers");
068: menu.addSeparator();
069: menu.add(editor, "Properties", 'I', "properties");
070: menu.addSeparator();
071: menu.add(editor, "Next Buffer", 'T', "nextBuffer");
072: menu.add(editor, "Previous Buffer", 'R', "prevBuffer");
073: menu.addSeparator();
074: menu.add(editor, "New Frame", 'M', "newFrame");
075: menu.add(editor, "Execute Command...", 'D',
076: "executeCommand");
077: menu.addSeparator();
078: menu.add(editor, "Print...", 'P', "print");
079: menu.addSeparator();
080: menu.add(editor, "Save All/Exit", '/', "saveAllExit");
081: menu.add(editor, "Exit", 'X', "quit");
082: } else if (text == "Edit") {
083: menu.add(editor, "Copy", 'C', "copyRegion");
084: menu.add(editor, "Copy Append", 'D', "copyAppend");
085: } else if (text == "Search") {
086: menu.add(editor, "Find...", 'F', "find");
087: menu.add(editor, "Find Next", 'T', "findNext");
088: menu.add(editor, "Find Previous", 'R', "findPrev");
089: } else if (text == "Go") {
090: menu.add(editor, "Go Back", 'B', "webBack");
091: menu.add(editor, "Go Forward", 'F', "webForward");
092: menu.addSeparator();
093: menu.add(editor, "Go To Next Occurrence of Word", 'T',
094: "findNextWord");
095: menu.add(editor, "Go To Previous Occurrence of Word", 'R',
096: "findPrevWord");
097: } else
098: super .populateMenu(editor, menu);
099: }
100:
101: public final Formatter getFormatter(Buffer buffer) {
102: return new WebFormatter(buffer);
103: }
104:
105: protected ToolBar getDefaultToolBar(Frame frame) {
106: return new WebModeToolBar(frame);
107: }
108:
109: public final String getContextString(Editor editor, boolean verbose /*ignored*/) {
110: return getContextString(editor.getDot());
111: }
112:
113: public final String getMouseMovedContextString(Editor editor,
114: Position pos) {
115: // We want to clear the status text if the mouse is not over a link, so
116: // return "" instead of null.
117: final String s = getContextString(pos);
118: return s != null ? s : "";
119: }
120:
121: private String getContextString(Position pos) {
122: if (pos != null && pos.getLine() instanceof WebLine) {
123: HtmlLineSegment segment = ((WebLine) pos.getLine())
124: .findSegment(pos.getOffset());
125: if (segment != null) {
126: Link link = segment.getLink();
127: if (link != null)
128: return link.getTarget();
129: }
130: }
131: return null;
132: }
133:
134: public static void google() {
135: final Editor editor = Editor.currentEditor();
136: InputDialog d = new InputDialog(editor, "Search for:",
137: "Google Search", null);
138: d.setHistory(new History("google.search"));
139: editor.centerDialog(d);
140: d.show();
141: String s = d.getInput();
142: if (s == null || s.length() == 0)
143: return;
144: editor.repaintNow();
145: google(s);
146: }
147:
148: public static void google(String s) {
149: query("http://www.google.com/search?q=", s);
150: }
151:
152: public static void query(String prefix, String s) {
153: s = s.trim();
154: // Strip enclosing quotes if any.
155: int length = s.length();
156: if (length > 1
157: && ((s.charAt(0) == '"' && s.charAt(length - 1) == '"') || (s
158: .charAt(0) == '\'' && s.charAt(length - 1) == '\''))) {
159: s = s.substring(1, length - 1).trim();
160: }
161: FastStringBuffer sb = new FastStringBuffer(prefix);
162: StringTokenizer st = new StringTokenizer(s);
163: final int count = st.countTokens();
164: for (int i = 0; i < count; i++) {
165: if (i != 0)
166: sb.append('+');
167: sb.append(st.nextToken());
168: }
169: HttpFile file = HttpFile.getHttpFile(sb.toString());
170: if (file != null) {
171: Buffer buf = null;
172: // Look for existing buffer.
173: for (BufferIterator it = new BufferIterator(); it.hasNext();) {
174: Buffer b = it.nextBuffer();
175: if (b instanceof WebBuffer && b.getFile().equals(file)) {
176: buf = b;
177: break;
178: }
179: }
180: if (buf == null)
181: buf = WebBuffer.createWebBuffer(file, null, null);
182: if (buf != null) {
183: final Editor editor = Editor.currentEditor();
184: editor.makeNext(buf);
185: editor.activate(buf);
186: }
187: }
188: }
189: }
|