KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > help > HelpTOCPanel


1 /*
2  * HelpTOCPanel.java - Help table of contents
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1999, 2004 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.help;
24
25 //{{{ Imports
26
import javax.swing.*;
27 import javax.swing.border.*;
28 import javax.swing.tree.*;
29 import java.awt.*;
30 import java.awt.event.*;
31 import java.io.*;
32 import java.net.*;
33 import java.util.*;
34
35 import org.xml.sax.Attributes JavaDoc;
36 import org.xml.sax.helpers.DefaultHandler JavaDoc;
37
38 import org.gjt.sp.jedit.browser.FileCellRenderer; // for icons
39
import org.gjt.sp.jedit.io.VFSManager;
40 import org.gjt.sp.jedit.*;
41 import org.gjt.sp.util.Log;
42 import org.gjt.sp.util.StandardUtilities;
43 import org.gjt.sp.util.XMLUtilities;
44 //}}}
45

46 public class HelpTOCPanel extends JPanel
47 {
48     //{{{ HelpTOCPanel constructor
49
public HelpTOCPanel(HelpViewerInterface helpViewer)
50     {
51         super(new BorderLayout());
52
53         this.helpViewer = helpViewer;
54         nodes = new Hashtable();
55
56         toc = new TOCTree();
57
58         // looks bad with the OS X L&F, apparently...
59
if(!OperatingSystem.isMacOSLF())
60             toc.putClientProperty("JTree.lineStyle", "Angled");
61
62         toc.setCellRenderer(new TOCCellRenderer());
63         toc.setEditable(false);
64         toc.setShowsRootHandles(true);
65
66         add(BorderLayout.CENTER,new JScrollPane(toc));
67
68         load();
69     } //}}}
70

71     //{{{ selectNode() method
72
public void selectNode(String JavaDoc shortURL)
73     {
74         if(tocModel == null)
75             return;
76
77         DefaultMutableTreeNode node = (DefaultMutableTreeNode)nodes.get(shortURL);
78
79         if(node == null)
80             return;
81
82         TreePath path = new TreePath(tocModel.getPathToRoot(node));
83         toc.expandPath(path);
84         toc.setSelectionPath(path);
85         toc.scrollPathToVisible(path);
86     } //}}}
87

88     //{{{ load() method
89
public void load()
90     {
91         DefaultTreeModel empty = new DefaultTreeModel(
92             new DefaultMutableTreeNode(
93             jEdit.getProperty("helpviewer.toc.loading")));
94         toc.setModel(empty);
95         toc.setRootVisible(true);
96
97         VFSManager.runInWorkThread(new Runnable JavaDoc()
98         {
99             public void run()
100             {
101                 createTOC();
102                 tocModel.reload(tocRoot);
103                 toc.setModel(tocModel);
104                 toc.setRootVisible(false);
105                 for(int i = 0; i <tocRoot.getChildCount(); i++)
106                 {
107                     DefaultMutableTreeNode node =
108                         (DefaultMutableTreeNode)
109                         tocRoot.getChildAt(i);
110                     toc.expandPath(new TreePath(
111                         node.getPath()));
112                 }
113                 if(helpViewer.getShortURL() != null)
114                     selectNode(helpViewer.getShortURL());
115             }
116         });
117     } //}}}
118

119     //{{{ Private members
120
private HelpViewerInterface helpViewer;
121     private DefaultTreeModel tocModel;
122     private DefaultMutableTreeNode tocRoot;
123     private JTree toc;
124     private Hashtable nodes;
125
126     //{{{ createNode() method
127
private DefaultMutableTreeNode createNode(String JavaDoc href, String JavaDoc title)
128     {
129         DefaultMutableTreeNode node = new DefaultMutableTreeNode(
130             new HelpNode(href,title),true);
131         nodes.put(href,node);
132         return node;
133     } //}}}
134

135     //{{{ createTOC() method
136
private void createTOC()
137     {
138         EditPlugin[] plugins = jEdit.getPlugins();
139         Arrays.sort(plugins,new PluginCompare());
140         tocRoot = new DefaultMutableTreeNode();
141
142         tocRoot.add(createNode("welcome.html",
143             jEdit.getProperty("helpviewer.toc.welcome")));
144
145         tocRoot.add(createNode("README.txt",
146             jEdit.getProperty("helpviewer.toc.readme")));
147         tocRoot.add(createNode("CHANGES.txt",
148             jEdit.getProperty("helpviewer.toc.changes")));
149         tocRoot.add(createNode("TODO.txt",
150             jEdit.getProperty("helpviewer.toc.todo")));
151         tocRoot.add(createNode("COPYING.txt",
152             jEdit.getProperty("helpviewer.toc.copying")));
153         tocRoot.add(createNode("COPYING.DOC.txt",
154             jEdit.getProperty("helpviewer.toc.copying-doc")));
155         tocRoot.add(createNode("Apache.LICENSE.txt",
156             jEdit.getProperty("helpviewer.toc.copying-apache")));
157         tocRoot.add(createNode("COPYING.PLUGINS.txt",
158             jEdit.getProperty("helpviewer.toc.copying-plugins")));
159
160         loadTOC(tocRoot,"news43/toc.xml");
161         loadTOC(tocRoot,"users-guide/toc.xml");
162         loadTOC(tocRoot,"FAQ/toc.xml");
163         loadTOC(tocRoot,"api/toc.xml");
164
165         DefaultMutableTreeNode pluginTree = new DefaultMutableTreeNode(
166             jEdit.getProperty("helpviewer.toc.plugins"),true);
167
168         for(int i = 0; i < plugins.length; i++)
169         {
170             EditPlugin plugin = plugins[i];
171
172             String JavaDoc name = plugin.getClassName();
173
174             String JavaDoc docs = jEdit.getProperty("plugin." + name + ".docs");
175             String JavaDoc label = jEdit.getProperty("plugin." + name + ".name");
176             if(docs != null)
177             {
178                 if(label != null && docs != null)
179                 {
180                     String JavaDoc path = plugin.getPluginJAR()
181                         .getClassLoader()
182                         .getResourceAsPath(docs);
183                     pluginTree.add(createNode(
184                         path,label));
185                 }
186             }
187         }
188
189         if(pluginTree.getChildCount() != 0)
190             tocRoot.add(pluginTree);
191         else
192         {
193             // so that HelpViewer constructor doesn't try to expand
194
pluginTree = null;
195         }
196
197         tocModel = new DefaultTreeModel(tocRoot);
198     } //}}}
199

200     //{{{ loadTOC() method
201
private void loadTOC(DefaultMutableTreeNode root, String JavaDoc path)
202     {
203         TOCHandler h = new TOCHandler(root,MiscUtilities.getParentOfPath(path));
204         try
205         {
206             XMLUtilities.parseXML(
207                 new URL(helpViewer.getBaseURL()
208                     + '/' + path).openStream(), h);
209         }
210         catch(IOException e)
211         {
212             Log.log(Log.ERROR,this,e);
213         }
214     } //}}}
215

216     //}}}
217

218     //{{{ HelpNode class
219
static class HelpNode
220     {
221         String JavaDoc href, title;
222
223         //{{{ HelpNode constructor
224
HelpNode(String JavaDoc href, String JavaDoc title)
225         {
226             this.href = href;
227             this.title = title;
228         } //}}}
229

230         //{{{ toString() method
231
public String JavaDoc toString()
232         {
233             return title;
234         } //}}}
235
} //}}}
236

237     //{{{ TOCHandler class
238
class TOCHandler extends DefaultHandler JavaDoc
239     {
240         String JavaDoc dir;
241
242         //{{{ TOCHandler constructor
243
TOCHandler(DefaultMutableTreeNode root, String JavaDoc dir)
244         {
245             nodes = new Stack();
246             node = root;
247             this.dir = dir;
248         } //}}}
249

250         //{{{ characters() method
251
public void characters(char[] c, int off, int len)
252         {
253             if(tag.equals("TITLE"))
254             {
255                 boolean firstNonWhitespace = false;
256                 for(int i = 0; i < len; i++)
257                 {
258                     char ch = c[off + i];
259                     if (!firstNonWhitespace && Character.isWhitespace(ch)) continue;
260                     firstNonWhitespace = true;
261                     title.append(ch);
262                 }
263             }
264
265
266         } //}}}
267

268         //{{{ startElement() method
269
public void startElement(String JavaDoc uri, String JavaDoc localName,
270                      String JavaDoc name, Attributes JavaDoc attrs)
271         {
272             tag = name;
273             if (name.equals("ENTRY"))
274                 href = attrs.getValue("HREF");
275         } //}}}
276

277         //{{{ endElement() method
278
public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc name)
279         {
280             if(name == null)
281                 return;
282
283             if(name.equals("TITLE"))
284             {
285                 DefaultMutableTreeNode newNode = createNode(
286                     dir + href,title.toString());
287                 node.add(newNode);
288                 nodes.push(node);
289                 node = newNode;
290                 title.setLength(0);
291             }
292             else if(name.equals("ENTRY"))
293             {
294                 node = (DefaultMutableTreeNode)nodes.pop();
295                 href = null;
296             }
297         } //}}}
298

299         //{{{ Private members
300
private String JavaDoc tag;
301         private StringBuffer JavaDoc title = new StringBuffer JavaDoc();
302         private String JavaDoc href;
303         private DefaultMutableTreeNode node;
304         private Stack nodes;
305         //}}}
306
} //}}}
307

308     //{{{ TOCTree class
309
class TOCTree extends JTree
310     {
311         //{{{ TOCTree constructor
312
TOCTree()
313         {
314             ToolTipManager.sharedInstance().registerComponent(this);
315         } //}}}
316

317         //{{{ getToolTipText() method
318
public final String JavaDoc getToolTipText(MouseEvent evt)
319         {
320             TreePath path = getPathForLocation(evt.getX(), evt.getY());
321             if(path != null)
322             {
323                 Rectangle cellRect = getPathBounds(path);
324                 if(cellRect != null && !cellRectIsVisible(cellRect))
325                     return path.getLastPathComponent().toString();
326             }
327             return null;
328         } //}}}
329

330         //{{{ getToolTipLocation() method
331
/* public final Point getToolTipLocation(MouseEvent evt)
332         {
333             TreePath path = getPathForLocation(evt.getX(), evt.getY());
334             if(path != null)
335             {
336                 Rectangle cellRect = getPathBounds(path);
337                 if(cellRect != null && !cellRectIsVisible(cellRect))
338                 {
339                     return new Point(cellRect.x + 14, cellRect.y);
340                 }
341             }
342             return null;
343         } */
//}}}
344

