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

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

Made HelpFrame.setView static to remove another Gatherer class dependency.

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