PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/help/HelpViewer.java

#
Java | 367 lines | 258 code | 40 blank | 69 comment | 32 complexity | 75aa5bdbcfed07f5c9f7a4f077dcd557 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * HelpViewer.java - HTML Help viewer
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999, 2000, 2001 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.help;
  23. //{{{ Imports
  24. import com.microstar.xml.*;
  25. import javax.swing.*;
  26. import javax.swing.border.*;
  27. import javax.swing.event.*;
  28. import javax.swing.text.html.*;
  29. import javax.swing.text.Document;
  30. import javax.swing.tree.*;
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import java.io.*;
  34. import java.net.*;
  35. import java.util.*;
  36. import org.gjt.sp.jedit.browser.FileCellRenderer; // for icons
  37. import org.gjt.sp.jedit.gui.RolloverButton;
  38. import org.gjt.sp.jedit.msg.PropertiesChanged;
  39. import org.gjt.sp.jedit.*;
  40. import org.gjt.sp.util.Log;
  41. //}}}
  42. /**
  43. * jEdit's HTML viewer. It uses a Swing JEditorPane to display the HTML,
  44. * and implements a URL history.
  45. * @author Slava Pestov
  46. * @version $Id: HelpViewer.java 4351 2002-10-04 19:41:08Z spestov $
  47. */
  48. public class HelpViewer extends JFrame implements EBComponent
  49. {
  50. //{{{ HelpViewer constructor
  51. /**
  52. * Creates a new help viewer with the default help page.
  53. * @since jEdit 4.0pre4
  54. */
  55. public HelpViewer()
  56. {
  57. this("welcome.html");
  58. } //}}}
  59. //{{{ HelpViewer constructor
  60. /**
  61. * Creates a new help viewer for the specified URL.
  62. * @param url The URL
  63. */
  64. public HelpViewer(URL url)
  65. {
  66. this(url.toString());
  67. } //}}}
  68. //{{{ HelpViewer constructor
  69. /**
  70. * Creates a new help viewer for the specified URL.
  71. * @param url The URL
  72. */
  73. public HelpViewer(String url)
  74. {
  75. super(jEdit.getProperty("helpviewer.title"));
  76. setIconImage(GUIUtilities.getEditorIcon());
  77. try
  78. {
  79. baseURL = new File(MiscUtilities.constructPath(
  80. jEdit.getJEditHome(),"doc")).toURL().toString();
  81. }
  82. catch(MalformedURLException mu)
  83. {
  84. Log.log(Log.ERROR,this,mu);
  85. // what to do?
  86. }
  87. history = new String[25];
  88. ActionHandler actionListener = new ActionHandler();
  89. JToolBar toolBar = new JToolBar();
  90. toolBar.setFloatable(false);
  91. JLabel label = new JLabel(jEdit.getProperty("helpviewer.url"));
  92. label.setBorder(new EmptyBorder(0,12,0,12));
  93. toolBar.add(label);
  94. Box box = new Box(BoxLayout.Y_AXIS);
  95. box.add(Box.createGlue());
  96. urlField = new JTextField();
  97. urlField.addKeyListener(new KeyHandler());
  98. Dimension dim = urlField.getPreferredSize();
  99. dim.width = Integer.MAX_VALUE;
  100. urlField.setMaximumSize(dim);
  101. box.add(urlField);
  102. box.add(Box.createGlue());
  103. toolBar.add(box);
  104. toolBar.add(Box.createHorizontalStrut(6));
  105. JPanel buttons = new JPanel();
  106. buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
  107. buttons.setBorder(new EmptyBorder(0,12,0,0));
  108. back = new RolloverButton(GUIUtilities.loadIcon(
  109. jEdit.getProperty("helpviewer.back.icon")));
  110. back.setToolTipText(jEdit.getProperty("helpviewer.back.label"));
  111. back.addActionListener(actionListener);
  112. toolBar.add(back);
  113. forward = new RolloverButton(GUIUtilities.loadIcon(
  114. jEdit.getProperty("helpviewer.forward.icon")));
  115. forward.addActionListener(actionListener);
  116. forward.setToolTipText(jEdit.getProperty("helpviewer.forward.label"));
  117. toolBar.add(forward);
  118. back.setPreferredSize(forward.getPreferredSize());
  119. getContentPane().add(BorderLayout.NORTH,toolBar);
  120. viewer = new JEditorPane();
  121. viewer.setEditable(false);
  122. viewer.addHyperlinkListener(new LinkHandler());
  123. viewer.setFont(new Font("Monospaced",Font.PLAIN,12));
  124. JTabbedPane tabs = new JTabbedPane();
  125. tabs.addTab(jEdit.getProperty("helpviewer.toc.label"),
  126. toc = new HelpTOCPanel(this));
  127. tabs.addTab(jEdit.getProperty("helpviewer.search.label"),
  128. new HelpSearchPanel(this));
  129. // search not finished yet so we don't show the search panel
  130. final JSplitPane splitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
  131. toc,new JScrollPane(viewer));
  132. splitter.setBorder(null);
  133. getContentPane().add(BorderLayout.CENTER,splitter);
  134. gotoURL(url,true);
  135. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  136. getRootPane().setPreferredSize(new Dimension(800,400));
  137. pack();
  138. GUIUtilities.loadGeometry(this,"helpviewer");
  139. EditBus.addToBus(this);
  140. show();
  141. SwingUtilities.invokeLater(new Runnable()
  142. {
  143. public void run()
  144. {
  145. splitter.setDividerLocation(250);
  146. }
  147. });
  148. } //}}}
  149. //{{{ gotoURL() method
  150. /**
  151. * Displays the specified URL in the HTML component.
  152. * @param url The URL
  153. * @param addToHistory Should the URL be added to the back/forward
  154. * history?
  155. */
  156. public void gotoURL(String url, boolean addToHistory)
  157. {
  158. String shortURL;
  159. if(MiscUtilities.isURL(url))
  160. {
  161. if(url.startsWith(baseURL))
  162. {
  163. shortURL = url.substring(baseURL.length());
  164. if(shortURL.startsWith("/"))
  165. shortURL = shortURL.substring(1);
  166. }
  167. else
  168. {
  169. shortURL = null;
  170. }
  171. }
  172. else
  173. {
  174. shortURL = url;
  175. if(baseURL.endsWith("/"))
  176. url = baseURL + url;
  177. else
  178. url = baseURL + '/' + url;
  179. }
  180. // reset default cursor so that the hand cursor doesn't
  181. // stick around
  182. viewer.setCursor(Cursor.getDefaultCursor());
  183. int index = url.indexOf('#');
  184. URL _url = null;
  185. try
  186. {
  187. _url = new URL(url);
  188. urlField.setText(_url.toString());
  189. viewer.setPage(_url);
  190. if(addToHistory)
  191. {
  192. history[historyPos] = url;
  193. if(historyPos + 1 == history.length)
  194. {
  195. System.arraycopy(history,1,history,
  196. 0,history.length - 1);
  197. history[historyPos] = null;
  198. }
  199. else
  200. historyPos++;
  201. }
  202. }
  203. catch(MalformedURLException mf)
  204. {
  205. Log.log(Log.ERROR,this,mf);
  206. String[] args = { url, mf.getMessage() };
  207. GUIUtilities.error(this,"badurl",args);
  208. return;
  209. }
  210. catch(IOException io)
  211. {
  212. Log.log(Log.ERROR,this,io);
  213. String[] args = { url, io.toString() };
  214. GUIUtilities.error(this,"read-error",args);
  215. return;
  216. }
  217. // select the appropriate tree node.
  218. if(shortURL != null)
  219. toc.selectNode(shortURL);
  220. } //}}}
  221. //{{{ dispose() method
  222. public void dispose()
  223. {
  224. EditBus.removeFromBus(this);
  225. GUIUtilities.saveGeometry(this,"helpviewer");
  226. super.dispose();
  227. } //}}}
  228. //{{{ handleMessage() method
  229. public void handleMessage(EBMessage msg)
  230. {
  231. if(msg instanceof PropertiesChanged)
  232. SwingUtilities.updateComponentTreeUI(getRootPane());
  233. } //}}}
  234. //{{{ getBaseURL() method
  235. public String getBaseURL()
  236. {
  237. return baseURL;
  238. } //}}}
  239. //{{{ Private members
  240. private String baseURL;
  241. private JButton back;
  242. private JButton forward;
  243. private JEditorPane viewer;
  244. private JTextField urlField;
  245. private String[] history;
  246. private int historyPos;
  247. private HelpTOCPanel toc;
  248. //}}}
  249. //{{{ Inner classes
  250. //{{{ ActionHandler class
  251. class ActionHandler implements ActionListener
  252. {
  253. //{{{ actionPerformed() class
  254. public void actionPerformed(ActionEvent evt)
  255. {
  256. Object source = evt.getSource();
  257. if(source == back)
  258. {
  259. if(historyPos <= 1)
  260. getToolkit().beep();
  261. else
  262. {
  263. String url = history[--historyPos - 1];
  264. gotoURL(url,false);
  265. }
  266. }
  267. else if(source == forward)
  268. {
  269. if(history.length - historyPos <= 1)
  270. getToolkit().beep();
  271. else
  272. {
  273. String url = history[historyPos];
  274. if(url == null)
  275. getToolkit().beep();
  276. else
  277. {
  278. historyPos++;
  279. gotoURL(url,false);
  280. }
  281. }
  282. }
  283. } //}}}
  284. } //}}}
  285. //{{{ LinkHandler class
  286. class LinkHandler implements HyperlinkListener
  287. {
  288. //{{{ hyperlinkUpdate() method
  289. public void hyperlinkUpdate(HyperlinkEvent evt)
  290. {
  291. if(evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
  292. {
  293. if(evt instanceof HTMLFrameHyperlinkEvent)
  294. {
  295. ((HTMLDocument)viewer.getDocument())
  296. .processHTMLFrameHyperlinkEvent(
  297. (HTMLFrameHyperlinkEvent)evt);
  298. }
  299. else
  300. {
  301. URL url = evt.getURL();
  302. if(url != null)
  303. gotoURL(url.toString(),true);
  304. }
  305. }
  306. else if (evt.getEventType() == HyperlinkEvent.EventType.ENTERED) {
  307. viewer.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  308. }
  309. else if (evt.getEventType() == HyperlinkEvent.EventType.EXITED) {
  310. viewer.setCursor(Cursor.getDefaultCursor());
  311. }
  312. } //}}}
  313. } //}}}
  314. //{{{ KeyHandler class
  315. class KeyHandler extends KeyAdapter
  316. {
  317. //{{{ keyPressed() method
  318. public void keyPressed(KeyEvent evt)
  319. {
  320. if(evt.getKeyCode() == KeyEvent.VK_ENTER)
  321. {
  322. gotoURL(urlField.getText(),true);
  323. }
  324. } //}}}
  325. } //}}}
  326. //}}}
  327. }