1 import java.awt.*; 2 import java.awt.event.*; 3 import java.io.File ; 4 import java.net.URL ; 5 import javax.swing.*; 6 import javax.swing.border.*; 7 import javax.swing.event.*; 8 9 import java.io.IOException ; 10 11 16 public final class HelpWindow{ 17 private static final int GAP=5; private static final File HELP_DIR=new File ("help"); private static final String DEFAULT="en"; 21 final JFrame frame; 22 24 28 final JEditorPane html=new JEditorPane(); 29 final JScrollPane scrollPane=new JScrollPane(html); 30 31 final JEditorPane contents=new JEditorPane(); 32 final JScrollPane contentsScrollPane=new JScrollPane(contents); 33 34 final JSplitPane splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,false,contentsScrollPane,scrollPane); 35 36 private String currentLang=""; 37 39 40 public HelpWindow(){ 41 frame=new JFrame(Lang.gs("help title")); 42 frame.setDefaultLookAndFeelDecorated(true); 43 frame.setIconImage(Icons.HELP.getImage()); 44 frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 45 Container cp=frame.getContentPane(); 46 cp.setLayout(new BorderLayout()); 47 splitpane.setResizeWeight(0.3); 60 GUI.setScrollBars(scrollPane); 61 GUI.setScrollBars(contentsScrollPane); 62 html.setEditable(false); 63 html.setContentType("text/html"); 64 html.addHyperlinkListener(new HyperLinkClick()); 65 contents.setEditable(false); 66 contents.setContentType("text/html"); 67 contents.addHyperlinkListener(new HyperLinkClick()); 68 cp.add(splitpane,BorderLayout.CENTER); 71 frame.pack(); 72 81 frame.setSize(Form.d2); 86 frame.addKeyListener(new KeyAction()); 87 frame.addMouseListener(new MouseAction()); 88 } 89 90 95 97 98 public void dispose(){ 99 frame.dispose(); 100 } 101 102 public JFrame getFrame(){ 103 return frame; 104 } 105 106 112 public static void show(Component caller, String topic){ 113 if(WhisperIM.Help_Window==null){ 114 WhisperIM.Help_Window=new HelpWindow(); 115 } 116 WhisperIM.Help_Window.showPage(caller,topic); 117 } 118 119 public void showPage(Component caller, String topic){ 120 boolean dosize=false; 121 if(!frame.isVisible()){ 122 frame.setLocationRelativeTo(caller); 123 dosize=true; } 126 frame.show(); 127 File lang=new File (HELP_DIR,currentLang); 129 final String userLang=WhisperIM.lang.getString("base_language_code"); 130 if(!currentLang.equals(userLang)){ 131 lang=new File (HELP_DIR,userLang); if(!lang.isDirectory()){ lang=new File (HELP_DIR, DEFAULT); currentLang=DEFAULT; 135 } 136 else{ 137 currentLang=userLang; 138 } 139 try{ frame.setCursor(GUI.WAIT); 141 contents.setPage(new URL ("file:"+(new File (lang,"index2.html")).getAbsolutePath())); 142 frame.setCursor(GUI.NORMAL); 143 } 144 catch(Exception e){ 145 GUI.showError(frame,"help title",null,e.getMessage()); 146 } 147 } 148 149 if(!lang.isDirectory()){ lang=new File (HELP_DIR, DEFAULT); } 152 153 File page=new File (lang, topic); 154 try{ 155 loadPage(new URL ("file:"+page.getAbsolutePath())); 156 if(dosize){ 157 frame.setSize(Form.d2); 158 } 159 } 160 catch(Exception e){ 161 GUI.errorStop(null,e); 162 } 163 } 164 165 private void loadPage(URL url){ 167 Cursor c=frame.getCursor(); html.setEnabled(false); 169 contents.setEnabled(false); 170 frame.getGlassPane().setVisible(true); 171 frame.setCursor(GUI.WAIT); 172 SwingUtilities.invokeLater(new loadThread(c, url)); 173 } 174 175 final class loadThread implements Runnable { 177 Cursor c; 178 URL url; 179 180 public loadThread(Cursor c, URL url){ 181 this.c=c; 182 this.url=url; 183 } 184 185 public void run() { 186 javax.swing.text.Document doc=html.getDocument(); 187 try { 188 html.setPage(url); frame.setCursor(GUI.NORMAL); frame.getGlassPane().setVisible(false); 191 html.setEnabled(true); 192 contents.setEnabled(true); 193 } 194 catch (IOException ioe){ 195 frame.setCursor(GUI.NORMAL); frame.getGlassPane().setVisible(false); 197 html.setEnabled(true); 198 contents.setEnabled(true); 199 html.setDocument(doc); 201 JOptionPane.showMessageDialog(frame,Lang.gs("cnlp")+"\n\r"+url.toString(),Lang.gs("error"), 202 JOptionPane.ERROR_MESSAGE); } 204 } 205 } 206 207 208 209 final class HyperLinkClick implements HyperlinkListener{ 210 public void hyperlinkUpdate(HyperlinkEvent e){ 211 if (e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){ 212 loadPage(e.getURL()); 213 } 214 } 215 } 216 } | Popular Tags |