1 import javax.swing.JFrame; 2 import javax.swing.JScrollPane; 3 import javax.swing.JEditorPane; 4 import javax.swing.JButton; 5 import java.net.URL; 6 import javax.swing.event.HyperlinkListener; 7 import javax.swing.event.HyperlinkEvent; 8 import java.net.MalformedURLException; 9 import java.io.IOException; 10 import java.io.File; 11 import java.awt.event.WindowAdapter; 12 import java.awt.event.WindowEvent; 13 import java.awt.GridBagLayout; 14 import java.awt.GridBagConstraints; 15 import java.awt.event.ActionListener; 16 import java.util.LinkedList; 17 18 class HelpWindow 19 implements ActionListener 20 { 21 public static synchronized HelpWindow Instance () 22 { 23 if (maInstance == null) 24 maInstance = new HelpWindow(); 25 return maInstance; 26 } 27 28 public void loadFile (String sFilename) 29 { 30 File aFile = new File (sFilename); 31 try 32 { 33 loadURL (aFile.toURL()); 34 } 35 catch (MalformedURLException e) 36 { 37 e.printStackTrace (System.err); 38 } 39 } 40 public void loadURL (String sURL) 41 { 42 try 43 { 44 loadURL (new URL (sURL)); 45 } 46 catch (MalformedURLException e) 47 { 48 e.printStackTrace (System.err); 49 } 50 } 51 52 53 54 55 public void loadURL (URL aURL) 56 { 57 maHistory.addLast (aURL); 58 selectHistoryPage (maHistory.size()-1); 59 maFrame.toFront (); 60 } 61 62 63 64 65 private HelpWindow () 66 { 67 try 68 { 69 maCurrentHistoryEntry = -1; 70 maHistory = new LinkedList(); 71 72 maFrame = new JFrame (); 73 maFrame.addWindowListener (new WindowAdapter () 74 { 75 public void windowClosing (WindowEvent e) 76 { 77 maInstance = null; 78 } 79 }); 80 maContent = createContentWidget(); 81 82 maFrame.getContentPane().setLayout (new GridBagLayout()); 83 GridBagConstraints aConstraints = new GridBagConstraints (); 84 aConstraints.gridx = 0; 85 aConstraints.gridy = 0; 86 aConstraints.gridwidth = 3; 87 aConstraints.weightx = 1; 88 aConstraints.weighty = 1; 89 aConstraints.fill = GridBagConstraints.BOTH; 90 maFrame.getContentPane().add (new JScrollPane (maContent), aConstraints); 91 92 aConstraints = new GridBagConstraints(); 93 aConstraints.gridx = 0; 94 aConstraints.gridy = 1; 95 maPrevButton = new JButton ("Prev"); 96 maFrame.getContentPane().add (maPrevButton, aConstraints); 97 maPrevButton.addActionListener (this); 98 99 aConstraints = new GridBagConstraints(); 100 aConstraints.gridx = 1; 101 aConstraints.gridy = 1; 102 maNextButton = new JButton ("Next"); 103 maFrame.getContentPane().add (maNextButton, aConstraints); 104 maNextButton.addActionListener (this); 105 106 aConstraints = new GridBagConstraints(); 107 aConstraints.gridx = 2; 108 aConstraints.gridy = 1; 109 aConstraints.anchor = GridBagConstraints.EAST; 110 JButton aButton = new JButton ("Close"); 111 maFrame.getContentPane().add (aButton, aConstraints); 112 aButton.addActionListener (this); 113 114 maFrame.setSize (600,400); 115 maFrame.setVisible (true); 116 } 117 catch (Exception e) 118 {} 119 } 120 121 public void actionPerformed (java.awt.event.ActionEvent e) 122 { 123 if (e.getActionCommand().equals("Prev")) 124 { 125 selectHistoryPage (maCurrentHistoryEntry - 1); 126 } 127 else if (e.getActionCommand().equals("Next")) 128 { 129 selectHistoryPage (maCurrentHistoryEntry + 1); 130 } 131 else if (e.getActionCommand().equals("Close")) 132 { 133 maFrame.dispose (); 134 maInstance = null; 135 } 136 } 137 138 private JEditorPane createContentWidget () 139 { 140 JEditorPane aContent = new JEditorPane (); 141 aContent.setEditable (false); 142 aContent.addHyperlinkListener (new HyperlinkListener() 143 { 144 public void hyperlinkUpdate (HyperlinkEvent e) 145 { 146 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) 147 HelpWindow.Instance().loadURL (e.getURL()); 148 } 149 }); 150 return aContent; 151 } 152 153 private void selectHistoryPage (int i) 154 { 155 if (i < 0) 156 i = 0; 157 else if (i >= maHistory.size()-1) 158 i = maHistory.size()-1; 159 if (i != maCurrentHistoryEntry) 160 { 161 URL aURL = (URL)maHistory.get (i); 162 try 163 { 164 maContent.setPage (aURL); 165 } 166 catch (java.io.IOException ex) 167 { 168 ex.printStackTrace(System.err); 169 } 170 171 maCurrentHistoryEntry = i; 172 } 173 174 maPrevButton.setEnabled (maCurrentHistoryEntry > 0); 175 maNextButton.setEnabled (maCurrentHistoryEntry < maHistory.size()-1); 176 } 177 178 private static HelpWindow maInstance = null; 179 private JFrame maFrame; 180 private JEditorPane maContent; 181 private LinkedList maHistory; 182 private int maCurrentHistoryEntry; 183 private JButton maPrevButton; 184 private JButton maNextButton; 185 } 186