Source code: jcurses/widgets/Widget.java
1 /**
2 * Dies ist die Root-Klasse für alle Widgets.
3 */
4
5
6 package jcurses.widgets;
7
8 import java.util.List;
9
10 import jcurses.system.CharColor;
11 import jcurses.system.InputChar;
12 import jcurses.util.Protocol;
13 import jcurses.util.Rectangle;
14
15 import java.util.Vector;
16
17 /**
18 * This class is superclass for all jcurses widgets.
19 * For implementing a ne widget you must derive it.
20 *
21 * An jcurses widget is already used within a window.
22 * Its task ist to help it's <code>WidgetContainer</code>
23 * to layout itself, giving needed informations, to paint itself
24 * and to handle input. Handling input is needed only, if the widget
25 * has is an input widget ( examples are text input widget, list widget)
26 * and has currently focus, that is is selected by user to handle input.
27 * This selectig ocurrs by typing a special key (currenty 'tab') to switch
28 * between input widgets.
29 *
30 * All widgets are ordered in a hierarchy. An widget is already has a container,
31 * if it isn't the root pane of a window.
32 */
33 public abstract class Widget {
34
35 WidgetContainer _parent = null;
36 Window _window = null;
37
38
39 /**
40 * @return widget's container
41 */
42
43 protected WidgetContainer getParent() {
44 return _parent;
45 }
46
47 /**
48 * Sets widget's container. Is called by framework, schouldn't be called writing applications
49 *
50 * @param parent new container
51 */
52 protected void setParent(WidgetContainer parent) {
53 _parent=parent;
54 }
55
56
57
58 /**
59 * /**
60 * Sets widget's window. Is called by framework, schouldn't be called writing applications
61 *
62 * @param window widget's window
63 */
64 protected void setWindow(Window window) {
65 _window = window;
66 }
67
68
69 /**
70 * @return widget's window
71 */
72 protected Window getWindow() {
73 if (getParent() == null) {
74 return _window;
75 } else {
76 return getParent().getWindow();
77 }
78 }
79
80
81
82 private int _x=0;
83 private int _y=0;
84 private Rectangle _size;
85
86 /**
87 * Sets the x coordinate within the container. Is called by framework, schouldn't be called writing applications
88 *
89 * @param x x coordinate within the container
90 */
91 protected void setX(int x) {
92 _x=x;
93 }
94
95
96 /**
97 * @return x coordinate within the container
98 */
99 protected int getX() {
100 return _x;
101 }
102
103
104 /**
105 * @return x coordinate on the screen
106 */
107 protected int getAbsoluteX() {
108 int result = 0;
109 if (getParent() == null) {
110 result = _x;
111 } else {
112 result = _x + getParent().getAbsoluteX();
113 if (getParent().getChildsRectangle()!=null) {
114 result = result + getParent().getChildsRectangle().getX();
115 }
116 }
117
118 return result;
119 }
120
121
122 /**
123 * Sets the y coordinate within the container. Is called by framework, schouldn't be called writing applications
124 *
125 * @param y y coordinate within the container
126 */
127 protected void setY(int y) {
128 _y = y;
129 }
130
131
132 /**
133 * @return y coordinate within the container
134 */
135 protected int getY() {
136 return _y;
137 }
138
139
140 /**
141 * @return y coordinate on the screen
142 */
143 protected int getAbsoluteY() {
144 int result = 0;
145 if (getParent() == null) {
146 result = _y;
147 } else {
148 result = _y + getParent().getAbsoluteY();
149 if (getParent().getChildsRectangle()!=null) {
150 result = result + getParent().getChildsRectangle().getY();
151 }
152 }
153
154 return result;
155 }
156
157
158 /**
159 * Returns the rectangle on the screen, that contains this widget
160 *
161 * @return the rectangle on the screen, that contains this widget
162 */
163 protected Rectangle getRectangle() {
164 Rectangle size = (Rectangle)getSize().clone();
165 size.setLocation(getAbsoluteX(),getAbsoluteY());
166 return size;
167 }
168
169
170
171 /**
172 * @return widget's size
173 */
174
175 protected Rectangle getSize() {
176 return (Rectangle)_size.clone();
177 }
178
179
180 /**
181 * Sets the size of the widget.
182 *
183 * @param size new size
184 */
185 protected void setSize(Rectangle size) {
186 _size = size;
187
188 }
189
190
191 /**
192 * This method gives the widget container the infomation about the
193 * preferred size of this widget. Must be implemented by derived classes.
194 */
195
196 protected abstract Rectangle getPreferredSize();
197
198
199 /**
200 * The method is called by the framework to paint the widget
201 */
202
203 protected void paint() {
204 if (isVisible()) {
205 doPaint();
206 }
207 }
208
209
210
211 /**
212 * This method paints the widget. Will be called by <code>paint()<code>,
213 * only if the widget is visible. Must be implemented be derived classes.
214 */
215 protected abstract void doPaint();
216
217
218
219 /**
220 * The method is called by the framework to repaint the widget
221 */
222 protected void repaint() {
223 if (isVisible()) {
224 doRepaint();
225 }
226 }
227
228 /**
229 * This method repaints the widget. Will be called by <code>paint()<code>,
230 * only if the widget is visible. Must be implemented be derived classes.
231 */
232 protected abstract void doRepaint();
233
234
235
236 /**
237 * The method declares, whether the widget can handle input ( get focus ), that is,
238 * whether this is an input widget.
239 *
240 * @return true, if the widget can handle input, in other case false
241 */
242
243 protected boolean isFocusable() {
244 return false;
245 }
246
247
248 private boolean _focus = false;
249
250 /**
251 * @return true, if the widget has currenty focus,that is handles input, in othe case false
252 */
253 public boolean hasFocus() {
254 return _focus;
255 }
256
257 /**
258 * The method switches focus to this widget, if it is focusable at all.
259 */
260
261 public void getFocus() {
262 if (getWindow()!=null) {
263 getWindow().changeFocus(this);
264 }
265 }
266
267 /**
268 * The method is called by framework if focus is switched,that is, either
269 * the widget has get or lost focus.
270 *
271 * @param value true, if the widget has get focus, in other case false
272 */
273
274 void setFocus(boolean value) {
275 _focus = value;
276 if (_focus) {
277 focus();
278 } else {
279 unfocus();
280 }
281 }
282
283
284 /**
285 * The method is called bei <code>setFocus</code> to tell widget, thas it has get focus.
286 * This method schold be overrided bei derived class to react getting focus, for examlple
287 * to repaint widget gettig focus.
288 */
289
290 protected void focus() {
291
292 }
293
294
295 /**
296 * The method is called bei <code>setFocus</code> to tell widget, thas it has lost focus.
297 * This method schold be overrided bei derived class to react losing focus, for examlple
298 * to repaint widget losing focus.
299 */
300 protected void unfocus() {
301 }
302
303 /**
304 * The method is called by framework to let the widget handle an input char.
305 * Schould be overrided be derived classes, if these can handle input.
306 *
307 * @return true, if the widget has handled the char, false in other case
308 */
309
310 protected boolean handleInput(InputChar inputChar) {
311 return false;
312 }
313
314 /**
315 * This method returns a list of short cut chars, that the widget want to handle.
316 * If a char from the list is typed by user, it will be handled always my this widget not
317 * bei the widget currenty having focus, except the having focus widget handles ALL chars
318 * and tells this throuth the method <code>handleAllPrintableChars</code>.
319 * To enable shortcuts for a new widget, you must override this method.
320 */
321
322 protected Vector getShortCutsList() {
323 return null;
324 }
325
326
327 /**
328 * Methoden, die Sichtbarkeit regeln.
329 * Ein widget ist dann sichtbar wenn er UND sein Parent sichtbar sind
330 */
331
332 private boolean _visible = true;
333
334 /**
335 * The method manages visibility
336 *
337 * @param visible true, if the widget is to make visible, false otherwise.
338 */
339 public void setVisible(boolean visible) {
340 _visible = visible;
341 }
342
343 /**
344 * The method returns true, if the visibility flag of the widget is true.
345 * This doesn't mean that the widget ist currently visible, because the parent whole
346 * window can be unvisible, use the method <code>isVisible</code> to query the visisbility
347 * @return true, if the visibility flag is set, false otherwise
348 */
349
350 public boolean getVisible() {
351 return _visible;
352 }
353
354 /**
355 * return true, if the widget is currently visible, false otherwise.
356 *
357 */
358 public boolean isVisible() {
359 Widget parent = getParent();
360 if ((parent!=null) && (!(parent.isVisible()))) {
361 return false;
362 }
363 Window w = getWindow();
364 boolean result = ((_visible) && (w!=null) && (w.isVisible()));
365 return result;
366 }
367
368
369
370
371 private CharColor _colors = null;
372
373 private static CharColor __defaultColors = new CharColor(CharColor.WHITE, CharColor.WHITE);
374
375 /**
376 * @return default colors for this widget. What this mentiones in a concret case, is dependent
377 * on the derived class.
378 */
379 protected CharColor getDefaultColors() {
380 return __defaultColors;
381 }
382
383
384 /**
385 * Set colors of the widget
386 *
387 * @param colors new colors
388 *
389 */
390 public void setColors(CharColor colors) {
391 _colors = colors;
392 }
393
394
395 /**
396 * @return colors of the widget
397 */
398 public CharColor getColors() {
399 return (_colors == null)?getDefaultColors():_colors;
400 }
401
402
403
404
405
406
407
408 }