View Javadoc
1   package org.opentrafficsim.gui;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Container;
5   import java.awt.FlowLayout;
6   import java.awt.event.ActionEvent;
7   import java.awt.event.ActionListener;
8   
9   import javax.swing.JComboBox;
10  import javax.swing.JLabel;
11  import javax.swing.JPanel;
12  import javax.swing.border.Border;
13  import javax.swing.border.LineBorder;
14  
15  import org.opentrafficsim.core.gtu.animation.GTUColorer;
16  import org.opentrafficsim.core.gtu.animation.SwitchableGTUColorer;
17  
18  /**
19   * Let the user select what the colors in the animation mean.
20   * <p>
21   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
22   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
23   * <p>
24   * $LastChangedDate: 2018-09-19 13:55:45 +0200 (Wed, 19 Sep 2018) $, @version $Revision: 4006 $, by $Author: averbraeck $,
25   * initial version 27 mei 2015 <br>
26   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
27   */
28  public class ColorControlPanel extends JPanel implements ActionListener
29  {
30      /** */
31      private static final long serialVersionUID = 20150527L;
32  
33      /** The combo box that sets the coloring for the GTUs. */
34      private JComboBox<GTUColorer> comboBoxGTUColor;
35  
36      /** The panel that holds the legend for the currently selected GTUColorer. */
37      private final JPanel legendPanel;
38  
39      /** The GTUColorer that is currently active. */
40      private GTUColorer gtuColorer;
41  
42      /** The SwitchableGTUColorer that is controlled by this ColorControlPanel, if available. */
43      private final SwitchableGTUColorer switchableGTUColorer;
44  
45      /**
46       * Add a ColorControlPanel to an AnimationPanel. Initially the ColorControlPanel will have no items. Items are added with
47       * the <code>addItem</code> method. The first item added automatically becomes the active one.
48       * @param gtuColorer SwitchableGTUColorer; the switchable GTU colorer that will be controlled by this ColorControlPanel
49       */
50      public ColorControlPanel(final GTUColorer gtuColorer)
51      {
52          this.setLayout(new FlowLayout(FlowLayout.LEFT));
53          this.legendPanel = new JPanel(new FlowLayout());
54  
55          if (gtuColorer instanceof SwitchableGTUColorer)
56          {
57              this.switchableGTUColorer = (SwitchableGTUColorer) gtuColorer;
58              class AppearanceControlComboBox<T> extends JComboBox<T> implements AppearanceControl
59              {
60                  /** */
61                  private static final long serialVersionUID = 1L;
62  
63                  /** {@inheritDoc} */
64                  @Override
65                  public boolean isFont()
66                  {
67                      return true;
68                  }
69  
70                  /** {@inheritDoc} */
71                  @Override
72                  public String toString()
73                  {
74                      return "AppearanceControlComboBox []";
75                  }
76              }
77              this.comboBoxGTUColor = new AppearanceControlComboBox<>();
78              this.add(this.comboBoxGTUColor);
79              this.comboBoxGTUColor.addActionListener(this);
80  
81              for (GTUColorer colorer : ((SwitchableGTUColorer) gtuColorer).getColorers())
82              {
83                  addItem(colorer);
84              }
85          }
86          else
87          {
88              this.gtuColorer = gtuColorer;
89              this.switchableGTUColorer = null;
90              rebuildLegend();
91          }
92  
93          this.add(this.legendPanel);
94      }
95  
96      /**
97       * Add one item to this ColorControlPanel. The <cite>getName</cite> method of the
98       * @param colorer GTUColorer; the GTUColorer that will be added
99       */
100     public final void addItem(final GTUColorer colorer)
101     {
102         this.comboBoxGTUColor.addItem(colorer);
103         // The first item added automatically becomes the current one and triggers a call to actionPerformed.
104     }
105 
106     /** {@inheritDoc} */
107     @Override
108     public final void actionPerformed(final ActionEvent e)
109     {
110         GTUColorer newColorer = (GTUColorer) this.comboBoxGTUColor.getSelectedItem();
111         if (null != newColorer)
112         {
113             this.gtuColorer = newColorer;
114             rebuildLegend();
115 
116             if (this.switchableGTUColorer != null)
117             {
118                 this.switchableGTUColorer.setGTUColorer(this.comboBoxGTUColor.getSelectedIndex());
119             }
120         }
121     }
122 
123     /**
124      * Build or rebuild the legend on the screen.
125      */
126     private void rebuildLegend()
127     {
128         this.legendPanel.removeAll();
129         for (GTUColorer.LegendEntry legendEntry : this.gtuColorer.getLegend())
130         {
131             JPanel panel = new JPanel(new BorderLayout());
132             class ColorBox extends JLabel implements AppearanceControl
133             {
134                 /** */
135                 private static final long serialVersionUID = 20180206L;
136 
137                 /**
138                  * Constructor.
139                  */
140                 public ColorBox()
141                 {
142                     super("     ");
143                 }
144 
145                 /** {@inheritDoc} */
146                 @Override
147                 public String toString()
148                 {
149                     return "ColorBox []";
150                 }
151             }
152             ColorBox colorBox = new ColorBox();
153             colorBox.setOpaque(true); // By default, the label is transparent
154             colorBox.setBackground(legendEntry.getColor());
155             Border border = LineBorder.createBlackLineBorder();
156             colorBox.setBorder(border);
157             panel.add(colorBox, BorderLayout.LINE_START);
158             JLabel name = new JLabel(" " + legendEntry.getName().trim());
159             panel.add(name, BorderLayout.CENTER);
160             name.setOpaque(true);
161             name.setForeground(getForeground());
162             name.setBackground(getBackground());
163             panel.setToolTipText(legendEntry.getDescription());
164             this.legendPanel.add(panel);
165         }
166         this.legendPanel.revalidate();
167 
168         Container parentPanel = this.getParent();
169 
170         if (parentPanel != null)
171         {
172             parentPanel.repaint();
173         }
174     }
175 
176     /** {@inheritDoc} */
177     @Override
178     public final String toString()
179     {
180         return "ColorControlPanel [gtuColorer=" + this.gtuColorer + "]";
181     }
182 
183 }