KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ManMode.java
3  *
4  * Copyright (C) 2000-2004 Peter Graves
5  * $Id: ManMode.java,v 1.4 2004/04/01 18:50:03 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.AWTEvent JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26 import java.awt.event.MouseEvent JavaDoc;
27 import javax.swing.JPopupMenu JavaDoc;
28
29 public final class ManMode extends AbstractMode implements Constants, Mode
30 {
31     private static final ManMode mode = new ManMode();
32
33     private ManMode()
34     {
35         super(MAN_MODE, MAN_MODE_NAME);
36         setProperty(Property.VERTICAL_RULE, 0);
37         setProperty(Property.SHOW_LINE_NUMBERS, false);
38         setProperty(Property.HIGHLIGHT_MATCHING_BRACKET, false);
39         setProperty(Property.HIGHLIGHT_BRACKETS, false);
40     }
41
42     public static final ManMode getMode()
43     {
44         return mode;
45     }
46
47     public JPopupMenu JavaDoc getContextMenu(Editor editor)
48     {
49         return null;
50     }
51
52     public Formatter getFormatter(Buffer buffer)
53     {
54         if (buffer.getType() != Buffer.TYPE_MAN)
55             return null;
56         return new ManFormatter(buffer, ((Man)buffer).isApropos());
57     }
58
59     protected void setKeyMapDefaults(KeyMap km)
60     {
61         km.mapKey(KeyEvent.VK_ENTER, 0, "manFollowLink");
62         km.mapKey(KeyEvent.VK_G, CTRL_MASK | SHIFT_MASK, "manFollowLink");
63         km.mapKey(VK_DOUBLE_MOUSE_1, 0, "manFollowLink");
64         km.mapKey(VK_MOUSE_2, 0, "manFollowLink");
65     }
66
67     private static void followLink(Editor editor)
68     {
69         if (editor.getBuffer().getType() == Buffer.TYPE_MAN) {
70             final Man man = (Man) editor.getBuffer();
71             final Line line = editor.getDotLine();
72             if (man.isApropos()) {
73                 String JavaDoc topic = null;
74                 String JavaDoc section = null;
75                 String JavaDoc text = line.getText();
76                 int index = text.indexOf(' ');
77                 if (index >= 0) {
78                     topic = text.substring(0, index);
79                     text = text.substring(index);
80                     index = text.indexOf('(');
81                     if (index >= 0) {
82                         int begin = index + 1;
83                         int end = text.indexOf(')', begin);
84                         if (end >= 0)
85                             section = text.substring(begin, end);
86                     }
87                 }
88                 if (topic != null) {
89                     if (section != null && section.length() > 0)
90                         man(editor, section + " " + topic);
91                     else
92                         man(editor, topic);
93                 }
94             } else {
95                 LineSegmentList segmentList = man.getFormatter().formatLine(line);
96                 int col = editor.getDotCol();
97                 int startCol = 0;
98                 for (int i = 0; i < segmentList.size(); i++) {
99                     LineSegment segment = segmentList.getSegment(i);
100                     if (startCol <= col && col < startCol + segment.length()) {
101                         String JavaDoc s;
102                         if (segment.getFormat() != 0) {
103                             // Segment is highlighted. Use the whole segment.
104
s = segment.getText();
105                         } else {
106                             // Not highlighted.
107
s = editor.getFilenameAtDot();
108                         }
109                         if (s != null) {
110                             if (s.startsWith("/")) {
111                                 // Looks like a Unix filename.
112
Buffer buf = editor.openFile(File.getInstance(s));
113                                 if (buf != null) {
114                                     editor.makeNext(buf);
115                                     editor.activate(buf);
116                                 }
117                             } else
118                                 man(editor, s);
119                         }
120                         break;
121                     }
122                     startCol += segment.length();
123                 }
124             }
125         }
126     }
127
128     public static String JavaDoc getTitle(String JavaDoc topic)
129     {
130         return "man " + topic;
131     }
132
133     public static void man()
134     {
135         if (!Platform.isPlatformUnix())
136             return;
137         final Editor editor = Editor.currentEditor();
138         ManDialog d = new ManDialog(editor);
139         editor.centerDialog(d);
140         d.show();
141         String JavaDoc topic = d.getInput();
142         if (topic != null && topic.length() != 0)
143             man(topic);
144     }
145
146     public static void man(String JavaDoc topic)
147     {
148         if (!Platform.isPlatformUnix())
149             return;
150         man(Editor.currentEditor(), topic);
151     }
152
153     private static void man(Editor editor, String JavaDoc topic)
154     {
155         editor.setWaitCursor();
156         try {
157             final String JavaDoc title = ManMode.getTitle(topic);
158             for (BufferIterator it = new BufferIterator(); it.hasNext();) {
159                 Buffer buf = it.nextBuffer();
160                 if (buf.getModeId() == MAN_MODE && title.equals(buf.getTitle())) {
161                     editor.makeNext(buf);
162                     editor.switchToBuffer(buf);
163                     return;
164                 }
165             }
166             File tempFile = Utilities.getTempFile();
167             String JavaDoc cmd = "man " + topic + " > " + tempFile.canonicalPath();
168             String JavaDoc[] cmdarray = {"/bin/sh", "-c", cmd};
169             try {
170                 Process JavaDoc process = Runtime.getRuntime().exec(cmdarray);
171                 process.waitFor();
172             }
173             catch (Exception JavaDoc e) {
174                 Log.error(e);
175             }
176             if (tempFile.isFile() && tempFile.length() > 0) {
177                 Man man = new Man(topic, tempFile);
178                 man.load();
179                 tempFile.delete();
180                 editor.makeNext(man);
181                 editor.switchToBuffer(man);
182             } else
183                 editor.status("No entry");
184         }
185         finally {
186             editor.setDefaultCursor();
187         }
188     }
189
190     public static void manFollowLink()
191     {
192         final Editor editor = Editor.currentEditor();
193         final Buffer buffer = editor.getBuffer();
194         if (buffer.getType() != Buffer.TYPE_MAN)
195             return;
196         // If this method is invoked via a mouse event mapping, move dot to
197
// location of mouse click before following link.
198
AWTEvent JavaDoc e = editor.getDispatcher().getLastEvent();
199         if (e instanceof MouseEvent JavaDoc)
200             editor.mouseMoveDotToPoint((MouseEvent JavaDoc)e);
201         followLink(editor);
202     }
203 }
204
Popular Tags