Coverage Report - com.timjohnstondev.unitconverter.view.UnitConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
UnitConverter
0%
0/89
0%
0/6
1.286
 
 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.Dimension;
 15  
 import java.awt.Image;
 16  
 import java.awt.event.MouseEvent;
 17  
 import java.awt.event.MouseMotionListener;
 18  
 import java.net.URL;
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 import java.util.ResourceBundle;
 22  
 import java.util.logging.Logger;
 23  
 import java.util.prefs.Preferences;
 24  
 import javax.swing.BoxLayout;
 25  
 import javax.swing.ImageIcon;
 26  
 import javax.swing.JFrame;
 27  
 import com.timjohnstondev.unitconverter.Launcher;
 28  
 import com.timjohnstondev.unitconverter.controller.LogicController;
 29  
 import com.timjohnstondev.unitconverter.controller.ModelController;
 30  
 import com.timjohnstondev.unitconverter.view.listener.FactorChangeEvent;
 31  
 import com.timjohnstondev.unitconverter.view.listener.FactorChangeListener;
 32  
 
 33  
 /**
 34  
  * This is the main class for this application. It sets the initial configuration for all panels. All configuration
 35  
  * changes come to the {@code UnitConverter} and get distributed to the other panels.
 36  
  */
 37  
 public class UnitConverter extends JFrame implements MouseMotionListener, FactorChangeListener, Configuration
 38  
 {
 39  0
   private static Logger logger = Logger.getLogger(UnitConverter.class.getName());
 40  
   private LogicController logicController;
 41  
   private ModelController modelController;
 42  
   private TopPanel topPanel;
 43  
   private UnitLabelPanel unitLabelPanel;
 44  
   private UnitPanel unitPanel;
 45  
   private String prefKeyBackground;
 46  
   private String prefKeyForeground;
 47  
   private ResourceBundle resources;
 48  
 
 49  
   /**
 50  
    * Constructs a {@code UnitConverter}. Sets the initial configuration for all panels.
 51  
    */
 52  
   public UnitConverter()
 53  0
   {
 54  0
     setSize(new Dimension(500, 650));
 55  0
     setLocationRelativeTo(null);
 56  0
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 57  0
     setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
 58  0
     setResizable(false);
 59  
 
 60  0
     loadResources();
 61  
 
 62  0
     logicController = new LogicController();
 63  0
     modelController = new ModelController();
 64  0
     topPanel = new TopPanel(this, modelController);
 65  0
     unitLabelPanel = new UnitLabelPanel();
 66  0
     unitPanel = new UnitPanel(modelController);
 67  
 
 68  0
     unitPanel.addFactorChangeListener(this);
 69  
 
 70  0
     add(topPanel);
 71  0
     add(unitLabelPanel);
 72  0
     add(unitPanel);
 73  0
     setColors();
 74  0
     setFontSize();
 75  0
     setIconImage(getImage());
 76  0
   }
 77  
 
 78  
   private void loadResources()
 79  
   {
 80  0
     resources = ResourceBundle.getBundle("com.timjohnstondev.unitconverter.view.View");
 81  0
     setTitle(resources.getString("UnitConverter.title"));
 82  0
     prefKeyBackground = resources.getString("UnitConverter.prefKeyBackground");
 83  0
     prefKeyForeground = resources.getString("UnitConverter.prefKeyForeground");
 84  0
   }
 85  
 
 86  
   private void setFontSize()
 87  
   {
 88  0
     setFontSize(logicController.getFontSizePreference(getContentPane().getFont().getSize()));
 89  0
   }
 90  
 
 91  
   private void setColors()
 92  
   {
 93  0
     setBackgroundColor(getColorPreference(prefKeyBackground));
 94  0
     setForegroundColor(getColorPreference(prefKeyForeground));
 95  0
   }
 96  
 
 97  
   @Override
 98  
   public final void setForegroundColor(final Color color)
 99  
   {
 100  0
     topPanel.setForegroundColor(color);
 101  0
     unitLabelPanel.setForegroundColor(color);
 102  0
     unitPanel.setForegroundColor(color);
 103  
 
 104  0
     getContentPane().setForeground(color);
 105  0
     setColorPreference(color, prefKeyForeground);
 106  0
   }
 107  
 
 108  
   @Override
 109  
   public final void setBackgroundColor(final Color color)
 110  
   {
 111  0
     topPanel.setBackgroundColor(color);
 112  0
     unitLabelPanel.setBackgroundColor(color);
 113  0
     unitPanel.setBackgroundColor(color);
 114  
 
 115  0
     getContentPane().setBackground(color);
 116  0
     setColorPreference(color, prefKeyBackground);
 117  0
   }
 118  
 
 119  
   private void setColorPreference(final Color color, final String colorPref)
 120  
   {
 121  0
     final Preferences userPref = Preferences.userNodeForPackage(Launcher.class);
 122  0
     userPref.put(colorPref, String.valueOf(color.getRed()) + "," + String.valueOf(color.getGreen()) + ","
 123  
         + String.valueOf(color.getBlue()));
 124  0
   }
 125  
 
 126  
   private Color getColorPreference(final String colorPref)
 127  
   {
 128  0
     final Preferences userPref = Preferences.userNodeForPackage(Launcher.class);
 129  0
     final String[] defaultColors = new String[3];
 130  0
     String[] colors = userPref.get(colorPref, "").split(",");
 131  0
     if (colors.length < 3)
 132  
     {
 133  0
       colors = defaultColors;
 134  
     }
 135  
 
 136  0
     return getColor(colorPref, colors[0], colors[1], colors[2]);
 137  
   }
 138  
 
 139  
   private Color getColor(final String colorPref, final String redStr, final String greenStr, final String blueStr)
 140  
   {
 141  0
     Color color = getContentPane().getBackground();
 142  0
     if (colorPref.equals(prefKeyForeground))
 143  
     {
 144  0
       color = getContentPane().getForeground();
 145  
     }
 146  
     try
 147  
     {
 148  0
       final int red = Integer.parseInt(redStr);
 149  0
       final int green = Integer.parseInt(greenStr);
 150  0
       final int blue = Integer.parseInt(blueStr);
 151  0
       color = new Color(red, green, blue);
 152  
     }
 153  0
     catch (NumberFormatException e)
 154  
     {
 155  0
       logger.info("No colors stored in preferences, using default colors.");
 156  0
     }
 157  0
     return color;
 158  
   }
 159  
 
 160  
   private Image getImage()
 161  
   {
 162  0
     final List <Image> images = new ArrayList <Image>();
 163  0
     URL imgURL = getClass().getResource(resources.getString("UnitConverter.iconFileName16x16"));
 164  0
     images.add(new ImageIcon(imgURL).getImage());
 165  0
     imgURL = getClass().getResource(resources.getString("UnitConverter.iconFileName32x32"));
 166  0
     images.add(new ImageIcon(imgURL).getImage());
 167  0
     imgURL = getClass().getResource(resources.getString("UnitConverter.iconFileName48x48"));
 168  0
     images.add(new ImageIcon(imgURL).getImage());
 169  0
     imgURL = getClass().getResource(resources.getString("UnitConverter.iconFileName64x64"));
 170  0
     images.add(new ImageIcon(imgURL).getImage());
 171  0
     imgURL = getClass().getResource(resources.getString("UnitConverter.iconFileName96x96"));
 172  0
     images.add(new ImageIcon(imgURL).getImage());
 173  0
     setIconImages(images);
 174  
 
 175  0
     return getIconImages().get(4);
 176  
   }
 177  
 
 178  
   @Override
 179  
   public final void mouseDragged(final MouseEvent event)
 180  0
   {}
 181  
 
 182  
   @Override
 183  
   public final void mouseMoved(final MouseEvent event)
 184  0
   {}
 185  
 
 186  
   @Override
 187  
   public final void factorChanged(final FactorChangeEvent event)
 188  
   {
 189  0
     if (event.getAction() == FactorChangeEvent.CLEAR_FACTOR_ACTION)
 190  
     {
 191  0
       topPanel.getCalculationPanel().clearFactor();
 192  
     }
 193  
     else
 194  
     {
 195  0
       topPanel.getCalculationPanel().setConversion(event.getCoversion());
 196  0
       topPanel.getCalculationPanel().updateCalculations(event.getMolecularWeight());
 197  
     }
 198  0
   }
 199  
 
 200  
   @Override
 201  
   public final void setFontSize(final float fontSize)
 202  
   {
 203  0
     topPanel.setFontSize(fontSize);
 204  0
     unitLabelPanel.setFontSize(fontSize);
 205  0
     unitPanel.setFontSize(fontSize);
 206  
 
 207  0
     getContentPane().setFont(getContentPane().getFont().deriveFont(fontSize));
 208  0
     logicController.setFontSizePreference((int) fontSize);
 209  0
   }
 210  
 }