1 package org.astrogrid.ui.help;
2
3 import java.awt.*;
4 import javax.swing.*;
5 import java.net.URL;
6 import javax.swing.text.html.*;
7 import java.util.Vector;
8 import java.io.IOException;
9 import java.net.MalformedURLException;
10 import java.awt.event.ActionListener;
11 import java.awt.event.ActionEvent;
12 import org.astrogrid.log.Log;
13
14
15 /*** JHelpBrowser
16 *
17 * A swing implementation of HelpBrowser, using HTMLDocument to view
18 * the pages.
19 *
20 * @Created : Feb 2001
21 * @Last Update :
22 *
23 * @author : M Hill
24 *
25 */
26
27 public class JHelpBrowser extends JFrame implements HelpBrowser, ActionListener
28 {
29 JEditorPane textPane = new JEditorPane();
30
31 History history = new History();
32
33 JButton doneButton;
34 JButton backButton;
35 JButton fwdButton;
36 JButton helpButton;
37
38 /***
39 * Constructor
40 */
41 public JHelpBrowser()
42 {
43 super(Help.getBrowserTitle());
44
45 textPane.setEditorKit(new HTMLEditorKit());
46 JScrollPane scroller = new JScrollPane(textPane);
47
48 JButton doneButton = newButton("Done", "Close Help");
49 JButton backButton = newButton("<", "Return to previous page");
50 JButton fwdButton = newButton(">", "Go to next visited page");
51 JButton helpButton = newButton("Help", "Help on how to use Help");
52
53 JPanel controlPanel = new JPanel();
54 controlPanel.add(backButton);
55 controlPanel.add(fwdButton);
56 controlPanel.add(doneButton);
57
58 getContentPane().setLayout(new BorderLayout());
59 getContentPane().add(scroller, BorderLayout.CENTER);
60 getContentPane().add(controlPanel, BorderLayout.SOUTH);
61
62 new org.astrogrid.ui.EscEnterListener(this, doneButton, doneButton);
63
64
65 }
66
67 /***
68 * Convenience routine that greates a standard button for this box
69 */
70 private JButton newButton(String label, String tooltip)
71 {
72 JButton b = null;
73 try {
74 String iconPath = label+".gif";
75 ImageIcon icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(iconPath));
76 if (icon != null)
77 b = new JButton(icon);
78 }
79 catch (Exception e) {}
80 if (b == null)
81 b = new JButton(label);
82
83 b.setToolTipText(tooltip);
84
85 b.addActionListener(this);
86
87 return b;
88 }
89
90 /***
91 * Show a page at particular url
92 */
93 public void showHelp(URL helpUrl) throws HelpNotFoundException
94 {
95 String path = null;
96 try
97 {
98 history.add(helpUrl);
99 textPane.setText("Loading "+helpUrl+"...");
100 textPane.setPage(helpUrl);
101 show();
102 }
103 catch (IOException ioe)
104 {
105 throw new HelpNotFoundException("I/O Error for "+path,ioe);
106 }
107
108 }
109
110 public void setEditable(boolean b)
111 {
112 textPane.setEditable(b);
113 }
114
115 public HelpBrowser createNewBrowser()
116 {
117 return new JHelpBrowser();
118 }
119
120 /***
121 * Button has been pressed
122 */
123 public void actionPerformed(ActionEvent anEvent)
124 {
125 if (anEvent.getSource() == backButton)
126 {
127 try
128 {
129 showHelp( (URL) history.moveBack());
130 }
131 catch (HelpNotFoundException hnfe)
132 {
133 Log.logError("Failed to load help @"+history.getCurrentItem(),hnfe);
134 }
135 }
136 else if (anEvent.getSource() == fwdButton)
137 {
138 try
139 {
140 showHelp( (URL) history.moveForward());
141 }
142 catch (HelpNotFoundException hnfe)
143 {
144 Log.logError("Failed to load help @"+history.getCurrentItem(),hnfe);
145 }
146 }
147 else if (anEvent.getSource() == helpButton)
148 {
149 try
150 {
151 showHelp(Help.makePageUrl(HELP_ON_HELP,""));
152 }
153 catch (HelpNotFoundException hnfe)
154 {
155 Log.logError("Failed to load help no help",hnfe);
156 }
157 catch (MalformedURLException mfue)
158 {
159 Log.logError("Help on help url is bad ",mfue);
160 }
161 }
162 else if (anEvent.getSource() == doneButton)
163 {
164 dispose();
165 }
166 }
167
168 /***
169 * Test harness
170 */
171 public static void main(String[] args)
172 {
173 Help.setMainBrowser(new JHelpBrowser());
174 Help.setRoot("file:///temp//");
175 Help.showHelp(Help.INDEX);
176 }
177
178 }