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

Last change on this file since 5521 was 5521, checked in by mdewsnip, 21 years ago

Rewrote to use the new XML help file and construct the contents automatically.

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