source: gli/tags/2-74/src/org/greenstone/gatherer/gui/HelpFrame.java@ 14422

Last change on this file since 14422 was 12119, checked in by kjdon, 18 years ago

Changed text handling to use Dictionary.get rather than Dictionary.setText or Dictionary.registerBoth etc. also removed mnemonics cos they suck for other languages

  • Property svn:keywords set to Author Date Id Revision
File size: 11.5 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 // Pretty corner icon
111 this.setIconImage(JarTools.getImage("gatherer_small.gif").getImage());
112
113 self = this;
114 }
115
116 public void destroy()
117 {
118 help_contents_tree = null;
119 help_pane = null;
120 }
121
122
123 /** Retrieve the relative file path to the language-specific help index xml file. */
124 private String getHelpFolder()
125 {
126 String help_folder = "help/" + Configuration.getLanguage() + "/";
127
128 // Applet case: get help files out of JAR file
129 if (Gatherer.isApplet && JarTools.getResource("/" + help_folder) != null) {
130 return help_folder;
131 }
132
133 // Normal case: get help files out of GLI "help" folder
134 if (new File(help_folder).exists()) {
135 return help_folder;
136 }
137
138 // Resort to English
139 return "help/" + StaticStrings.ENGLISH_LANGUAGE_STR + "/";
140 }
141
142
143 private URL getURLForSection(String section_name)
144 {
145 // Form the path to the help file from the section name
146 if (section_name != null) {
147 String help_file_path = getHelpFolder() + section_name + StaticStrings.HTM_FILE_EXTENSION;
148
149 try {
150 // Applet case: get help file out of JAR file
151 if (Gatherer.isApplet) {
152 return JarTools.getResource("/" + help_file_path);
153 }
154
155 // Normal case: get help file out of GLI "help" folder
156 return (new File(help_file_path)).toURL();
157 }
158 catch (Exception exception) {
159 DebugStream.printStackTrace(exception);
160 }
161 }
162
163 return null;
164 }
165
166
167 static private void selectHelpContentsTreeNode(String section_name)
168 {
169 // Find the tree node with this section name, then select it
170 selectHelpContentsTreeNode(help_contents_tree_model.getHelpContentsTreeNodeNamed(section_name));
171 }
172
173
174 static private void selectHelpContentsTreeNode(HelpContentsTreeNode help_tree_node)
175 {
176 // Ensure the node in the help contents tree is selected and visible
177 TreePath help_tree_node_path = new TreePath(help_tree_node.getPath());
178 help_contents_tree.setSelectionPath(help_tree_node_path);
179 help_contents_tree.scrollPathToVisible(help_tree_node_path);
180 }
181
182
183 static public void setView(String section_name)
184 {
185 // Select the appropriate node in the help contents tree
186 selectHelpContentsTreeNode(section_name);
187
188 // Show the help page with the specified section name
189 self.showHelpPage(section_name);
190 }
191
192
193 private void showHelpPage(String section_name)
194 {
195 // Find the tree node with this section name, then show the page for the node
196 showHelpPage(getURLForSection(section_name));
197 }
198
199
200 private void showHelpPage(URL help_page_url)
201 {
202 // Display the selected page
203 if (help_page_url != null) {
204 try {
205 help_pane.setPage(help_page_url);
206 }
207 catch (Exception exception) {
208 DebugStream.printStackTrace(exception);
209 }
210 }
211
212 // Make the help frame visible
213 setVisible(true);
214 split_pane.setDividerLocation(0.4);
215 }
216
217
218 private class HelpContentsTreeModel
219 extends DefaultTreeModel
220 {
221 public HelpContentsTreeModel(MutableTreeNode help_tree_root_node)
222 {
223 super(help_tree_root_node);
224
225 // Load the XML help index file and build the contents structure from it
226 try {
227 String help_index_file_path = getHelpFolder() + "help_index.xml";
228 Document document = XMLTools.parseXMLFile(help_index_file_path, true);
229
230 // Traverse the help hierarchy, building a tree representing its structure
231 Node document_node = document.getFirstChild();
232
233 // Add a help contents tree node for each major section
234 int section_number = 0;
235 NodeList children = document_node.getChildNodes();
236 for (int i = 0; i < children.getLength(); i++) {
237 Node child = children.item(i);
238 if (child.getNodeName().equals(StaticStrings.SECTION_ELEMENT)) {
239 section_number++;
240 buildHelpContentsTree(help_tree_root_node, section_number + StaticStrings.EMPTY_STR, child);
241 }
242 }
243 }
244 catch (Exception exception) {
245 DebugStream.printStackTrace(exception);
246 }
247 }
248
249
250 private void buildHelpContentsTree(MutableTreeNode parent, String pos, Node node)
251 {
252 // Determine the section name
253 String section_name = ((Element) node).getAttribute(StaticStrings.NAME_ATTRIBUTE);
254
255 // Determine the section title
256 String section_title = "";
257 NodeList children = node.getChildNodes();
258 for (int i = 0; i < children.getLength(); i++) {
259 Node child = children.item(i);
260 if (child.getNodeName().equals(StaticStrings.TITLE_ELEMENT)) {
261 section_title = pos + ": ";
262 if (child.getFirstChild() != null) {
263 section_title += child.getFirstChild().getNodeValue();
264 }
265 }
266 }
267
268 // Add the node into the tree
269 HelpContentsTreeNode help_tree_node = new HelpContentsTreeNode(section_name, section_title);
270 insertNodeInto(help_tree_node, parent, parent.getChildCount());
271
272 // Apply recursively to the children of this node
273 int section_number = 0;
274 for (int i = 0; i < children.getLength(); i++) {
275 Node child = children.item(i);
276 if (child.getNodeName().equals(StaticStrings.SECTION_ELEMENT)) {
277 section_number++;
278 buildHelpContentsTree(help_tree_node, pos + StaticStrings.STOP_CHARACTER + section_number, child);
279 }
280 }
281 }
282
283
284 private HelpContentsTreeNode getHelpContentsTreeNodeNamed(String section_name)
285 {
286 // Find the node with name matching section name, somewhere in the tree
287 if (section_name != null) {
288 Enumeration descendants = ((DefaultMutableTreeNode) root).preorderEnumeration();
289 while (descendants.hasMoreElements()) {
290 HelpContentsTreeNode help_tree_node = (HelpContentsTreeNode) descendants.nextElement();
291 if (section_name.equals(help_tree_node.section_name)) {
292 return help_tree_node;
293 }
294 }
295 }
296
297 // Couldn't be found, so just return the root (Contents) node
298 return (HelpContentsTreeNode) root;
299 }
300 }
301
302
303 /** This listener is used to listen for selection changes in the contents tree, and
304 * to show the appropriate page as required.
305 */
306 private class HelpContentsTreeSelectionListener
307 implements TreeSelectionListener
308 {
309 /** Any implementation of <i>TreeSelectionListener</i> must include this method so we can be informed when the tree selection changes.
310 * @param event A <strong>TreeSelectionEvent</strong> containing al the relevant information about the event itself.
311 */
312 public void valueChanged(TreeSelectionEvent event)
313 {
314 HelpContentsTreeNode help_tree_node = (HelpContentsTreeNode) event.getPath().getLastPathComponent();
315 selectHelpContentsTreeNode(help_tree_node);
316 showHelpPage(help_tree_node.section_name);
317 }
318 }
319
320
321 /** This class provides a wrapper around a <strong>DefaultMutableTreeNode</strong> which
322 * provides the ability to set an html page to be loaded when this node is selected.
323 */
324 private class HelpContentsTreeNode
325 extends DefaultMutableTreeNode
326 {
327 /** The unique name of the section (matches the HTML filename) */
328 public String section_name = null;
329 /** The title to be displayed for this tree node. */
330 public String section_title = null;
331
332 public HelpContentsTreeNode(String section_name, String section_title)
333 {
334 this.section_name = section_name;
335 this.section_title = section_title;
336 }
337
338 public String toString()
339 {
340 return section_title;
341 }
342 }
343
344
345 private class HelpPaneHyperlinkListener
346 implements HyperlinkListener
347 {
348 public void hyperlinkUpdate(HyperlinkEvent e)
349 {
350 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
351 // Determine which node in the help contents tree to select, then select it
352 String section_link = e.getURL().getFile();
353 if (section_link.endsWith(".htm")) {
354 section_link = section_link.substring(section_link.lastIndexOf("/") + 1);
355 section_link = section_link.substring(0, section_link.length() - ".htm".length());
356 selectHelpContentsTreeNode(section_link);
357 }
358
359 // Change to the page specified by the link
360 showHelpPage(e.getURL());
361 }
362 }
363 }
364}
Note: See TracBrowser for help on using the repository browser.