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

Last change on this file since 10009 was 10009, checked in by mdewsnip, 19 years ago

Moved Utility.getHelpFolder into HelpFrame, as part of tidying up the Utility class.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.0 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.util.StaticStrings;
50import org.greenstone.gatherer.util.Utility;
51import org.greenstone.gatherer.util.XMLTools;
52import org.w3c.dom.*;
53
54/**
55 * This class provides a nice help facility. It is a separate frame that can be positioned
56 * as the user wishes, and provides a contents page and the help information.
57 * @author Michael Dewsnip, NZDL Project
58 */
59public class HelpFrame
60 extends JFrame {
61
62 static private final String NULL_STRING = "<null>";
63 /** The size of the frame */
64 static private final Dimension SIZE = new Dimension(760, 560);
65
66 static private HelpFrame self = null;
67
68 /** The HTML rendering pane */
69 private JEditorPane view = null;
70 /** The contents tree model */
71 private ContentsModel model = null;
72 /** A contents tree for the top of the frame */
73 private JTree contents = null;
74
75 private JSplitPane split_pane = null;
76
77
78 public HelpFrame()
79 {
80 setDefaultCloseOperation(HIDE_ON_CLOSE);
81 setSize(SIZE);
82 Dictionary.registerText(this, "Help.Title");
83
84 view = new JEditorPane();
85 view.setEditable(false);
86 view.addHyperlinkListener(new ViewHyperlinkListener());
87
88 HelpItem rootNode = new HelpItem(Dictionary.get("Help.Contents"), NULL_STRING);
89 model = new ContentsModel(rootNode);
90 contents = new JTree((DefaultTreeModel) model);
91 contents.addTreeSelectionListener(new ContentsListener());
92 contents.setExpandsSelectedPaths(true);
93
94 // Creation
95 JPanel content_pane = (JPanel) this.getContentPane();
96 split_pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
97 JScrollPane index_scroll = new JScrollPane(contents);
98 JScrollPane view_scroll = new JScrollPane(view);
99
100 // Layout
101 split_pane.add(index_scroll, JSplitPane.LEFT);
102 split_pane.add(view_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(Utility.getImage("gatherer_small.gif").getImage());
112
113 self = this;
114 }
115
116
117 public void destroy()
118 {
119 view = null;
120 contents = null;
121 }
122
123
124 /** Retrieve the full file path to the help index xml file.
125 * @return the full path as a String
126 */
127 private String getHelpFolder()
128 {
129 String help_folder = "help/" + Configuration.getLanguage() + "/";
130
131 // Try in the JAR/classes directory first
132 URL help_folder_url = Utility.base.getResource("/" + help_folder); // !!
133 if (help_folder_url != null) {
134 return help_folder;
135 }
136
137 // Look in the base directory next
138 File help_folder_file = new File(help_folder);
139 if (help_folder_file.exists()) {
140 return help_folder;
141 }
142
143 // Resort to English
144 return "help/" + StaticStrings.ENGLISH_LANGUAGE_STR + "/";
145 }
146
147
148 static public void setView(String section_name)
149 {
150 self.setViewInternal(section_name);
151 }
152
153
154 public void setViewInternal(String sectionName)
155 {
156 // Find the node in the tree with the specified sectionName
157 HelpItem node = model.getHelpItemNamed(sectionName);
158
159 // Ensure the node is selected and visible
160 TreePath path = new TreePath(node.getPath());
161 contents.setSelectionPath(path);
162 contents.scrollPathToVisible(path);
163
164 // Display the selected page
165 URL url = node.getURL();
166 if (url != null) {
167 try {
168 view.setPage(url);
169 }
170 catch (Exception exception) {
171 DebugStream.printStackTrace(exception);
172 }
173 }
174
175 // Make the help frame visible
176 setVisible(true);
177 split_pane.setDividerLocation(0.2);
178 }
179
180
181 private class ContentsModel
182 extends DefaultTreeModel {
183
184 public ContentsModel(MutableTreeNode rootNode) {
185 super(rootNode);
186
187 // Load the XML help file and build the contents structure from it
188 try {
189 String help_index = getHelpFolder() + StaticStrings.HELP_INDEX_FILENAME;
190 Document document = XMLTools.parseXMLFile(help_index, true);
191
192 // Traverse the document hierarchy, building a tree representing its structure
193 Node documentNode = document.getFirstChild();
194
195 int sectionNum = 0;
196 NodeList children = documentNode.getChildNodes();
197 for (int i = 0; i < children.getLength(); i++) {
198 Node child = children.item(i);
199 if (child.getNodeName().equals(StaticStrings.SECTION_ELEMENT)) {
200 sectionNum++;
201 buildContentsHierarchy(rootNode, sectionNum + StaticStrings.EMPTY_STR, child);
202 }
203 }
204 }
205 catch (Exception ex) {
206 ex.printStackTrace();
207 }
208 }
209
210
211 public void buildContentsHierarchy(MutableTreeNode parent, String pos, Node node)
212 {
213 // Determine the section name
214 String sectionName = ((Element) node).getAttribute(StaticStrings.NAME_ATTRIBUTE);
215
216 // Determine the section title
217 String sectionTitle = "";
218 NodeList children = node.getChildNodes();
219 for (int i = 0; i < children.getLength(); i++) {
220 Node child = children.item(i);
221 if (child.getNodeName().equals(StaticStrings.TITLE_ELEMENT)) {
222 sectionTitle = pos + ": ";
223 if (child.getFirstChild() != null) {
224 sectionTitle = sectionTitle + child.getFirstChild().getNodeValue();
225 }
226 }
227 }
228
229 // Add the node into the tree
230 HelpItem item = new HelpItem(sectionTitle, sectionName);
231 insertNodeInto(item, parent, parent.getChildCount());
232
233 // Apply recursively to the children of this node
234 int sectionNum = 0;
235 for (int i = 0; i < children.getLength(); i++) {
236 Node child = children.item(i);
237 if (child.getNodeName().equals(StaticStrings.SECTION_ELEMENT)) {
238 sectionNum++;
239 buildContentsHierarchy(item, pos + StaticStrings.STOP_CHARACTER + sectionNum, child);
240 }
241 }
242 }
243
244
245 public HelpItem getHelpItemNamed(String sectionName)
246 {
247 // Find the node with name matching sectionName, somewhere in the tree
248 Enumeration descendants = ((DefaultMutableTreeNode) root).preorderEnumeration();
249 while (descendants.hasMoreElements()) {
250 HelpItem node = (HelpItem) descendants.nextElement();
251 if (node.name.equals(sectionName)) {
252 return node;
253 }
254 }
255
256 // Couldn't be found, so just return the root (Contents) node
257 return (HelpItem) root;
258 }
259 }
260
261
262 /** This listener is used to listen for selection changes in the contents tree, and
263 * to show the appropriate page as required.
264 */
265 private class ContentsListener
266 implements TreeSelectionListener {
267
268 /** Any implementation of <i>TreeSelectionListener</i> must include this method so we can be informed when the tree selection changes.
269 * @param event A <strong>TreeSelectionEvent</strong> containing al the relevant information about the event itself.
270 */
271 public void valueChanged(TreeSelectionEvent event)
272 {
273 TreePath path = event.getPath();
274 HelpItem node = (HelpItem) path.getLastPathComponent();
275 URL url = node.getURL();
276 if (url != null) {
277 try {
278 view.setPage(url);
279 }
280 catch (Exception exception) {
281 DebugStream.printStackTrace(exception);
282 }
283 }
284 }
285 }
286
287
288 /** This class provides a wrapper around a <strong>DefaultMutableTreeNode</strong> which
289 * provides the ability to set an html page to be loaded when this node is selected.
290 */
291 private class HelpItem
292 extends DefaultMutableTreeNode {
293
294 /** The unique name of the section (matches the HTML filename) */
295 public String name = null;
296 /** The title to be displayed for this tree node. */
297 public String title = null;
298
299
300 /** Constructor.
301 * @param title The title to be shown for this node as a <strong>String</strong>.
302 * @param name The name of the section as a <strong>String</strong>.
303 */
304 public HelpItem(String title, String name)
305 {
306 this.name = name;
307 this.title = title;
308 }
309
310
311 /** Method to create a url from the file name.
312 * @return A <strong>URL</strong> that points to the file's location.
313 */
314 public URL getURL()
315 {
316 URL url = null;
317
318 String help_file = getHelpFolder() + name + StaticStrings.HTM_FILE_EXTENSION;
319
320 if (name != null && !name.equals(NULL_STRING)) {
321 url = Utility.base.getResource("/" + help_file);
322 if (url == null) {
323 File file = new File(Utility.BASE_DIR + help_file);
324 try {
325 url = file.toURL();
326 }
327 catch (Exception ex) {
328 ex.printStackTrace();
329 }
330 }
331 }
332
333 return url;
334 }
335
336
337 public String toString()
338 {
339 return title;
340 }
341 }
342
343
344 private class ViewHyperlinkListener
345 implements HyperlinkListener {
346 public void hyperlinkUpdate(HyperlinkEvent e) {
347 if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
348 try {
349 view.setPage(e.getURL());
350 }
351 catch(Exception exception) {
352 DebugStream.printStackTrace(exception);
353 }
354 }
355 }
356 }
357}
Note: See TracBrowser for help on using the repository browser.