PageRenderTime 153ms CodeModel.GetById 135ms app.highlight 14ms RepoModel.GetById 2ms 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
  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
 23package org.gjt.sp.jedit.help;
 24
 25//{{{ Imports
 26import com.microstar.xml.*;
 27import javax.swing.*;
 28import javax.swing.border.*;
 29import javax.swing.event.*;
 30import javax.swing.text.html.*;
 31import javax.swing.text.Document;
 32import javax.swing.tree.*;
 33import java.awt.*;
 34import java.awt.event.*;
 35import java.io.*;
 36import java.net.*;
 37import java.util.*;
 38import org.gjt.sp.jedit.browser.FileCellRenderer; // for icons
 39import org.gjt.sp.jedit.gui.RolloverButton;
 40import org.gjt.sp.jedit.msg.PropertiesChanged;
 41import org.gjt.sp.jedit.*;
 42import org.gjt.sp.util.Log;
 43//}}}
 44
 45/**
 46 * jEdit's HTML viewer. It uses a Swing JEditorPane to display the HTML,
 47 * and implements a URL history.
 48 * @author Slava Pestov
 49 * @version $Id: HelpViewer.java 4351 2002-10-04 19:41:08Z spestov $
 50 */
 51public class HelpViewer extends JFrame implements EBComponent
 52{
 53	//{{{ HelpViewer constructor
 54	/**
 55	 * Creates a new help viewer with the default help page.
 56	 * @since jEdit 4.0pre4
 57	 */
 58	public HelpViewer()
 59	{
 60		this("welcome.html");
 61	} //}}}
 62
 63	//{{{ HelpViewer constructor
 64	/**
 65	 * Creates a new help viewer for the specified URL.
 66	 * @param url The URL
 67	 */
 68	public HelpViewer(URL url)
 69	{
 70		this(url.toString());
 71	} //}}}
 72
 73	//{{{ HelpViewer constructor
 74	/**
 75	 * Creates a new help viewer for the specified URL.
 76	 * @param url The URL
 77	 */
 78	public HelpViewer(String url)
 79	{
 80		super(jEdit.getProperty("helpviewer.title"));
 81
 82		setIconImage(GUIUtilities.getEditorIcon());
 83
 84		try
 85		{
 86			baseURL = new File(MiscUtilities.constructPath(
 87				jEdit.getJEditHome(),"doc")).toURL().toString();
 88		}
 89		catch(MalformedURLException mu)
 90		{
 91			Log.log(Log.ERROR,this,mu);
 92			// what to do?
 93		}
 94
 95		history = new String[25];
 96
 97		ActionHandler actionListener = new ActionHandler();
 98
 99		JToolBar toolBar = new JToolBar();
100		toolBar.setFloatable(false);
101
102		JLabel label = new JLabel(jEdit.getProperty("helpviewer.url"));
103		label.setBorder(new EmptyBorder(0,12,0,12));
104		toolBar.add(label);
105		Box box = new Box(BoxLayout.Y_AXIS);
106		box.add(Box.createGlue());
107		urlField = new JTextField();
108		urlField.addKeyListener(new KeyHandler());
109		Dimension dim = urlField.getPreferredSize();
110		dim.width = Integer.MAX_VALUE;
111		urlField.setMaximumSize(dim);
112		box.add(urlField);
113		box.add(Box.createGlue());
114		toolBar.add(box);
115
116		toolBar.add(Box.createHorizontalStrut(6));
117
118		JPanel buttons = new JPanel();
119		buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
120		buttons.setBorder(new EmptyBorder(0,12,0,0));
121		back = new RolloverButton(GUIUtilities.loadIcon(
122			jEdit.getProperty("helpviewer.back.icon")));
123		back.setToolTipText(jEdit.getProperty("helpviewer.back.label"));
124		back.addActionListener(actionListener);
125		toolBar.add(back);
126		forward = new RolloverButton(GUIUtilities.loadIcon(
127			jEdit.getProperty("helpviewer.forward.icon")));
128		forward.addActionListener(actionListener);
129		forward.setToolTipText(jEdit.getProperty("helpviewer.forward.label"));
130		toolBar.add(forward);
131		back.setPreferredSize(forward.getPreferredSize());
132
133		getContentPane().add(BorderLayout.NORTH,toolBar);
134
135		viewer = new JEditorPane();
136		viewer.setEditable(false);
137		viewer.addHyperlinkListener(new LinkHandler());
138		viewer.setFont(new Font("Monospaced",Font.PLAIN,12));
139
140		JTabbedPane tabs = new JTabbedPane();
141		tabs.addTab(jEdit.getProperty("helpviewer.toc.label"),
142			toc = new HelpTOCPanel(this));
143		tabs.addTab(jEdit.getProperty("helpviewer.search.label"),
144			new HelpSearchPanel(this));
145
146		// search not finished yet so we don't show the search panel
147		final JSplitPane splitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
148			toc,new JScrollPane(viewer));
149		splitter.setBorder(null);
150
151		getContentPane().add(BorderLayout.CENTER,splitter);
152
153		gotoURL(url,true);
154
155		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
156
157		getRootPane().setPreferredSize(new Dimension(800,400));
158
159		pack();
160		GUIUtilities.loadGeometry(this,"helpviewer");
161
162		EditBus.addToBus(this);
163
164		show();
165
166		SwingUtilities.invokeLater(new Runnable()
167		{
168			public void run()
169			{
170				splitter.setDividerLocation(250);
171			}
172		});
173	} //}}}
174
175	//{{{ gotoURL() method
176	/**
177	 * Displays the specified URL in the HTML component.
178	 * @param url The URL
179	 * @param addToHistory Should the URL be added to the back/forward
180	 * history?
181	 */
182	public void gotoURL(String url, boolean addToHistory)
183	{
184		String shortURL;
185		if(MiscUtilities.isURL(url))
186		{
187			if(url.startsWith(baseURL))
188			{
189				shortURL = url.substring(baseURL.length());
190				if(shortURL.startsWith("/"))
191					shortURL = shortURL.substring(1);
192			}
193			else
194			{
195				shortURL = null;
196			}
197		}
198		else
199		{
200			shortURL = url;
201			if(baseURL.endsWith("/"))
202				url = baseURL + url;
203			else
204				url = baseURL + '/' + url;
205		}
206
207		// reset default cursor so that the hand cursor doesn't
208		// stick around
209		viewer.setCursor(Cursor.getDefaultCursor());
210
211		int index = url.indexOf('#');
212
213		URL _url = null;
214		try
215		{
216			_url = new URL(url);
217
218			urlField.setText(_url.toString());
219			viewer.setPage(_url);
220			if(addToHistory)
221			{
222				history[historyPos] = url;
223				if(historyPos + 1 == history.length)
224				{
225					System.arraycopy(history,1,history,
226						0,history.length - 1);
227					history[historyPos] = null;
228				}
229				else
230					historyPos++;
231			}
232		}
233		catch(MalformedURLException mf)
234		{
235			Log.log(Log.ERROR,this,mf);
236			String[] args = { url, mf.getMessage() };
237			GUIUtilities.error(this,"badurl",args);
238			return;
239		}
240		catch(IOException io)
241		{
242			Log.log(Log.ERROR,this,io);
243			String[] args = { url, io.toString() };
244			GUIUtilities.error(this,"read-error",args);
245			return;
246		}
247
248		// select the appropriate tree node.
249		if(shortURL != null)
250			toc.selectNode(shortURL);
251	} //}}}
252
253	//{{{ dispose() method
254	public void dispose()
255	{
256		EditBus.removeFromBus(this);
257		GUIUtilities.saveGeometry(this,"helpviewer");
258		super.dispose();
259	} //}}}
260
261	//{{{ handleMessage() method
262	public void handleMessage(EBMessage msg)
263	{
264		if(msg instanceof PropertiesChanged)
265			SwingUtilities.updateComponentTreeUI(getRootPane());
266	} //}}}
267
268	//{{{ getBaseURL() method
269	public String getBaseURL()
270	{
271		return baseURL;
272	} //}}}
273
274	//{{{ Private members
275	private String baseURL;
276	private JButton back;
277	private JButton forward;
278	private JEditorPane viewer;
279	private JTextField urlField;
280	private String[] history;
281	private int historyPos;
282	private HelpTOCPanel toc;
283	//}}}
284
285	//{{{ Inner classes
286
287	//{{{ ActionHandler class
288	class ActionHandler implements ActionListener
289	{
290		//{{{ actionPerformed() class
291		public void actionPerformed(ActionEvent evt)
292		{
293			Object source = evt.getSource();
294			if(source == back)
295			{
296				if(historyPos <= 1)
297					getToolkit().beep();
298				else
299				{
300					String url = history[--historyPos - 1];
301					gotoURL(url,false);
302				}
303			}
304			else if(source == forward)
305			{
306				if(history.length - historyPos <= 1)
307					getToolkit().beep();
308				else
309				{
310					String url = history[historyPos];
311					if(url == null)
312						getToolkit().beep();
313					else
314					{
315						historyPos++;
316						gotoURL(url,false);
317					}
318				}
319			}
320		} //}}}
321	} //}}}
322
323	//{{{ LinkHandler class
324	class LinkHandler implements HyperlinkListener
325	{
326		//{{{ hyperlinkUpdate() method
327		public void hyperlinkUpdate(HyperlinkEvent evt)
328		{
329			if(evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
330			{
331				if(evt instanceof HTMLFrameHyperlinkEvent)
332				{
333					((HTMLDocument)viewer.getDocument())
334						.processHTMLFrameHyperlinkEvent(
335						(HTMLFrameHyperlinkEvent)evt);
336				}
337				else
338				{
339					URL url = evt.getURL();
340					if(url != null)
341						gotoURL(url.toString(),true);
342				}
343			}
344			else if (evt.getEventType() == HyperlinkEvent.EventType.ENTERED) {
345				viewer.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
346			}
347			else if (evt.getEventType() == HyperlinkEvent.EventType.EXITED) {
348				viewer.setCursor(Cursor.getDefaultCursor());
349			}
350		} //}}}
351	} //}}}
352
353	//{{{ KeyHandler class
354	class KeyHandler extends KeyAdapter
355	{
356		//{{{ keyPressed() method
357		public void keyPressed(KeyEvent evt)
358		{
359			if(evt.getKeyCode() == KeyEvent.VK_ENTER)
360			{
361				gotoURL(urlField.getText(),true);
362			}
363		} //}}}
364	} //}}}
365
366	//}}}
367}