KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > HTMLFrame


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui;
35
36 import javax.swing.*;
37 import javax.swing.event.*;
38 import javax.swing.border.*;
39 import javax.swing.text.html.*;
40 import java.awt.event.*;
41 import java.awt.*;
42 import java.net.*;
43 import java.io.*;
44 import java.util.Vector JavaDoc;
45
46 import edu.rice.cs.util.FileOps;
47 import edu.rice.cs.util.UnexpectedException;
48 import edu.rice.cs.util.swing.BorderlessScrollPane;
49
50 /** The frame for displaying the HTML help files.
51  * @version $Id: HTMLFrame.java 3984 2006-08-31 18:56:29Z rcartwright $
52  */

53 public class HTMLFrame extends JFrame {
54
55   private static final int FRAME_WIDTH = 750;
56   private static final int FRAME_HEIGHT = 600;
57   private static final int LEFT_PANEL_WIDTH = 250;
58   private JEditorPane _mainDocPane;
59   private JScrollPane _mainScroll;
60   private JSplitPane _splitPane;
61   private JPanel _splitPaneHolder;
62   private JEditorPane _contentsDocPane;
63   private JPanel _closePanel;
64   private JButton _closeButton;
65   private JButton _backButton;
66   private JButton _forwardButton;
67   protected URL _baseURL;
68   private Vector JavaDoc<HyperlinkListener> _hyperlinkListeners;
69   private boolean _linkError;
70   private URL _lastURL;
71
72   private JPanel _navPane;
73
74   protected HistoryList _history;
75
76   private HyperlinkListener _resetListener = new HyperlinkListener() {
77     public void hyperlinkUpdate(HyperlinkEvent e) {
78       if (_linkError && e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
79         _resetMainPane();
80       }
81     }
82   };
83
84   protected static class HistoryList {
85     private HistoryList next = null;
86     private final HistoryList prev;
87     protected final URL contents;
88     private HistoryList(URL contents) {
89       this.contents = contents;
90       this.prev = null;
91     }
92     private HistoryList(URL contents, HistoryList prev) {
93       this.contents = contents;
94       this.prev = prev;
95       prev.next = this;
96     }
97   }
98
99   public static abstract class ResourceAction extends AbstractAction {
100     public ResourceAction(String JavaDoc name, String JavaDoc iconName) {
101       super(name,MainFrame.getIcon(iconName));
102     }
103   }
104
105   private static abstract class ConsolidatedAction extends ResourceAction {
106     private ConsolidatedAction(String JavaDoc name) {
107       super(name,name+"16.gif");
108     }
109   }
110
111   private Action _forwardAction = new ConsolidatedAction("Forward") {
112     public void actionPerformed(ActionEvent e) {
113       _history = _history.next;
114
115       // user is always allowed to move back after a forward.
116
_backAction.setEnabled(true);
117
118       if (_history.next == null) {
119         // no more forwards after this
120
_forwardAction.setEnabled(false);
121       }
122       _displayPage(_history.contents);
123     }
124   };
125
126   private Action _backAction = new ConsolidatedAction("Back") {
127     public void actionPerformed(ActionEvent e) {
128       _history = _history.prev;
129
130       // user is always allowed to move forward after backing up
131
_forwardAction.setEnabled(true);
132
133       if (_history.prev == null) {
134         // no more backing up
135
_backAction.setEnabled(false);
136       }
137       _displayPage(_history.contents);
138     }
139   };
140
141   private Action _closeAction = new AbstractAction("Close") {
142     public void actionPerformed(ActionEvent e) {
143       HTMLFrame.this.setVisible(false);
144     }
145   };
146
147   private static JButton makeButton(Action a, int horTextPos,
148                                     int left, int right) {
149     JButton j = new JButton(a);
150     j.setHorizontalTextPosition(horTextPos);
151     j.setVerticalTextPosition(JButton.CENTER);
152     //Insets i = j.getMargin();
153
//j.setMargin(new Insets(i.top,left,i.bottom,right));
154
j.setMargin(new Insets(3,left+3,3,right+3));
155     return j;
156   }
157
158   public void addHyperlinkListener(HyperlinkListener linkListener) {
159     _hyperlinkListeners.add(linkListener);
160     _contentsDocPane.addHyperlinkListener(linkListener);
161     _mainDocPane.addHyperlinkListener(linkListener);
162   }
163
164   /**
165    * Sets up the frame and displays it.
166    */

167   public HTMLFrame(String JavaDoc frameName, URL introUrl, URL indexUrl, String JavaDoc iconString) {
168     this(frameName, introUrl, indexUrl, iconString, null);
169   }
170
171   /**
172    * Sets up the frame and displays it.
173    */

174   public HTMLFrame(String JavaDoc frameName, URL introUrl, URL indexUrl, String JavaDoc iconString, File baseDir) {
175     super(frameName);
176
177     _contentsDocPane = new JEditorPane();
178     _contentsDocPane.setEditable(false);
179     JScrollPane contentsScroll = new BorderlessScrollPane(_contentsDocPane);
180
181     _mainDocPane = new JEditorPane();
182     _mainDocPane.setEditable(false);
183     _mainScroll = new BorderlessScrollPane(_mainDocPane);
184
185     _splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, contentsScroll, _mainScroll);
186     _splitPane.setDividerLocation(LEFT_PANEL_WIDTH);
187     _splitPaneHolder = new JPanel(new GridLayout(1,1));
188     _splitPaneHolder.setBorder(new EmptyBorder(0,5,0,5));
189     _splitPaneHolder.add(_splitPane);
190     // _splitPane.setBorder(new CompoundBorder(new EmptyBorder(0,5,0,5),_splitPane.getBorder()));
191
_closeButton = new JButton(_closeAction);
192     _backButton = makeButton(_backAction,JButton.RIGHT,0,3);
193     _forwardButton = makeButton(_forwardAction,JButton.LEFT,3,0);
194     _backAction.setEnabled(false);
195     _forwardAction.setEnabled(false);
196     _closePanel = new JPanel(new BorderLayout());
197     _closePanel.add(_closeButton, BorderLayout.EAST);
198     _closePanel.setBorder(new EmptyBorder(5,5,5,5)); // padding
199
_navPane = new JPanel();
200     _navPane.setBackground(new Color(0xCCCCFF));
201     _navPane.setLayout(new BoxLayout(_navPane,BoxLayout.X_AXIS));
202     JLabel icon = new JLabel(MainFrame.getIcon(iconString));
203     _navPane.add(icon);
204     _navPane.add(Box.createHorizontalStrut(8));
205     _navPane.add(Box.createHorizontalGlue());
206     _navPane.add(_backButton);
207     _navPane.add(Box.createHorizontalStrut(8));
208     _navPane.add(_forwardButton);
209     _navPane.add(Box.createHorizontalStrut(3));
210     _navPane.setBorder(new EmptyBorder(0,0,0,5));
211     JPanel navContainer = new JPanel(new GridLayout(1,1));
212     navContainer.setBorder(new CompoundBorder(new EmptyBorder(5,5,5,5), new EtchedBorder()));
213                                               //new BevelBorder(BevelBorder.LOWERED)));
214
navContainer.add(_navPane);
215     Container cp = getContentPane();
216     cp.setLayout(new BorderLayout());
217     cp.add(navContainer, BorderLayout.NORTH);
218     cp.add(_splitPaneHolder, BorderLayout.CENTER);
219     cp.add(_closePanel, BorderLayout.SOUTH);
220
221     _linkError = false;
222     _hyperlinkListeners = new Vector JavaDoc<HyperlinkListener>();
223     _hyperlinkListeners.add(_resetListener);
224     _mainDocPane.addHyperlinkListener(_resetListener);
225
226     if (baseDir == null) _baseURL = null;
227     else
228       try { _baseURL = FileOps.toURL(baseDir); }
229       catch(MalformedURLException ex) {
230         throw new UnexpectedException(ex);
231       }
232
233     // Load contents page
234
if (indexUrl == null) _displayContentsError(null);
235     else
236       try {
237         _contentsDocPane.setPage(indexUrl);
238         if (_baseURL != null) ((HTMLDocument)_contentsDocPane.getDocument()).setBase(_baseURL);
239       }
240       catch (IOException ioe) {
241         // Show some error page?
242
_displayContentsError(indexUrl, ioe);
243       }
244       
245     if (introUrl == null) _displayMainError(null);
246     else {
247       _history = new HistoryList(introUrl);
248       _displayPage(introUrl);
249       _displayPage(introUrl);
250     }
251
252     // Set all dimensions ----
253
setSize(FRAME_WIDTH, FRAME_HEIGHT);
254     MainFrame.setPopupLoc(this, null);
255   }
256
257   /** Hides the navigation panel on the left. Cannot currently be undone. */
258   protected void _hideNavigationPane() {
259     _splitPaneHolder.remove(_splitPane);
260     _splitPaneHolder.add(_mainScroll);
261   }
262
263   private void _resetMainPane() {
264     _linkError = false;
265
266     _mainDocPane = new JEditorPane();
267     _mainDocPane.setEditable(false);
268     for (int i = 0; i < _hyperlinkListeners.size(); i++) {
269       _mainDocPane.addHyperlinkListener(_hyperlinkListeners.get(i));
270     }
271     _displayPage(_lastURL);
272
273     _splitPane.setRightComponent(new BorderlessScrollPane(_mainDocPane));
274     _splitPane.setDividerLocation(LEFT_PANEL_WIDTH);
275   }
276
277   /** Displays the given URL in the main pane. Changed to private, because of history system.
278    * @param url URL to display
279    */

