001: package jimm.datavision.gui;
002:
003: import jimm.datavision.ErrorHandler;
004: import jimm.util.I18N;
005: import java.awt.Dimension;
006: import java.awt.BorderLayout;
007: import java.awt.event.ActionListener;
008: import java.awt.event.ActionEvent;
009: import java.net.URL;
010: import javax.swing.*;
011: import javax.swing.text.html.HTMLDocument;
012: import javax.swing.text.html.HTMLFrameHyperlinkEvent;
013: import javax.swing.event.HyperlinkListener;
014: import javax.swing.event.HyperlinkEvent;
015:
016: /**
017: * A help window. Opens on docs/DataVision/DataVision.html.
018: *
019: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
020: */
021: public class HelpWin extends JFrame implements HyperlinkListener {
022:
023: protected static final int START_WIDTH = 400;
024: protected static final int START_HEIGHT = 500;
025: protected static final String DOCS_DIR = "docs";
026: protected static final String HTML_DIR = "DataVision";
027: protected static final String START_CONTENT_FILE = "DataVision.html";
028:
029: protected static final String HOME_ICON = "images/Home16.gif";
030: protected static final String PREV_ICON = "images/Back16.gif";
031: protected static final String NEXT_ICON = "images/Forward16.gif";
032:
033: protected static HelpWin helpWin;
034:
035: protected JEditorPane contentEditorPane;
036: protected HelpURLStack pages;
037: protected Action goHomeAction;
038: protected Action goPrevAction;
039: protected Action goNextAction;
040: protected JTextField urlField;
041:
042: public static synchronized HelpWin instance() {
043: if (helpWin == null)
044: helpWin = new HelpWin();
045: return helpWin;
046: }
047:
048: protected HelpWin() {
049: super (I18N.get("HelpWin.title"));
050: buildWindow();
051: pages = new HelpURLStack(contentEditorPane, urlField);
052: pack();
053: setVisible(true);
054:
055: new Thread(new Runnable() {
056: public void run() {
057: loadHelp();
058: }
059: }).start();
060: }
061:
062: protected void buildWindow() {
063: makeActions();
064:
065: getContentPane().setLayout(new BorderLayout());
066: getContentPane().add(makeToolbar(), BorderLayout.NORTH);
067: getContentPane().add(buildContentPane(), BorderLayout.CENTER);
068: }
069:
070: protected JComponent buildContentPane() {
071: contentEditorPane = new JEditorPane();
072: contentEditorPane.setEditable(false);
073:
074: // Scroll pane.
075: JScrollPane scrollPane = new JScrollPane(contentEditorPane);
076: scrollPane
077: .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
078: scrollPane.setPreferredSize(new Dimension(START_WIDTH,
079: START_HEIGHT));
080:
081: // Start listening to hyperlink events.
082: contentEditorPane.addHyperlinkListener(this );
083:
084: return scrollPane;
085: }
086:
087: /**
088: * Creates the actions used by menu items and toolbar widgets.
089: */
090: protected void makeActions() {
091: URL url = getClass().getClassLoader().getResource(HOME_ICON);
092: String str = I18N.get("HelpWin.cmd_home");
093: goHomeAction = new AbstractAction(str, new ImageIcon(url, str)) {
094: public void actionPerformed(ActionEvent e) {
095: pages.goToHomePage();
096: updateNavActions();
097: }
098: };
099: goHomeAction.putValue(Action.SHORT_DESCRIPTION, str);
100: goHomeAction.setEnabled(true);
101:
102: url = getClass().getClassLoader().getResource(PREV_ICON);
103: str = I18N.get("HelpWin.cmd_prev");
104: goPrevAction = new AbstractAction(str, new ImageIcon(url, str)) {
105: public void actionPerformed(ActionEvent e) {
106: pages.goToPreviousPage();
107: updateNavActions();
108: }
109: };
110: goPrevAction.putValue(Action.SHORT_DESCRIPTION, str);
111: goPrevAction.setEnabled(false);
112:
113: url = getClass().getClassLoader().getResource(NEXT_ICON);
114: str = I18N.get("HelpWin.cmd_next");
115: goNextAction = new AbstractAction(str, new ImageIcon(url, str)) {
116: public void actionPerformed(ActionEvent e) {
117: pages.goToNextPage();
118: updateNavActions();
119: }
120: };
121: goNextAction.putValue(Action.SHORT_DESCRIPTION, str);
122: goNextAction.setEnabled(false);
123: }
124:
125: /**
126: * Creates and returns a new tool bar.
127: */
128: protected JToolBar makeToolbar() {
129: JToolBar bar = new JToolBar(
130: javax.swing.SwingConstants.HORIZONTAL);
131: bar.add(goHomeAction);
132: bar.add(goPrevAction);
133: bar.add(goNextAction);
134:
135: urlField = new JTextField(40);
136: urlField.addActionListener(new ActionListener() {
137: public void actionPerformed(ActionEvent e) {
138: pages.goTo(urlField.getText());
139: }
140: });
141: bar.add(urlField);
142:
143: return bar;
144: }
145:
146: /**
147: * Updates navigation buttons based on number of pages and current
148: * display page.
149: */
150: protected void updateNavActions() {
151: goPrevAction.setEnabled(pages.hasPrevious());
152: goNextAction.setEnabled(pages.hasNext());
153: }
154:
155: protected void loadHelp() {
156: // Create a URL object for the file docs/DataVision/DataVision.html.
157: String s = null;
158: URL helpURL = null;
159: try {
160: String sep = System.getProperty("file.separator");
161: s = "file:" + System.getProperty("user.dir") + sep
162: + DOCS_DIR + sep + HTML_DIR + sep
163: + START_CONTENT_FILE;
164: helpURL = new URL(s);
165: } catch (Exception e) {
166: ErrorHandler.error(I18N.get("HelpWin.error") + ' ' + s, e);
167: }
168:
169: // Load the URL.
170: if (helpURL != null) {
171: pages.goTo(helpURL);
172: updateNavActions();
173: }
174: }
175:
176: public void hyperlinkUpdate(HyperlinkEvent e) {
177: if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED)
178: return;
179:
180: if (e instanceof HTMLFrameHyperlinkEvent) {
181: HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
182: HTMLDocument doc = (HTMLDocument) contentEditorPane
183: .getDocument();
184: doc.processHTMLFrameHyperlinkEvent(evt);
185: } else {
186: try {
187: pages.goTo(e.getURL());
188: updateNavActions();
189: } catch (Throwable t) {
190: ErrorHandler.error(t);
191: }
192: }
193: }
194:
195: }
|