1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components.wheelswitch;
21
22 import java.awt.Color;
23 import java.awt.Dimension;
24 import java.awt.event.MouseListener;
25
26 import javax.swing.JComponent;
27 import javax.swing.event.EventListenerList;
28
29 import com.cosylab.gui.components.ArrowButton;
30 import com.cosylab.gui.components.util.ColorHelper;
31
32
33
34
35
36
37
38
39 public class UpDownButton extends JComponent {
40
41 private static final long serialVersionUID = 1L;
42
43 private ArrowButton down;
44
45 private ArrowButton up;
46
47 private Color foregroundColor = Color.BLACK;
48
49 private int outOfBounds = 0;
50
51
52
53
54 public UpDownButton() {
55 super();
56 listenerList = new EventListenerList();
57 setLayout(new UpDownLayout());
58 add(getUpButton());
59 add(getDownButton());
60 }
61
62
63
64
65
66
67
68
69 public Dimension getPreferredSize() {
70 return new Dimension(12,24);
71 }
72
73
74
75
76
77
78
79
80 public Dimension getMinimumSize() {
81 return new Dimension(12,24);
82 }
83
84
85
86 public void setEnabled(boolean arg0) {
87 super.setEnabled(arg0);
88 getDownButton().setEnabled(arg0);
89 getUpButton().setEnabled(arg0);
90 }
91
92
93
94
95
96
97
98 public void setOutOfBounds(int i) {
99 outOfBounds = i;
100 if (i>0) {
101 getUpButton().setEnabled(false);
102 if (isEnabled())getDownButton().setEnabled(true);
103 getUpButton().setForeground(foregroundColor);
104 getDownButton().setForeground(ColorHelper.getAlarmOutline());
105 } else if (i<0) {
106 if (isEnabled())getUpButton().setEnabled(true);
107 getDownButton().setEnabled(false);
108 getUpButton().setForeground(ColorHelper.getAlarmOutline());
109 getDownButton().setForeground(foregroundColor);
110 } else {
111 if (isEnabled()) {
112 getUpButton().setEnabled(true);
113 getDownButton().setEnabled(true);
114 }
115 getUpButton().setForeground(foregroundColor);
116 getDownButton().setForeground(foregroundColor);
117 }
118 }
119
120
121
122
123
124
125 public ArrowButton getUpButton() {
126 if (up==null) {
127 up = new ArrowButton(ArrowButton.UP);
128 up.setFocusable(false);
129 up.setBackground(ColorHelper.getCosyControl());
130 }
131
132 return up;
133 }
134
135
136
137
138
139
140 public ArrowButton getDownButton() {
141 if (down==null) {
142 down = new ArrowButton(ArrowButton.DOWN);
143 down.setFocusable(false);
144 down.setBackground(ColorHelper.getCosyControl());
145 }
146
147 return down;
148 }
149
150
151
152
153
154 @Override
155 public void setBackground(Color bg) {
156 super.setBackground(bg);
157 getUpButton().setBackground(bg);
158 getDownButton().setBackground(bg);
159 }
160
161
162
163
164
165 @Override
166 public void setForeground(Color fg) {
167 super.setForeground(fg);
168 foregroundColor = fg;
169 setOutOfBounds(outOfBounds);
170 }
171
172 @Override
173 public synchronized void addMouseListener(MouseListener l) {
174 super.addMouseListener(l);
175 getUpButton().addMouseListener(l);
176 getDownButton().addMouseListener(l);
177 }
178
179 }
180