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

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

Changed calls to the Dictionary.

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