345         //{{{ processMouseEvent() method
346
protected void processMouseEvent(MouseEvent evt)
347         {
348             //ToolTipManager ttm = ToolTipManager.sharedInstance();
349

350             switch(evt.getID())
351             {
352             /* case MouseEvent.MOUSE_ENTERED:
353                 toolTipInitialDelay = ttm.getInitialDelay();
354                 toolTipReshowDelay = ttm.getReshowDelay();
355                 ttm.setInitialDelay(200);
356                 ttm.setReshowDelay(0);
357                 super.processMouseEvent(evt);
358                 break;
359             case MouseEvent.MOUSE_EXITED:
360                 ttm.setInitialDelay(toolTipInitialDelay);
361                 ttm.setReshowDelay(toolTipReshowDelay);
362                 super.processMouseEvent(evt);
363                 break; */

364             case MouseEvent.MOUSE_CLICKED:
365                 TreePath path = getPathForLocation(evt.getX(),evt.getY());
366                 if(path != null)
367                 {
368                     if(!isPathSelected(path))
369                         setSelectionPath(path);
370
371                     Object JavaDoc obj = ((DefaultMutableTreeNode)
372                         path.getLastPathComponent())
373                         .getUserObject();
374                     if(!(obj instanceof HelpNode))
375                     {
376                         this.expandPath(path);
377                         return;
378                     }
379
380                     HelpNode node = (HelpNode)obj;
381
382                     helpViewer.gotoURL(node.href,true,0);
383                 }
384
385                 super.processMouseEvent(evt);
386                 break;
387             default:
388                 super.processMouseEvent(evt);
389                 break;
390             }
391         } //}}}
392