280   private void _displayPage(URL url) {
281     try {
282       _mainDocPane.setPage(url);
283       if (_baseURL != null) {
284         ((HTMLDocument)_contentsDocPane.getDocument()).setBase(_baseURL);
285       }
286       _lastURL = url;
287     }
288     catch (IOException ioe) {
289       String JavaDoc path = url.getPath();
290       try {
291         URL newURL = new URL(_baseURL + path);
292         _mainDocPane.setPage(newURL);
293         if (_baseURL != null) {
294           ((HTMLDocument)_contentsDocPane.getDocument()).setBase(_baseURL);
295         }
296         _lastURL = newURL;
297       }
298       catch (IOException ioe2) {
299         // Show some error page?
300
_displayMainError(url, ioe2);
301         //System.err.println("couldn't find url: " + url);
302
}
303     }
304   }
305
306   /** Prints an error indicating that the HTML file to load in the main pane could not be found. */
307   private void _displayMainError(URL url) {
308     if (!_linkError) {
309       _linkError = true;
310       _mainDocPane.setText(getErrorText(url));
311     }
312     else _resetMainPane();
313   }
314
315   /** Prints an error indicating that the HTML file to load in the main pane could not be found
316    */

317   private void _displayMainError(URL url, Exception JavaDoc ex) {
318     if (!_linkError) {
319       _linkError = true;
320       _mainDocPane.setText(getErrorText(url) + "\n" + ex);
321     }
322     else _resetMainPane();
323   }
324
325   /**
326    * Prints an error indicating that the HTML file to load in the contentes pane
327    * could not be found
328    */

329   private void _displayContentsError(URL url) {
330    _contentsDocPane.setText(getErrorText(url));
331   }
332
333   /** Prints an error indicating that the HTML file to load in the contentes pane could not be found. */
334   private void _displayContentsError(URL url, Exception JavaDoc ex) {
335     _contentsDocPane.setText(getErrorText(url) + "\n" + ex);
336   }
337
338   /** This method returns the error text to display when something goes wrong. */
339   protected String JavaDoc getErrorText(URL url) {
340     return "Could not load the specified URL: " + url;
341   }
342
343   public void jumpTo(URL url) {
344     _history = new HistoryList(url,_history); // current history is prev for this node
345

346     _backAction.setEnabled(true); // now we can back up.
347
_forwardAction.setEnabled(false); // can't go any more forward
348
// (any applicable previous forward info is lost) because you nuked the forward list
349
_displayPage(url);
350   }
351 }
352
Popular Tags