1 package jimm.datavision.gui; 2 import jimm.datavision.ErrorHandler; 3 import jimm.util.I18N; 4 import java.awt.Dimension ; 5 import java.awt.BorderLayout ; 6 import java.awt.event.ActionListener ; 7 import java.awt.event.ActionEvent ; 8 import java.net.URL ; 9 import javax.swing.*; 10 import javax.swing.text.html.HTMLDocument ; 11 import javax.swing.text.html.HTMLFrameHyperlinkEvent ; 12 import javax.swing.event.HyperlinkListener ; 13 import javax.swing.event.HyperlinkEvent ; 14 15 20 public class HelpWin extends JFrame implements HyperlinkListener { 21 22 protected static final int START_WIDTH = 400; 23 protected static final int START_HEIGHT = 500; 24 protected static final String DOCS_DIR = "docs"; 25 protected static final String HTML_DIR = "DataVision"; 26 protected static final String START_CONTENT_FILE = "DataVision.html"; 27 28 protected static final String HOME_ICON = "images/Home16.gif"; 29 protected static final String PREV_ICON = "images/Back16.gif"; 30 protected static final String NEXT_ICON = "images/Forward16.gif"; 31 32 protected static HelpWin helpWin; 33 34 protected JEditorPane contentEditorPane; 35 protected HelpURLStack pages; 36 protected Action goHomeAction; 37 protected Action goPrevAction; 38 protected Action goNextAction; 39 protected JTextField urlField; 40 41 public static synchronized HelpWin instance() { 42 if (helpWin == null) 43 helpWin = new HelpWin(); 44 return helpWin; 45 } 46 47 protected HelpWin() { 48 super(I18N.get("HelpWin.title")); 49 buildWindow(); 50 pages = new HelpURLStack(contentEditorPane, urlField); 51 pack(); 52 setVisible(true); 53 54 new Thread (new Runnable () { 55 public void run() { loadHelp(); } 56 }).start(); 57 } 58 59 protected void buildWindow() { 60 makeActions(); 61 62 getContentPane().setLayout(new BorderLayout ()); 63 getContentPane().add(makeToolbar(), BorderLayout.NORTH); 64 getContentPane().add(buildContentPane(), BorderLayout.CENTER); 65 } 66 67 protected JComponent buildContentPane() { 68 contentEditorPane = new JEditorPane(); 69 contentEditorPane.setEditable(false); 70 71 JScrollPane scrollPane = new JScrollPane(contentEditorPane); 73 scrollPane.setVerticalScrollBarPolicy( 74 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 75 scrollPane.setPreferredSize(new Dimension (START_WIDTH, START_HEIGHT)); 76 77 contentEditorPane.addHyperlinkListener(this); 79 80 return scrollPane; 81 } 82 83 86 protected void makeActions() { 87 URL url = getClass().getClassLoader().getResource(HOME_ICON); 88 String str = I18N.get("HelpWin.cmd_home"); 89 goHomeAction = new AbstractAction(str, new ImageIcon(url, str)) { 90 public void actionPerformed(ActionEvent e) { 91 pages.goToHomePage(); 92 updateNavActions(); 93 } 94 }; 95 goHomeAction.putValue(Action.SHORT_DESCRIPTION, str); 96 goHomeAction.setEnabled(true); 97 98 url = getClass().getClassLoader().getResource(PREV_ICON); 99 str = I18N.get("HelpWin.cmd_prev"); 100 goPrevAction = new AbstractAction(str, new ImageIcon(url, str)) { 101 public void actionPerformed(ActionEvent e) { 102 pages.goToPreviousPage(); 103 updateNavActions(); 104 } 105 }; 106 goPrevAction.putValue(Action.SHORT_DESCRIPTION, str); 107 goPrevAction.setEnabled(false); 108 109 url = getClass().getClassLoader().getResource(NEXT_ICON); 110 str = I18N.get("HelpWin.cmd_next"); 111 goNextAction = new AbstractAction(str, new ImageIcon(url, str)) { 112 public void actionPerformed(ActionEvent e) { 113 pages.goToNextPage(); 114 updateNavActions(); 115 } 116 }; 117 goNextAction.putValue(Action.SHORT_DESCRIPTION, str); 118 goNextAction.setEnabled(false); 119 } 120 121 124 protected JToolBar makeToolbar() { 125 JToolBar bar = new JToolBar(javax.swing.SwingConstants.HORIZONTAL); 126 bar.add(goHomeAction); 127 bar.add(goPrevAction); 128 bar.add(goNextAction); 129 130 urlField = new JTextField(40); 131 urlField.addActionListener(new ActionListener () { 132 public void actionPerformed(ActionEvent e) { 133 pages.goTo(urlField.getText()); 134 } 135 }); 136 bar.add(urlField); 137 138 return bar; 139 } 140 141 145 protected void updateNavActions() { 146 goPrevAction.setEnabled(pages.hasPrevious()); 147 goNextAction.setEnabled(pages.hasNext()); 148 } 149 150 protected void loadHelp() { 151 String s = null; 153 URL helpURL = null; 154 try { 155 String sep = System.getProperty("file.separator"); 156 s = "file:" + System.getProperty("user.dir") + sep 157 + DOCS_DIR + sep + HTML_DIR + sep + START_CONTENT_FILE; 158 helpURL = new URL (s); 159 } catch (Exception e) { 160 ErrorHandler.error(I18N.get("HelpWin.error") + ' ' + s, e); 161 } 162 163 if (helpURL != null) { 165 pages.goTo(helpURL); 166 updateNavActions(); 167 } 168 } 169 170 public void hyperlinkUpdate(HyperlinkEvent e) { 171 if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED) 172 return; 173 174 if (e instanceof HTMLFrameHyperlinkEvent ) { 175 HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent )e; 176 HTMLDocument doc = (HTMLDocument )contentEditorPane.getDocument(); 177 doc.processHTMLFrameHyperlinkEvent(evt); 178 } 179 else { 180 try { 181 pages.goTo(e.getURL()); 182 updateNavActions(); 183 } 184 catch (Throwable t) { 185 ErrorHandler.error(t); 186 } 187 } 188 } 189 190 } 191 | Popular Tags |