393         //{{{ cellRectIsVisible() method
394
private boolean cellRectIsVisible(Rectangle cellRect)
395         {
396             Rectangle vr = TOCTree.this.getVisibleRect();
397             return vr.contains(cellRect.x,cellRect.y) &&
398                 vr.contains(cellRect.x + cellRect.width,
399                 cellRect.y + cellRect.height);
400         } //}}}
401
} //}}}
402

403     //{{{ TOCCellRenderer class
404
class TOCCellRenderer extends DefaultTreeCellRenderer
405     {
406         EmptyBorder border = new EmptyBorder(1,0,1,1);
407
408         public Component getTreeCellRendererComponent(JTree tree,
409             Object JavaDoc value, boolean sel, boolean expanded,
410             boolean leaf, int row, boolean focus)
411         {
412             super.getTreeCellRendererComponent(tree,value,sel,
413                 expanded,leaf,row,focus);
414             setIcon(leaf ? FileCellRenderer.fileIcon
415                 : (expanded ? FileCellRenderer.openDirIcon
416                 : FileCellRenderer.dirIcon));
417             setBorder(border);
418
419             return this;
420         }
421     } //}}}
422

423     //{{{ PluginCompare class
424
static class PluginCompare implements Comparator
425     {
426         public int compare(Object JavaDoc o1, Object JavaDoc o2)
427         {
428             EditPlugin p1 = (EditPlugin)o1;
429             EditPlugin p2 = (EditPlugin)o2;
430             return StandardUtilities.compareStrings(
431                 jEdit.getProperty("plugin." + p1.getClassName() + ".name"),
432                 jEdit.getProperty("plugin." + p2.getClassName() + ".name"),
433                 true);
434         }
435     } //}}}
436
}
437
Popular Tags