View Javadoc

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    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    {
54      setSize(new Dimension(500, 650));
55      setLocationRelativeTo(null);
56      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
57      setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
58      setResizable(false);
59  
60      loadResources();
61  
62      logicController = new LogicController();
63      modelController = new ModelController();
64      topPanel = new TopPanel(this, modelController);
65      unitLabelPanel = new UnitLabelPanel();
66      unitPanel = new UnitPanel(modelController);
67  
68      unitPanel.addFactorChangeListener(this);
69  
70      add(topPanel);
71      add(unitLabelPanel);
72      add(unitPanel);
73      setColors();
74      setFontSize();
75      setIconImage(getImage());
76    }
77  
78    private void loadResources()
79    {
80      resources = ResourceBundle.getBundle("com.timjohnstondev.unitconverter.view.View");
81      setTitle(resources.getString("UnitConverter.title"));
82      prefKeyBackground = resources.getString("UnitConverter.prefKeyBackground");
83      prefKeyForeground = resources.getString("UnitConverter.prefKeyForeground");
84    }
85  
86    private void setFontSize()
87    {
88      setFontSize(logicController.getFontSizePreference(getContentPane().getFont().getSize()));
89    }
90  
91    private void setColors()
92    {
93      setBackgroundColor(getColorPreference(prefKeyBackground));
94      setForegroundColor(getColorPreference(prefKeyForeground));
95    }
96  
97    @Override
98    public final void setForegroundColor(final Color color)
99    {
100     topPanel.setForegroundColor(color);
101     unitLabelPanel.setForegroundColor(color);
102     unitPanel.setForegroundColor(color);
103 
104     getContentPane().setForeground(color);
105     setColorPreference(color, prefKeyForeground);
106   }
107 
108   @Override
109   public final void setBackgroundColor(final Color color)
110   {
111     topPanel.setBackgroundColor(color);
112     unitLabelPanel.setBackgroundColor(color);
113     unitPanel.setBackgroundColor(color);
114 
115     getContentPane().setBackground(color);
116     setColorPreference(color, prefKeyBackground);
117   }
118 
119   private void setColorPreference(final Color color, final String colorPref)
120   {
121     final Preferences userPref = Preferences.userNodeForPackage(Launcher.class);
122     userPref.put(colorPref, String.valueOf(color.getRed()) + "," + String.valueOf(color.getGreen()) + ","
123         + String.valueOf(color.getBlue()));
124   }
125 
126   private Color getColorPreference(final String colorPref)
127   {
128     final Preferences userPref = Preferences.userNodeForPackage(Launcher.class);
129     final String[] defaultColors = new String[3];
130     String[] colors = userPref.get(colorPref, "").split(",");
131     if (colors.length < 3)
132     {
133       colors = defaultColors;
134     }
135 
136     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     Color color = getContentPane().getBackground();
142     if (colorPref.equals(prefKeyForeground))
143     {
144       color = getContentPane().getForeground();
145     }
146     try
147     {
148       final int red = Integer.parseInt(redStr);
149       final int green = Integer.parseInt(greenStr);
150       final int blue = Integer.parseInt(blueStr);
151       color = new Color(red, green, blue);
152     }
153     catch (NumberFormatException e)
154     {
155       logger.info("No colors stored in preferences, using default colors.");
156     }
157     return color;
158   }
159 
160   private Image getImage()
161   {
162     final List <Image> images = new ArrayList <Image>();
163     URL imgURL = getClass().getResource(resources.getString("UnitConverter.iconFileName16x16"));
164     images.add(new ImageIcon(imgURL).getImage());
165     imgURL = getClass().getResource(resources.getString("UnitConverter.iconFileName32x32"));
166     images.add(new ImageIcon(imgURL).getImage());
167     imgURL = getClass().getResource(resources.getString("UnitConverter.iconFileName48x48"));
168     images.add(new ImageIcon(imgURL).getImage());
169     imgURL = getClass().getResource(resources.getString("UnitConverter.iconFileName64x64"));
170     images.add(new ImageIcon(imgURL).getImage());
171     imgURL = getClass().getResource(resources.getString("UnitConverter.iconFileName96x96"));
172     images.add(new ImageIcon(imgURL).getImage());
173     setIconImages(images);
174 
175     return getIconImages().get(4);
176   }
177 
178   @Override
179   public final void mouseDragged(final MouseEvent event)
180   {}
181 
182   @Override
183   public final void mouseMoved(final MouseEvent event)
184   {}
185 
186   @Override
187   public final void factorChanged(final FactorChangeEvent event)
188   {
189     if (event.getAction() == FactorChangeEvent.CLEAR_FACTOR_ACTION)
190     {
191       topPanel.getCalculationPanel().clearFactor();
192     }
193     else
194     {
195       topPanel.getCalculationPanel().setConversion(event.getCoversion());
196       topPanel.getCalculationPanel().updateCalculations(event.getMolecularWeight());
197     }
198   }
199 
200   @Override
201   public final void setFontSize(final float fontSize)
202   {
203     topPanel.setFontSize(fontSize);
204     unitLabelPanel.setFontSize(fontSize);
205     unitPanel.setFontSize(fontSize);
206 
207     getContentPane().setFont(getContentPane().getFont().deriveFont(fontSize));
208     logicController.setFontSizePreference((int) fontSize);
209   }
210 }