KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > helpbrowser > HelpBrowser


1 /*
2 * Lucane - a collaborative platform
3 * Copyright (C) 2002 Vincent Fiack <vfiack@mail15.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */

19 package org.lucane.applications.helpbrowser;
20
21 import org.lucane.client.*;
22 import org.lucane.client.widgets.ManagedWindow;
23 import org.lucane.common.*;
24
25 import java.awt.*;
26 import java.awt.event.*;
27 import java.util.Iterator JavaDoc;
28
29 import javax.swing.*;
30 import javax.swing.event.*;
31 import javax.swing.tree.*;
32
33 /**
34 * The help browser plugin
35 */

36 public class HelpBrowser extends StandalonePlugin
37 implements TreeSelectionListener, ActionListener
38 {
39     //-- widgets
40
private ManagedWindow window;
41     private JTree sections;
42     private JEditorPane bighelp;
43     private JTextArea minihelp;
44     private HelpParser parser;
45     private JComboBox plugins;
46     
47     /**
48      * Constructor
49      */

50     public HelpBrowser()
51     {
52     }
53     
54     /**
55      * Init for plugin loader
56      */

57     public Plugin newInstance(ConnectInfo[] friends)
58     {
59         return new HelpBrowser();
60     }
61     
62     /**
63      * Plugin started
64      */

65     public void start()
66     {
67         window = new ManagedWindow(this, getTitle());
68         window.getContentPane().setLayout(new BorderLayout());
69         window.setName("frame");
70         
71         bighelp = new JEditorPane();
72         bighelp.setEditable(false);
73         bighelp.setContentType("text/html");
74         minihelp = new JTextArea("");
75         minihelp.setEditable(false);
76         bighelp.addHyperlinkListener(new LinkListener(this, bighelp, minihelp));
77         
78         JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
79                 new JScrollPane(bighelp),
80                 new JScrollPane(minihelp));
81         split.setOneTouchExpandable(true);
82         split.setName("split");
83         window.getContentPane().add(split, BorderLayout.CENTER);
84         sections = new JTree(new DefaultMutableTreeNode());
85         sections.setRootVisible(false);
86         sections.setEditable(false);
87         sections.setShowsRootHandles(true);
88         sections.addTreeSelectionListener(this);
89         window.getContentPane().add(sections, BorderLayout.WEST);
90         plugins = new JComboBox();
91         plugins.addActionListener(this);
92         
93         Iterator JavaDoc i = PluginManager.getInstance().getAvailablePlugins();
94         while(i.hasNext())
95             plugins.addItem(i.next());
96         
97         window.getContentPane().add(plugins, BorderLayout.NORTH);
98         window.setPreferredSize(new Dimension(600, 400));
99         window.show();
100
101         split.setDividerLocation(310);
102     }
103
104
105     //-- listeners
106

107     public void actionPerformed(ActionEvent ae)
108     {
109         Plugin p = (Plugin)plugins.getSelectedItem();
110         if(p != null)
111             showHelp(p);
112     }
113
114     public void valueChanged(TreeSelectionEvent tse)
115     {
116         TreePath tp = sections.getSelectionPath();
117         
118         if(tp != null)
119             showHelpSection(tp);
120     }
121     
122     //-- useful methods
123

124     /**
125      * Show the help for a plugin
126      */

127     public void showHelp(Plugin p)
128     {
129         try
130         {
131             parser = new HelpParser(p.getDirectory() + "help/", tr("file"));
132             sections.setModel(new DefaultTreeModel(parser.getSections()));
133             expandSections();
134
135             bighelp.setText("");
136             
137             //select first element
138
TreeNode root = (TreeNode)sections.getModel().getRoot();
139             TreePath path = new TreePath(root);
140             path = path.pathByAddingChild(((DefaultMutableTreeNode)root).getFirstChild());
141             sections.setSelectionPath(path);
142             valueChanged(null);
143         }
144         catch(Exception JavaDoc e)
145         {
146             sections.setModel(new DefaultTreeModel(null));
147             bighelp.setText("<html><head></head><body>" + tr("nohelp") + "</body></html>");
148         }
149         
150         minihelp.setText("");
151     }
152     
153     public void hidePluginList()
154     {
155         this.plugins.setVisible(false);
156     }
157     
158     /**
159      * Go to a particular section
160      */

161     public void gotoSection(String JavaDoc section)
162     {
163         TreeNode root = (TreeNode)sections.getModel().getRoot();
164         TreePath path = new TreePath(root);
165         path = findSection(path, section);
166         
167         if(path != null)
168         {
169             sections.setSelectionPath(path);
170             showHelpSection(path);
171         }
172     }
173     
174     /**
175      * Show a section
176      */

177     private void showHelpSection(TreePath tp)
178     {
179         bighelp.setText(parser.htmlForSection(tp));
180         bighelp.setCaretPosition(0);
181     }
182     
183     /**
184      * Find a section in the tree
185      */

186     private TreePath findSection(TreePath path, String JavaDoc section)
187     {
188         TreePath result = null;
189         DefaultMutableTreeNode n = (DefaultMutableTreeNode)path.getLastPathComponent();
190         
191         if(section.equals(n.getUserObject()))
192             return path;
193         
194         TreePath newpath;
195         
196         for(int i = 0; i < n.getChildCount(); i++)
197         {
198             newpath = path.pathByAddingChild(n.getChildAt(i));
199             result = findSection(newpath, section);
200             
201             if(result != null)
202                 break;
203         }
204         
205         return result;
206     }
207     
208     /**
209      * Expand all sections
210      */

211     private void expandSections()
212     {
213         for(int i=0;i<this.sections.getRowCount();i++)
214         {
215             this.sections.expandRow(i);
216         }
217     }
218 }
Popular Tags