forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponent.java
More file actions
316 lines (248 loc) · 6.21 KB
/
Copy pathComponent.java
File metadata and controls
316 lines (248 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package javaforce.ui;
/** Component - base class for all UI elements.
*
* @author pquiring
*/
import javaforce.*;
import javaforce.ui.theme.*;
public class Component implements KeyEvents, MouseEvents {
protected Color foreClr;
protected Color backClr;
protected Color disabledClr;
protected Color selectedClr;
protected Point pos = new Point();
protected Dimension size = new Dimension();
protected Component parent;
protected int layer = 0;
protected int borderStyle = 0;
protected boolean enabled = true;
protected boolean editable = true;
protected boolean focused = false;
protected boolean focusable = false;
protected boolean visible = true;
/** Component consumes mouse up/down events. */
protected boolean consumer = true;
public static final Dimension zero = new Dimension();
public static boolean debug = false;
public Component() {
Theme theme = Theme.getTheme();
setForeColor(theme.getForeColor());
setBackColor(theme.getBackColor());
setDisabledColor(theme.getDisabledColor());
setSelectedColor(theme.getSelectedColor());
}
/** Loads a complete form from a XML file (*.ui). */
public static Component load(String fn) {
return null;
}
/** Saves a component (and all children) to an XML file (*.ui). */
public void save(String fn) {
}
public boolean isContainer() {
return false;
}
protected boolean isConsumer() {
return consumer;
}
protected void setParent(Component parent) {
this.parent = parent;
}
protected void setConsumer(boolean consumes) {
consumer = consumes;
}
public Point getPosition() {
return pos;
}
public int getX() {
return pos.x;
}
public int getY() {
return pos.y;
}
public void setPosition(Point pt) {
setPosition(pt.x, pt.y);
}
public void setPosition(int x, int y) {
pos.x = x;
pos.y = y;
}
public Dimension getSize() {
return size;
}
public int getWidth() {return size.width;}
public int getHeight() {return size.height;}
public void setSize(Dimension dim) {
//NOTE : setSize(int,int) may be overrided
setSize(dim.width, dim.height);
}
public void setSize(int width, int height) {
size.width = width;
size.height = height;
}
public Dimension getMinSize() {
return size;
}
public int getMinWidth() {
return getMinSize().width;
}
public int getMinHeight() {
return getMinSize().height;
}
public int getLayer() {return layer;}
/** Sets a component layer. Default = 0. Currently the only other supported layer is 1 for popup menus and lists. */
public void setLayer(int layer) {
this.layer = layer;
}
public boolean isInside(Point pt) {
int x1 = pos.x;
int y1 = pos.y;
int x2 = x1 + size.width - 1;
int y2 = y1 + size.height - 1;
return (pt.x >= x1 && pt.x <= x2 && pt.y >= y1 && pt.y <= y2);
}
public void render(Image image) {
if (!isVisible()) return;
image.fill(pos.x, pos.y, size.width, size.height, getBackColor().getColor());
if (borderStyle != LineStyle.NONE) {
image.setLineStyle(borderStyle);
image.setForeColor(foreClr);
image.drawBox(pos.x, pos.y, size.width, size.height);
image.setLineStyle(LineStyle.SOLID);
}
}
public void renderLayer(int layer, Image image) {
if (getLayer() == layer) render(image);
}
public void layout(LayoutMetrics metrics) {
setPosition(metrics.pos);
setSize(metrics.size);
}
public void setFocusable(boolean state) {
if (focusable == state) return; //no change
focusable = state;
/*/
if (focusable) {
//add to focus list
getTopContainer().addFocusable(this);
} else {
//remove from focus list
getTopContainer().removeFocusable(this);
}
/*/
}
public boolean isVisible() {
return visible;
}
public void setVisible(boolean state) {
visible = state;
}
public Container getTopContainer() {
Component top = parent;
while (top.parent != null) {
if (top.parent == top) {
JFLog.logTrace("UI Loop detected");
}
top = top.parent;
}
return (Container)top;
}
public void onFocus() {
focused = true;
}
public void onBlur() {
focused = false;
}
public void setFocus() {
getTopContainer().setFocus(this);
}
public boolean isFocused() {
return focused;
}
public boolean isFocusable() {
return focusable;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean state) {
enabled = state;
}
public boolean isEditable() {
return editable;
}
public void setEditable(boolean state) {
editable = state;
}
public Color getForeColor() {
return foreClr;
}
public Color getBackColor() {
return backClr;
}
public Color getDisabledColor() {
return disabledClr;
}
public Color getSelectedColor() {
return selectedClr;
}
public void setForeColor(Color clr) {
foreClr = clr;
}
public void setBackColor(Color clr) {
backClr = clr;
}
public void setDisabledColor(Color clr) {
disabledClr = clr;
}
public void setSelectedColor(Color clr) {
selectedClr = clr;
}
public int getBorderStyle() {
return borderStyle;
}
public void setBorderStyle(int lineStyle) {
borderStyle = lineStyle;
}
public int getMouseX() {
return mousePos.x;
}
public int getMouseY() {
return mousePos.y;
}
public Point getMousePosition() {
return mousePos;
}
public boolean getKeyState(int vk) {
if (vk < 0 || vk >= keyState.length) return false;
return keyState[vk];
}
private Point mousePos = new Point();
private static boolean[] keyState = new boolean[512];
//keyboard input
public void keyTyped(char ch) {
}
public void keyPressed(int key) {
if (key >= 0 && key < keyState.length) {
keyState[key] = true;
}
}
public void keyReleased(int key) {
if (key >= 0 && key < keyState.length) {
keyState[key] = false;
}
}
//mouse input
public void mouseMove(int x, int y) {
mousePos.x = x;
mousePos.y = y;
}
public void mouseDown(int button) {
if (isFocusable()) {
setFocus();
}
}
public void mouseUp(int button) {
}
public void mouseScroll(int dx, int dy) {
}
}