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

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

Broke the help while messing around with paths for the remote building stuff. Will have to check this again to make sure it still works when isGsdlRemote is true.

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