1
2
3
4
5
6
7
8
9
10
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
34
35
36
37 public class DropDownBoxComponent extends JPanel implements ActionListener {
38
39
40 protected JComboBox comboBox;
41
42
43 protected JButton goButton;
44
45
46 protected String current;
47
48
49 protected List<ActionListener> listeners = new ArrayList<ActionListener>();
50
51
52 public static String GO_ICON_RESOURCE_NAME = "/org/abstracthorizon/aequo/action/Go.png";
53
54
55
56
57
58
59 public DropDownBoxComponent(String current, List<String> elements) {
60 this(null, current, elements);
61 }
62
63
64
65
66
67
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
120
121
122 public String getSelected() {
123 return (String)comboBox.getSelectedItem();
124 }
125
126
127
128
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
140
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
152
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 }