KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > help > HelpManager


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16

17 package org.columba.core.help;
18
19 import java.awt.Component JavaDoc;
20 import java.awt.event.ActionEvent JavaDoc;
21 import java.awt.event.ActionListener JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.Locale JavaDoc;
24
25 import javax.help.HelpBroker;
26 import javax.help.HelpSet;
27 import javax.help.JHelp;
28 import javax.help.TextHelpModel;
29 import javax.swing.JFrame JavaDoc;
30 import javax.swing.JMenu JavaDoc;
31 import javax.swing.JMenuBar JavaDoc;
32 import javax.swing.JMenuItem JavaDoc;
33 import javax.swing.JOptionPane JavaDoc;
34
35 import org.columba.core.gui.frame.FrameManager;
36
37 /**
38  * @author fdietz
39  *
40  * This class manages all JavaHelp relevant helpsets, its also encapsulates the
41  * broker which is used for context sensitiv help. This class is a singleton.
42  */

43 public class HelpManager {
44     private static HelpManager instance;
45
46     // name of helpset resource
47
final static String JavaDoc helpsetName = "jhelpset";
48
49     private JHelp jh = null;
50
51     private HelpSet hs = null;
52
53     private HelpBroker hb = null;
54
55     private JFrame JavaDoc frame;
56
57     /**
58      * Creates a new instance. This method is private because it should only get
59      * called from the static getHelpManager() method.
60      */

61     private HelpManager() {
62         ClassLoader JavaDoc loader = getClass().getClassLoader();
63         URL JavaDoc url = HelpSet.findHelpSet(loader, helpsetName, "", Locale
64                 .getDefault());
65
66         if (url == null) {
67             url = HelpSet.findHelpSet(loader, helpsetName, ".hs", Locale
68                     .getDefault());
69
70             if (url == null) {
71                 // could not find it!
72
JOptionPane.showMessageDialog(FrameManager.getInstance()
73                         .getActiveFrame(), "HelpSet not found",
74                         "Error", JOptionPane.ERROR_MESSAGE);
75
76                 return;
77             }
78         }
79
80         try {
81             hs = new HelpSet(loader, url);
82         } catch (Exception JavaDoc ee) {
83             JOptionPane.showMessageDialog(FrameManager.getInstance()
84                     .getActiveFrame(), "HelpSet not found", "Error",
85                     JOptionPane.ERROR_MESSAGE);
86
87             return;
88         }
89
90         // The JavaHelp can't be added to a BorderLayout because it
91
// isnt' a component. For this demo we'll use the embeded method
92
// since we don't want a Frame to be created.
93
hb = hs.createHelpBroker();
94
95     }
96
97     /**
98      * Opens the help frame.
99      */

100     public void openHelpFrame() {
101
102         jh = new JHelp(hs);
103
104         TextHelpModel m = jh.getModel();
105         HelpSet hs = m.getHelpSet();
106         String JavaDoc title = hs.getTitle();
107
108         if (title == null || title.equals("")) {
109             title = "Unnamed HelpSet"; // maybe based on HS?
110
}
111
112         frame = new JFrame JavaDoc(title);
113         frame.getContentPane().add(jh);
114         JMenuBar JavaDoc menuBar = new JMenuBar JavaDoc();
115         JMenuItem JavaDoc mi;
116         JMenu JavaDoc file = (JMenu JavaDoc) menuBar.add(new JMenu JavaDoc("File"));
117         file.setMnemonic('F');
118
119         mi = (JMenuItem JavaDoc) file.add(new JMenuItem JavaDoc("Exit"));
120         mi.setMnemonic('x');
121         mi.addActionListener(new ActionListener JavaDoc() {
122             public void actionPerformed(ActionEvent JavaDoc e) {
123                 frame.setVisible(false);
124             }
125         });
126
127         // JMenu options = (JMenu) menuBar.add(new JMenu("Options"));
128
// options.setMnemonic('O');
129
frame.setJMenuBar(menuBar);
130         frame.pack();
131         frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
132
133         frame.setVisible(true);
134     }
135
136     /**
137      * @return
138      */

139     public HelpBroker getHelpBroker() {
140         return hb;
141     }
142
143     /**
144      * Associate button with topic ID.
145      *
146      * Topic ID's are listed in jhelpmap.jhm in package lib/usermanual.jar
147      *
148      * @param c
149      * component
150      * @param helpID
151      * helpID
152      */

153     public void enableHelpOnButton(Component JavaDoc c, String JavaDoc helpID) {
154         getHelpBroker().enableHelpOnButton(c, helpID, hs);
155     }
156
157     /**
158      * Enables the F1 help key on components.
159      */

160     public void enableHelpKey(Component JavaDoc c, String JavaDoc helpID) {
161         getHelpBroker().enableHelpKey(c, helpID, hs);
162     }
163
164     /**
165      * Returns the singleton help manager instance.
166      */

167     public static HelpManager getInstance() {
168         if (instance == null) {
169             instance = new HelpManager();
170         }
171
172         return instance;
173     }
174 }
175
Popular Tags