1 22 23 package org.gjt.sp.jedit.help; 24 25 import javax.swing.*; 27 import javax.swing.event.*; 28 import javax.swing.text.html.*; 29 import java.awt.*; 30 import java.awt.event.*; 31 import java.beans.*; 32 import java.io.*; 33 import java.net.*; 34 import org.gjt.sp.jedit.msg.PluginUpdate; 35 import org.gjt.sp.jedit.msg.PropertiesChanged; 36 import org.gjt.sp.jedit.*; 37 import org.gjt.sp.util.Log; 38 40 46 public class HelpViewer extends JFrame implements HelpViewerInterface, EBComponent, HelpHistoryModelListener 47 { 48 53 public HelpViewer() 54 { 55 this("welcome.html"); 56 } 58 63 public HelpViewer(URL url) 64 { 65 this(url.toString()); 66 } 68 73 public HelpViewer(String url) 74 { 75 super(jEdit.getProperty("helpviewer.title")); 76 77 setIconImage(GUIUtilities.getEditorIcon()); 78 79 try 80 { 81 baseURL = new File(MiscUtilities.constructPath( 82 jEdit.getJEditHome(),"doc")).toURL().toString(); 83 } 84 catch(MalformedURLException mu) 85 { 86 Log.log(Log.ERROR,this,mu); 87 } 89 90 ActionHandler actionListener = new ActionHandler(); 91 92 JTabbedPane tabs = new JTabbedPane(); 93 tabs.addTab(jEdit.getProperty("helpviewer.toc.label"), 94 toc = new HelpTOCPanel(this)); 95 tabs.addTab(jEdit.getProperty("helpviewer.search.label"), 96 new HelpSearchPanel(this)); 97 tabs.setMinimumSize(new Dimension(0,0)); 98 99 JPanel rightPanel = new JPanel(new BorderLayout()); 100 101 Box toolBar = new Box(BoxLayout.X_AXIS); 102 104 toolBar.add(title = new JLabel()); 105 toolBar.add(Box.createGlue()); 106 historyModel = new HelpHistoryModel(25); 107 back = new HistoryButton(HistoryButton.BACK,historyModel); 108 back.addActionListener(actionListener); 109 toolBar.add(back); 110 forward = new HistoryButton(HistoryButton.FORWARD,historyModel); 111 forward.addActionListener(actionListener); 112 toolBar.add(forward); 113 back.setPreferredSize(forward.getPreferredSize()); 114 rightPanel.add(BorderLayout.NORTH,toolBar); 115 116 viewer = new JEditorPane(); 117 viewer.setEditable(false); 118 viewer.addHyperlinkListener(new LinkHandler()); 119 viewer.setFont(new Font("Monospaced",Font.PLAIN,12)); 120 viewer.addPropertyChangeListener(new PropertyChangeHandler()); 121 122 rightPanel.add(BorderLayout.CENTER,new JScrollPane(viewer)); 123 124 splitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, 125 jEdit.getBooleanProperty("appearance.continuousLayout"), 126 tabs, 127 rightPanel); 128 splitter.setBorder(null); 129 130 131 getContentPane().add(BorderLayout.CENTER,splitter); 132 133 historyModel.addHelpHistoryModelListener(this); 134 historyUpdated(); 135 136 gotoURL(url,true, 0); 137 138 setDefaultCloseOperation(DISPOSE_ON_CLOSE); 139 140 getRootPane().setPreferredSize(new Dimension(750,500)); 141 142 pack(); 143 GUIUtilities.loadGeometry(this,"helpviewer"); 144 GUIUtilities.addSizeSaver(this,"helpviewer"); 145 146 EditBus.addToBus(this); 147 148 setVisible(true); 149 150 SwingUtilities.invokeLater(new Runnable () 151 { 152 public void run() 153 { 154 splitter.setDividerLocation(jEdit.getIntegerProperty( 155 "helpviewer.splitter",250)); 156 viewer.requestFocus(); 157 } 158 }); 159 } 161 168 public void gotoURL(String url, boolean addToHistory, int scrollPos) 169 { 170 String shortURL; 173 if(MiscUtilities.isURL(url)) 174 { 175 if(url.startsWith(baseURL)) 176 { 177 shortURL = url.substring(baseURL.length()); 178 if(shortURL.startsWith("/")) 179 shortURL = shortURL.substring(1); 180 } 181 else 182 { 183 shortURL = url; 184 } 185 } 186 else 187 { 188 shortURL = url; 189 if(baseURL.endsWith("/")) 190 url = baseURL + url; 191 else 192 url = baseURL + '/' + url; 193 } 194 195 viewer.setCursor(Cursor.getDefaultCursor()); 198 199 try 200 { 201 URL _url = new URL(url); 202 203 if(!_url.equals(viewer.getPage())) 204 title.setText(jEdit.getProperty("helpviewer.loading")); 205 else 206 { 207 209 } 210 211 viewer.setPage(_url); 212 if(addToHistory) 213 historyModel.addToHistory(url); 214 } 215 catch(MalformedURLException mf) 216 { 217 Log.log(Log.ERROR,this,mf); 218 String [] args = { url, mf.getMessage() }; 219 GUIUtilities.error(this,"badurl",args); 220 return; 221 } 222 catch(IOException io) 223 { 224 Log.log(Log.ERROR,this,io); 225 String [] args = { url, io.toString() }; 226 GUIUtilities.error(this,"read-error",args); 227 return; 228 } 229 230 this.shortURL = shortURL; 231 232 if(shortURL != null) 234 toc.selectNode(shortURL); 235 } 237 public void dispose() 239 { 240 EditBus.removeFromBus(this); 241 jEdit.setIntegerProperty("helpviewer.splitter", 242 splitter.getDividerLocation()); 243 super.dispose(); 244 } 246 public void handleMessage(EBMessage msg) 248 { 249 if(msg instanceof PluginUpdate) 250 { 251 PluginUpdate pmsg = (PluginUpdate)msg; 252 if(pmsg.getWhat() == PluginUpdate.LOADED 253 || pmsg.getWhat() == PluginUpdate.UNLOADED) 254 { 255 if(!pmsg.isExiting()) 256 { 257 if(!queuedTOCReload) 258 queueTOCReload(); 259 queuedTOCReload = true; 260 } 261 } 262 } 263 else if (msg instanceof PropertiesChanged) 264 { 265 GUIUtilities.initContinuousLayout(splitter); 266 } 267 } 269 public String getBaseURL() 271 { 272 return baseURL; 273 } 275 public String getShortURL() 277 { 278 return shortURL; 279 } 281 public void historyUpdated() 283 { 284 back.setEnabled(historyModel.hasPrevious()); 285 forward.setEnabled(historyModel.hasNext()); 286 } 288 290 private String baseURL; 292 private String shortURL; 293 private HistoryButton back; 294 private HistoryButton forward; 295 private JEditorPane viewer; 296 private JLabel title; 297 private JSplitPane splitter; 298 private HelpHistoryModel historyModel; 299 private HelpTOCPanel toc; 300 private boolean queuedTOCReload; 301 303 public void queueTOCReload() 305 { 306 SwingUtilities.invokeLater(new Runnable () 307 { 308 public void run() 309 { 310 queuedTOCReload = false; 311 toc.load(); 312 } 313 }); 314 } 316 318 320 class ActionHandler implements ActionListener 322 { 323 public void actionPerformed(ActionEvent evt) 325 { 326 Object source = evt.getSource(); 327 String url = evt.getActionCommand(); 328 if (url.length() != 0) 329 { 330 gotoURL(url,false,0); 331 return; 332 } 333 334 if(source == back) 335 { 336 url = historyModel.back(); 337 if(url == null) 338 getToolkit().beep(); 339 else 340 { 341 gotoURL(url,false,0); 342 } 343 } 344 else if(source == forward) 345 { 346 url = historyModel.forward(); 347 if(url == null) 348 getToolkit().beep(); 349 else 350 { 351 gotoURL(url,false,0); 352 } 353 } 354 } } 357 class LinkHandler implements HyperlinkListener 359 { 360 public void hyperlinkUpdate(HyperlinkEvent evt) 362 { 363 if(evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) 364 { 365 if(evt instanceof HTMLFrameHyperlinkEvent) 366 { 367 ((HTMLDocument)viewer.getDocument()) 368 .processHTMLFrameHyperlinkEvent( 369 (HTMLFrameHyperlinkEvent)evt); 370 historyUpdated(); 371 } 372 else 373 { 374 URL url = evt.getURL(); 375 if(url != null) 376 gotoURL(url.toString(),true,0); 377 } 378 } 379 else if (evt.getEventType() == HyperlinkEvent.EventType.ENTERED) { 380 viewer.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 381 } 382 else if (evt.getEventType() == HyperlinkEvent.EventType.EXITED) { 383 viewer.setCursor(Cursor.getDefaultCursor()); 384 } 385 } } 388 class PropertyChangeHandler implements PropertyChangeListener 390 { 391 public void propertyChange(PropertyChangeEvent evt) 392 { 393 if("page".equals(evt.getPropertyName())) 394 { 395 String titleStr = (String )viewer.getDocument() 396 .getProperty("title"); 397 if(titleStr == null) 398 { 399 titleStr = MiscUtilities.getFileName( 400 viewer.getPage().toString()); 401 } 402 title.setText(titleStr); 403 historyModel.updateTitle(viewer.getPage().toString(), 404 titleStr); 405 } 406 } 407 } 409 public Component getComponent() 410 { 411 return getRootPane(); 412 } 413 414 } 416 | Popular Tags |