Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 353   Methods: 21
NCLOC: 244   Classes: 4
 
 Source file Conditionals Statements Methods TOTAL
HTMLFrame.java 23.1% 56.4% 38.1% 49.4%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK
 2    *
 3    * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4    * All rights reserved.
 5    *
 6    * Redistribution and use in source and binary forms, with or without
 7    * modification, are permitted provided that the following conditions are met:
 8    * * Redistributions of source code must retain the above copyright
 9    * notice, this list of conditions and the following disclaimer.
 10    * * Redistributions in binary form must reproduce the above copyright
 11    * notice, this list of conditions and the following disclaimer in the
 12    * documentation and/or other materials provided with the distribution.
 13    * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14    * names of its contributors may be used to endorse or promote products
 15    * derived from this software without specific prior written permission.
 16    *
 17    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28    *
 29    * This software is Open Source Initiative approved Open Source Software.
 30    * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31    *
 32    * This file is part of DrJava. Download the current version of this project
 33    * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34    *
 35    * END_COPYRIGHT_BLOCK*/
 36   
 37    package edu.rice.cs.drjava.ui;
 38   
 39    import javax.swing.*;
 40    import javax.swing.event.*;
 41    import javax.swing.border.*;
 42    import javax.swing.text.html.*;
 43    import java.awt.event.*;
 44    import java.awt.*;
 45    import java.net.*;
 46    import java.io.*;
 47    import java.util.ArrayList;
 48   
 49    import edu.rice.cs.util.FileOps;
 50    import edu.rice.cs.util.UnexpectedException;
 51    import edu.rice.cs.util.swing.BorderlessScrollPane;
 52    import edu.rice.cs.util.swing.SwingFrame;
 53    import edu.rice.cs.util.swing.Utilities;
 54   
 55    /** The frame for displaying the HTML help files.
 56    * @version $Id: HTMLFrame.java 5175 2010-01-20 08:46:32Z mgricken $
 57    */
 58    public class HTMLFrame extends SwingFrame {
 59   
 60    private static final int FRAME_WIDTH = 750;
 61    private static final int FRAME_HEIGHT = 600;
 62    private static final int LEFT_PANEL_WIDTH = 250;
 63    private JEditorPane _mainDocPane;
 64    private JScrollPane _mainScroll;
 65    private JSplitPane _splitPane;
 66    private JPanel _splitPaneHolder;
 67    private JEditorPane _contentsDocPane;
 68    private JPanel _closePanel;
 69    private JButton _closeButton;
 70    private JButton _backButton;
 71    private JButton _forwardButton;
 72    protected URL _baseURL;
 73    private ArrayList<HyperlinkListener> _hyperlinkListeners;
 74    private boolean _linkError;
 75    private URL _lastURL;
 76   
 77    private JPanel _navPane;
 78   
 79    protected HistoryList _history;
 80   
 81    private HyperlinkListener _resetListener = new HyperlinkListener() {
 82  0 public void hyperlinkUpdate(HyperlinkEvent e) {
 83  0 if (_linkError && e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
 84  0 _resetMainPane();
 85    }
 86    }
 87    };
 88   
 89    protected static class HistoryList {
 90    private HistoryList next = null;
 91    private final HistoryList prev;
 92    protected final URL contents;
 93  76 private HistoryList(URL contents) {
 94  76 this.contents = contents;
 95  76 this.prev = null;
 96    }
 97  0 private HistoryList(URL contents, HistoryList prev) {
 98  0 this.contents = contents;
 99  0 this.prev = prev;
 100  0 prev.next = this;
 101    }
 102    }
 103   
 104    public static abstract class ResourceAction extends AbstractAction {
 105  152 public ResourceAction(String name, String iconName) {
 106  152 super(name,MainFrame.getIcon(iconName));
 107    }
 108    }
 109   
 110    private static abstract class ConsolidatedAction extends ResourceAction {
 111  152 private ConsolidatedAction(String name) {
 112  152 super(name,name + "16.gif");
 113    }
 114    }
 115   
 116    private Action _forwardAction = new ConsolidatedAction("Forward") {
 117  0 public void actionPerformed(ActionEvent e) {
 118  0 _history = _history.next;
 119   
 120    // user is always allowed to move back after a forward.
 121  0 _backAction.setEnabled(true);
 122   
 123  0 if (_history.next == null) {
 124    // no more forwards after this
 125  0 _forwardAction.setEnabled(false);
 126    }
 127  0 _displayPage(_history.contents);
 128    }
 129    };
 130   
 131    private Action _backAction = new ConsolidatedAction("Back") {
 132  0 public void actionPerformed(ActionEvent e) {
 133  0 _history = _history.prev;
 134   
 135    // user is always allowed to move forward after backing up
 136  0 _forwardAction.setEnabled(true);
 137   
 138  0 if (_history.prev == null) {
 139    // no more backing up
 140  0 _backAction.setEnabled(false);
 141    }
 142  0 _displayPage(_history.contents);
 143    }
 144    };
 145   
 146    private Action _closeAction = new AbstractAction("Close") {
 147  0 public void actionPerformed(ActionEvent e) {
 148  0 HTMLFrame.this.setVisible(false);
 149    }
 150    };
 151   
 152  152 private static JButton makeButton(Action a, int horTextPos, int left, int right) {
 153  152 JButton j = new JButton(a);
 154  152 j.setHorizontalTextPosition(horTextPos);
 155  152 j.setVerticalTextPosition(JButton.CENTER);
 156    //Insets i = j.getMargin();
 157    //j.setMargin(new Insets(i.top,left,i.bottom,right));
 158  152 j.setMargin(new Insets(3,left+3,3,right+3));
 159  152 return j;
 160    }
 161   
 162  76 public void addHyperlinkListener(HyperlinkListener linkListener) {
 163  76 _hyperlinkListeners.add(linkListener);
 164  76 _contentsDocPane.addHyperlinkListener(linkListener);
 165  76 _mainDocPane.addHyperlinkListener(linkListener);
 166    }
 167   
 168    /** Sets up the frame and displays it. */
 169  76 public HTMLFrame(String frameName, URL introUrl, URL indexUrl, String iconString) {
 170  76 this(frameName, introUrl, indexUrl, iconString, null);
 171    }
 172   
 173    /** Sets up the frame and displays it. */
 174  76 public HTMLFrame(String frameName, URL introUrl, URL indexUrl, String iconString, File baseDir) {
 175  76 super(frameName);
 176   
 177  76 _contentsDocPane = new JEditorPane();
 178  76 _contentsDocPane.setEditable(false);
 179  76 JScrollPane contentsScroll = new BorderlessScrollPane(_contentsDocPane);
 180   
 181  76 _mainDocPane = new JEditorPane();
 182  76 _mainDocPane.setEditable(false);
 183  76 _mainScroll = new BorderlessScrollPane(_mainDocPane);
 184   
 185  76 _splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, contentsScroll, _mainScroll);
 186  76 _splitPane.setDividerLocation(LEFT_PANEL_WIDTH);
 187  76 _splitPaneHolder = new JPanel(new GridLayout(1,1));
 188  76 _splitPaneHolder.setBorder(new EmptyBorder(0,5,0,5));
 189  76 _splitPaneHolder.add(_splitPane);
 190    // _splitPane.setBorder(new CompoundBorder(new EmptyBorder(0,5,0,5),_splitPane.getBorder()));
 191  76 _closeButton = new JButton(_closeAction);
 192  76 _backButton = makeButton(_backAction,JButton.RIGHT,0,3);
 193  76 _forwardButton = makeButton(_forwardAction,JButton.LEFT,3,0);
 194  76 _backAction.setEnabled(false);
 195  76 _forwardAction.setEnabled(false);
 196  76 _closePanel = new JPanel(new BorderLayout());
 197  76 _closePanel.add(_closeButton, BorderLayout.EAST);
 198  76 _closePanel.setBorder(new EmptyBorder(5,5,5,5)); // padding
 199  76 _navPane = new JPanel();
 200  76 _navPane.setBackground(new Color(0xCCCCFF));
 201  76 _navPane.setLayout(new BoxLayout(_navPane,BoxLayout.X_AXIS));
 202  76 JLabel icon = new JLabel(MainFrame.getIcon(iconString));
 203  76 _navPane.add(icon);
 204  76 _navPane.add(Box.createHorizontalStrut(8));
 205  76 _navPane.add(Box.createHorizontalGlue());
 206  76 _navPane.add(_backButton);
 207  76 _navPane.add(Box.createHorizontalStrut(8));
 208  76 _navPane.add(_forwardButton);
 209  76 _navPane.add(Box.createHorizontalStrut(3));
 210  76 _navPane.setBorder(new EmptyBorder(0,0,0,5));
 211  76 JPanel navContainer = new JPanel(new GridLayout(1,1));
 212  76 navContainer.setBorder(new CompoundBorder(new EmptyBorder(5,5,5,5), new EtchedBorder()));
 213    //new BevelBorder(BevelBorder.LOWERED)));
 214  76 navContainer.add(_navPane);
 215  76 Container cp = getContentPane();
 216  76 cp.setLayout(new BorderLayout());
 217  76 cp.add(navContainer, BorderLayout.NORTH);
 218  76 cp.add(_splitPaneHolder, BorderLayout.CENTER);
 219  76 cp.add(_closePanel, BorderLayout.SOUTH);
 220   
 221  76 _linkError = false;
 222  76 _hyperlinkListeners = new ArrayList<HyperlinkListener>();
 223  76 _hyperlinkListeners.add(_resetListener);
 224  76 _mainDocPane.addHyperlinkListener(_resetListener);
 225   
 226  76 if (baseDir == null) _baseURL = null;
 227    else
 228  0 try { _baseURL = FileOps.toURL(baseDir); }
 229    catch(MalformedURLException ex) {
 230  0 throw new UnexpectedException(ex);
 231    }
 232   
 233    // Load contents page
 234  0 if (indexUrl == null) _displayContentsError(null);
 235    else
 236  76 try {
 237  76 _contentsDocPane.setPage(indexUrl);
 238  0 if (_baseURL != null) ((HTMLDocument)_contentsDocPane.getDocument()).setBase(_baseURL);
 239    }
 240    catch (IOException ioe) {
 241    // Show some error page?
 242  0 _displayContentsError(indexUrl, ioe);
 243    }
 244   
 245  0 if (introUrl == null) _displayMainError(null);
 246    else {
 247  76 _history = new HistoryList(introUrl);
 248  76 _displayPage(introUrl);
 249  76 _displayPage(introUrl);
 250    }
 251   
 252    // Set all dimensions ----
 253  76 setSize(FRAME_WIDTH, FRAME_HEIGHT);
 254  76 Utilities.setPopupLoc(this, null);
 255   
 256  76 initDone(); // call mandated by SwingFrame contract
 257    }
 258   
 259    /** Hides the navigation panel on the left. Cannot currently be undone. */
 260  0 protected void _hideNavigationPane() {
 261  0 _splitPaneHolder.remove(_splitPane);
 262  0 _splitPaneHolder.add(_mainScroll);
 263    }
 264   
 265  0 private void _resetMainPane() {
 266  0 _linkError = false;
 267   
 268  0 _mainDocPane = new JEditorPane();
 269  0 _mainDocPane.setEditable(false);
 270  0 for (int i = 0; i < _hyperlinkListeners.size(); i++) {
 271  0 _mainDocPane.addHyperlinkListener(_hyperlinkListeners.get(i));
 272    }
 273  0 _displayPage(_lastURL);
 274   
 275  0 _splitPane.setRightComponent(new BorderlessScrollPane(_mainDocPane));
 276  0 _splitPane.setDividerLocation(LEFT_PANEL_WIDTH);
 277    }
 278   
 279    /** Displays the given URL in the main pane. Changed to private, because of history system.
 280    * @param url URL to display
 281    */
 282  152 private void _displayPage(URL url) {
 283  0 if (url == null) return;
 284  152 try {
 285  152 _mainDocPane.setPage(url);
 286  152 if (_baseURL != null) {
 287  0 ((HTMLDocument)_contentsDocPane.getDocument()).setBase(_baseURL);
 288    }
 289  152 _lastURL = url;
 290    }
 291    catch (IOException ioe) {
 292  0 String path = url.getPath();
 293  0 try {
 294  0 URL newURL = new URL(_baseURL + path);
 295  0 _mainDocPane.setPage(newURL);
 296  0 if (_baseURL != null) {
 297  0 ((HTMLDocument)_contentsDocPane.getDocument()).setBase(_baseURL);
 298    }
 299  0 _lastURL = newURL;
 300    }
 301    catch (IOException ioe2) {
 302    // Show some error page?
 303  0 _displayMainError(url, ioe2);
 304    //System.err.println("couldn't find url: " + url);
 305    }
 306    }
 307    }
 308   
 309    /** Prints an error indicating that the HTML file to load in the main pane could not be found. */
 310  0 private void _displayMainError(URL url) {
 311  0 if (!_linkError) {
 312  0 _linkError = true;
 313  0 _mainDocPane.setText(getErrorText(url));
 314    }
 315  0 else _resetMainPane();
 316    }
 317   
 318    /** Prints an error indicating that the HTML file to load in the main pane could not be found
 319    */
 320  0 private void _displayMainError(URL url, Exception ex) {
 321  0 if (!_linkError) {
 322  0 _linkError = true;
 323  0 _mainDocPane.setText(getErrorText(url) + "\n" + ex);
 324    }
 325  0 else _resetMainPane();
 326    }
 327   
 328    /** Prints an error indicating that the HTML file to load in the contentes pane
 329    * could not be found
 330    */
 331  0 private void _displayContentsError(URL url) {
 332  0 _contentsDocPane.setText(getErrorText(url));
 333    }
 334   
 335    /** Prints an error indicating that the HTML file to load in the contentes pane could not be found. */
 336  0 private void _displayContentsError(URL url, Exception ex) {
 337  0 _contentsDocPane.setText(getErrorText(url) + "\n" + ex);
 338    }
 339   
 340    /** This method returns the error text to display when something goes wrong. */
 341  0 protected String getErrorText(URL url) {
 342  0 return "Could not load the specified URL: " + url;
 343    }
 344   
 345  0 public void jumpTo(URL url) {
 346  0 _history = new HistoryList(url,_history); // current history is prev for this node
 347   
 348  0 _backAction.setEnabled(true); // now we can back up.
 349  0 _forwardAction.setEnabled(false); // can't go any more forward
 350    // (any applicable previous forward info is lost) because you nuked the forward list
 351  0 _displayPage(url);
 352    }
 353    }