Coverage Report - com.timjohnstondev.unitconverter.view.CalculationPanel
 
Classes in this File Line Coverage Branch Coverage Complexity
CalculationPanel
0%
0/63
0%
0/12
1.412
CalculationPanel$1
N/A
N/A
1.412
CalculationPanel$ConversionTextField
0%
0/4
N/A
1.412
CalculationPanel$DoNothingKeyListener
0%
0/6
0%
0/2
1.412
CalculationPanel$InputDocumentListener
0%
0/7
N/A
1.412
 
 1  
 /**
 2  
  * Copyright 2009 Timothy Johnston Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
 3  
  * file except in compliance with the License. You may obtain a copy of the License at
 4  
  * 
 5  
  * http://www.apache.org/licenses/LICENSE-2.0
 6  
  * 
 7  
  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 8  
  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 9  
  * specific language governing permissions and limitations under the License.
 10  
  */
 11  
 package com.timjohnstondev.unitconverter.view;
 12  
 
 13  
 import java.awt.Color;
 14  
 import java.awt.Component;
 15  
 import java.awt.Dimension;
 16  
 import java.awt.GridLayout;
 17  
 import java.awt.event.KeyAdapter;
 18  
 import java.awt.event.KeyEvent;
 19  
 import java.math.BigDecimal;
 20  
 import java.util.ResourceBundle;
 21  
 import javax.swing.BorderFactory;
 22  
 import javax.swing.JLabel;
 23  
 import javax.swing.JPanel;
 24  
 import javax.swing.JTextField;
 25  
 import javax.swing.event.DocumentEvent;
 26  
 import javax.swing.event.DocumentListener;
 27  
 import com.timjohnstondev.unitconverter.controller.LogicController;
 28  
 
 29  
 /**
 30  
  * This panel contains all of the {@code JTextField}s for input, conversion factor and result display. All fields can be
 31  
  * copied from, but only the input field can take number input.
 32  
  */
 33  0
 public class CalculationPanel extends JPanel implements Configuration
 34  
 {
 35  
   private LogicController logicController;
 36  
   private JLabel inputLabel;
 37  
   private JLabel conversionFactorLabel;
 38  
   private JLabel resultLabel;
 39  
   private JTextField input;
 40  
   private ConversionTextField conversionFactor;
 41  
   private JTextField result;
 42  
   private ResourceBundle resources;
 43  
 
 44  
   /**
 45  
    * Constructs a {@code CalculationPanel} and sets the layout and size of the panel and its contents.
 46  
    */
 47  
   public CalculationPanel()
 48  0
   {
 49  0
     setMaximumSize(new Dimension(500, 80));
 50  0
     setLayout(new GridLayout(2, 3, 10, 5));
 51  0
     setBorder(BorderFactory.createEmptyBorder(20, 10, 5, 10));
 52  0
     logicController = new LogicController();
 53  
 
 54  0
     resources = ResourceBundle.getBundle("com.timjohnstondev.unitconverter.view.View");
 55  0
     inputLabel = new JLabel(resources.getString("CalculationPanel.inputLabel"));
 56  0
     conversionFactorLabel = new JLabel(resources.getString("CalculationPanel.conversionFactorLabel"));
 57  0
     resultLabel = new JLabel(resources.getString("CalculationPanel.resultLabel"));
 58  0
     configureLabels();
 59  
 
 60  0
     input = new JTextField();
 61  0
     input.setDocument(new InputFilter(resources.getString("CalculationPanel.allowableCharacters")));
 62  0
     conversionFactor = new ConversionTextField();
 63  0
     result = new JTextField();
 64  0
     addListeners();
 65  
 
 66  0
     add(inputLabel);
 67  0
     add(conversionFactorLabel);
 68  0
     add(resultLabel);
 69  0
     add(input);
 70  0
     add(conversionFactor);
 71  0
     add(result);
 72  0
   }
 73  
 
 74  
   /**
 75  
    * Updates the conversion factor field and stores the exact number or formula because the display may limit the
 76  
    * characters to show.
 77  
    * 
 78  
    * @param conversion the conversion factor or formula
 79  
    */
 80  
   public final void setConversion(final String conversion)
 81  
   {
 82  0
     conversionFactor.setConversion(conversion);
 83  0
     final String text = logicController.getConversionFactor(conversion);
 84  0
     conversionFactor.setText(text);
 85  0
   }
 86  
 
 87  
   /**
 88  
    * Updates the result field if the input field has valid data and a conversion factor is selected.
 89  
    * 
 90  
    * @param molecularWeightCorrection the correction factor to adjust for molecular weight in mole to mass and mass to
 91  
    *          mole conversions
 92  
    */
 93  
   public final void updateCalculations(final BigDecimal molecularWeightCorrection)
 94  
   {
 95  0
     final String conversion = conversionFactor.getConversion();
 96  0
     final String resultText = logicController.getResult(input.getText(), conversion, molecularWeightCorrection);
 97  0
     result.setText(resultText);
 98  0
   }
 99  
 
 100  
   private void updateCalculations()
 101  
   {
 102  0
     updateCalculations(BigDecimal.ONE);
 103  0
   }
 104  
 
 105  
   /**
 106  
    * Clears out the text of the conversion factor and results displays by setting their text to {@code null}.
 107  
    */
 108  
   public final void clearFactor()
 109  
   {
 110  0
     conversionFactor.setConversion(null);
 111  0
     conversionFactor.setText(null);
 112  0
     result.setText(null);
 113  0
   }
 114  
 
 115  
   private void configureLabels()
 116  
   {
 117  0
     final JLabel[] labels = {inputLabel, conversionFactorLabel, resultLabel};
 118  0
     for (JLabel label : labels)
 119  
     {
 120  0
       label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
 121  
     }
 122  0
   }
 123  
 
 124  
   private void addListeners()
 125  
   {
 126  0
     input.getDocument().addDocumentListener(new InputDocumentListener());
 127  0
     conversionFactor.addKeyListener(new DoNothingKeyListener());
 128  0
     result.addKeyListener(new DoNothingKeyListener());
 129  0
   }
 130  
 
 131  0
   private class InputDocumentListener implements DocumentListener
 132  
   {
 133  
     @Override
 134  
     public final void changedUpdate(final DocumentEvent e)
 135  
     {
 136  0
       updateCalculations();
 137  0
     }
 138  
 
 139  
     @Override
 140  
     public final void insertUpdate(final DocumentEvent e)
 141  
     {
 142  0
       changedUpdate(e);
 143  0
     }
 144  
 
 145  
     @Override
 146  
     public final void removeUpdate(final DocumentEvent e)
 147  
     {
 148  0
       changedUpdate(e);
 149  0
     }
 150  
   }
 151  
 
 152  0
   private class DoNothingKeyListener extends KeyAdapter
 153  
   {
 154  
     @Override
 155  
     public final void keyPressed(final KeyEvent e)
 156  
     {
 157  0
       if (e.getKeyCode() == KeyEvent.VK_V)
 158  
       {
 159  0
         e.consume();
 160  
       }
 161  0
     }
 162  
 
 163  
     @Override
 164  
     public final void keyTyped(final KeyEvent e)
 165  
     {
 166  0
       e.consume();
 167  0
     }
 168  
   }
 169  
 
 170  0
   private class ConversionTextField extends JTextField
 171  
   {
 172  
     private String conversion;
 173  
 
 174  
     private void setConversion(final String newConversion)
 175  
     {
 176  0
       conversion = newConversion;
 177  0
     }
 178  
 
 179  
     private String getConversion()
 180  
     {
 181  0
       return conversion;
 182  
     }
 183  
   }
 184  
 
 185  
   @Override
 186  
   public final void setBackgroundColor(final Color color)
 187  
   {
 188  0
     setBackground(color);
 189  0
     final Component[] children = getComponents();
 190  0
     for (Component child : children)
 191  
     {
 192  0
       if (child instanceof JLabel)
 193  
       {
 194  0
         child.setBackground(color);
 195  
       }
 196  0
       if (child instanceof JTextField)
 197  
       {
 198  0
         child.setBackground(color.brighter());
 199  
       }
 200  
     }
 201  0
   }
 202  
 
 203  
   @Override
 204  
   public final void setForegroundColor(final Color color)
 205  
   {
 206  0
     setForeground(color);
 207  0
     final Component[] children = getComponents();
 208  0
     for (Component child : children)
 209  
     {
 210  0
       child.setForeground(color);
 211  
     }
 212  0
   }
 213  
 
 214  
   @Override
 215  
   public final void setFontSize(final float fontSize)
 216  
   {
 217  0
     setFont(getFont().deriveFont(fontSize));
 218  0
     final Component[] children = getComponents();
 219  0
     for (Component child : children)
 220  
     {
 221  0
       child.setFont(child.getFont().deriveFont(fontSize));
 222  
     }
 223  0
   }
 224  
 }