source: trunk/gli/src/org/greenstone/gatherer/help/HelpFrame.java@ 8236

Last change on this file since 8236 was 8236, checked in by mdewsnip, 20 years ago

Replaced all Gatherer.print* with DebugStream.print*.

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