Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 280   Methods: 17
NCLOC: 170   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
KeyStrokeOptionComponent.java 14.3% 19.6% 23.5% 19.5%
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 edu.rice.cs.drjava.config.*;
 40    import edu.rice.cs.drjava.*;
 41    import edu.rice.cs.util.swing.SwingFrame;
 42    import edu.rice.cs.util.swing.Utilities;
 43   
 44    import java.awt.*;
 45    import java.awt.event.*;
 46    import java.util.HashMap;
 47    import javax.swing.*;
 48   
 49    /**
 50    * Graphical form of a KeyStrokeOption.
 51    * @version $Id: KeyStrokeOptionComponent.java 5232 2010-04-24 00:14:05Z mgricken $
 52    */
 53    public class KeyStrokeOptionComponent extends OptionComponent<KeyStroke,JPanel>
 54    implements Comparable<KeyStrokeOptionComponent> {
 55    private static final int DIALOG_HEIGHT = 185;
 56   
 57    public static final HashMap<KeyStroke, KeyStrokeOptionComponent> _keyToKSOC =
 58    new HashMap<KeyStroke, KeyStrokeOptionComponent>();
 59    private JButton _button;
 60    private JPanel _panel;
 61    private static GetKeyDialog _getKeyDialog = null;
 62   
 63    private KeyStroke _key;
 64   
 65  3 public KeyStrokeOptionComponent(KeyStrokeOption opt, String text, final SwingFrame parent) {
 66  3 super(opt, text, parent);
 67   
 68  3 _key = DrJava.getConfig().getSetting(opt);
 69   
 70  3 _button = new JButton();
 71  3 _button.addActionListener(new ActionListener() {
 72  0 public void actionPerformed(ActionEvent ae) {
 73   
 74  0 if (_getKeyDialog == null) { _getKeyDialog = new GetKeyDialog(parent, "Specify Shortcut", true); }
 75   
 76  0 String oldText = _button.getText();
 77  0 _getKeyDialog.promptKey(KeyStrokeOptionComponent.this);
 78  0 if (!_button.getText().equals(oldText)) { notifyChangeListeners(); }
 79    }
 80    });
 81  3 _button.setText(_option.format(_key));
 82   
 83  3 _panel = new JPanel(new BorderLayout());
 84  3 _panel.add(_button, BorderLayout.CENTER);
 85   
 86  3 GridLayout gl = new GridLayout(1,0);
 87  3 gl.setHgap(15);
 88  3 _keyToKSOC.put(_key, this);
 89  3 setComponent(_panel);
 90    }
 91   
 92    /** Constructor that allows for a tooltip description. */
 93  0 public KeyStrokeOptionComponent(KeyStrokeOption opt, String text,
 94    SwingFrame parent, String description) {
 95  0 this(opt, text, parent);
 96  0 setDescription(description);
 97    }
 98   
 99    /** Sets the tooltip description text for this option.
 100    * @param description the tooltip text
 101    */
 102  0 public void setDescription(String description) {
 103  0 _panel.setToolTipText(description);
 104  0 _button.setToolTipText(description);
 105  0 _label.setToolTipText(description);
 106    }
 107   
 108    /** Returns a custom string representation of this option component. */
 109  0 public String toString() {
 110  0 return "<KSOC>label:" + getLabelText() + "ks: " +
 111    getKeyStroke() + "jb: " + _button.getText() + "</KSOC>\n";
 112    }
 113   
 114    /** Updates the config object with the new setting. Should run in event thread.
 115    * @return true if the new value is set successfully
 116    */
 117  4 public boolean updateConfig() {
 118  4 if (!_key.equals(getConfigKeyStroke())) {
 119  3 DrJava.getConfig().setSetting(_option, _key);
 120  3 setValue(_key);
 121    }
 122  4 return true;
 123    }
 124   
 125    /** Displays the given value.
 126    */
 127  11 public void setValue(KeyStroke value) {
 128  11 _key = value;
 129  11 _button.setText(_option.format(value));
 130    }
 131   
 132    /** Compares two KeyStrokeOptionComponents based on the text of their labels.
 133    * @return Comparison based on labels, or 1 if o is not a KeyStrokeOptionComponent
 134    */
 135  0 public int compareTo(KeyStrokeOptionComponent other) {
 136  0 return this.getLabelText().compareTo(other.getLabelText());
 137    }
 138   
 139    /** Returns the currently selected KeyStroke.
 140    */
 141  0 public KeyStroke getKeyStroke() {
 142  0 return _key;
 143    }
 144   
 145    /** Returns the KeyStroke current set in the Config settings.
 146    */
 147  4 public KeyStroke getConfigKeyStroke() {
 148  4 return DrJava.getConfig().getSetting(_option);
 149    }
 150   
 151    /** A dialog that allows the user to type in a keystroke to be bound
 152    * to the action that was clicked. If the user types a keystroke that
 153    * is bound to another action, the dialog will display that information.
 154    */
 155    private class GetKeyDialog extends JDialog {
 156    private InputField _inputField;
 157    private JButton _clearButton;
 158    private JButton _cancelButton;
 159    private JButton _okButton;
 160    private JLabel _instructionLabel;
 161    private JLabel _currentLabel;
 162    private JLabel _actionLabel;
 163    private JPanel _inputAndClearPanel;
 164    // private JPanel _labelsPanel;
 165    private JPanel _cancelAndOKPanel;
 166    private KeyStroke _currentKeyStroke;
 167    private KeyStrokeOptionComponent _ksoc;
 168    // private Frame frame;
 169   
 170  0 public GetKeyDialog(SwingFrame f, String title, boolean modal) {
 171  0 super(f, title, modal);
 172    // frame = f;
 173    // Should all of the following code be run in event thread?
 174  0 _inputField = new InputField();
 175  0 _clearButton = new JButton("Clear");
 176  0 _clearButton.addActionListener(new ActionListener() {
 177  0 public void actionPerformed(ActionEvent ae) {
 178  0 _inputField.setText("");
 179  0 _actionLabel.setText("<none>");
 180  0 _currentKeyStroke = KeyStrokeOption.NULL_KEYSTROKE;
 181  0 _inputField.requestFocusInWindow();
 182    }
 183    });
 184  0 _cancelButton = new JButton("Cancel");
 185  0 _cancelButton.addActionListener(new ActionListener() {
 186  0 public void actionPerformed(ActionEvent ae) {
 187  0 _inputField.requestFocusInWindow();
 188  0 GetKeyDialog.this.dispose();
 189    }
 190    });
 191  0 _okButton = new JButton("OK");
 192  0 _okButton.addActionListener(new ActionListener() {
 193  0 public void actionPerformed(ActionEvent ae) {
 194  0 if (!_ksoc.getKeyStroke().equals(_currentKeyStroke)) {
 195  0 _keyToKSOC.remove(_ksoc.getKeyStroke());
 196   
 197  0 KeyStrokeOptionComponent conflict = _keyToKSOC.get(_currentKeyStroke);
 198   
 199  0 if (conflict != null) {
 200  0 _keyToKSOC.remove(_currentKeyStroke);
 201  0 conflict.setValue(KeyStrokeOption.NULL_KEYSTROKE);
 202    }
 203  0 _keyToKSOC.put(_currentKeyStroke, _ksoc);
 204  0 _ksoc.setValue(_currentKeyStroke);
 205    }
 206  0 _inputField.requestFocusInWindow();
 207  0 GetKeyDialog.this.dispose();
 208    }
 209    });
 210  0 _instructionLabel = new JLabel("Type in the keystroke you want to use " +
 211    "and click \"OK\"");
 212  0 _currentLabel = new JLabel("Current action bound to the keystroke:");
 213  0 _actionLabel = new JLabel("<none>");
 214   
 215  0 _inputAndClearPanel = new JPanel(new BorderLayout());
 216  0 _inputAndClearPanel.add(_inputField, BorderLayout.CENTER);
 217  0 _inputAndClearPanel.add(_clearButton, BorderLayout.EAST);
 218   
 219    //_labelsPanel = new JPanel();
 220    //_labelsPanel.add(_currentLabel);
 221    //_labelsPanel.add(_actionLabel);
 222   
 223  0 _cancelAndOKPanel = new JPanel(new GridLayout(1,0));
 224  0 _cancelAndOKPanel.add(_okButton);
 225  0 _cancelAndOKPanel.add(_cancelButton);
 226   
 227  0 JPanel panel = (JPanel)this.getContentPane();
 228   
 229  0 panel.setLayout(new GridLayout(0, 1));
 230  0 panel.add(_instructionLabel);
 231  0 panel.add(_inputAndClearPanel);
 232    //panel.add(_labelsPanel);
 233  0 panel.add(_currentLabel);
 234  0 panel.add(_actionLabel);
 235  0 panel.add(_cancelAndOKPanel);
 236  0 setSize((int)_instructionLabel.getPreferredSize().getWidth() + 30, DIALOG_HEIGHT);
 237    //centerOnScreen();
 238  0 EventQueue.invokeLater(new Runnable() { public void run() { GetKeyDialog.this.pack(); } });
 239    }
 240   
 241  0 public void promptKey(KeyStrokeOptionComponent k) {
 242  0 _ksoc = k;
 243  0 _instructionLabel.setText("Type in the keystroke you want to use for \"" +
 244    k.getLabelText() +
 245    "\" and click \"OK\"");
 246  0 _currentKeyStroke = k.getKeyStroke();
 247  0 _actionLabel.setText(k.getLabelText());
 248  0 _inputField.setText(_option.format(_currentKeyStroke));
 249    //this.setLocation(frame.getLocation());
 250  0 this.setSize((int)_instructionLabel.getPreferredSize().getWidth() + 30, DIALOG_HEIGHT);
 251  0 Utilities.setPopupLoc(this, getOwner());
 252  0 this.setVisible(true);
 253    }
 254   
 255    /** A textfield that takes in one keystroke at a time and displays its formatted String version. It updates the
 256    * label that displays what action the currently displayed keystroke is bound to.
 257    */
 258    private class InputField extends JTextField {
 259    /*public boolean getFocusTraversalKeysEnabled() {
 260    return false;
 261    }*/
 262   
 263  0 public void processKeyEvent(KeyEvent e) {
 264  0 KeyStroke ks = KeyStroke.getKeyStrokeForEvent(e);
 265  0 if (e.getID() == KeyEvent.KEY_PRESSED) {
 266  0 this.setText(_option.format(ks));
 267  0 KeyStrokeOptionComponent configKs = _keyToKSOC.get(ks);
 268  0 if (configKs == null)
 269  0 _actionLabel.setText("<none>");
 270    else {
 271  0 String name = configKs.getLabelText();//KeyBindingManager.Singleton.getName(configKs.getConfigKeyStroke());
 272  0 _actionLabel.setText(name);
 273    }
 274  0 _currentKeyStroke = ks;
 275    }
 276    }
 277    }
 278    }
 279   
 280    }