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

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

Moved Utility.parse to XMLTools.parseXMLFile, as part of tidying up the Utility class.

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