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

Last change on this file since 17916 was 17916, checked in by davidb, 15 years ago

FLI based version now uses blue fli-*.gif images rather than the default green ones

  • Property svn:keywords set to Author Date Id Revision
File size: 12.1 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.Configuration;
47import org.greenstone.gatherer.DebugStream;
48import org.greenstone.gatherer.Dictionary;
49import org.greenstone.gatherer.Gatherer;
50import org.greenstone.gatherer.util.JarTools;
51import org.greenstone.gatherer.util.StaticStrings;
52import org.greenstone.gatherer.util.XMLTools;
53import org.w3c.dom.*;
54
55
56/**
57 * This class provides a nice help facility. It is a separate frame that can be positioned
58 * as the user wishes, and provides a contents page and the help information.
59 * @author Michael Dewsnip, NZDL Project
60 */
61public class HelpFrame
62 extends JFrame
63{
64 /** The size of the frame */
65 static private final Dimension SIZE = new Dimension(800, 560);
66
67 static private HelpFrame self = null;
68
69 /** The help view at the bottom of the frame. */
70 static private JEditorPane help_pane = null;
71 /** The help contents tree at the top of the frame. */
72 static private JTree help_contents_tree = null;
73 /** The help contents tree model. */
74 static private HelpContentsTreeModel help_contents_tree_model = null;
75 /** The split between the contents tree and the page view. */
76 static private JSplitPane split_pane = null;
77
78 public HelpFrame()
79 {
80 setDefaultCloseOperation(HIDE_ON_CLOSE);
81 setSize(SIZE);
82 setTitle(Dictionary.get("Help.Title"));
83
84 help_pane = new JEditorPane();
85 help_pane.setEditable(false);
86 help_pane.addHyperlinkListener(new HelpPaneHyperlinkListener());
87
88 HelpContentsTreeNode help_tree_root_node = new HelpContentsTreeNode(null, Dictionary.get("Help.Contents"));
89 help_contents_tree_model = new HelpContentsTreeModel(help_tree_root_node);
90 help_contents_tree = new JTree((DefaultTreeModel) help_contents_tree_model);
91 help_contents_tree.addTreeSelectionListener(new HelpContentsTreeSelectionListener());
92 help_contents_tree.setExpandsSelectedPaths(true);
93
94 // Creation
95 JPanel content_pane = (JPanel) this.getContentPane();
96 split_pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
97 JScrollPane help_contents_tree_scroll = new JScrollPane(help_contents_tree);
98 JScrollPane help_pane_scroll = new JScrollPane(help_pane);
99
100 // Layout
101 split_pane.add(help_contents_tree_scroll, JSplitPane.LEFT);
102 split_pane.add(help_pane_scroll, JSplitPane.RIGHT);
103 content_pane.setLayout(new BorderLayout());
104 content_pane.add(split_pane, BorderLayout.CENTER);
105
106 // Center
107 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
108 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
109
110
111 // Pretty corner icon
112 String gsmall_image = "gatherer_small.gif";
113 if (Configuration.fedora_info.isActive()) {
114 gsmall_image = "fli-" + gsmall_image;
115 }
116
117 this.setIconImage(JarTools.getImage(gsmall_image).getImage());
118
119 self = this;
120 }
121
122 public void destroy()
123 {
124 help_contents_tree = null;
125 help_pane = null;
126 }
127
128
129 /** Retrieve the relative file path to the language-specific help index xml file. */
130 private String getHelpFolder()
131 {
132 String help_folder = "help/" + Configuration.getLanguage() + "/";
133
134 // Applet case: get help files out of JAR file
135 if (Gatherer.isApplet && JarTools.getResource("/" + help_folder) != null) {
136 return help_folder;
137 }
138
139 // Normal case: get help files out of GLI "help" folder
140 if (new File(help_folder).exists()) {
141 return help_folder;
142 }
143
144 // Resort to English
145 return "help/" + StaticStrings.ENGLISH_LANGUAGE_STR + "/";
146 }
147
148
149 private URL getURLForSection(String section_name)
150 {
151 // Form the path to the help file from the section name
152 if (section_name != null) {
153 String help_file_path = getHelpFolder() + section_name + StaticStrings.HTM_FILE_EXTENSION;
154
155 try {
156 // Applet case: get help file out of JAR file
157 if (Gatherer.isApplet) {
158 return JarTools.getResource("/" + help_file_path);
159 }
160
161 // Normal case: get help file out of GLI "help" folder
162 return (new File(help_file_path)).toURL();
163 }
164 catch (Exception exception) {
165 DebugStream.printStackTrace(exception);
166 }
167 }
168
169 return null;
170 }
171
172
173 static private void selectHelpContentsTreeNode(String section_name)
174 {
175 // Find the tree node with this section name, then select it
176 selectHelpContentsTreeNode(help_contents_tree_model.getHelpContentsTreeNodeNamed(section_name));
177 }
178
179
180 static private void selectHelpContentsTreeNode(HelpContentsTreeNode help_tree_node)
181 {
182 // Ensure the node in the help contents tree is selected and visible
183 TreePath help_tree_node_path = new TreePath(help_tree_node.getPath());
184 help_contents_tree.setSelectionPath(help_tree_node_path);
185 help_contents_tree.scrollPathToVisible(help_tree_node_path);
186 }
187
188
189 static public void setView(String section_name)
190 {
191 // Select the appropriate node in the help contents tree
192 selectHelpContentsTreeNode(section_name);
193
194 // Show the help page with the specified section name
195 self.showHelpPage(section_name);
196 }
197
198
199 private void showHelpPage(String section_name)
200 {
201 // Find the tree node with this section name, then show the page for the node
202 showHelpPage(getURLForSection(section_name));
203 }
204
205
206 private void showHelpPage(URL help_page_url)
207 {
208 // Display the selected page
209 if (help_page_url != null) {
210 try {
211 help_pane.setPage(help_page_url);
212 }
213 catch (Exception exception) {
214 DebugStream.printStackTrace(exception);
215 }
216 }
217
218 // Make the help frame visible
219 setVisible(true);
220 split_pane.setDividerLocation(0.4);
221 }
222
223
224 private class HelpContentsTreeModel
225 extends DefaultTreeModel
226 {
227 public HelpContentsTreeModel(MutableTreeNode help_tree_root_node)
228 {
229 super(help_tree_root_node);
230
231 // Load the XML help index file and build the contents structure from it
232 try {
233 String help_index_file_path = getHelpFolder() + "help_index.xml";
234 Document document = null;
235 if (Gatherer.isApplet && JarTools.getResource(help_index_file_path) != null) {
236 document = XMLTools.parseXMLFile(help_index_file_path, true);
237 }
238
239 if (new File(help_index_file_path).exists()) {
240 document = XMLTools.parseXMLFile(help_index_file_path, false);
241 }
242
243 if (document == null) {
244 String errormsg = "HelpFrame.HelpContentsTreeModel constructor(): There's no help document after parsing";
245 System.err.println(errormsg);
246 DebugStream.println(errormsg);
247 return;
248 }
249 // Traverse the help hierarchy, building a tree representing its structure
250 Node document_node = document.getFirstChild();
251
252 // Add a help contents tree node for each major section
253 int section_number = 0;
254 NodeList children = document_node.getChildNodes();
255 for (int i = 0; i < children.getLength(); i++) {
256 Node child = children.item(i);
257 if (child.getNodeName().equals(StaticStrings.SECTION_ELEMENT)) {
258 section_number++;
259 buildHelpContentsTree(help_tree_root_node, section_number + StaticStrings.EMPTY_STR, child);
260 }
261 }
262 }
263 catch (Exception exception) {
264 DebugStream.printStackTrace(exception);
265 }
266 }
267
268
269 private void buildHelpContentsTree(MutableTreeNode parent, String pos, Node node)
270 {
271 // Determine the section name
272 String section_name = ((Element) node).getAttribute(StaticStrings.NAME_ATTRIBUTE);
273
274 // Determine the section title
275 String section_title = "";
276 NodeList children = node.getChildNodes();
277 for (int i = 0; i < children.getLength(); i++) {
278 Node child = children.item(i);
279 if (child.getNodeName().equals(StaticStrings.TITLE_ELEMENT)) {
280 section_title = pos + ": ";
281 if (child.getFirstChild() != null) {
282 section_title += child.getFirstChild().getNodeValue();
283 }
284 }
285 }
286
287 // Add the node into the tree
288 HelpContentsTreeNode help_tree_node = new HelpContentsTreeNode(section_name, section_title);
289 insertNodeInto(help_tree_node, parent, parent.getChildCount());
290
291 // Apply recursively to the children of this node
292 int section_number = 0;
293 for (int i = 0; i < children.getLength(); i++) {
294 Node child = children.item(i);
295 if (child.getNodeName().equals(StaticStrings.SECTION_ELEMENT)) {
296 section_number++;
297 buildHelpContentsTree(help_tree_node, pos + StaticStrings.STOP_CHARACTER + section_number, child);
298 }
299 }
300 }
301
302
303 private HelpContentsTreeNode getHelpContentsTreeNodeNamed(String section_name)
304 {
305 // Find the node with name matching section name, somewhere in the tree
306 if (section_name != null) {
307 Enumeration descendants = ((DefaultMutableTreeNode) root).preorderEnumeration();
308 while (descendants.hasMoreElements()) {
309 HelpContentsTreeNode help_tree_node = (HelpContentsTreeNode) descendants.nextElement();
310 if (section_name.equals(help_tree_node.section_name)) {
311 return help_tree_node;
312 }
313 }
314 }
315
316 // Couldn't be found, so just return the root (Contents) node
317 return (HelpContentsTreeNode) root;
318 }
319 }
320
321
322 /** This listener is used to listen for selection changes in the contents tree, and
323 * to show the appropriate page as required.
324 */
325 private class HelpContentsTreeSelectionListener
326 implements TreeSelectionListener
327 {
328 /** Any implementation of <i>TreeSelectionListener</i> must include this method so we can be informed when the tree selection changes.
329 * @param event A <strong>TreeSelectionEvent</strong> containing al the relevant information about the event itself.
330 */
331 public void valueChanged(TreeSelectionEvent event)
332 {
333 HelpContentsTreeNode help_tree_node = (HelpContentsTreeNode) event.getPath().getLastPathComponent();
334 selectHelpContentsTreeNode(help_tree_node);
335 showHelpPage(help_tree_node.section_name);
336 }
337 }
338
339
340 /** This class provides a wrapper around a <strong>DefaultMutableTreeNode</strong> which
341 * provides the ability to set an html page to be loaded when this node is selected.
342 */
343 private class HelpContentsTreeNode
344 extends DefaultMutableTreeNode
345 {
346 /** The unique name of the section (matches the HTML filename) */
347 public String section_name = null;
348 /** The title to be displayed for this tree node. */
349 public String section_title = null;
350
351 public HelpContentsTreeNode(String section_name, String section_title)
352 {
353 this.section_name = section_name;
354 this.section_title = section_title;
355 }
356
357 public String toString()
358 {
359 return section_title;
360 }
361 }
362
363
364 private class HelpPaneHyperlinkListener
365 implements HyperlinkListener
366 {
367 public void hyperlinkUpdate(HyperlinkEvent e)
368 {
369 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
370 // Determine which node in the help contents tree to select, then select it
371 String section_link = e.getURL().getFile();
372 if (section_link.endsWith(".htm")) {
373 section_link = section_link.substring(section_link.lastIndexOf("/") + 1);
374 section_link = section_link.substring(0, section_link.length() - ".htm".length());
375 selectHelpContentsTreeNode(section_link);
376 }
377
378 // Change to the page specified by the link
379 showHelpPage(e.getURL());
380 }
381 }
382 }
383}
Note: See TracBrowser for help on using the repository browser.