Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 303   Methods: 21
NCLOC: 192   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RecentDocFrame.java 7.1% 35.7% 23.8% 28%
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.border.LineBorder;
 41    import javax.swing.border.EmptyBorder;
 42    import java.awt.*;
 43    import java.util.LinkedList;
 44    import edu.rice.cs.drjava.model.OpenDefinitionsDocument;
 45    import edu.rice.cs.drjava.DrJava;
 46    import edu.rice.cs.drjava.config.*;
 47    import edu.rice.cs.util.swing.DisplayManager;
 48    import edu.rice.cs.util.swing.Utilities;
 49   
 50    /** This class extends a Swing view class. Hence it should only be accessed from the event-handling thread. */
 51    public class RecentDocFrame extends JWindow {
 52    // MainFrame
 53    MainFrame _frame;
 54   
 55    // The manager that gives filenames and icons
 56    DisplayManager<OpenDefinitionsDocument> _displayManager = MainFrame.getOddDisplayManager30();
 57   
 58    // the label that shows the icon and filename
 59    JLabel _label;
 60    // the panel that holds the label and textpane
 61    JPanel _panel;
 62    // the pane that holds the sample of source
 63    JTextPane _textpane;
 64    // the scroller that holds the text
 65    JScrollPane _scroller;
 66    // the currently selected document
 67    int _current = 0;
 68   
 69    int _padding = 4;
 70   
 71    LinkedList<OpenDefinitionsDocument> _docs = new LinkedList<OpenDefinitionsDocument>();
 72   
 73    private OptionListener<Color> _colorListener = new OptionListener<Color>() {
 74  0 public void optionChanged(OptionEvent<Color> oce) { updateFontColor(); }
 75    };
 76   
 77    private OptionListener<Font> _fontListener = new OptionListener<Font>() {
 78  0 public void optionChanged(OptionEvent<Font> oce) { updateFontColor(); }
 79    };
 80   
 81    private OptionListener<Boolean> _antialiasListener = new OptionListener<Boolean>() {
 82  0 public void optionChanged(OptionEvent<Boolean> oce) { updateFontColor(); }
 83    };
 84   
 85    private OptionListener<Boolean> _showSourceListener = new OptionListener<Boolean>() {
 86  0 public void optionChanged(OptionEvent<Boolean> oce) { _showSource = oce.value; }
 87    };
 88   
 89    /* if the pane should antialias itself */
 90    boolean _antiAliasText = false;
 91   
 92    /* if we should show source code when switching */
 93    boolean _showSource;
 94   
 95  38 public RecentDocFrame(MainFrame f) {
 96  38 super();
 97  38 _frame = f;
 98  38 _current = 0;
 99  38 _label = new JLabel("...") {
 100    // Enable anti-aliased text by overriding paintComponent.
 101  0 protected void paintComponent(Graphics g) {
 102  0 if (_antiAliasText && g instanceof Graphics2D) {
 103  0 Graphics2D g2d = (Graphics2D)g;
 104  0 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
 105    }
 106  0 super.paintComponent(g);
 107    }
 108    };
 109  38 _panel = new JPanel();
 110  38 _scroller = new JScrollPane();
 111  38 _textpane = new JTextPane() {
 112    // Enable anti-aliased text by overriding paintComponent.
 113  0 protected void paintComponent(Graphics g) {
 114  0 if (_antiAliasText && g instanceof Graphics2D) {
 115  0 Graphics2D g2d = (Graphics2D)g;
 116  0 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
 117    }
 118  0 super.paintComponent(g);
 119    }
 120    };
 121   
 122  38 _textpane.setText("...");
 123  38 _scroller.getViewport().add(_textpane);
 124  38 _scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
 125  38 _scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
 126  38 _scroller.setMaximumSize(new Dimension(300,200));
 127   
 128  38 _panel.setLayout(new BorderLayout());
 129  38 _panel.add(_label, BorderLayout.NORTH);
 130  38 _panel.add(_scroller, BorderLayout.SOUTH);
 131   
 132  38 getContentPane().add(_panel);
 133  38 pack();
 134  38 updateFontColor();
 135  38 _showSource = DrJava.getConfig().getSetting(OptionConstants.SHOW_SOURCE_WHEN_SWITCHING);
 136  38 DrJava.getConfig().addOptionListener(OptionConstants.DEFINITIONS_BACKGROUND_COLOR, _colorListener);
 137  38 DrJava.getConfig().addOptionListener(OptionConstants.DEFINITIONS_NORMAL_COLOR, _colorListener);
 138  38 DrJava.getConfig().addOptionListener(OptionConstants.FONT_MAIN, _fontListener);
 139  38 DrJava.getConfig().addOptionListener(OptionConstants.TEXT_ANTIALIAS, _antialiasListener);
 140  38 DrJava.getConfig().addOptionListener(OptionConstants.SHOW_SOURCE_WHEN_SWITCHING, _showSourceListener);
 141    }
 142   
 143  38 private void updateFontColor() {
 144  38 Font mainFont = DrJava.getConfig().getSetting(OptionConstants.FONT_MAIN);
 145  38 Color backColor = DrJava.getConfig().getSetting(OptionConstants.DEFINITIONS_BACKGROUND_COLOR);
 146  38 Color fontColor = DrJava.getConfig().getSetting(OptionConstants.DEFINITIONS_NORMAL_COLOR);
 147    /* make it bigger */
 148  38 Font titleFont = mainFont.deriveFont((float) (mainFont.getSize() + 3));
 149  38 _antiAliasText = DrJava.getConfig().getSetting(OptionConstants.TEXT_ANTIALIAS).booleanValue();
 150   
 151  38 _label.setForeground(fontColor);
 152  38 _panel.setBackground(backColor);
 153  38 _label.setFont(titleFont);
 154  38 _textpane.setForeground(fontColor);
 155  38 _textpane.setFont(mainFont);;
 156  38 _textpane.setBackground(backColor);
 157  38 _scroller.setBackground(backColor);
 158  38 _scroller.setBorder(new EmptyBorder(0,0,0,0));
 159  38 _panel.setBorder(new LineBorder(fontColor, 1));
 160    }
 161    /** Moves the document d to the beginning of the list if it's already in the list, or it adds it to the
 162    * beginning if its not already in the list.
 163    */
 164  102 public void pokeDocument(OpenDefinitionsDocument d) {
 165  102 if (_docs.contains(d)) {
 166  14 _current = _docs.indexOf(d);
 167  14 reset();
 168    }
 169  88 else _docs.addFirst(d);
 170    }
 171   
 172    /** Removes the document from the list. */
 173  38 public void closeDocument(OpenDefinitionsDocument d) { _docs.remove(d); }
 174   
 175  0 private void show(int _current) {
 176  0 OpenDefinitionsDocument doc = _docs.get(_current);
 177   
 178  0 String text = getTextFor(doc);
 179   
 180  0 _label.setText(_displayManager.getName(doc));
 181  0 _label.setIcon(_displayManager.getIcon(doc));
 182   
 183  0 if (text.length() > 0) {
 184    // as wide as the text area wants, but only 200px high
 185  0 _textpane.setText(text);
 186  0 _scroller.setPreferredSize(_textpane.getPreferredScrollableViewportSize());
 187  0 if (_scroller.getPreferredSize().getHeight() > 200)
 188  0 _scroller.setPreferredSize(new Dimension((int)_scroller.getPreferredSize().getWidth(), 200));
 189   
 190  0 _scroller.setVisible(_showSource);
 191    }
 192  0 else _scroller.setVisible(false);
 193   
 194  0 Dimension d = _label.getMinimumSize();
 195  0 d.setSize(d.getWidth() + _padding*2, d.getHeight() + _padding*2);
 196  0 _label.setPreferredSize(d);
 197  0 _label.setHorizontalAlignment(SwingConstants.CENTER);
 198  0 _label.setVerticalAlignment(SwingConstants.CENTER);
 199  0 pack();
 200  0 centerH();
 201    }
 202   
 203    /** Sets the current document to be the next document in the list. */
 204  0 public void next() {
 205  0 if (_docs.size() > 0) {
 206  0 _current++;
 207  0 if (_current >= _docs.size()) _current = 0;
 208  0 show(_current);
 209    }
 210    }
 211   
 212    /** Sets the current document to be the previous document in the list. */
 213  0 public void prev() {
 214  0 if (_docs.size() > 0) {
 215  0 _current--;
 216  0 if (_current < 0) _current = _docs.size() - 1;
 217  0 show(_current);
 218    }
 219    }
 220   
 221  0 private String getTextFor(OpenDefinitionsDocument doc) {
 222  0 DefinitionsPane pane = _frame.getDefPaneGivenODD(doc);
 223  0 String endl = "\n"; // was StringOps.EOL; but Swing uses '\n' for newLine
 224  0 int loc = pane.getCaretPosition();
 225  0 int start = loc;
 226  0 int end = loc;
 227  0 String text;
 228  0 text = doc.getText();
 229   
 230    /* get the starting point of 2 lines up... */
 231  0 for (int i = 0; i < 4; i++) {
 232  0 if (start > 0) start = text.lastIndexOf(endl, start-endl.length());
 233    }
 234  0 if (start == -1) start = 0;
 235   
 236    // skip the end line, if we're at one
 237    // if (doc.getLength() >= endl.length() && text.substring(start, start+endl.length()) == endl)
 238    // start += endl.length();
 239  0 if (doc.getLength() >= endl.length() && text.substring(start, start+endl.length()).equals(endl))
 240  0 start += endl.length();
 241    /* get the ending point 2 lines down */
 242  0 int index;
 243  0 for (int i = 0; i < 4; i++) {
 244  0 if (end < doc.getLength()) {
 245  0 index = text.indexOf(endl, end + endl.length());
 246  0 if (index != -1) end = index;
 247    }
 248    }
 249  0 if (end < start) end = start;
 250  0 text = text.substring(start, end);
 251  0 return text;
 252    }
 253   
 254    /** Resets the frame to point to the first document in the list. */
 255  0 public void first() {
 256  0 _current = 0;
 257  0 next();
 258    }
 259   
 260  0 public void refreshColor() { }
 261   
 262    /** Sets this frame as visible only if _docs is non empty. Also resets the frame accordingly */
 263  0 public void setVisible(boolean v) {
 264  0 centerH();
 265  0 if (_docs.size() > 0) {
 266  0 if (v) {
 267  0 centerV();
 268  0 refreshColor();
 269  0 first();
 270    }
 271  0 else reset();
 272  0 super.setVisible(v);
 273    }
 274    }
 275   
 276    /** Centers the frame in the screen. */
 277  0 private void centerH() { Utilities.setPopupLoc(this, _frame); }
 278   
 279    /** Centers the frame in the screen. */
 280  0 private void centerV() {
 281  0 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
 282  0 Dimension frameSize = getSize();
 283  0 setLocation((int)getLocation().getX(), (screenSize.height - frameSize.height) / 2);
 284    }
 285   
 286    /** Moves the selected document to the front of the list. */
 287  14 public void reset() {
 288  14 if (_current < _docs.size()) _docs.addFirst(_docs.remove(_current));
 289    }
 290   
 291    /** Returns null if the list is empty, or the currently prefered OpenDefinitionsDocument. */
 292  0 public OpenDefinitionsDocument getDocument() {
 293  0 if (_docs.size() > 0) return _docs.getFirst();
 294  0 return null;
 295    }
 296   
 297    // private ImageIcon _getIconResource(String name) {
 298    // URL url = RecentDocFrame.class.getResource("icons/" + name);
 299    // if (url != null) return new ImageIcon(url);
 300    // return null;
 301    // }
 302   
 303    }