1 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 ; 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 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 <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 name, String iconName) { 101 super(name,MainFrame.getIcon(iconName)); 102 } 103 } 104 105 private static abstract class ConsolidatedAction extends ResourceAction { 106 private ConsolidatedAction(String 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 _backAction.setEnabled(true); 117 118 if (_history.next == null) { 119 _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 _forwardAction.setEnabled(true); 132 133 if (_history.prev == null) { 134 _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 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 167 public HTMLFrame(String frameName, URL introUrl, URL indexUrl, String iconString) { 168 this(frameName, introUrl, indexUrl, iconString, null); 169 } 170 171 174 public HTMLFrame(String frameName, URL introUrl, URL indexUrl, String 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 _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)); _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 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 <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 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 _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 setSize(FRAME_WIDTH, FRAME_HEIGHT); 254 MainFrame.setPopupLoc(this, null); 255 } 256 257 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 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 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 _displayMainError(url, ioe2); 301 } 303 } 304 } 305 306 307 private void _displayMainError(URL url) { 308 if (!_linkError) { 309 _linkError = true; 310 _mainDocPane.setText(getErrorText(url)); 311 } 312 else _resetMainPane(); 313 } 314 315 317 private void _displayMainError(URL url, Exception ex) { 318 if (!_linkError) { 319 _linkError = true; 320 _mainDocPane.setText(getErrorText(url) + "\n" + ex); 321 } 322 else _resetMainPane(); 323 } 324 325 329 private void _displayContentsError(URL url) { 330 _contentsDocPane.setText(getErrorText(url)); 331 } 332 333 334 private void _displayContentsError(URL url, Exception ex) { 335 _contentsDocPane.setText(getErrorText(url) + "\n" + ex); 336 } 337 338 339 protected String 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); 346 _backAction.setEnabled(true); _forwardAction.setEnabled(false); _displayPage(url); 350 } 351 } 352 | Popular Tags |