View Javadoc

1   /*
2    * Copyright (c) 2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.aequo.gui;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Component;
17  import java.awt.Dimension;
18  import java.awt.Insets;
19  import java.awt.event.ActionEvent;
20  import java.awt.event.ActionListener;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.swing.Icon;
25  import javax.swing.ImageIcon;
26  import javax.swing.JButton;
27  import javax.swing.JComboBox;
28  import javax.swing.JComponent;
29  import javax.swing.JLabel;
30  import javax.swing.JPanel;
31  
32  /**
33   * Input component for filters
34   *
35   * @author Daniel Sendula
36   */
37  public class DropDownBoxComponent extends JPanel implements ActionListener {
38  
39      /** Combo box */
40      protected JComboBox comboBox;
41      
42      /** Go button */
43      protected JButton goButton;
44      
45      /** Current filter */
46      protected String current;
47      
48      /** Action listeners */
49      protected List<ActionListener> listeners = new ArrayList<ActionListener>();
50      
51      /** GO icon resource name */
52      public static String GO_ICON_RESOURCE_NAME = "/org/abstracthorizon/aequo/action/Go.png";
53      
54      /**
55       * Constructor
56       * @param context global context
57       * @param current current filter
58       */
59      public DropDownBoxComponent(String current, List<String> elements) {
60          this(null, current, elements);
61      }
62  
63      /**
64       * Constructor
65       * @param label label to be presented infornt of the drop down box. It can be {@link Component}, {@link String} or {@link Icon}.
66       * @param context global context
67       * @param current current filter
68       */
69      public DropDownBoxComponent(Object label, String current, List<String> elements) {
70          super(new BorderLayout());
71          setOpaque(false);
72  
73          this.current = current;
74          
75          if (label != null) {
76              Component comp = null;
77              if (label instanceof Component) {
78                  comp = (Component)label;
79              } else if (label instanceof String) {
80                  comp = new JLabel((String)label);
81              } else if (label instanceof Icon) {
82                  comp = new JLabel((Icon)label);
83              }
84              if (comp != null) {
85                  if (comp instanceof JComponent) {
86                      ((JComponent)comp).setOpaque(false);
87                  }
88                  add(comp, BorderLayout.WEST);
89              }
90          }
91          
92          int index = 0;
93          
94          if (!elements.contains(current)) {
95              elements.add(0, current);
96          } else {
97              index = elements.indexOf(current);
98          }
99          
100         String[] values = new String[elements.size()];
101         for (int i = 0; i < values.length; i++) {
102             values[i] = elements.get(i);
103         }
104         
105         comboBox = new JComboBox(values);
106         comboBox.setEditable(true);
107         comboBox.setSelectedIndex(index);
108         add(comboBox, BorderLayout.CENTER);
109         setMaximumSize(new Dimension(300, getPreferredSize().height));
110         setMinimumSize(new Dimension(10, 10));
111         comboBox.setMaximumSize(new Dimension(300, comboBox.getPreferredSize().height));
112         
113         goButton = new JButton(new ImageIcon(getClass().getResource(GO_ICON_RESOURCE_NAME)));
114         goButton.setMargin(new Insets(1, 1, 1, 1));
115         add(goButton, BorderLayout.EAST);
116     }
117 
118     /**
119      * Returns selected index as a string
120      * @return selected index as a string
121      */
122     public String getSelected() {
123         return (String)comboBox.getSelectedItem();
124     }
125     
126     /**
127      * Adds action listeners to the combo box
128      * @param listener action listeners to the combo box
129      */
130     public void addActionListener(ActionListener listener) {
131         listeners.add(listener);
132         if (listeners.size() == 1) {
133             comboBox.addActionListener(this);
134             goButton.addActionListener(this);
135         }
136     }
137     
138     /**
139      * Removes action listeners to the combo box
140      * @param listener action listeners to the combo box
141      */
142     public void removeActionListener(ActionListener listener) {
143         listeners.remove(listener);
144         if (listeners.size() == 0) {
145             comboBox.removeActionListener(this);
146             goButton.removeActionListener(this);
147         }
148     }
149 
150     /**
151      * Enter is pressed or button selected
152      * @param e event
153      */
154     public void actionPerformed(ActionEvent e) {
155         ActionEvent event = new ActionEvent(this, e.getID(), e.getActionCommand(), e.getWhen(), e.getModifiers());
156         for (ActionListener listener : listeners) {
157             listener.actionPerformed(event);
158         }
159     }
160     
161 }