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.Component;
15  import java.awt.Font;
16  import java.awt.GridLayout;
17  import java.awt.Insets;
18  import java.awt.event.ActionEvent;
19  import java.awt.event.ActionListener;
20  import java.awt.event.MouseEvent;
21  import java.awt.event.MouseListener;
22  import java.math.BigDecimal;
23  import java.util.ResourceBundle;
24  import javax.swing.JButton;
25  import javax.swing.JLabel;
26  import javax.swing.JPanel;
27  import javax.swing.JTextField;
28  import javax.swing.SwingConstants;
29  import javax.swing.event.DocumentEvent;
30  import javax.swing.event.DocumentListener;
31  import com.timjohnstondev.unitconverter.controller.LogicController;
32  
33  /**
34   * This panel displays all of the information regarding mole to mass and mass to mole conversions.
35   */
36  public class MolarMassPanel extends JPanel implements Configuration, ActionListener, MouseListener
37  {
38    private UnitPanel unitPanel;
39    private JButton mole2Mass;
40    private JButton mass2Mole;
41    private JButton neither;
42    private JLabel massMoleLabel;
43    private JTextField massMoleField;
44    private JLabel massMoleFormula;
45    private Object selectedButton;
46    private ResourceBundle resources;
47  
48    /**
49     * Constructs a {@code MolarMassPanel}
50     * 
51     * @param panel parent panel
52     */
53    public MolarMassPanel(final UnitPanel panel)
54    {
55      unitPanel = panel;
56      setLayout(new GridLayout(6, 1, 0, 3));
57      resources = ResourceBundle.getBundle("com.timjohnstondev.unitconverter.view.View");
58  
59      mole2Mass = new JButton(resources.getString("MolarMassPanel.mole2Mass"));
60      mass2Mole = new JButton(resources.getString("MolarMassPanel.mass2Mole"));
61      neither = new JButton(resources.getString("MolarMassPanel.neither"));
62  
63      massMoleLabel = new JLabel(resources.getString("MolarMassPanel.massMoleLabel"), SwingConstants.CENTER);
64      massMoleLabel.setToolTipText(resources.getString("MolarMassPanel.massMoleField"));
65      massMoleField = new JTextField();
66      massMoleField.setToolTipText(resources.getString("MolarMassPanel.massMoleField"));
67  
68      massMoleField.setDocument(new InputFilter(resources.getString("MolarMassPanel.allowableCharacters")));
69      massMoleField.getDocument().addDocumentListener(new InputDocumentListener());
70      massMoleFormula = new JLabel("", SwingConstants.CENTER);
71  
72      add(mole2Mass);
73      add(mass2Mole);
74      add(neither);
75      add(massMoleLabel);
76      add(massMoleField);
77      add(massMoleFormula);
78      configureButtons();
79    }
80  
81    /**
82     * @return the molecular weight
83     */
84    final BigDecimal getMolecularWeightCorrection()
85    {
86      final LogicController logicController = new LogicController();
87      return logicController.getMolecularWeightCorrection(massMoleField, mass2Mole, selectedButton, unitPanel
88          .isMolUnitDivisor());
89    }
90  
91    /**
92     * @return {@code true} if mode is Mass to Mole, else false
93     */
94    final boolean isMassToMoleSelected()
95    {
96      return mass2Mole == selectedButton;
97    }
98  
99    private void setItemsVisible(final boolean isVisible)
100   {
101     neither.setVisible(isVisible);
102     massMoleLabel.setVisible(isVisible);
103     massMoleField.setVisible(isVisible);
104     massMoleFormula.setVisible(isVisible);
105   }
106 
107   private void configureButtons()
108   {
109     final Component[] components = getComponents();
110     for (Component component : components)
111     {
112       if (component instanceof JButton)
113       {
114         final JButton button = (JButton) component;
115         button.setMargin(new Insets(0, 0, 0, 0));
116         button.addActionListener(this);
117         button.addMouseListener(this);
118         button.setEnabled(false);
119       }
120     }
121   }
122 
123   @Override
124   public final void actionPerformed(final ActionEvent event)
125   {
126     if (event.getSource() == mole2Mass || event.getSource() == mass2Mole)
127     {
128       setItemsVisible(true);
129       selectedButton = event.getSource();
130       unitPanel.updateFields();
131       updatePanelDisplays(event.getSource());
132     }
133     if (event.getSource() == neither)
134     {
135       setItemsVisible(false);
136       mole2Mass.setEnabled(false);
137       mass2Mole.setEnabled(false);
138       selectedButton = null;
139       massMoleField.setText(null);
140       unitPanel.clearMassMoleLabels();
141       unitPanel.updateFields();
142     }
143   }
144 
145   private void updatePanelDisplays(final Object source)
146   {
147     String text = resources.getString("MolarMassPanel.moleMassFormula");
148     String toolTipText = resources.getString("MolarMassPanel.moleMassFormulaToolTip");
149     final String altText = resources.getString("MolarMassPanel.massMoleFormula");
150     final String altToolTipText = resources.getString("MolarMassPanel.massMoleFormulaToolTip");
151 
152     if (source == mole2Mass)
153     {
154       mass2Mole.setEnabled(false);
155       unitPanel.updateMoleToMassLabels();
156       if (unitPanel.isMolUnitDivisor())
157       {
158         text = altText;
159         toolTipText = altToolTipText;
160       }
161     }
162     else if (source == mass2Mole)
163     {
164       mole2Mass.setEnabled(false);
165       unitPanel.updateMassToMoleLabels();
166 
167       if (!unitPanel.isMolUnitDivisor())
168       {
169         text = altText;
170         toolTipText = altToolTipText;
171       }
172     }
173     massMoleFormula.setText(text);
174     massMoleFormula.setToolTipText(toolTipText);
175   }
176 
177   @Override
178   public final void setBackgroundColor(final Color color)
179   {
180     setBackground(color);
181     final Component[] children = getComponents();
182     for (Component child : children)
183     {
184       child.setBackground(color);
185     }
186   }
187 
188   @Override
189   public final void setFontSize(final float fontSize)
190   {
191     final Font font = getFont().deriveFont(fontSize);
192     setFont(font);
193     final Component[] children = getComponents();
194     for (Component child : children)
195     {
196       child.setFont(font);
197     }
198   }
199 
200   @Override
201   public final void setForegroundColor(final Color color)
202   {
203     setForeground(color);
204     final Component[] children = getComponents();
205     for (Component child : children)
206     {
207       child.setForeground(color);
208     }
209   }
210 
211   @Override
212   public void mouseClicked(final MouseEvent event)
213   {}
214 
215   @Override
216   public final void mouseEntered(final MouseEvent event)
217   {
218     event.getComponent().setEnabled(true);
219   }
220 
221   @Override
222   public final void mouseExited(final MouseEvent event)
223   {
224     if (selectedButton == null || event.getComponent() != selectedButton)
225     {
226       event.getComponent().setEnabled(false);
227     }
228   }
229 
230   @Override
231   public void mousePressed(final MouseEvent event)
232   {}
233 
234   @Override
235   public void mouseReleased(final MouseEvent event)
236   {}
237 
238   /**
239    * Displays this {@code MolarMassPanel} with only the mole to mass and mass to mole buttons showing initially.
240    * 
241    * @param y this panel's new Y coordinate
242    */
243   final void showPanel(final int y)
244   {
245     setVisible(true);
246     setItemsVisible(false);
247     massMoleField.setText(null);
248     setBounds(getX(), y, getWidth(), getHeight());
249     mole2Mass.setEnabled(false);
250     mass2Mole.setEnabled(false);
251     selectedButton = null;
252   }
253 
254   /**
255    * Hides this {@code MolarMassPanel} usually because it is not appropriate for the given property
256    */
257   final void hidePanel()
258   {
259     setVisible(false);
260     setItemsVisible(false);
261   }
262 
263   private class InputDocumentListener implements DocumentListener
264   {
265     @Override
266     public final void changedUpdate(final DocumentEvent e)
267     {
268       unitPanel.updateFields();
269     }
270 
271     @Override
272     public final void insertUpdate(final DocumentEvent e)
273     {
274       changedUpdate(e);
275     }
276 
277     @Override
278     public final void removeUpdate(final DocumentEvent e)
279     {
280       changedUpdate(e);
281     }
282   }
283 }