KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > help > HelpViewer


1 /*
2  * HelpViewerDialog.java - HTML Help viewer
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1999, 2005 Slava Pestov, Nicholas O'Leary
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
23 package org.gjt.sp.jedit.help;
24
25 //{{{ Imports
26
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 //}}}
39

40 /**
41  * jEdit's searchable help viewer. It uses a Swing JEditorPane to display the HTML,
42  * and implements a URL history.
43  * @author Slava Pestov
44  * @version $Id: HelpViewer.java 8676 2007-01-19 20:14:58Z kpouer $
45  */

46 public class HelpViewer extends JFrame implements HelpViewerInterface, EBComponent, HelpHistoryModelListener
47 {
48     //{{{ HelpViewer constructor
49
/**
50      * Creates a new help viewer with the default help page.
51      * @since jEdit 4.0pre4
52      */

53     public HelpViewer()
54     {
55         this("welcome.html");
56     } //}}}
57

58     //{{{ HelpViewer constructor
59
/**
60      * Creates a new help viewer for the specified URL.
61      * @param url The URL
62      */

63     public HelpViewer(URL url)
64     {
65         this(url.toString());
66     } //}}}
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 JavaDoc 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             // what to do?
88
}
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         //toolBar.setFloatable(false);
103

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 JavaDoc()
151         {
152             public void run()
153             {
154                 splitter.setDividerLocation(jEdit.getIntegerProperty(
155                     "helpviewer.splitter",250));
156                 viewer.requestFocus();
157             }
158         });
159     } //}}}
160

161     //{{{ gotoURL() method
162
/**
163      * Displays the specified URL in the HTML component.
164      * @param url The URL
165      * @param addToHistory Should the URL be added to the back/forward
166      * history?
167      */

168     public void gotoURL(String JavaDoc url, boolean addToHistory, int scrollPos)
169     {
170         // the TOC pane looks up user's guide URLs relative to the
171
// doc directory...
172
String JavaDoc 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         // reset default cursor so that the hand cursor doesn't
196
// stick around
197
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                 /* don't show loading msg because we won't
208                    receive a propertyChanged */

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 JavaDoc[] 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 JavaDoc[] args = { url, io.toString() };
226             GUIUtilities.error(this,"read-error",args);
227             return;
228         }
229
230         this.shortURL = shortURL;
231
232         // select the appropriate tree node.
233
if(shortURL != null)
234             toc.selectNode(shortURL);
235     } //}}}
236

237     //{{{ dispose() method
238
public void dispose()
239     {
240         EditBus.removeFromBus(this);
241         jEdit.setIntegerProperty("helpviewer.splitter",
242             splitter.getDividerLocation());
243         super.dispose();
244     } //}}}
245

246     //{{{ handleMessage() method
247
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     } //}}}
268

269     //{{{ getBaseURL() method
270
public String JavaDoc getBaseURL()
271     {
272         return baseURL;
273     } //}}}
274

275     //{{{ getShortURL() method
276
public String JavaDoc getShortURL()
277     {
278         return shortURL;
279     } //}}}
280

281     //{{{ historyUpdated() method
282
public void historyUpdated()
283     {
284         back.setEnabled(historyModel.hasPrevious());
285         forward.setEnabled(historyModel.hasNext());
286     } //}}}
287

288     //{{{ Private members
289

290     //{{{ Instance members
291
private String JavaDoc baseURL;
292     private String JavaDoc 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     //}}}
302

303     //{{{ queueTOCReload() method
304
public void queueTOCReload()
305     {
306         SwingUtilities.invokeLater(new Runnable JavaDoc()
307         {
308             public void run()
309             {
310                 queuedTOCReload = false;
311                 toc.load();
312             }
313         });
314     } //}}}
315

316     //}}}
317

318     //{{{ Inner classes
319

320     //{{{ ActionHandler class
321
class ActionHandler implements ActionListener
322     {
323         //{{{ actionPerformed() class
324
public void actionPerformed(ActionEvent evt)
325         {
326             Object JavaDoc source = evt.getSource();
327             String JavaDoc 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         } //}}}
355
} //}}}
356

357     //{{{ LinkHandler class
358
class LinkHandler implements HyperlinkListener
359     {
360         //{{{ hyperlinkUpdate() method
361
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         } //}}}
386
} //}}}
387

388     //{{{ PropertyChangeHandler class
389
class PropertyChangeHandler implements PropertyChangeListener
390     {
391         public void propertyChange(PropertyChangeEvent evt)
392         {
393             if("page".equals(evt.getPropertyName()))
394             {
395                 String JavaDoc titleStr = (String JavaDoc)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     } //}}}
408

409     public Component getComponent()
410     {
411         return getRootPane();
412     }
413
414     //}}}
415
}
416
Popular Tags