Coverage Report - us.daveread.utility.sixsigma.gui.SigmaCalculatorDialog
 
Classes in this File Line Coverage Branch Coverage Complexity
SigmaCalculatorDialog
0%
0/108
0%
0/13
1.875
 
 1  
 package us.daveread.utility.sixsigma.gui;
 2  
 
 3  
 import javax.swing.*;
 4  
 import java.awt.*;
 5  
 import java.awt.event.*;
 6  
 import us.daveread.utility.sixsigma.SigmaCalculator;
 7  
 
 8  
 /**
 9  
  * <p>Title: Sigma Calculator GUI
 10  
  * <p>Description: Allow interactive calculation of sigma
 11  
  * <p>Copyright: Copyright (c) 2005
 12  
  * <p>This program is free software; you can redistribute it and/or modify
 13  
  * it under the terms of the GNU General Public License as published by
 14  
  * the Free Software Foundation; either version 2 of the License, or
 15  
  * (at your option) any later version.
 16  
  * <p>This program is distributed in the hope that it will be useful,
 17  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19  
  * GNU General Public License for more details.
 20  
  * <p>You should have received a copy of the GNU General Public License
 21  
  * along with this program; if not, write to the Free Software
 22  
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23  
  * </p>
 24  
  *
 25  
  * @author David Read
 26  
  * @version $Id: SigmaCalculatorDialog.java,v 1.1.1.1 2006/05/22 02:14:09 daveread Exp $
 27  
  */
 28  
 public class SigmaCalculatorDialog
 29  
     extends JDialog
 30  
     implements ActionListener, WindowListener {
 31  
 
 32  
   /** Widgets to hold input values */
 33  
   JTextField opportunities, defects, sigmaShift;
 34  
 
 35  
   /** Widgets to control dialog */
 36  
   JButton calculate, close;
 37  
 
 38  
   /** Widgets to hold resulting calculated values */
 39  
   JLabel DPMO, defectPercent, yieldPercent, sigma;
 40  
 
 41  
   /**
 42  
    * Setup a modal dialog, tied to the parent frame.
 43  
    *
 44  
    * @param parent Frame The parent frame
 45  
    *
 46  
    * @throws HeadlessException If there is no GUI output device
 47  
    */
 48  
   public SigmaCalculatorDialog(Frame parent) throws HeadlessException {
 49  0
     super(parent, "Sigma Calculator", true);
 50  
 
 51  0
     addWindowListener(this);
 52  0
     setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
 53  
 
 54  0
     setupGUI();
 55  0
     getRootPane().setDefaultButton(calculate);
 56  
 
 57  0
     center(this, parent);
 58  0
   }
 59  
 
 60  
   /**
 61  
    * Creates the GUI widgets for the sigma calculator.
 62  
    */
 63  
   private void setupGUI() {
 64  
     JPanel temp;
 65  
 
 66  0
     getContentPane().setLayout(new BorderLayout());
 67  
 
 68  
     // Center houses input and output fields
 69  0
     temp = new JPanel();
 70  0
     temp.setLayout(new GridLayout(0, 2));
 71  
 
 72  
     // Inputs
 73  0
     temp.add(new JLabel("Opportunities:"));
 74  0
     temp.add(opportunities = new JTextField(10));
 75  0
     temp.add(new JLabel("Defects:"));
 76  0
     temp.add(defects = new JTextField(10));
 77  0
     temp.add(new JLabel("Sigma Shift:"));
 78  0
     temp.add(sigmaShift = new JTextField(5));
 79  0
     temp.add(new JLabel("DPMO:"));
 80  0
     temp.add(DPMO = new JLabel());
 81  0
     temp.add(new JLabel("Defect (%):"));
 82  0
     temp.add(defectPercent = new JLabel());
 83  0
     temp.add(new JLabel("Yield (%):"));
 84  0
     temp.add(yieldPercent = new JLabel());
 85  0
     temp.add(new JLabel("Sigma:"));
 86  0
     temp.add(sigma = new JLabel());
 87  
 
 88  0
     getContentPane().add(temp, BorderLayout.CENTER);
 89  
 
 90  
     // South houses controls
 91  0
     temp = new JPanel();
 92  0
     temp.setLayout(new FlowLayout());
 93  
 
 94  
     // Controls
 95  0
     temp.add(calculate = new JButton("Calculate"));
 96  0
     calculate.setMnemonic('C');
 97  0
     calculate.addActionListener(this);
 98  0
     temp.add(close = new JButton("Exit"));
 99  0
     close.setMnemonic('x');
 100  0
     close.addActionListener(this);
 101  
 
 102  0
     getContentPane().add(temp, BorderLayout.SOUTH);
 103  
 
 104  0
     pack();
 105  0
   }
 106  
 
 107  
   /**
 108  
    * Determines whether a string is empty (null or 0-length).
 109  
    *
 110  
    * @param value String The string to check
 111  
    *
 112  
    * @return boolean True if the string is empty
 113  
    */
 114  
   private boolean isEmpty(String value) {
 115  0
     return value == null || value.trim().length() == 0;
 116  
   }
 117  
 
 118  
   /**
 119  
    * Determines whether a string contains a numeric value.
 120  
    *
 121  
    * @param value String The string to check
 122  
    *
 123  
    * @return boolean True if the string contains a numeric value
 124  
    */
 125  
   private boolean isNumber(String value) {
 126  
     boolean number;
 127  
 
 128  
     try {
 129  0
       if (isEmpty(value)) {
 130  0
         throw new IllegalArgumentException("Value is empty");
 131  
       }
 132  0
       doubleValue(value);
 133  0
       number = true;
 134  
     }
 135  0
     catch (Throwable any) {
 136  0
       number = false;
 137  0
     }
 138  
 
 139  0
     return number;
 140  
   }
 141  
 
 142  
   /**
 143  
    * Returns the numeric value in the string as a double.
 144  
    *
 145  
    * @param value String The string containing the value
 146  
    *
 147  
    * @return double The value in the string
 148  
    */
 149  
   private double doubleValue(String value) {
 150  0
     return Double.parseDouble(value);
 151  
   }
 152  
 
 153  
   /**
 154  
    * Utilizes the sigma calculator to update the display with the calculated
 155  
    * sigma, and other attributes.
 156  
    */
 157  
   private void calculateSigma() {
 158  
     SigmaCalculator calc;
 159  
     String lOpportunities, lDefects, lSigmaShift;
 160  
     String errorMessage;
 161  
 
 162  0
     lOpportunities = opportunities.getText();
 163  0
     lDefects = defects.getText();
 164  0
     lSigmaShift = sigmaShift.getText();
 165  0
     calc = new SigmaCalculator(0, 0);
 166  0
     errorMessage = "";
 167  
 
 168  0
     if (isNumber(lOpportunities)) {
 169  0
       calc.setOpportunities(doubleValue(lOpportunities));
 170  
     }
 171  
     else {
 172  0
       errorMessage += "Opportunities is not a number (" + lOpportunities +
 173  
           ")\n";
 174  
     }
 175  
 
 176  0
     if (isNumber(lDefects)) {
 177  0
       calc.setDefects(doubleValue(lDefects));
 178  
     }
 179  
     else {
 180  0
       errorMessage += "Defects is not a number (" + lDefects + ")\n";
 181  
     }
 182  
 
 183  0
     if (isEmpty(lSigmaShift)) {
 184  0
       lSigmaShift = "0.0";
 185  
     }
 186  0
     if (isNumber(lSigmaShift)) {
 187  0
       calc.setSigmaShift(doubleValue(lSigmaShift));
 188  
     }
 189  
     else {
 190  0
       errorMessage += "Sigma Shift is not a number (" + lSigmaShift + ")\n";
 191  
     }
 192  
 
 193  0
     if (errorMessage.length() == 0) {
 194  0
       DPMO.setText(calc.getDPMO(0));
 195  0
       defectPercent.setText(calc.getDefectPercent(2));
 196  0
       yieldPercent.setText(calc.getYieldPercent(2));
 197  0
       sigma.setText(calc.getSigma(2));
 198  
     }
 199  
     else {
 200  0
       DPMO.setText("");
 201  0
       defectPercent.setText("");
 202  0
       yieldPercent.setText("");
 203  0
       sigma.setText("");
 204  0
       JOptionPane.showMessageDialog(this, errorMessage, "Incorrect Value",
 205  
                                     JOptionPane.ERROR_MESSAGE);
 206  
     }
 207  0
   }
 208  
 
 209  
   /**
 210  
    * Close the sigma calculator dialog
 211  
    */
 212  
   private void closeApplication() {
 213  0
     dispose();
 214  0
   }
 215  
 
 216  
   /**
 217  
    * Centers a window on the parent window or the screen.  If the parent
 218  
    * is null, or smaller than the child, the child is centered on the
 219  
    * screen.
 220  
    *
 221  
    * @param winaChild The window being centered.
 222  
    * @param winaParent The parent window, or null if no parent frame exists.
 223  
    */
 224  
   private void center(Window winaChild, Window winaParent) {
 225  
     Dimension dimlParentSize, dimlMySize;
 226  
     Point ptlUpperLeft;
 227  
     int ilX, ilY;
 228  0
     dimlMySize = winaChild.getSize();
 229  0
     if (winaParent == null) {
 230  0
       dimlParentSize = Toolkit.getDefaultToolkit().getScreenSize();
 231  0
       ptlUpperLeft = new Point(0, 0);
 232  
     }
 233  
     else {
 234  0
       dimlParentSize = winaParent.getSize();
 235  0
       ptlUpperLeft = winaParent.getLocation();
 236  
 
 237  0
       if (dimlMySize.width >= dimlParentSize.width ||
 238  
           dimlMySize.height >= dimlParentSize.height) {
 239  0
         dimlParentSize = Toolkit.getDefaultToolkit().getScreenSize();
 240  0
         ptlUpperLeft = new Point(0, 0);
 241  
       }
 242  
     }
 243  0
     if (dimlParentSize.width >= dimlMySize.width) {
 244  0
       ilX = (dimlParentSize.width - dimlMySize.width) / 2;
 245  
     }
 246  
     else {
 247  0
       ilX = 0;
 248  
     }
 249  0
     if (dimlParentSize.height >= dimlMySize.height) {
 250  0
       ilY = (dimlParentSize.height - dimlMySize.height) / 2;
 251  
     }
 252  
     else {
 253  0
       ilY = 0;
 254  
     }
 255  0
     ilX += ptlUpperLeft.x;
 256  0
     ilY += ptlUpperLeft.y;
 257  0
     winaChild.setLocation(ilX, ilY);
 258  0
   }
 259  
 
 260  
   // Begin Interface ActionListener
 261  
 
 262  
   public void actionPerformed(ActionEvent evtaWhat) {
 263  
     Object objlSource;
 264  
 
 265  0
     objlSource = evtaWhat.getSource();
 266  
 
 267  0
     if (objlSource == calculate) {
 268  0
       calculateSigma();
 269  
     }
 270  0
     else if (objlSource == close) {
 271  0
       closeApplication();
 272  
     }
 273  0
   }
 274  
 
 275  
   // End Interface ActionListener
 276  
 
 277  
   // Begin Interface WindowListener
 278  
 
 279  
   public void windowActivated(WindowEvent evtaWhat) {
 280  0
   }
 281  
 
 282  
   public void windowClosed(WindowEvent evtaWhat) {
 283  0
   }
 284  
 
 285  
   public void windowClosing(WindowEvent evtaWhat) {
 286  0
     closeApplication();
 287  0
   }
 288  
 
 289  
   public void windowDeactivated(WindowEvent evtaWhat) {
 290  0
   }
 291  
 
 292  
   public void windowDeiconified(WindowEvent evtaWhat) {
 293  0
   }
 294  
 
 295  
   public void windowIconified(WindowEvent evtaWhat) {
 296  0
   }
 297  
 
 298  
   public void windowOpened(WindowEvent evtaWhat) {
 299  0
   }
 300  
 
 301  
   // End Interface WindowListener
 302  
 
 303  
 }