| 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 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
public class SigmaCalculatorDialog |
| 29 | |
extends JDialog |
| 30 | |
implements ActionListener, WindowListener { |
| 31 | |
|
| 32 | |
|
| 33 | |
JTextField opportunities, defects, sigmaShift; |
| 34 | |
|
| 35 | |
|
| 36 | |
JButton calculate, close; |
| 37 | |
|
| 38 | |
|
| 39 | |
JLabel DPMO, defectPercent, yieldPercent, sigma; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 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 | |
|
| 62 | |
|
| 63 | |
private void setupGUI() { |
| 64 | |
JPanel temp; |
| 65 | |
|
| 66 | 0 | getContentPane().setLayout(new BorderLayout()); |
| 67 | |
|
| 68 | |
|
| 69 | 0 | temp = new JPanel(); |
| 70 | 0 | temp.setLayout(new GridLayout(0, 2)); |
| 71 | |
|
| 72 | |
|
| 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 | |
|
| 91 | 0 | temp = new JPanel(); |
| 92 | 0 | temp.setLayout(new FlowLayout()); |
| 93 | |
|
| 94 | |
|
| 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 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
private boolean isEmpty(String value) { |
| 115 | 0 | return value == null || value.trim().length() == 0; |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 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 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
private double doubleValue(String value) { |
| 150 | 0 | return Double.parseDouble(value); |
| 151 | |
} |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 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 | |
|
| 211 | |
|
| 212 | |
private void closeApplication() { |
| 213 | 0 | dispose(); |
| 214 | 0 | } |
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 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 | |
|
| 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 | |
|
| 276 | |
|
| 277 | |
|
| 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 | |
|
| 302 | |
|
| 303 | |
} |