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

Last change on this file since 6580 was 6580, checked in by jmt12, 20 years ago

Now looks for help files in the right place. Also replaced CalPane with JEditorPane.

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