-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathScreen.java
More file actions
501 lines (433 loc) · 10.9 KB
/
Screen.java
File metadata and controls
501 lines (433 loc) · 10.9 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package org.lcdproc.lcdjava;
import java.util.HashMap;
import java.util.Map;
/**
A Screen that can contain Widgets.
* <p>Copyright (c) 2004-2005 Darren Greaves.
* @author Darren Greaves
* @version $Id: Screen.java,v 1.3 2008-07-06 15:38:34 boncey Exp $
*/
public class Screen
{
/**
* Version details.
*/
public static final String CVSID =
"$Id: Screen.java,v 1.3 2008-07-06 15:38:34 boncey Exp $";
/**
* Set heartbeat to 'Heart icon'.
*/
public static final String HEARTBEAT_ON = "on";
/**
* Set heartbeat to 'Heart icon'.
*/
public static final String HEARTBEAT_HEART = "heart";
/**
* Set heartbeat to 'User's choice'.
*/
public static final String HEARTBEAT_NORMAL = "normal";
/**
* Set heartbeat to 'User's choice'.
*/
public static final String HEARTBEAT_DEFAULT = "default";
/**
* Set heartbeat to 'None'.
*/
public static final String HEARTBEAT_OFF = "off";
/**
* Set heartbeat to 'None'.
*/
public static final String HEARTBEAT_NONE = "none";
/**
* Set heartbeat to 'Rotating slash'.
*/
public static final String HEARTBEAT_SLASH = "slash";
/**
* The LCD that owns this Screen.
*/
private LCD _lcd;
/**
* The id of the Screen.
*/
private int _id;
/**
* The name of the Screen.
*/
private String _name;
/**
* The priority of the Screen.
*/
private String _priority;
/**
* The duration of the Screen.
*/
private int _duration;
/**
* The width of the Screen.
*/
private int _width;
/**
* The height of the Screen.
*/
private int _height;
/**
* A Map of Widgets added to this Screen.
*/
private Map<Integer, Widget> _widgets;
/**
* The count of widgets we have created.
*/
private int _widgetCounter;
/**
* Whether or not the server is listening to this screen.
*/
private boolean _listening;
/**
* The heartbeat type.
*/
private String _heartbeat = HEARTBEAT_OFF;
/**
* The priority to set a Screen to "hidden" level.
*/
public static final String PRIORITY_HIDDEN = "hidden";
/**
* The priority to set a Screen to "background" level.
*/
public static final String PRIORITY_BACKGROUND = "background";
/**
* The priority to set a Screen to "info" level.
*/
public static final String PRIORITY_INFO = "info";
/**
* The priority to set a Screen to "foreground" level.
*/
public static final String PRIORITY_FOREGROUND = "foreground";
/**
* The priority to set a Screen to "alert" level.
*/
public static final String PRIORITY_ALERT = "alert";
/**
* Protected constructor.
* <p>There is no need to call this contructor, call
* {@link LCD#constructScreen(String)} instead.
* @param lcd the LCD server object.
* @param id the Screen id.
* @param name the Screen name.
*/
protected Screen(LCD lcd, int id, String name)
{
_lcd = lcd;
_id = id;
_name = name;
_widgets = new HashMap<>();
}
/**
* Protected constructor.
* <p>There is no need to call this contructor, call
* {@link LCD#constructScreen(String)} instead.
* @param lcd the LCD server object.
* @param id the Screen id.
*/
protected Screen(LCD lcd, int id)
{
this(lcd, id, null);
}
/**
* Get the screen id.
* @return the screen id.
*/
public int getId()
{
return _id;
}
/**
* Set the name.
* @param name the name.
*/
public void setName(String name)
{
_name = name;
update();
}
/**
* Get the name.
* @return the name.
*/
public String getName()
{
return _name;
}
/**
* Set the priority.
* @param priority the priority.
*/
public void setPriority(String priority)
{
_priority = priority;
update();
}
/**
* Get the priority.
* @return the priority.
*/
public String getPriority()
{
return _priority;
}
/**
* Set the duration.
* @param duration the duration.
*/
public void setDuration(int duration)
{
_duration = duration;
update();
}
/**
* Get the duration.
* @return the duration.
*/
public int getDuration()
{
return _duration;
}
/**
* Set the width.
* @param width the width.
*/
public void setWidth(int width)
{
_width = width;
update();
}
/**
* Get the width.
* @return the width.
*/
public int getWidth()
{
return _width;
}
/**
* Set the height.
* @param height the height.
*/
public void setHeight(int height)
{
_height = height;
update();
}
/**
* Get the height.
* @return the height.
*/
public int getHeight()
{
return _height;
}
/**
* Set the heartbeat.
* @param heartbeat the heartbeat.
*/
public void setHeartbeat(String heartbeat)
{
_heartbeat = heartbeat;
update();
}
/**
* Get the heartbeat.
* @return the heartbeat.
*/
public String getHeartbeat()
{
return _heartbeat;
}
/**
* Set if the server is listening to us.
* @param listening <code>true</code> if the server is listening to us,
* <code>false</code> if not.
*/
public void setListening(boolean listening)
{
_listening = listening;
}
/**
* Is the server listening to us?
* @return <code>true</code> if the server is listening to us,
* <code>false</code> if not.
*/
public boolean isListening()
{
return _listening;
}
/**
* Update this Screen's state to the server.
*/
private void update()
{
_lcd.updateScreen(this);
}
/**
* Activate this Screen.
*/
public void activate()
{
_lcd.addScreen(this);
}
/**
* Remove this Screen.
*/
public void remove()
{
_lcd.removeScreen(this);
}
/**
* Get this Screen's data in a format for writing to the LCDd server.
* @return this Screen's state.
*/
public String getData()
{
return _id +
" -priority " + _priority +
" -name \"" + _name + "\"" +
" -duration " + _duration +
" -wid " + _width +
" -hgt " + _height +
" -heartbeat " + _heartbeat;
}
/**
* Return a String representing this object.
* @return a String representing this object.
*/
public String toString()
{
return " priority " + _priority +
" name \"" + _name + "\"" +
" duration " + _duration +
" wid " + _width +
" hgt " + _height +
" heartbeat " + _heartbeat;
}
/**
* Add a Widget to this Screen.
* @param widget the Widget to add.
* @return whether or not the widget was added successfully.
*/
protected synchronized boolean addWidget(Widget widget)
{
boolean success = false;
Integer widgetId = new Integer(widget.getId());
if (!_widgets.containsKey(widgetId))
{
_widgets.put(widgetId, widget);
_lcd.write(LCD.CMD_WIDGET_ADD + _id + " " + widgetId +
" " + widget.getType());
success = updateWidget(widget);
}
return success;
}
/**
* Update a Widget to this Screen.
* @param widget the Widget to update.
* @return whether or not the widget was updated successfully.
*/
protected synchronized boolean updateWidget(Widget widget)
{
boolean success = false;
Integer widgetId = new Integer(widget.getId());
if (_widgets.containsKey(widgetId))
{
_lcd.write(LCD.CMD_WIDGET_SET + _id + " " + widgetId +
" " + widget.getData());
success = true;
}
return success;
}
/**
* Delete a Widget from this Screen.
* @param widget the Widget to delete.
*/
protected synchronized void removeWidget(Widget widget)
{
Integer widgetId = new Integer(widget.getId());
if (_widgets.containsKey(widgetId))
{
_lcd.write(LCD.CMD_WIDGET_DEL + _id + " " + widgetId);
_widgets.remove(widgetId);
}
}
/**
* Create a Widget for this screen.
* @param type the Widget type, see {@link Widget} for a list of types.
* @return the created Widget.
* @throws LCDException if the Widget type is not recognised.
*/
public synchronized Widget constructWidget(String type)
throws LCDException
{
Widget widget;
if (type.equals(Widget.WIDGET_TITLE))
{
widget = new TitleWidget(_widgetCounter, this);
}
else if (type.equals(Widget.WIDGET_STRING))
{
widget = new StringWidget(_widgetCounter, this);
}
else if (type.equals(Widget.WIDGET_HBAR))
{
widget = new HBarWidget(_widgetCounter, this);
}
else if (type.equals(Widget.WIDGET_VBAR))
{
widget = new VBarWidget(_widgetCounter, this);
}
else if (type.equals(Widget.WIDGET_NUM))
{
widget = new NumWidget(_widgetCounter, this);
}
else if (type.equals(Widget.WIDGET_SCROLLER))
{
widget = new ScrollerWidget(_widgetCounter, this);
}
else if (type.equals(Widget.WIDGET_ICON))
{
widget = new IconWidget(_widgetCounter, this);
}
else
{
throw new LCDException(
"Unable to create a Widget of type " + type);
}
_widgetCounter++;
return widget;
}
/**
* Determine if this Screen is equal to the passed in Object.
* <p>They are considered equal if the passed in Object is not null and is a
* Screen object that has the same id as this Screen.
* <p>Note: Only the id is used for comparison, all other fields are
* ignored.
* @param object the Object to compare.
* @return <code>true</code> if the Screen is equal, <code>false</code>
* otherwise.
*/
public boolean equals(Object object)
{
boolean equal = false;
if (object instanceof Screen)
{
Screen s = (Screen)object;
equal = _id == s._id;
}
return equal;
}
/**
* Return the hash code for this Screen, simply the id of the Screen.
* @return the id of the Screen.
*/
public int hashCode()
{
return _id;
}
}