1 package org.catacomb.druid.swing;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.Graphics;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.MouseEvent;
9 import java.awt.event.MouseListener;
10 import java.util.ArrayList;
11
12 import javax.swing.Icon;
13 import javax.swing.JButton;
14 import javax.swing.JComponent;
15 import javax.swing.JPopupMenu;
16 import javax.swing.event.ChangeEvent;
17
18 import org.catacomb.druid.event.LabelActor;
19 import org.catacomb.interlish.interact.DComponent;
20 import org.catacomb.interlish.structure.SpecialStrings;
21
22
23
24 public class DValueHistory extends JButton implements DComponent,
25 ActionListener, LabelActor, Icon, MouseListener {
26
27 static final long serialVersionUID = 1001;
28
29 LabelActor lact;
30
31 JPopupMenu menu;
32
33 ArrayList<String> values = new ArrayList<String>();
34
35 RolloverEffect rollover;
36
37
38
39 public DValueHistory() {
40 this(new String[0]);
41 }
42
43 public DValueHistory(String[] vals) {
44 super("");
45 setIcon(this);
46
47 rollover = new RolloverEffect(this);
48 addMouseListener(rollover);
49
50
51
52 addActionListener(this);
53 addMouseListener(this);
54 setFocusPainted(false);
55
56
57 setOptions(vals);
58 }
59
60
61
62 public void setBg(Color c) {
63 setBackground(c);
64 menu.setBackground(c);
65 rollover.setBg(c);
66 }
67
68
69
70
71 public void checkContains(String s) {
72 if (!values.contains(s)) {
73 values.add(0, s);
74 checkSize();
75 updateOptions();
76 }
77 }
78
79
80
81 public void setOptions(String[] vals) {
82 for (String s : vals) {
83 if (values.contains(s)) {
84
85 } else {
86 values.add(0, s);
87 }
88 }
89 checkSize();
90 updateOptions();
91 }
92
93
94 private void checkSize() {
95 if (values.size() > 12) {
96 for (int i = values.size()-1; i >= 12; i--) {
97 values.remove(i);
98 }
99 }
100 }
101
102 public void updateOptions() {
103 menu = new JPopupMenu();
104
105 DMenuItem dminone = new DMenuItem(SpecialStrings.NONE_STRING);
106 dminone.setLabelActor(this);
107 menu.add(dminone);
108 menu.addSeparator();
109
110 for (String s : values) {
111 DMenuItem dmi = new DMenuItem(s);
112 dmi.setLabelActor(this);
113 menu.add(dmi);
114
115 }
116
117 }
118
119
120 public void labelAction(String s, boolean b) {
121 deliverAction(s, true);
122 }
123
124
125
126 public void actionPerformed(ActionEvent aev) {
127
128
129 }
130
131 public void stateChanged(ChangeEvent cev) {
132
133 }
134
135
136 public void showMenu() {
137 menu.show(this, 0, 18);
138 }
139
140
141
142 public void setLabelActor(LabelActor bl) {
143 lact = bl;
144 }
145
146
147
148 public void deliverAction(String s, boolean b) {
149 if (lact != null) {
150 lact.labelAction(s, b);
151 }
152 }
153
154
155
156
157 public void paintIcon(Component c, Graphics g, int x, int y) {
158 JComponent component = (JComponent)c;
159 int iconWidth = getIconWidth();
160
161 g.translate(x, y);
162
163 g.setColor(component.isEnabled() ? Color.gray : Color.blue);
164
165 g.drawLine(2, 0, iconWidth - 1, 0);
166 g.drawLine(3, 1, 1 + (iconWidth - 3), 1);
167 g.drawLine(5, 3, 3 + (iconWidth - 7), 3);
168 g.drawLine(6, 4, 4 + (iconWidth - 9), 4);
169
170 g.translate(-x, -y);
171 }
172
173
174 public int getIconWidth() {
175 return 12;
176 }
177
178
179 public int getIconHeight() {
180 return 5;
181 }
182
183
184
185 public void mouseClicked(MouseEvent e) {
186 }
187
188 public void mouseReleased(MouseEvent e) {
189 }
190
191 public void mouseEntered(MouseEvent e) {
192 }
193
194 public void mouseExited(MouseEvent e) {
195 }
196
197
198 public void mousePressed(MouseEvent e) {
199 showMenu();
200 }
201
202
203
204 public void setTooltip(String s) {
205
206
207 }
208
209
210 }