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

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

Removed some dead code.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 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.Dictionary;
48import org.greenstone.gatherer.Gatherer;
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
109 private class ViewHyperlinkListener
110 implements HyperlinkListener {
111 public void hyperlinkUpdate(HyperlinkEvent e) {
112 if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
113 try {
114 view.setPage(e.getURL());
115 }
116 catch(Exception exception) {
117 Gatherer.printStackTrace(exception);
118 }
119 }
120 }
121 }
122
123 public void destroy()
124 {
125 view = null;
126 contents = null;
127 }
128
129
130 public void setView(String sectionName)
131 {
132 // Find the node in the tree with the specified sectionName
133 HelpItem node = model.getHelpItemNamed(sectionName);
134
135 // Ensure the node is selected and visible
136 TreePath path = new TreePath(node.getPath());
137 contents.setSelectionPath(path);
138 contents.scrollPathToVisible(path);
139
140 // Display the selected page
141 URL url = node.getURL();
142 if (url != null) {
143 try {
144 view.setPage(url);
145 }
146 catch (Exception exception) {
147 Gatherer.printStackTrace(exception);
148 }
149 }
150
151 // Make the help frame visible
152 show();
153 split_pane.setDividerLocation(0.2);
154 }
155
156
157 private class ContentsModel
158 extends DefaultTreeModel {
159
160 public ContentsModel(MutableTreeNode rootNode) {
161 super(rootNode);
162
163 // Load the XML help file and build the contents structure from it
164 try {
165 DOMParser parser = new DOMParser();
166 parser.parse(Utility.getHelpFolder() + StaticStrings.HELP_INDEX_FILENAME);
167 Document document = parser.getDocument();
168
169 // Traverse the document hierarchy, building a tree representing its structure
170 Node documentNode = document.getFirstChild();
171
172 int sectionNum = 0;
173 NodeList children = documentNode.getChildNodes();
174 for (int i = 0; i < children.getLength(); i++) {
175 Node child = children.item(i);
176 if (child.getNodeName().equals(StaticStrings.SECTION_ELEMENT)) {
177 sectionNum++;
178 buildContentsHierarchy(rootNode, sectionNum + StaticStrings.EMPTY_STR, child);
179 }
180 }
181 }
182 catch (Exception ex) {
183 ex.printStackTrace();
184 }
185 }
186
187
188 public void buildContentsHierarchy(MutableTreeNode parent, String pos, Node node)
189 {
190 // Determine the section name
191 String sectionName = ((Element) node).getAttribute(StaticStrings.NAME_ATTRIBUTE);
192
193 // Determine the section title
194 String sectionTitle = "";
195 NodeList children = node.getChildNodes();
196 for (int i = 0; i < children.getLength(); i++) {
197 Node child = children.item(i);
198 if (child.getNodeName().equals(StaticStrings.TITLE_ELEMENT)) {
199 sectionTitle = pos + ": ";
200 if (child.getFirstChild() != null) {
201 sectionTitle = sectionTitle + child.getFirstChild().getNodeValue();
202 }
203 }
204 }
205
206 // Add the node into the tree
207 HelpItem item = new HelpItem(sectionTitle, sectionName);
208 insertNodeInto(item, parent, parent.getChildCount());
209
210 // Apply recursively to the children of this node
211 int sectionNum = 0;
212 for (int i = 0; i < children.getLength(); i++) {
213 Node child = children.item(i);
214 if (child.getNodeName().equals(StaticStrings.SECTION_ELEMENT)) {
215 sectionNum++;
216 buildContentsHierarchy(item, pos + StaticStrings.STOP_CHARACTER + sectionNum, child);
217 }
218 }
219 }
220
221
222 public HelpItem getHelpItemNamed(String sectionName)
223 {
224 // Find the node with name matching sectionName, somewhere in the tree
225 Enumeration descendants = ((DefaultMutableTreeNode) root).preorderEnumeration();
226 while (descendants.hasMoreElements()) {
227 HelpItem node = (HelpItem) descendants.nextElement();
228 if (node.name.equals(sectionName)) {
229 return node;
230 }
231 }
232
233 // Couldn't be found, so just return the root (Contents) node
234 return (HelpItem) root;
235 }
236 }
237
238
239 /** This listener is used to listen for selection changes in the contents tree, and
240 * to show the appropriate page as required.
241 */
242 private class ContentsListener
243 implements TreeSelectionListener {
244
245 /** Any implementation of <i>TreeSelectionListener</i> must include this method so we can be informed when the tree selection changes.
246 * @param event A <strong>TreeSelectionEvent</strong> containing al the relevant information about the event itself.
247 */
248 public void valueChanged(TreeSelectionEvent event)
249 {
250 TreePath path = event.getPath();
251 HelpItem node = (HelpItem) path.getLastPathComponent();
252 URL url = node.getURL();
253 if (url != null) {
254 //view.showHTMLDocument(url);
255 try {
256 view.setPage(url);
257 }
258 catch (Exception exception) {
259 Gatherer.printStackTrace(exception);
260 }
261 }
262 }
263 }
264
265
266 /** This class provides a wrapper around a <strong>DefaultMutableTreeNode</strong> which
267 * provides the ability to set an html page to be loaded when this node is selected.
268 */
269 private class HelpItem
270 extends DefaultMutableTreeNode {
271
272 /** The unique name of the section (matches the HTML filename) */
273 public String name = null;
274 /** The title to be displayed for this tree node. */
275 public String title = null;
276
277
278 /** Constructor.
279 * @param title The title to be shown for this node as a <strong>String</strong>.
280 * @param name The name of the section as a <strong>String</strong>.
281 */
282 public HelpItem(String title, String name)
283 {
284 this.name = name;
285 this.title = title;
286 }
287
288
289 /** Method to create a url from the file name.
290 * @return A <strong>URL</strong> that points to the file's location.
291 */
292 public URL getURL()
293 {
294 if (name != null && !name.equals(NULL_STRING)) {
295 try {
296 File file = new File(Utility.getHelpFolder() + name + StaticStrings.HTM_FILE_EXTENSION);
297 return file.toURL();
298 }
299 catch (Exception e) {
300 }
301 }
302
303 return null;
304 }
305
306
307 public String toString()
308 {
309 return title;
310 }
311 }
312}
313
314
315
316
317
318
Note: See TracBrowser for help on using the repository browser.