KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * WebMode.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: WebMode.java,v 1.3 2003/01/18 17:46:52 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 import java.util.StringTokenizer JavaDoc;
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 JavaDoc 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 JavaDoc getContextString(Editor editor, boolean verbose /*ignored*/)
113     {
114         return getContextString(editor.getDot());
115     }
116
117     public final String JavaDoc getMouseMovedContextString(Editor editor, Position pos)
118     {
119         // We want to clear the status text if the mouse is not over a link, so
120
// return "" instead of null.
121
final String JavaDoc s = getContextString(pos);
122         return s != null ? s : "";
123     }
124
125     private String JavaDoc 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 JavaDoc 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 JavaDoc s)
154     {
155         query("http://www.google.com/search?q=", s);
156     }
157
158     public static void query(String JavaDoc prefix, String JavaDoc s)
159     {
160         s = s.trim();
161         // Strip enclosing quotes if any.
162
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 JavaDoc st = new StringTokenizer JavaDoc(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             // Look for existing buffer.
180
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