001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016:
017: package org.columba.core.help;
018:
019: import java.awt.Component;
020: import java.awt.event.ActionEvent;
021: import java.awt.event.ActionListener;
022: import java.net.URL;
023: import java.util.Locale;
024:
025: import javax.help.HelpBroker;
026: import javax.help.HelpSet;
027: import javax.help.JHelp;
028: import javax.help.TextHelpModel;
029: import javax.swing.JFrame;
030: import javax.swing.JMenu;
031: import javax.swing.JMenuBar;
032: import javax.swing.JMenuItem;
033: import javax.swing.JOptionPane;
034:
035: import org.columba.core.gui.frame.FrameManager;
036:
037: /**
038: * @author fdietz
039: *
040: * This class manages all JavaHelp relevant helpsets, its also encapsulates the
041: * broker which is used for context sensitiv help. This class is a singleton.
042: */
043: public class HelpManager {
044: private static HelpManager instance;
045:
046: // name of helpset resource
047: final static String helpsetName = "jhelpset";
048:
049: private JHelp jh = null;
050:
051: private HelpSet hs = null;
052:
053: private HelpBroker hb = null;
054:
055: private JFrame frame;
056:
057: /**
058: * Creates a new instance. This method is private because it should only get
059: * called from the static getHelpManager() method.
060: */
061: private HelpManager() {
062: ClassLoader loader = getClass().getClassLoader();
063: URL url = HelpSet.findHelpSet(loader, helpsetName, "", Locale
064: .getDefault());
065:
066: if (url == null) {
067: url = HelpSet.findHelpSet(loader, helpsetName, ".hs",
068: Locale.getDefault());
069:
070: if (url == null) {
071: // could not find it!
072: JOptionPane.showMessageDialog(FrameManager
073: .getInstance().getActiveFrame(),
074: "HelpSet not found", "Error",
075: JOptionPane.ERROR_MESSAGE);
076:
077: return;
078: }
079: }
080:
081: try {
082: hs = new HelpSet(loader, url);
083: } catch (Exception ee) {
084: JOptionPane.showMessageDialog(FrameManager.getInstance()
085: .getActiveFrame(), "HelpSet not found", "Error",
086: JOptionPane.ERROR_MESSAGE);
087:
088: return;
089: }
090:
091: // The JavaHelp can't be added to a BorderLayout because it
092: // isnt' a component. For this demo we'll use the embeded method
093: // since we don't want a Frame to be created.
094: hb = hs.createHelpBroker();
095:
096: }
097:
098: /**
099: * Opens the help frame.
100: */
101: public void openHelpFrame() {
102:
103: jh = new JHelp(hs);
104:
105: TextHelpModel m = jh.getModel();
106: HelpSet hs = m.getHelpSet();
107: String title = hs.getTitle();
108:
109: if (title == null || title.equals("")) {
110: title = "Unnamed HelpSet"; // maybe based on HS?
111: }
112:
113: frame = new JFrame(title);
114: frame.getContentPane().add(jh);
115: JMenuBar menuBar = new JMenuBar();
116: JMenuItem mi;
117: JMenu file = (JMenu) menuBar.add(new JMenu("File"));
118: file.setMnemonic('F');
119:
120: mi = (JMenuItem) file.add(new JMenuItem("Exit"));
121: mi.setMnemonic('x');
122: mi.addActionListener(new ActionListener() {
123: public void actionPerformed(ActionEvent e) {
124: frame.setVisible(false);
125: }
126: });
127:
128: // JMenu options = (JMenu) menuBar.add(new JMenu("Options"));
129: // options.setMnemonic('O');
130: frame.setJMenuBar(menuBar);
131: frame.pack();
132: frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
133:
134: frame.setVisible(true);
135: }
136:
137: /**
138: * @return
139: */
140: public HelpBroker getHelpBroker() {
141: return hb;
142: }
143:
144: /**
145: * Associate button with topic ID.
146: *
147: * Topic ID's are listed in jhelpmap.jhm in package lib/usermanual.jar
148: *
149: * @param c
150: * component
151: * @param helpID
152: * helpID
153: */
154: public void enableHelpOnButton(Component c, String helpID) {
155: getHelpBroker().enableHelpOnButton(c, helpID, hs);
156: }
157:
158: /**
159: * Enables the F1 help key on components.
160: */
161: public void enableHelpKey(Component c, String helpID) {
162: getHelpBroker().enableHelpKey(c, helpID, hs);
163: }
164:
165: /**
166: * Returns the singleton help manager instance.
167: */
168: public static HelpManager getInstance() {
169: if (instance == null) {
170: instance = new HelpManager();
171: }
172:
173: return instance;
174: }
175: }
|