Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 350   Methods: 17
NCLOC: 239   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
InsertVariableDialog.java 14.7% 62% 41.2% 53%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK
 2    *
 3    * Copyright (c) 2001-2010, JavaPLT group at Rice University (javaplt@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 edu.rice.cs.drjava.config.OptionConstants;
 40    import edu.rice.cs.drjava.config.PropertyMaps;
 41    import edu.rice.cs.drjava.config.DrJavaProperty;
 42    import edu.rice.cs.plt.lambda.Runnable1;
 43    import edu.rice.cs.plt.lambda.LambdaUtil;
 44    import edu.rice.cs.plt.concurrent.CompletionMonitor;
 45    import edu.rice.cs.plt.tuple.Pair;
 46    import edu.rice.cs.util.swing.SwingFrame;
 47    import edu.rice.cs.util.swing.Utilities;
 48   
 49    import javax.swing.*;
 50    import javax.swing.event.*;
 51    import javax.swing.table.DefaultTableModel;
 52    import java.awt.*;
 53    import java.awt.event.*;
 54    import java.util.*;
 55   
 56    /**
 57    * Dialog allowing the user to select a variable.
 58    */
 59    public class InsertVariableDialog extends SwingFrame implements OptionConstants {
 60    /** Tab pane. */
 61    JTabbedPane _tabbedPane = new JTabbedPane();
 62   
 63    /** Table with variables.
 64    */
 65    private Map<String, JTable> _varTable = new HashMap<String, JTable>();
 66   
 67    /** Model for the table.
 68    */
 69    private Map<String, DefaultTableModel> _varTableModel = new HashMap<String, DefaultTableModel>();
 70   
 71    /** Field to preview the value of the variable.
 72    */
 73    private JTextField _varValueField;
 74   
 75    /** Help/Description for the variable. */
 76    private JTextPane _helpPane;
 77   
 78    /** Button to accept the selection.
 79    */
 80    private JButton _okBtn;
 81   
 82    /** Button to cancel.
 83    */
 84    private JButton _cancelBtn;
 85   
 86    /** Main frame. */
 87    private MainFrame _mainFrame;
 88   
 89    /** Selected entry, or null if canceled. */
 90    private Pair<String,DrJavaProperty> _selected = null;
 91   
 92    /** Completion monitor to tell the calling dialog that we're done. */
 93    private CompletionMonitor _cm;
 94   
 95    /** Create a dialog.
 96    * @param mf the instance of mainframe to query into the project
 97    */
 98  38 public InsertVariableDialog(MainFrame mf, CompletionMonitor cm) {
 99  38 super("Insert Variable");
 100  38 _mainFrame = mf;
 101  38 _cm = cm;
 102  38 initComponents();
 103  38 initDone(); // call mandated by SwingFrame contract
 104  38 pack();
 105  38 Utilities.setPopupLoc(InsertVariableDialog.this, _mainFrame);
 106    }
 107   
 108    /** Build the dialog. */
 109  38 private void initComponents() {
 110  38 super.getContentPane().setLayout(new GridLayout(1,1));
 111   
 112  38 Action okAction = new AbstractAction("Select") {
 113  0 public void actionPerformed(ActionEvent e) {
 114  0 _okCommand();
 115    }
 116    };
 117  38 _okBtn = new JButton(okAction);
 118   
 119  38 Action cancelAction = new AbstractAction("Cancel") {
 120  0 public void actionPerformed(ActionEvent e) {
 121  0 _cancelCommand();
 122    }
 123    };
 124  38 _cancelBtn = new JButton(cancelAction);
 125   
 126  38 JPanel buttons = new JPanel();
 127  38 buttons.add(_okBtn);
 128  38 buttons.add(_cancelBtn);
 129   
 130  38 _helpPane = new JTextPane();
 131  38 _helpPane.setToolTipText("Description of the variable.");
 132  38 _helpPane.setEditable(false);
 133  38 _helpPane.setPreferredSize(new Dimension(500,150));
 134  38 _helpPane.setBorder(new javax.swing.border.EmptyBorder(0,10,0,10));
 135  38 JScrollPane helpPaneSP = new JScrollPane(_helpPane);
 136  38 helpPaneSP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
 137   
 138  38 _varValueField = new JTextField();
 139  38 updatePanes();
 140  38 _tabbedPane.addChangeListener(new ChangeListener() {
 141  0 public void stateChanged(ChangeEvent e) {
 142  0 if (_tabbedPane.getSelectedIndex() < 0) { return; }
 143  0 String category = _tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex());
 144  0 Map<String, DrJavaProperty> properties = PropertyMaps.TEMPLATE.getProperties(category);
 145  0 int row = _varTable.get(category).getSelectedRow();
 146  0 if (row < 0) { return; }
 147  0 String key = _varTableModel.get(category).getValueAt(row,0).toString();
 148  0 DrJavaProperty value = properties.get(key);
 149  0 _varValueField.setText(value.toString());
 150  0 _helpPane.setText(value.getHelp());
 151  0 _helpPane.setCaretPosition(0);
 152  0 _selected = Pair.make(key, value);
 153    }
 154    });
 155   
 156  38 JPanel main = new JPanel(new BorderLayout());
 157   
 158  38 JPanel bottom = new JPanel(new BorderLayout());
 159  38 bottom.add(_varValueField, BorderLayout.CENTER);
 160  38 bottom.add(buttons, BorderLayout.SOUTH);
 161  38 main.add(bottom, BorderLayout.SOUTH);
 162   
 163  38 GridBagLayout gridbag = new GridBagLayout();
 164  38 GridBagConstraints c = new GridBagConstraints();
 165  38 JPanel top = new JPanel(gridbag);
 166  38 Insets insets = new Insets(0, 10, 5, 10);
 167    // JPanel top = new JPanel(new GridLayout(2,1));
 168   
 169  38 c.fill = GridBagConstraints.BOTH;
 170  38 c.weightx = 1.0;
 171  38 c.weighty = 3.0;
 172  38 c.gridwidth = GridBagConstraints.REMAINDER;
 173  38 gridbag.setConstraints(_tabbedPane, c);
 174  38 top.add(_tabbedPane);
 175   
 176  38 c.fill = GridBagConstraints.BOTH;
 177  38 c.weighty = 1.0;
 178  38 c.insets = insets;
 179  38 gridbag.setConstraints(helpPaneSP, c);
 180  38 top.add(helpPaneSP);
 181  38 main.add(top, BorderLayout.CENTER);
 182   
 183    //The following line enables to use scrolling tabs.
 184  38 _tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
 185   
 186    // do not allow preview to have focus
 187  38 _tabbedPane.addFocusListener(new FocusAdapter() {
 188  0 public void focusLost(FocusEvent e) {
 189  0 if (e.getOppositeComponent() == _varValueField) {
 190  0 _tabbedPane.getSelectedComponent().requestFocus();
 191    }
 192    }
 193    });
 194   
 195  38 super.getContentPane().add(main);
 196  38 super.setResizable(false);
 197    }
 198   
 199    /** Create a scroll pane for the specified category with the properties provided in the map.
 200    * @param category category name
 201    * @param props map from property names to actual properties in this category */
 202  236 protected JScrollPane createPane(final String category, final Map<String, DrJavaProperty> props) {
 203  236 _varTableModel.put(category,new DefaultTableModel(0,1) {
 204  236 public String getColumnName(int column) {
 205  236 switch(column) {
 206  236 case 0: return "Variable";
 207  0 default: return super.getColumnName(column);
 208    }
 209    }
 210   
 211  0 public Class<?> getColumnClass(int columnIndex) {
 212  0 switch(columnIndex) {
 213  0 case 0: return String.class;
 214  0 default: return super.getColumnClass(columnIndex);
 215    }
 216    }
 217  0 public boolean isCellEditable(int row, int column) { return false; }
 218    });
 219   
 220  236 _varTable.put(category, new JTable(_varTableModel.get(category)));
 221  236 JScrollPane varTableSP = new JScrollPane(_varTable.get(category));
 222  236 varTableSP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
 223  236 varTableSP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
 224  236 _varTable.get(category).setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 225  236 _varTable.get(category).setDragEnabled(false);
 226  236 _varTable.get(category).setPreferredScrollableViewportSize(new Dimension(500,250));
 227  236 _varTable.get(category).putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
 228  236 ListSelectionModel lsm = _varTable.get(category).getSelectionModel();
 229  236 lsm.addListSelectionListener(new ListSelectionListener() {
 230  236 public void valueChanged(ListSelectionEvent e) {
 231  236 int row = _varTable.get(category).getSelectedRow();
 232  0 if (row < 0) { return; }
 233  236 String key = _varTableModel.get(category).getValueAt(row,0).toString();
 234  236 DrJavaProperty value = PropertyMaps.TEMPLATE.getProperty(category,key);
 235  236 _selected = Pair.make(key, value);
 236  236 _varValueField.setText(value.getLazy(PropertyMaps.TEMPLATE));
 237  236 _helpPane.setText(value.getHelp());
 238  236 _helpPane.setCaretPosition(0);
 239    }
 240    });
 241  236 _varTable.get(category).setSelectionModel(lsm);
 242   
 243  236 TreeSet<String> sorted = new TreeSet<String>();
 244  236 for(DrJavaProperty p: PropertyMaps.TEMPLATE.getProperties(category).values()) {
 245  16952 sorted.add(p.getName());
 246    }
 247   
 248  236 for(String key: sorted) {
 249  16952 Vector<String> row = new Vector<String>(); // Vector is mandated by interface to DefaultTableModel
 250  16952 row.add(key);
 251  16952 _varTableModel.get(category).addRow(row);
 252    }
 253   
 254  236 _varTable.get(category).setRowSelectionInterval(0,0);
 255   
 256  236 return varTableSP;
 257    }
 258   
 259    /** Close the dialog, keeping the last selection in _selected. */
 260  0 protected void _okCommand() {
 261  0 setVisible(false);
 262  0 _cm.signal();
 263    }
 264   
 265    /** Cancel and close the dialog. */
 266  0 protected void _cancelCommand() {
 267  0 _selected = null;
 268  0 setVisible(false);
 269  0 _cm.signal();
 270    }
 271   
 272    /** Update the properties in all the panes. */
 273  38 protected void updatePanes() {
 274  38 Pair<String,DrJavaProperty> sel = getSelected();
 275  38 String selCategory = null;
 276  38 if (sel != null) {
 277  0 selCategory = _tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex());
 278    }
 279  38 _tabbedPane.removeAll();
 280  38 for (String category: PropertyMaps.TEMPLATE.getCategories()) {
 281  236 _tabbedPane.addTab(category, createPane(category, PropertyMaps.TEMPLATE.getProperties(category)));
 282    }
 283  38 if (sel != null) {
 284  0 if (selCategory == null) { sel = null; } else {
 285  0 int i;
 286  0 for (i = 0; i < _tabbedPane.getTabCount(); ++i) {
 287  0 if (_tabbedPane.getTitleAt(i).equals(selCategory)) { _tabbedPane.setSelectedIndex(i); break; }
 288    }
 289  0 if (i == _tabbedPane.getTabCount()) { sel = null; } else {
 290  0 DefaultTableModel tm = _varTableModel.get(selCategory);
 291  0 for (i = 0; i < tm.getRowCount(); ++i) {
 292  0 String key = tm.getValueAt(i,0).toString();
 293  0 if (key.equals(sel.second().getName())) {
 294  0 _varTable.get(selCategory).getSelectionModel().setSelectionInterval(i,i);
 295  0 break;
 296    }
 297    }
 298  0 if (i==tm.getRowCount()) {
 299    // not found, select first
 300  0 _varTable.get(selCategory).getSelectionModel().setSelectionInterval(0,0);
 301    }
 302  0 _varValueField.setText(sel.second().toString());
 303  0 _helpPane.setText(sel.second().getHelp());
 304  0 _helpPane.setCaretPosition(0);
 305  0 _selected = sel;
 306    }
 307    }
 308    }
 309  38 if (sel == null) {
 310  38 _tabbedPane.setSelectedIndex(0);
 311  38 String category = _tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex());
 312  38 Map<String, DrJavaProperty> properties = PropertyMaps.TEMPLATE.getProperties(category);
 313  38 _varTable.get(category).getSelectionModel().setSelectionInterval(0,0);
 314  38 int row = _varTable.get(category).getSelectedRow();
 315  38 if (row >= 0) {
 316  38 String key = _varTableModel.get(category).getValueAt(row,0).toString();
 317  38 DrJavaProperty value = properties.get(key);
 318  38 _varValueField.setText(value.toString());
 319  38 _helpPane.setText(value.getHelp());
 320  38 _helpPane.setCaretPosition(0);
 321  38 _selected = Pair.make(key, value);
 322    }
 323    }
 324    }
 325   
 326    /** Return a pair consisting of the name of the property and the property itself. */
 327  38 public Pair<String,DrJavaProperty> getSelected() { return _selected; }
 328   
 329    /** Runnable1 that calls _cancel. */
 330    protected final Runnable1<WindowEvent> CANCEL = new Runnable1<WindowEvent>() {
 331  0 public void run(WindowEvent e) { _cancelCommand(); }
 332    };
 333   
 334    /** Toggle visibility of this frame. Warning, it behaves like a modal dialog. */
 335  0 public void setVisible(boolean vis) {
 336  0 assert EventQueue.isDispatchThread();
 337  0 validate();
 338  0 if (vis) {
 339  0 updatePanes();
 340  0 _mainFrame.hourglassOn();
 341  0 _mainFrame.installModalWindowAdapter(this, LambdaUtil.NO_OP, CANCEL);
 342    }
 343    else {
 344  0 _mainFrame.removeModalWindowAdapter(this);
 345  0 _mainFrame.hourglassOff();
 346  0 _mainFrame.toFront();
 347    }
 348  0 super.setVisible(vis);
 349    }
 350    }