Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 247   Methods: 21
NCLOC: 166   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
VectorKeyStrokeOptionComponent.java 0% 15.9% 38.1% 18.8%
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.config;
 38   
 39    import java.awt.*;
 40    import java.awt.event.*;
 41    import javax.swing.*;
 42    import javax.swing.table.*;
 43    import javax.swing.*;
 44   
 45    import java.util.*;
 46   
 47    import edu.rice.cs.drjava.*;
 48    import edu.rice.cs.drjava.ui.*;
 49    import edu.rice.cs.drjava.config.*;
 50    import edu.rice.cs.util.swing.SwingFrame;
 51    import edu.rice.cs.util.swing.CheckBoxJList;
 52    import edu.rice.cs.util.swing.SwingFrame;
 53    import edu.rice.cs.util.swing.Utilities;
 54   
 55    /** Graphical form of a VectorOption for the KeyStroke options.
 56    * @version $Id: VectorKeyStrokeOptionComponent.java 5363 2010-08-14 04:07:34Z mgricken $
 57    */
 58    public class VectorKeyStrokeOptionComponent extends VectorOptionComponent<KeyStroke> implements OptionConstants, Comparable<VectorKeyStrokeOptionComponent> {
 59    private static final int DIALOG_HEIGHT = 185;
 60    public static final HashMap<KeyStroke, VectorKeyStrokeOptionComponent> _keyToKSOC =
 61    new HashMap<KeyStroke, VectorKeyStrokeOptionComponent>();
 62   
 63    /** Reset the current keystroke map to the values in the backup map. */
 64  0 public static void resetCurrentKeyStrokeMap() {
 65  0 HashMap<KeyStroke, VectorKeyStrokeOptionComponent> newKeyToKSOC =
 66    new HashMap<KeyStroke, VectorKeyStrokeOptionComponent>();
 67   
 68  0 for(VectorKeyStrokeOptionComponent vksoc: _keyToKSOC.values()) {
 69  0 for(KeyStroke k: vksoc.getKeyStrokes()) newKeyToKSOC.put(k, vksoc);
 70    }
 71   
 72  0 _keyToKSOC.clear();
 73  0 for(Map.Entry<KeyStroke, VectorKeyStrokeOptionComponent> e: newKeyToKSOC.entrySet()) {
 74  0 _keyToKSOC.put(e.getKey(), e.getValue());
 75    }
 76    }
 77   
 78  3 public VectorKeyStrokeOptionComponent (VectorOption<KeyStroke> opt, String text, SwingFrame parent) {
 79  3 this(opt, text, parent, null);
 80  3 for(KeyStroke k: getKeyStrokes()) _keyToKSOC.put(k, this);
 81    }
 82   
 83    /** Constructor that allows for a tooltip description. */
 84  23553 public VectorKeyStrokeOptionComponent (VectorOption<KeyStroke> opt, String text, SwingFrame parent, String description) {
 85  23553 this(opt, text, parent, description, false);
 86  16549 for(KeyStroke k: getKeyStrokes()) _keyToKSOC.put(k, this);
 87    }
 88   
 89    /** Constructor with flag for move buttons. */
 90  23553 public VectorKeyStrokeOptionComponent (VectorOption<KeyStroke> opt, String text, SwingFrame parent,
 91    String description, boolean moveButtonEnabled) {
 92  23553 super(opt, text, parent, new String[] { }, description, moveButtonEnabled); // creates all four buttons
 93  16549 for(KeyStroke k: getKeyStrokes()) _keyToKSOC.put(k, this);
 94    }
 95   
 96    /** Returns the table model. Can be overridden by subclasses. */
 97  23553 protected AbstractTableModel _makeTableModel() {
 98  23553 return new AbstractTableModel() {
 99  113321 public int getRowCount() { return _data.size(); }
 100  117765 public int getColumnCount() { return 1; }
 101  0 public Object getValueAt(int row, int col) {
 102  0 switch(col) {
 103  0 case 0: return KeyStrokeOption.formatKeyStroke(_data.get(row));
 104    }
 105  0 throw new IllegalArgumentException("Illegal column");
 106    }
 107  0 public Class<?> getColumnClass(int col) {
 108  0 switch(col) {
 109  0 case 0: return String.class;
 110    }
 111  0 throw new IllegalArgumentException("Illegal column");
 112    }
 113    };
 114    }
 115   
 116    /** Compare two components to have them sorted alphabetically. */
 117  0 public int compareTo(VectorKeyStrokeOptionComponent o) {
 118  0 return this.getLabelText().compareTo(o.getLabelText());
 119    }
 120   
 121  47109 Vector<KeyStroke> getKeyStrokes() { return new Vector<KeyStroke>(_data); }
 122   
 123    /** Shows a dialog for adding a keystroke to the element. */
 124  0 public void chooseKeyStroke() {
 125  0 _table.getSelectionModel().clearSelection();
 126  0 GetKeyDialog getKeyDialog = new GetKeyDialog(_parent, "Specify Shortcut", true);
 127  0 getKeyDialog.promptKey(KeyStrokeOption.NULL_KEYSTROKE);
 128    }
 129   
 130    /** Also have to remove the keystroke from the map, in addition to what the superclass already does. */
 131  0 protected void _removeIndex(int i) {
 132  0 _keyToKSOC.remove(_data.get(i));
 133  0 super._removeIndex(i);
 134    }
 135   
 136  23553 protected Action _getAddAction() {
 137  23553 return new AbstractAction("Add") {
 138  0 public void actionPerformed(ActionEvent ae) {
 139  0 chooseKeyStroke();
 140    }
 141    };
 142    }
 143   
 144    /** A dialog that allows the user to type in a keystroke to be bound
 145    * to the action that was clicked. If the user types a keystroke that
 146    * is bound to another action, the dialog will display that information.
 147    */
 148    public class GetKeyDialog extends JDialog {
 149    private InputField _inputField;
 150    private JButton _cancelButton;
 151    private JButton _okButton;
 152    private JLabel _instructionLabel;
 153    private JLabel _currentLabel;
 154    private JLabel _actionLabel;
 155    private JPanel _inputAndClearPanel;
 156    private JPanel _cancelAndOKPanel;
 157    private KeyStroke _currentKeyStroke;
 158   
 159  0 public GetKeyDialog(SwingFrame f, String title, boolean modal) {
 160  0 super(f, title, modal);
 161    // Should all of the following code be run in event thread?
 162  0 _inputField = new InputField();
 163  0 _cancelButton = new JButton("Cancel");
 164  0 _cancelButton.addActionListener(new ActionListener() {
 165  0 public void actionPerformed(ActionEvent ae) {
 166  0 _inputField.requestFocusInWindow();
 167  0 GetKeyDialog.this.dispose();
 168    }
 169    });
 170  0 _okButton = new JButton("OK");
 171  0 _okButton.addActionListener(new ActionListener() {
 172  0 public void actionPerformed(ActionEvent ae) {
 173  0 if ((_currentKeyStroke!=KeyStrokeOption.NULL_KEYSTROKE) &&
 174    (!getKeyStrokes().contains(_currentKeyStroke))) {
 175  0 VectorKeyStrokeOptionComponent conflict = _keyToKSOC.get(_currentKeyStroke);
 176  0 if (conflict != null) {
 177    // key used by another VectorKeyStrokeOptionComponent already
 178    // remove key from that component
 179  0 Vector<KeyStroke> v = conflict.getKeyStrokes();
 180  0 v.removeElement(_currentKeyStroke);
 181  0 conflict.setValue(v);
 182    }
 183   
 184  0 _keyToKSOC.put(_currentKeyStroke, VectorKeyStrokeOptionComponent.this);
 185  0 _addValue(_currentKeyStroke);
 186    }
 187  0 _inputField.requestFocusInWindow();
 188  0 GetKeyDialog.this.dispose();
 189    }
 190    });
 191  0 _instructionLabel = new JLabel("Type in the keystroke you want to use " +
 192    "and click \"OK\"");
 193  0 _currentLabel = new JLabel("Current action bound to the keystroke:");
 194  0 _actionLabel = new JLabel("<none>");
 195   
 196  0 _inputAndClearPanel = new JPanel(new BorderLayout());
 197  0 _inputAndClearPanel.add(_inputField, BorderLayout.CENTER);
 198   
 199  0 _cancelAndOKPanel = new JPanel(new GridLayout(1,0));
 200  0 _cancelAndOKPanel.add(_okButton);
 201  0 _cancelAndOKPanel.add(_cancelButton);
 202   
 203  0 JPanel panel = (JPanel)this.getContentPane();
 204   
 205  0 panel.setLayout(new GridLayout(0, 1));
 206  0 panel.add(_instructionLabel);
 207  0 panel.add(_inputAndClearPanel);
 208  0 panel.add(_currentLabel);
 209  0 panel.add(_actionLabel);
 210  0 panel.add(_cancelAndOKPanel);
 211  0 setSize((int)_instructionLabel.getPreferredSize().getWidth() + 30, DIALOG_HEIGHT);
 212  0 EventQueue.invokeLater(new Runnable() { public void run() { GetKeyDialog.this.pack(); } });
 213    }
 214   
 215  0 public void promptKey(KeyStroke initial) {
 216  0 _instructionLabel.setText("Type in the keystroke you want to use for \"" +
 217    getLabelText() +
 218    "\" and click \"OK\"");
 219  0 _currentKeyStroke = initial;
 220  0 _actionLabel.setText(getLabelText());
 221  0 _inputField.setText(KeyStrokeOption.formatKeyStroke(_currentKeyStroke));
 222  0 this.setSize((int)_instructionLabel.getPreferredSize().getWidth() + 30, DIALOG_HEIGHT);
 223  0 Utilities.setPopupLoc(this, getOwner());
 224  0 this.setVisible(true);
 225    }
 226   
 227    /** A textfield that takes in one keystroke at a time and displays its formatted String version. It updates the
 228    * label that displays what action the currently displayed keystroke is bound to.
 229    */
 230    private class InputField extends JTextField {
 231  0 public void processKeyEvent(KeyEvent e) {
 232  0 KeyStroke ks = KeyStroke.getKeyStrokeForEvent(e);
 233  0 if (e.getID() == KeyEvent.KEY_PRESSED) {
 234  0 this.setText(KeyStrokeOption.formatKeyStroke(ks));
 235  0 VectorKeyStrokeOptionComponent configKs = _keyToKSOC.get(ks);
 236  0 if (configKs == null)
 237  0 _actionLabel.setText("<none>");
 238    else {
 239  0 String name = configKs.getLabelText();//KeyBindingManager.Singleton.getName(configKs.getConfigKeyStroke());
 240  0 _actionLabel.setText(name);
 241    }
 242  0 _currentKeyStroke = ks;
 243    }
 244    }
 245    }
 246    }
 247    }