source: gli/branches/rtl-gli/src/org/greenstone/gatherer/gui/HelpFrame.java@ 18297

Last change on this file since 18297 was 18297, checked in by kjdon, 15 years ago

interface updated to display right to left for rtl languages. This code is thanks to Amin Hejazi. It seems to be only partially complete. Amin was working with Greenstone 3 so might have missed some panels

  • Property svn:keywords set to Author Date Id Revision
File size: 12.2 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.gui;
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.greenstone.gatherer.Configuration;
47import org.greenstone.gatherer.DebugStream;
48import org.greenstone.gatherer.Dictionary;
49import org.greenstone.gatherer.Gatherer;
50import org.greenstone.gatherer.util.JarTools;
51import org.greenstone.gatherer.util.StaticStrings;
52import org.greenstone.gatherer.util.XMLTools;
53import org.w3c.dom.*;
54
55
56/**
57 * This class provides a nice help facility. It is a separate frame that can be positioned
58 * as the user wishes, and provides a contents page and the help information.
59 * @author Michael Dewsnip, NZDL Project
60 */
61public class HelpFrame
62 extends JFrame
63{
64 /** The size of the frame */
65 static private final Dimension SIZE = new Dimension(800, 560);
66
67 static private HelpFrame self = null;
68
69 /** The help view at the bottom of the frame. */
70 static private JEditorPane help_pane = null;
71 /** The help contents tree at the top of the frame. */
72 static private JTree help_contents_tree = null;
73 /** The help contents tree model. */
74 static private HelpContentsTreeModel help_contents_tree_model = null;
75 /** The split between the contents tree and the page view. */
76 static private JSplitPane split_pane = null;
77
78 public HelpFrame()
79 {
80 setDefaultCloseOperation(HIDE_ON_CLOSE);
81 setSize(SIZE);
82 setTitle(Dictionary.get("Help.Title"));
83 this.setComponentOrientation(Dictionary.getOrientation());
84
85 help_pane = new JEditorPane();
86 help_pane.setComponentOrientation(Dictionary.getOrientation());
87 help_pane.setEditable(false);
88 help_pane.addHyperlinkListener(new HelpPaneHyperlinkListener());
89
90 HelpContentsTreeNode help_tree_root_node = new HelpContentsTreeNode(null, Dictionary.get("Help.Contents"));
91 help_contents_tree_model = new HelpContentsTreeModel(help_tree_root_node);
92 help_contents_tree = new JTree((DefaultTreeModel) help_contents_tree_model);
93 help_contents_tree.setComponentOrientation(Dictionary.getOrientation());
94 help_contents_tree.addTreeSelectionListener(new HelpContentsTreeSelectionListener());
95 help_contents_tree.setExpandsSelectedPaths(true);
96
97 // Creation
98 JPanel content_pane = (JPanel) this.getContentPane();
99 content_pane.setComponentOrientation(Dictionary.getOrientation());
100 split_pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
101 JScrollPane help_contents_tree_scroll = new JScrollPane(help_contents_tree);
102 help_contents_tree_scroll.setComponentOrientation(Dictionary.getOrientation());
103 JScrollPane help_pane_scroll = new JScrollPane(help_pane);
104 help_pane_scroll.setComponentOrientation(Dictionary.getOrientation());
105
106 // Layout
107 if (Dictionary.getOrientation().isLeftToRight()){
108 split_pane.add(help_contents_tree_scroll, JSplitPane.LEFT);
109 split_pane.add(help_pane_scroll, JSplitPane.RIGHT);
110 }else{
111 split_pane.add(help_contents_tree_scroll, JSplitPane.RIGHT);
112 split_pane.add(help_pane_scroll, JSplitPane.LEFT);
113 }
114
115 content_pane.setLayout(new BorderLayout());
116 content_pane.add(split_pane, BorderLayout.CENTER);
117
118 // Center
119 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
120 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
121
122 // Pretty corner icon
123 this.setIconImage(JarTools.getImage("gatherer_small.gif").getImage());
124
125 self = this;
126 }
127
128 public void destroy()
129 {
130 help_contents_tree = null;
131 help_pane = null;
132 }
133
134
135 /** Retrieve the relative file path to the language-specific help index xml file. */
136 private String getHelpFolder()
137 {
138 String help_folder = "help/" + Configuration.getLanguage() + "/";
139
140 // Applet case: get help files out of JAR file
141 if (Gatherer.isApplet && JarTools.getResource("/" + help_folder) != null) {
142 return help_folder;
143 }
144
145 // Normal case: get help files out of GLI "help" folder
146 if (new File(help_folder).exists()) {
147 return help_folder;
148 }
149
150 // Resort to English
151 return "help/" + StaticStrings.ENGLISH_LANGUAGE_STR + "/";
152 }
153
154
155 private URL getURLForSection(String section_name)
156 {
157 // Form the path to the help file from the section name
158 if (section_name != null) {
159 String help_file_path = getHelpFolder() + section_name + StaticStrings.HTM_FILE_EXTENSION;
160
161 try {
162 // Applet case: get help file out of JAR file
163 if (Gatherer.isApplet) {
164 return JarTools.getResource("/" + help_file_path);
165 }
166
167 // Normal case: get help file out of GLI "help" folder
168 return (new File(help_file_path)).toURL();
169 }
170 catch (Exception exception) {
171 DebugStream.printStackTrace(exception);
172 }
173 }
174
175 return null;
176 }
177
178
179 static private void selectHelpContentsTreeNode(String section_name)
180 {
181 // Find the tree node with this section name, then select it
182 selectHelpContentsTreeNode(help_contents_tree_model.getHelpContentsTreeNodeNamed(section_name));
183 }
184
185
186 static private void selectHelpContentsTreeNode(HelpContentsTreeNode help_tree_node)
187 {
188 // Ensure the node in the help contents tree is selected and visible
189 TreePath help_tree_node_path = new TreePath(help_tree_node.getPath());
190 help_contents_tree.setSelectionPath(help_tree_node_path);
191 help_contents_tree.scrollPathToVisible(help_tree_node_path);
192 }
193
194
195 static public void setView(String section_name)
196 {
197 // Select the appropriate node in the help contents tree
198 selectHelpContentsTreeNode(section_name);
199
200 // Show the help page with the specified section name
201 self.showHelpPage(section_name);
202 }
203
204
205 private void showHelpPage(String section_name)
206 {
207 // Find the tree node with this section name, then show the page for the node
208 showHelpPage(getURLForSection(section_name));
209 }
210
211
212 private void showHelpPage(URL help_page_url)
213 {
214 // Display the selected page
215 if (help_page_url != null) {
216 try {
217 help_pane.setPage(help_page_url);
218 }
219 catch (Exception exception) {
220 DebugStream.printStackTrace(exception);
221 }
222 }
223
224 // Make the help frame visible
225 setVisible(true);
226 split_pane.setDividerLocation(0.4);
227 }
228
229
230 private class HelpContentsTreeModel
231 extends DefaultTreeModel
232 {
233 public HelpContentsTreeModel(MutableTreeNode help_tree_root_node)
234 {
235 super(help_tree_root_node);
236
237 // Load the XML help index file and build the contents structure from it
238 try {
239 String help_index_file_path = getHelpFolder() + "help_index.xml";
240 Document document = XMLTools.parseXMLFile(help_index_file_path, true);
241
242 // Traverse the help hierarchy, building a tree representing its structure
243 Node document_node = document.getFirstChild();
244
245 // Add a help contents tree node for each major section
246 int section_number = 0;
247 NodeList children = document_node.getChildNodes();
248 for (int i = 0; i < children.getLength(); i++) {
249 Node child = children.item(i);
250 if (child.getNodeName().equals(StaticStrings.SECTION_ELEMENT)) {
251 section_number++;
252 buildHelpContentsTree(help_tree_root_node, section_number + StaticStrings.EMPTY_STR, child);
253 }
254 }
255 }
256 catch (Exception exception) {
257 DebugStream.printStackTrace(exception);
258 }
259 }
260
261
262 private void buildHelpContentsTree(MutableTreeNode parent, String pos, Node node)
263 {
264 // Determine the section name
265 String section_name = ((Element) node).getAttribute(StaticStrings.NAME_ATTRIBUTE);
266
267 // Determine the section title
268 String section_title = "";
269 NodeList children = node.getChildNodes();
270 for (int i = 0; i < children.getLength(); i++) {
271 Node child = children.item(i);
272 if (child.getNodeName().equals(StaticStrings.TITLE_ELEMENT)) {
273 section_title = pos + ": ";
274 if (child.getFirstChild() != null) {
275 section_title += child.getFirstChild().getNodeValue();
276 }
277 }
278 }
279
280 // Add the node into the tree
281 HelpContentsTreeNode help_tree_node = new HelpContentsTreeNode(section_name, section_title);
282 insertNodeInto(help_tree_node, parent, parent.getChildCount());
283
284 // Apply recursively to the children of this node
285 int section_number = 0;
286 for (int i = 0; i < children.getLength(); i++) {
287 Node child = children.item(i);
288 if (child.getNodeName().equals(StaticStrings.SECTION_ELEMENT)) {
289 section_number++;
290 buildHelpContentsTree(help_tree_node, pos + StaticStrings.STOP_CHARACTER + section_number, child);
291 }
292 }
293 }
294
295
296 private HelpContentsTreeNode getHelpContentsTreeNodeNamed(String section_name)
297 {
298 // Find the node with name matching section name, somewhere in the tree
299 if (section_name != null) {
300 Enumeration descendants = ((DefaultMutableTreeNode) root).preorderEnumeration();
301 while (descendants.hasMoreElements()) {
302 HelpContentsTreeNode help_tree_node = (HelpContentsTreeNode) descendants.nextElement();
303 if (section_name.equals(help_tree_node.section_name)) {
304 return help_tree_node;
305 }
306 }
307 }
308
309 // Couldn't be found, so just return the root (Contents) node
310 return (HelpContentsTreeNode) root;
311 }
312 }
313
314
315 /** This listener is used to listen for selection changes in the contents tree, and
316 * to show the appropriate page as required.
317 */
318 private class HelpContentsTreeSelectionListener
319 implements TreeSelectionListener
320 {
321 /** Any implementation of <i>TreeSelectionListener</i> must include this method so we can be informed when the tree selection changes.
322 * @param event A <strong>TreeSelectionEvent</strong> containing al the relevant information about the event itself.
323 */
324 public void valueChanged(TreeSelectionEvent event)
325 {
326 HelpContentsTreeNode help_tree_node = (HelpContentsTreeNode) event.getPath().getLastPathComponent();
327 selectHelpContentsTreeNode(help_tree_node);
328 showHelpPage(help_tree_node.section_name);
329 }
330 }
331
332
333 /** This class provides a wrapper around a <strong>DefaultMutableTreeNode</strong> which
334 * provides the ability to set an html page to be loaded when this node is selected.
335 */
336 private class HelpContentsTreeNode
337 extends DefaultMutableTreeNode
338 {
339 /** The unique name of the section (matches the HTML filename) */
340 public String section_name = null;
341 /** The title to be displayed for this tree node. */
342 public String section_title = null;
343
344 public HelpContentsTreeNode(String section_name, String section_title)
345 {
346 this.section_name = section_name;
347 this.section_title = section_title;
348 }
349
350 public String toString()
351 {
352 return section_title;
353 }
354 }
355
356
357 private class HelpPaneHyperlinkListener
358 implements HyperlinkListener
359 {
360 public void hyperlinkUpdate(HyperlinkEvent e)
361 {
362 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
363 // Determine which node in the help contents tree to select, then select it
364 String section_link = e.getURL().getFile();
365 if (section_link.endsWith(".htm")) {
366 section_link = section_link.substring(section_link.lastIndexOf("/") + 1);
367 section_link = section_link.substring(0, section_link.length() - ".htm".length());
368 selectHelpContentsTreeNode(section_link);
369 }
370
371 // Change to the page specified by the link
372 showHelpPage(e.getURL());
373 }
374 }
375 }
376}
Note: See TracBrowser for help on using the repository browser.