source: trunk/gli/src/org/greenstone/gatherer/gui/HelpFrame.java@ 9888

Last change on this file since 9888 was 8579, checked in by mdewsnip, 20 years ago

Moved the HelpFrame class out of "help" into "gui.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Principal Author: John Thompson, NZDL Project, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.gui;
38
39import java.awt.*;
40import java.io.*;
41import java.net.*;
42import java.util.*;
43import javax.swing.*;
44import javax.swing.event.*;
45import javax.swing.tree.*;
46import org.greenstone.gatherer.DebugStream;
47import org.greenstone.gatherer.Dictionary;
48import org.greenstone.gatherer.util.StaticStrings;
49import org.greenstone.gatherer.util.Utility;
50import org.w3c.dom.*;
51
52/**
53 * This class provides a nice help facility. It is a separate frame that can be positioned
54 * as the user wishes, and provides a contents page and the help information.
55 * @author Michael Dewsnip, NZDL Project
56 */
57public class HelpFrame
58 extends JFrame {
59
60 static private final String NULL_STRING = "<null>";
61 /** The size of the frame */
62 static private final Dimension SIZE = new Dimension(760, 560);
63
64 static private HelpFrame self = null;
65
66 /** The HTML rendering pane */
67 private JEditorPane view = null;
68 /** The contents tree model */
69 private ContentsModel model = null;
70 /** A contents tree for the top of the frame */
71 private JTree contents = null;
72
73 private JSplitPane split_pane = null;
74
75
76 public HelpFrame()
77 {
78 setDefaultCloseOperation(HIDE_ON_CLOSE);
79 setSize(SIZE);
80 Dictionary.registerText(this, "Help.Title");
81
82 view = new JEditorPane();
83 view.setEditable(false);
84 view.addHyperlinkListener(new ViewHyperlinkListener());
85
86 HelpItem rootNode = new HelpItem(Dictionary.get("Help.Contents"), NULL_STRING);
87 model = new ContentsModel(rootNode);
88 contents = new JTree((DefaultTreeModel) model);
89 contents.addTreeSelectionListener(new ContentsListener());
90 contents.setExpandsSelectedPaths(true);
91
92 // Creation
93 JPanel content_pane = (JPanel) this.getContentPane();
94 split_pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
95 JScrollPane index_scroll = new JScrollPane(contents);
96 JScrollPane view_scroll = new JScrollPane(view);
97
98 // Layout
99 split_pane.add(index_scroll, JSplitPane.LEFT);
100 split_pane.add(view_scroll, JSplitPane.RIGHT);
101 content_pane.setLayout(new BorderLayout());
102 content_pane.add(split_pane, BorderLayout.CENTER);
103
104 // Center
105 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
106 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
107
108 // Pretty corner icon
109 this.setIconImage(Utility.getImage("gatherer_small.gif").getImage());
110
111 self = this;
112 }
113
114
115 public void destroy()
116 {
117 view = null;
118 contents = null;
119 }
120
121
122 static public void setView(String section_name)
123 {
124 self.setViewInternal(section_name);
125 }
126
127
128 public void setViewInternal(String sectionName)
129 {
130 // Find the node in the tree with the specified sectionName
131 HelpItem node = model.getHelpItemNamed(sectionName);
132
133 // Ensure the node is selected and visible
134 TreePath path = new TreePath(node.getPath());
135 contents.setSelectionPath(path);
136 contents.scrollPathToVisible(path);
137
138 // Display the selected page
139 URL url = node.getURL();
140 if (url != null) {
141 try {
142 view.setPage(url);
143 }
144 catch (Exception exception) {
145 DebugStream.printStackTrace(exception);
146 }
147 }
148
149 // Make the help frame visible
150 setVisible(true);
151 split_pane.setDividerLocation(0.2);
152 }
153
154
155 private class ContentsModel
156 extends DefaultTreeModel {
157
158 public ContentsModel(MutableTreeNode rootNode) {
159 super(rootNode);
160
161 // Load the XML help file and build the contents structure from it
162 try {
163 String help_index = Utility.getHelpFolder() + StaticStrings.HELP_INDEX_FILENAME;
164 Document document = Utility.parse(help_index,true);
165
166 // Traverse the document hierarchy, building a tree representing its structure
167 Node documentNode = document.getFirstChild();
168
169 int sectionNum = 0;
170 NodeList children = documentNode.getChildNodes();
171 for (int i = 0; i < children.getLength(); i++) {
172 Node child = children.item(i);
173 if (child.getNodeName().equals(StaticStrings.SECTION_ELEMENT)) {
174 sectionNum++;
175 buildContentsHierarchy(rootNode, sectionNum + StaticStrings.EMPTY_STR, child);
176 }
177 }
178 }
179 catch (Exception ex) {
180 ex.printStackTrace();
181 }
182 }
183
184
185 public void buildContentsHierarchy(MutableTreeNode parent, String pos, Node node)
186 {
187 // Determine the section name
188 String sectionName = ((Element) node).getAttribute(StaticStrings.NAME_ATTRIBUTE);
189
190 // Determine the section title
191 String sectionTitle = "";
192 NodeList children = node.getChildNodes();
193 for (int i = 0; i < children.getLength(); i++) {
194 Node child = children.item(i);
195 if (child.getNodeName().equals(StaticStrings.TITLE_ELEMENT)) {
196 sectionTitle = pos + ": ";
197 if (child.getFirstChild() != null) {
198 sectionTitle = sectionTitle + child.getFirstChild().getNodeValue();
199 }
200 }
201 }
202
203 // Add the node into the tree
204 HelpItem item = new HelpItem(sectionTitle, sectionName);
205 insertNodeInto(item, parent, parent.getChildCount());
206
207 // Apply recursively to the children of this node
208 int sectionNum = 0;
209 for (int i = 0; i < children.getLength(); i++) {
210 Node child = children.item(i);
211 if (child.getNodeName().equals(StaticStrings.SECTION_ELEMENT)) {
212 sectionNum++;
213 buildContentsHierarchy(item, pos + StaticStrings.STOP_CHARACTER + sectionNum, child);
214 }
215 }
216 }
217
218
219 public HelpItem getHelpItemNamed(String sectionName)
220 {
221 // Find the node with name matching sectionName, somewhere in the tree
222 Enumeration descendants = ((DefaultMutableTreeNode) root).preorderEnumeration();
223 while (descendants.hasMoreElements()) {
224 HelpItem node = (HelpItem) descendants.nextElement();
225 if (node.name.equals(sectionName)) {
226 return node;
227 }
228 }
229
230 // Couldn't be found, so just return the root (Contents) node
231 return (HelpItem) root;
232 }
233 }
234
235
236 /** This listener is used to listen for selection changes in the contents tree, and
237 * to show the appropriate page as required.
238 */
239 private class ContentsListener
240 implements TreeSelectionListener {
241
242 /** Any implementation of <i>TreeSelectionListener</i> must include this method so we can be informed when the tree selection changes.
243 * @param event A <strong>TreeSelectionEvent</strong> containing al the relevant information about the event itself.
244 */
245 public void valueChanged(TreeSelectionEvent event)
246 {
247 TreePath path = event.getPath();
248 HelpItem node = (HelpItem) path.getLastPathComponent();
249 URL url = node.getURL();
250 if (url != null) {
251 try {
252 view.setPage(url);
253 }
254 catch (Exception exception) {
255 DebugStream.printStackTrace(exception);
256 }
257 }
258 }
259 }
260
261
262 /** This class provides a wrapper around a <strong>DefaultMutableTreeNode</strong> which
263 * provides the ability to set an html page to be loaded when this node is selected.
264 */
265 private class HelpItem
266 extends DefaultMutableTreeNode {
267
268 /** The unique name of the section (matches the HTML filename) */
269 public String name = null;
270 /** The title to be displayed for this tree node. */
271 public String title = null;
272
273
274 /** Constructor.
275 * @param title The title to be shown for this node as a <strong>String</strong>.
276 * @param name The name of the section as a <strong>String</strong>.
277 */
278 public HelpItem(String title, String name)
279 {
280 this.name = name;
281 this.title = title;
282 }
283
284
285 /** Method to create a url from the file name.
286 * @return A <strong>URL</strong> that points to the file's location.
287 */
288 public URL getURL()
289 {
290 URL url = null;
291
292 String help_file = Utility.getHelpFolder() + name + StaticStrings.HTM_FILE_EXTENSION;
293
294 if (name != null && !name.equals(NULL_STRING)) {
295 url = Utility.base.getResource("/" + help_file);
296 if (url == null) {
297 File file = new File(Utility.BASE_DIR + help_file);
298 try {
299 url = file.toURL();
300 }
301 catch (Exception ex) {
302 ex.printStackTrace();
303 }
304 }
305 }
306
307 return url;
308 }
309
310
311 public String toString()
312 {
313 return title;
314 }
315 }
316
317
318 private class ViewHyperlinkListener
319 implements HyperlinkListener {
320 public void hyperlinkUpdate(HyperlinkEvent e) {
321 if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
322 try {
323 view.setPage(e.getURL());
324 }
325 catch(Exception exception) {
326 DebugStream.printStackTrace(exception);
327 }
328 }
329 }
330 }
331}
Note: See TracBrowser for help on using the repository browser.