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.Insets;
16  import java.util.ResourceBundle;
17  import javax.swing.JTabbedPane;
18  import javax.swing.UIManager;
19  import javax.swing.event.ChangeEvent;
20  import javax.swing.event.ChangeListener;
21  import com.timjohnstondev.unitconverter.controller.ModelController;
22  
23  /**
24   * This {@code JTabbedPane} provided a way to add the {@link ConfigurationPanel} and {@link AboutPanel} without adding
25   * more to the window size. It holds all of the configuration options for the application.
26   */
27  public class TopPanel extends JTabbedPane implements Configuration
28  {
29    private CalculationPanel calculationPanel;
30    private ConfigurationPanel configurationPanel;
31    private AboutPanel aboutPanel;
32  
33    /**
34     * Constructs a {@code TopPanel} with a {@link UnitConverter} to allow contained panels access to other panels for
35     * updating.
36     * 
37     * @param appPanel the {@code UnitConverter} to access to other panels
38     * @param controller the {@code Controller} to access to the model layer
39     */
40    public TopPanel(final UnitConverter appPanel, final ModelController controller)
41    {
42      setPreferredSize(new Dimension(500, 100));
43      setMaximumSize(new Dimension(500, 100));
44  
45      calculationPanel = new CalculationPanel();
46      configurationPanel = new ConfigurationPanel(appPanel, controller);
47      aboutPanel = new AboutPanel();
48  
49      final ResourceBundle resources = ResourceBundle.getBundle("com.timjohnstondev.unitconverter.view.View");
50      add(resources.getString("TopPanel.calculationsTabTitle"), calculationPanel);
51      add(resources.getString("TopPanel.configurationTabTitle"), configurationPanel);
52      add(resources.getString("TopPanel.aboutTabTitle"), aboutPanel);
53  
54      UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0));
55      updateUI();
56  
57      addChangeListener(new ChangeListener()
58      {
59        @Override
60        public final void stateChanged(final ChangeEvent event)
61        {
62          setTabColors();
63        }
64      });
65    }
66  
67    private void setTabColors()
68    {
69      final int selectedIndex = getSelectedIndex();
70      final int tabCount = getTabCount();
71      setForegroundAt(selectedIndex, aboutPanel.getForeground());
72      for (int i = 0; i < tabCount; i++)
73      {
74        if (i != selectedIndex)
75        {
76          setForegroundAt(i, aboutPanel.getBackground().darker());
77        }
78      }
79  
80      UIManager.put("TabbedPane.selected", aboutPanel.getBackground());
81      UIManager.put("TabbedPane.focus", aboutPanel.getBackground());
82      updateUI();
83    }
84  
85    /**
86     * Returns the contained {@code CalculationPanel} to other panels for updating.
87     * 
88     * @return the contained {@code CalculationPanel}
89     */
90    final CalculationPanel getCalculationPanel()
91    {
92      return calculationPanel;
93    }
94  
95    @Override
96    public final void setBackgroundColor(final Color color)
97    {
98      setBackground(color);
99      calculationPanel.setBackgroundColor(color);
100     configurationPanel.setBackgroundColor(color);
101     aboutPanel.setBackgroundColor(color);
102     setTabColors();
103   }
104 
105   @Override
106   public final void setFontSize(final float fontSize)
107   {
108     setFont(getFont().deriveFont(fontSize));
109     calculationPanel.setFontSize(fontSize);
110     configurationPanel.setFontSize(fontSize);
111     aboutPanel.setFontSize(fontSize);
112   }
113 
114   @Override
115   public final void setForegroundColor(final Color color)
116   {
117     setForeground(color);
118     calculationPanel.setForegroundColor(color);
119     configurationPanel.setForegroundColor(color);
120     aboutPanel.setForegroundColor(color);
121     setTabColors();
122   }
123 }