Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 248   Methods: 11
NCLOC: 163   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DrJavaSurveyPopup.java 0% 0% 0% 0%
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 java.awt.event.*;
 42    import java.awt.*;
 43    import java.io.*;
 44    import java.net.*;
 45    import java.util.Date;
 46   
 47    import edu.rice.cs.drjava.DrJava;
 48    import edu.rice.cs.drjava.Version;
 49    import edu.rice.cs.drjava.config.OptionConstants;
 50    import edu.rice.cs.drjava.platform.*;
 51    import edu.rice.cs.util.swing.UneditableTableModel;
 52    import edu.rice.cs.util.swing.BorderlessScrollPane;
 53    import edu.rice.cs.plt.lambda.Runnable1;
 54    import edu.rice.cs.plt.lambda.LambdaUtil;
 55   
 56    /** Asks whether DrJava may contact the DrJava developers and send information about
 57    * the operating system and the Java version used.
 58    * @version $Id: DrJavaSurveyPopup.java 5175 2010-01-20 08:46:32Z mgricken $
 59    */
 60    public class DrJavaSurveyPopup extends JDialog {
 61    /** the keys of the system properties that we want to send */
 62    public static final String[] DRJAVA_SURVEY_KEYS = new String[] {"os.name","os.version","java.version","java.vendor"};
 63   
 64    /** the no button */
 65    private JButton _noButton;
 66    /** the yes button */
 67    private JButton _yesButton;
 68    /** the parent frame */
 69    private MainFrame _mainFrame;
 70    /** the version information pane */
 71    private JOptionPane _questionPanel;
 72    /** the table with the information that DrJava will send */
 73    private JTable _propertiesTable;
 74    /** don't ask user again */
 75    private JCheckBox _neverAskAgain;
 76   
 77    /** Creates a window to display whether a new version of DrJava is available. */
 78  0 public DrJavaSurveyPopup(MainFrame parent) {
 79  0 super(parent, "Send System Information to DrJava Developers");
 80  0 setResizable(false);
 81  0 setSize(550,350);
 82  0 _mainFrame = parent;
 83   
 84  0 _yesButton = new JButton(_yesAction);
 85  0 _noButton = new JButton(_noAction);
 86  0 _neverAskAgain = new JCheckBox("Never ask me again",
 87    !DrJava.getConfig().getSetting(OptionConstants.DIALOG_DRJAVA_SURVEY_ENABLED).booleanValue());
 88  0 _neverAskAgain.addChangeListener(new ChangeListener() {
 89  0 public void stateChanged(ChangeEvent e) {
 90  0 DrJava.getConfig().setSetting(OptionConstants.DIALOG_DRJAVA_SURVEY_ENABLED, !_neverAskAgain.isSelected());
 91    }
 92    });
 93   
 94  0 JPanel buttonPanel = new JPanel();
 95  0 buttonPanel.add(_neverAskAgain);
 96  0 buttonPanel.add(_yesButton);
 97  0 buttonPanel.add(_noButton);
 98   
 99  0 _questionPanel = new JOptionPane("May DrJava anonymously send the information\nbelow to the DrJava developers?",
 100    JOptionPane.QUESTION_MESSAGE,JOptionPane.DEFAULT_OPTION,null,
 101    new Object[0]);
 102  0 int size = DRJAVA_SURVEY_KEYS.length + 2;
 103  0 String[][] rowData = new String[size][2];
 104  0 int rowNum = 0;
 105  0 for(String k: DRJAVA_SURVEY_KEYS) {
 106  0 rowData[rowNum][0] = k;
 107  0 rowData[rowNum][1] = System.getProperty(k);
 108  0 ++rowNum;
 109    }
 110  0 rowData[rowNum ][0] = "DrJava revision";
 111  0 rowData[rowNum++][1] = String.valueOf(Version.getRevisionNumber());
 112  0 rowData[rowNum ][0] = "DrJava build time";
 113  0 rowData[rowNum++][1] = String.valueOf(Version.getBuildTimeString());
 114  0 java.util.Arrays.sort(rowData,new java.util.Comparator<String[]>() {
 115  0 public int compare(String[] o1, String[] o2) {
 116  0 return o1[0].compareTo(o2[0]);
 117    }
 118    });
 119  0 String[] nvStrings = new String[] {"Name","Value"};
 120  0 UneditableTableModel model = new UneditableTableModel(rowData, nvStrings);
 121  0 _propertiesTable = new JTable(model);
 122  0 JScrollPane scroller = new BorderlessScrollPane(_propertiesTable);
 123   
 124  0 JPanel centerPanel = new JPanel(new BorderLayout());
 125  0 centerPanel.add(_questionPanel, BorderLayout.NORTH);
 126  0 centerPanel.add(scroller, BorderLayout.CENTER);
 127   
 128  0 getContentPane().setLayout(new BorderLayout());
 129  0 getContentPane().add(centerPanel, BorderLayout.CENTER);
 130  0 getContentPane().add(buttonPanel, BorderLayout.SOUTH);
 131  0 _mainFrame.setPopupLoc(this);
 132    }
 133   
 134    /* Close the window. */
 135    private Action _noAction = new AbstractAction("No") {
 136  0 public void actionPerformed(ActionEvent e) { noAction(); }
 137    };
 138   
 139    /** Close this window, but display the full DrJava Errors window. */
 140    private Action _yesAction = new AbstractAction("Yes") {
 141  0 public void actionPerformed(ActionEvent e) { yesAction(); }
 142    };
 143   
 144  0 protected void noAction() {
 145    // set the date we asked even if the user pressed no
 146    // so the user won't be bothered the next time he starts DrJava
 147    // next popup will occur in DRJAVA_SURVEY_DAYS (91) days.
 148  0 DrJava.getConfig().setSetting(OptionConstants.LAST_DRJAVA_SURVEY, new Date().getTime());
 149  0 DrJava.getConfig().setSetting(OptionConstants.LAST_DRJAVA_SURVEY_RESULT, getSurveyURL());
 150  0 setVisible(false);
 151  0 dispose();
 152    }
 153   
 154    public static final edu.rice.cs.util.Log LOG = new edu.rice.cs.util.Log("survey.txt",false);
 155   
 156    /** Return the URL that would be used to answer the DrJava survey. */
 157  0 public static String getSurveyURL() {
 158  0 final String DRJAVA_SURVEY_PAGE = "http://www.drjava.org/submit-usage.php?";
 159  0 StringBuilder sb = new StringBuilder();
 160  0 sb.append(DRJAVA_SURVEY_PAGE);
 161  0 sb.append("rev=");
 162  0 sb.append(Version.getRevisionNumber());
 163  0 for(String k: DRJAVA_SURVEY_KEYS) {
 164  0 sb.append('&');
 165  0 sb.append(k);
 166  0 sb.append('=');
 167  0 sb.append(System.getProperty(k));
 168    }
 169  0 LOG.log(sb.toString());
 170  0 return sb.toString().replaceAll(" ","%20");
 171    }
 172   
 173    /** Returns true if the user may participate in the survey, i.e. either the configuration
 174    * string has changed from the last time, or enough days have passed since the last test (3 months) */
 175  0 public static boolean maySubmitSurvey() {
 176    // check how many days have passed since the last survey
 177  0 int days = DrJava.getConfig().getSetting(OptionConstants.DRJAVA_SURVEY_DAYS);
 178  0 Date nextCheck =
 179    new Date(DrJava.getConfig().getSetting(OptionConstants.LAST_DRJAVA_SURVEY) +
 180    days * 24L * 60 * 60 * 1000); // x days after last check; 24L ensures long accumulation
 181  0 return (new Date().after(nextCheck)) ||
 182    (!DrJava.getConfig().getSetting(OptionConstants.LAST_DRJAVA_SURVEY_RESULT).equals(getSurveyURL()));
 183    }
 184   
 185  0 protected void yesAction() {
 186  0 try {
 187    // append build time here so it does not change when comparing the survey information
 188    // to the information submitted the last time; the revision number does matter, though
 189  0 String result = getSurveyURL() + "&buildtime=" + Version.getBuildTimeString();
 190  0 LOG.log(result);
 191   
 192  0 if (!maySubmitSurvey()) {
 193    // not enough days have passed, or the configuration has not changed, quietly terminate
 194  0 return;
 195    }
 196   
 197  0 BufferedReader br = null;
 198  0 try {
 199  0 URL url = new URL(result);
 200  0 InputStream urls = url.openStream();
 201  0 InputStreamReader is = new InputStreamReader(urls);
 202  0 br = new BufferedReader(is);
 203  0 String line;
 204  0 StringBuilder sb = new StringBuilder();
 205  0 while((line = br.readLine()) != null) { sb.append(line); sb.append(System.getProperty("line.separator")); }
 206  0 LOG.log(sb.toString());
 207    }
 208    catch(IOException e) {
 209    // could not open URL using Java, try web browser
 210  0 LOG.log("Could not open URL using Java", e);
 211  0 try {
 212  0 PlatformFactory.ONLY.openURL(new URL(result));
 213  0 DrJava.getConfig().setSetting(OptionConstants.LAST_DRJAVA_SURVEY_RESULT, result);
 214    }
 215    catch(IOException e2) {
 216    // could not open using Java or web browser, ignore
 217  0 LOG.log("Could not open URL using web browser", e2);
 218    }
 219    }
 220    finally { // close open input stream
 221  0 try { if (br != null) br.close(); }
 222    catch(IOException e) { /* ignore */ }
 223    }
 224    }
 225  0 finally { noAction(); }
 226    }
 227   
 228    /** Lambda that calls noAction. */
 229    protected final Runnable1<WindowEvent> NO = new Runnable1<WindowEvent>() {
 230  0 public void run(WindowEvent e) { noAction(); }
 231    };
 232   
 233    /** Toggle visibility of this frame. Warning, it behaves like a modal dialog. */
 234  0 public void setVisible(boolean vis) {
 235  0 assert EventQueue.isDispatchThread();
 236  0 validate();
 237  0 if (vis) {
 238  0 _mainFrame.hourglassOn();
 239  0 _mainFrame.installModalWindowAdapter(this, LambdaUtil.NO_OP, NO);
 240    }
 241    else {
 242  0 _mainFrame.removeModalWindowAdapter(this);
 243  0 _mainFrame.hourglassOff();
 244  0 _mainFrame.toFront();
 245    }
 246  0 super.setVisible(vis);
 247    }
 248    }