1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2010 IBM Corporation and others.
|
3
|
* All rights reserved. This program and the accompanying materials
|
4
|
* are made available under the terms of the Eclipse Public License v1.0
|
5
|
* which accompanies this distribution, and is available at
|
6
|
* http://www.eclipse.org/legal/epl-v10.html
|
7
|
*
|
8
|
* Contributors:
|
9
|
* IBM Corporation - initial API and implementation
|
10
|
*******************************************************************************/
|
11
|
package org.eclipse.draw2d;
|
12
|
|
13
|
import java.util.Iterator;
|
14
|
import java.util.Timer;
|
15
|
import java.util.TimerTask;
|
16
|
|
17
|
//import org.eclipse.draw2d.internal.Timer;
|
18
|
|
19
|
/**
|
20
|
* A model for buttons containing several properties, including enabled,
|
21
|
* pressed, selected, rollover enabled and mouseover.
|
22
|
*/
|
23
|
public class ButtonModel {
|
24
|
|
25
|
/** Enabled property */
|
26
|
public static final String ENABLED_PROPERTY = "enabled"; //$NON-NLS-1$
|
27
|
/** Pressed property */
|
28
|
public static final String PRESSED_PROPERTY = "pressed"; //$NON-NLS-1$
|
29
|
/** Selected property */
|
30
|
public static final String SELECTED_PROPERTY = "selected"; //$NON-NLS-1$
|
31
|
/** Rollover Enabled property */
|
32
|
public static final String ROLLOVER_ENABLED_PROPERTY = "rollover enabled"; //$NON-NLS-1$
|
33
|
/** Mouseover property */
|
34
|
public static final String MOUSEOVER_PROPERTY = "mouseover"; //$NON-NLS-1$
|
35
|
|
36
|
/** Armed property */
|
37
|
public static final String ARMED_PROPERTY = "armed"; //$NON-NLS-1$
|
38
|
|
39
|
/** Flag for armed button state */
|
40
|
protected static final int ARMED_FLAG = 1;
|
41
|
/** Flag for pressed button state */
|
42
|
protected static final int PRESSED_FLAG = 2;
|
43
|
/** Flag for mouseOver state */
|
44
|
protected static final int MOUSEOVER_FLAG = 4;
|
45
|
/** Flag for selected button state */
|
46
|
protected static final int SELECTED_FLAG = 8;
|
47
|
/** Flag for enablement button state */
|
48
|
protected static final int ENABLED_FLAG = 16;
|
49
|
/** Flag for rollover enablement button state */
|
50
|
protected static final int ROLLOVER_ENABLED_FLAG = 32;
|
51
|
/** Flag that can be used by subclasses to define more states */
|
52
|
protected static final int MAX_FLAG = ROLLOVER_ENABLED_FLAG;
|
53
|
|
54
|
private int state = ENABLED_FLAG;
|
55
|
private Object data;
|
56
|
|
57
|
/**
|
58
|
* Action performed events are not fired until the mouse button is released.
|
59
|
*/
|
60
|
public static final int DEFAULT_FIRING_BEHAVIOR = 0;
|
61
|
|
62
|
/**
|
63
|
* Action performed events fire repeatedly until the mouse button is
|
64
|
* released.
|
65
|
*/
|
66
|
public static final int REPEAT_FIRING_BEHAVIOR = 1;
|
67
|
|
68
|
/**
|
69
|
* The name of the action associated with this button.
|
70
|
*/
|
71
|
protected String actionName;
|
72
|
|
73
|
/**
|
74
|
* The ButtonGroup this button belongs to (if any).
|
75
|
*/
|
76
|
protected ButtonGroup group = null;
|
77
|
|
78
|
private EventListenerList listeners = new EventListenerList();
|
79
|
|
80
|
/**
|
81
|
* Listens to button state transitions and fires action performed events
|
82
|
* based on the desired behavior ({@link #DEFAULT_FIRING_BEHAVIOR} or
|
83
|
* {@link #REPEAT_FIRING_BEHAVIOR}).
|
84
|
*/
|
85
|
protected ButtonStateTransitionListener firingBehavior;
|
86
|
{
|
87
|
installFiringBehavior();
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Registers the given listener as an ActionListener.
|
92
|
*
|
93
|
* @param listener
|
94
|
* The ActionListener to add
|
95
|
* @since 2.0
|
96
|
*/
|
97
|
public void addActionListener(ActionListener listener) {
|
98
|
if (listener == null)
|
99
|
throw new IllegalArgumentException();
|
100
|
listeners.addListener(ActionListener.class, listener);
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Registers the given listener as a ChangeListener.
|
105
|
*
|
106
|
* @param listener
|
107
|
* The ChangeListener to add
|
108
|
* @since 2.0
|
109
|
*/
|
110
|
public void addChangeListener(ChangeListener listener) {
|
111
|
if (listener == null)
|
112
|
throw new IllegalArgumentException();
|
113
|
listeners.addListener(ChangeListener.class, listener);
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* Registers the given listener as a ButtonStateTransitionListener.
|
118
|
*
|
119
|
* @param listener
|
120
|
* The ButtonStateTransitionListener to add
|
121
|
* @since 2.0
|
122
|
*/
|
123
|
public void addStateTransitionListener(
|
124
|
ButtonStateTransitionListener listener) {
|
125
|
if (listener == null)
|
126
|
throw new IllegalArgumentException();
|
127
|
listeners.addListener(ButtonStateTransitionListener.class, listener);
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* Notifies any ActionListeners on this ButtonModel that an action has been
|
132
|
* performed.
|
133
|
*
|
134
|
* @since 2.0
|
135
|
*/
|
136
|
protected void fireActionPerformed() {
|
137
|
Iterator iter = listeners.getListeners(ActionListener.class);
|
138
|
ActionEvent action = new ActionEvent(this);
|
139
|
while (iter.hasNext())
|
140
|
((ActionListener) iter.next()).actionPerformed(action);
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* Notifies any listening ButtonStateTransitionListener that the pressed
|
145
|
* state of this button has been cancelled.
|
146
|
*
|
147
|
* @since 2.0
|
148
|
*/
|
149
|
protected void fireCanceled() {
|
150
|
Iterator iter = listeners
|
151
|
.getListeners(ButtonStateTransitionListener.class);
|
152
|
while (iter.hasNext())
|
153
|
((ButtonStateTransitionListener) iter.next()).canceled();
|
154
|
}
|
155
|
|
156
|
/**
|
157
|
* Notifies any listening ButtonStateTransitionListener that this button has
|
158
|
* been pressed.
|
159
|
*
|
160
|
* @since 2.0
|
161
|
*/
|
162
|
protected void firePressed() {
|
163
|
Iterator iter = listeners
|
164
|
.getListeners(ButtonStateTransitionListener.class);
|
165
|
while (iter.hasNext())
|
166
|
((ButtonStateTransitionListener) iter.next()).pressed();
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* Notifies any listening ButtonStateTransitionListener that this button has
|
171
|
* been released.
|
172
|
*
|
173
|
* @since 2.0
|
174
|
*/
|
175
|
protected void fireReleased() {
|
176
|
Iterator iter = listeners
|
177
|
.getListeners(ButtonStateTransitionListener.class);
|
178
|
while (iter.hasNext())
|
179
|
((ButtonStateTransitionListener) iter.next()).released();
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* Notifies any listening ButtonStateTransitionListeners that this button
|
184
|
* has resumed activity.
|
185
|
*
|
186
|
* @since 2.0
|
187
|
*/
|
188
|
protected void fireResume() {
|
189
|
Iterator iter = listeners
|
190
|
.getListeners(ButtonStateTransitionListener.class);
|
191
|
while (iter.hasNext())
|
192
|
((ButtonStateTransitionListener) iter.next()).resume();
|
193
|
}
|
194
|
|
195
|
/**
|
196
|
* Notifies any listening ChangeListeners that this button's state has
|
197
|
* changed.
|
198
|
*
|
199
|
* @param property
|
200
|
* The name of the property that changed
|
201
|
* @since 2.0
|
202
|
*/
|
203
|
protected void fireStateChanged(String property) {
|
204
|
Iterator iter = listeners.getListeners(ChangeListener.class);
|
205
|
ChangeEvent change = new ChangeEvent(this, property);
|
206
|
while (iter.hasNext())
|
207
|
((ChangeListener) iter.next()).handleStateChanged(change);
|
208
|
}
|
209
|
|
210
|
/**
|
211
|
* Notifies any listening ButtonStateTransitionListeners that this button
|
212
|
* has suspended activity.
|
213
|
*
|
214
|
* @since 2.0
|
215
|
*/
|
216
|
protected void fireSuspend() {
|
217
|
Iterator iter = listeners
|
218
|
.getListeners(ButtonStateTransitionListener.class);
|
219
|
while (iter.hasNext())
|
220
|
((ButtonStateTransitionListener) iter.next()).suspend();
|
221
|
}
|
222
|
|
223
|
boolean getFlag(int which) {
|
224
|
return (state & which) != 0;
|
225
|
}
|
226
|
|
227
|
/**
|
228
|
* Returns the group to which this model belongs.
|
229
|
*
|
230
|
* @return The ButtonGroup to which this model belongs
|
231
|
* @since 2.0
|
232
|
*/
|
233
|
public ButtonGroup getGroup() {
|
234
|
return group;
|
235
|
}
|
236
|
|
237
|
/**
|
238
|
* Returns an object representing user data.
|
239
|
*
|
240
|
* @return User data
|
241
|
* @since 2.0
|
242
|
*/
|
243
|
public Object getUserData() {
|
244
|
return data;
|
245
|
}
|
246
|
|
247
|
/**
|
248
|
* Sets the firing behavior for this button.
|
249
|
*
|
250
|
* @since 2.0
|
251
|
*/
|
252
|
protected void installFiringBehavior() {
|
253
|
setFiringBehavior(DEFAULT_FIRING_BEHAVIOR);
|
254
|
}
|
255
|
|
256
|
/**
|
257
|
* Returns <code>true</code> if this button is armed. If a button is armed,
|
258
|
* it will fire an ActionPerformed when released.
|
259
|
*
|
260
|
* @return <code>true</code> if this button is armed
|
261
|
* @since 2.0
|
262
|
*/
|
263
|
public boolean isArmed() {
|
264
|
return (state & ARMED_FLAG) != 0;
|
265
|
}
|
266
|
|
267
|
/**
|
268
|
* Returns <code>true</code> if this button is enabled.
|
269
|
*
|
270
|
* @return <code>true</code> if this button is enabled
|
271
|
* @since 2.0
|
272
|
*/
|
273
|
public boolean isEnabled() {
|
274
|
return (state & ENABLED_FLAG) != 0;
|
275
|
}
|
276
|
|
277
|
/**
|
278
|
* Returns <code>true</code> if the mouse is over this button.
|
279
|
*
|
280
|
* @return <code>true</code> if the mouse is over this button
|
281
|
* @since 2.0
|
282
|
*/
|
283
|
public boolean isMouseOver() {
|
284
|
return (state & MOUSEOVER_FLAG) != 0;
|
285
|
}
|
286
|
|
287
|
/**
|
288
|
* Returns <code>true</code> if this button is pressed.
|
289
|
*
|
290
|
* @return <code>true</code> if this button is pressed
|
291
|
* @since 2.0
|
292
|
*/
|
293
|
public boolean isPressed() {
|
294
|
return (state & PRESSED_FLAG) != 0;
|
295
|
}
|
296
|
|
297
|
/**
|
298
|
* Returns the selection state of this model. If this model belongs to any
|
299
|
* group, the group is queried for selection state, else the flags are used.
|
300
|
*
|
301
|
* @return <code>true</code> if this button is selected
|
302
|
* @since 2.0
|
303
|
*/
|
304
|
public boolean isSelected() {
|
305
|
if (group == null) {
|
306
|
return (state & SELECTED_FLAG) != 0;
|
307
|
} else {
|
308
|
return group.isSelected(this);
|
309
|
}
|
310
|
}
|
311
|
|
312
|
/**
|
313
|
* Removes the given ActionListener.
|
314
|
*
|
315
|
* @param listener
|
316
|
* The ActionListener to remove
|
317
|
* @since 2.0
|
318
|
*/
|
319
|
public void removeActionListener(ActionListener listener) {
|
320
|
listeners.removeListener(ActionListener.class, listener);
|
321
|
}
|
322
|
|
323
|
/**
|
324
|
* Removes the given ChangeListener.
|
325
|
*
|
326
|
* @param listener
|
327
|
* The ChangeListener to remove
|
328
|
* @since 2.0
|
329
|
*/
|
330
|
public void removeChangeListener(ChangeListener listener) {
|
331
|
listeners.removeListener(ChangeListener.class, listener);
|
332
|
}
|
333
|
|
334
|
/**
|
335
|
* Removes the given ButtonStateTransitionListener.
|
336
|
*
|
337
|
* @param listener
|
338
|
* The ButtonStateTransitionListener to remove
|
339
|
* @since 2.0
|
340
|
*/
|
341
|
public void removeStateTransitionListener(
|
342
|
ButtonStateTransitionListener listener) {
|
343
|
listeners.removeListener(ButtonStateTransitionListener.class, listener);
|
344
|
}
|
345
|
|
346
|
/**
|
347
|
* Sets this button to be armed. If a button is armed, it will fire an
|
348
|
* ActionPerformed when released.
|
349
|
*
|
350
|
* @param value
|
351
|
* The armed state
|
352
|
* @since 2.0
|
353
|
*/
|
354
|
public void setArmed(boolean value) {
|
355
|
if (isArmed() == value)
|
356
|
return;
|
357
|
if (!isEnabled())
|
358
|
return;
|
359
|
setFlag(ARMED_FLAG, value);
|
360
|
fireStateChanged(ARMED_PROPERTY);
|
361
|
}
|
362
|
|
363
|
/**
|
364
|
* Sets this button to be enabled.
|
365
|
*
|
366
|
* @param value
|
367
|
* The enabled state
|
368
|
* @since 2.0
|
369
|
*/
|
370
|
public void setEnabled(boolean value) {
|
371
|
if (isEnabled() == value)
|
372
|
return;
|
373
|
if (!value) {
|
374
|
setMouseOver(false);
|
375
|
setArmed(false);
|
376
|
setPressed(false);
|
377
|
}
|
378
|
setFlag(ENABLED_FLAG, value);
|
379
|
fireStateChanged(ENABLED_PROPERTY);
|
380
|
}
|
381
|
|
382
|
/**
|
383
|
* Sets the firing behavior for this button.
|
384
|
* {@link #DEFAULT_FIRING_BEHAVIOR} is the default behavior, where action
|
385
|
* performed events are not fired until the mouse button is released.
|
386
|
* {@link #REPEAT_FIRING_BEHAVIOR} causes action performed events to fire
|
387
|
* repeatedly until the mouse button is released.
|
388
|
*
|
389
|
* @param type
|
390
|
* The firing behavior type
|
391
|
* @since 2.0
|
392
|
*
|
393
|
*/
|
394
|
public void setFiringBehavior(int type) {
|
395
|
if (firingBehavior != null)
|
396
|
removeStateTransitionListener(firingBehavior);
|
397
|
switch (type) {
|
398
|
case REPEAT_FIRING_BEHAVIOR:
|
399
|
firingBehavior = new RepeatFiringBehavior();
|
400
|
break;
|
401
|
default:
|
402
|
firingBehavior = new DefaultFiringBehavior();
|
403
|
}
|
404
|
addStateTransitionListener(firingBehavior);
|
405
|
}
|
406
|
|
407
|
void setFlag(int flag, boolean value) {
|
408
|
if (value)
|
409
|
state |= flag;
|
410
|
else
|
411
|
state &= ~flag;
|
412
|
}
|
413
|
|
414
|
/**
|
415
|
* Sets the ButtonGroup to which this model belongs to. Adds this model as a
|
416
|
* listener to the group.
|
417
|
*
|
418
|
* @param bg
|
419
|
* The group to which this model belongs.
|
420
|
* @since 2.0
|
421
|
*/
|
422
|
public void setGroup(ButtonGroup bg) {
|
423
|
if (group == bg)
|
424
|
return;
|
425
|
if (group != null)
|
426
|
group.remove(this);
|
427
|
group = bg;
|
428
|
if (group != null)
|
429
|
group.add(this);
|
430
|
}
|
431
|
|
432
|
/**
|
433
|
* Sets the mouseover property of this button.
|
434
|
*
|
435
|
* @param value
|
436
|
* The value the mouseover property will be set to
|
437
|
* @since 2.0
|
438
|
*/
|
439
|
public void setMouseOver(boolean value) {
|
440
|
if (isMouseOver() == value)
|
441
|
return;
|
442
|
if (isPressed())
|
443
|
if (value)
|
444
|
fireResume();
|
445
|
else
|
446
|
fireSuspend();
|
447
|
setFlag(MOUSEOVER_FLAG, value);
|
448
|
fireStateChanged(MOUSEOVER_PROPERTY);
|
449
|
}
|
450
|
|
451
|
/**
|
452
|
* Sets the pressed property of this button.
|
453
|
*
|
454
|
* @param value
|
455
|
* The value the pressed property will be set to
|
456
|
* @since 2.0
|
457
|
*/
|
458
|
public void setPressed(boolean value) {
|
459
|
if (isPressed() == value)
|
460
|
return;
|
461
|
setFlag(PRESSED_FLAG, value);
|
462
|
if (value)
|
463
|
firePressed();
|
464
|
else {
|
465
|
if (isArmed())
|
466
|
fireReleased();
|
467
|
else
|
468
|
fireCanceled();
|
469
|
}
|
470
|
fireStateChanged(PRESSED_PROPERTY);
|
471
|
}
|
472
|
|
473
|
/**
|
474
|
* Sets this button to be selected.
|
475
|
*
|
476
|
* @param value
|
477
|
* The value the selected property will be set to
|
478
|
* @since 2.0
|
479
|
*/
|
480
|
public void setSelected(boolean value) {
|
481
|
if (group == null) {
|
482
|
if (isSelected() == value)
|
483
|
return;
|
484
|
} else {
|
485
|
group.setSelected(this, value);
|
486
|
if (getFlag(SELECTED_FLAG) == isSelected())
|
487
|
return;
|
488
|
}
|
489
|
setFlag(SELECTED_FLAG, value);
|
490
|
fireStateChanged(SELECTED_PROPERTY);
|
491
|
}
|
492
|
|
493
|
/**
|
494
|
* Sets user data.
|
495
|
*
|
496
|
* @param data
|
497
|
* The user data
|
498
|
* @since 2.0
|
499
|
*/
|
500
|
public void setUserData(Object data) {
|
501
|
this.data = data;
|
502
|
}
|
503
|
|
504
|
class DefaultFiringBehavior extends ButtonStateTransitionListener {
|
505
|
public void released() {
|
506
|
fireActionPerformed();
|
507
|
}
|
508
|
}
|
509
|
|
510
|
class RepeatFiringBehavior extends ButtonStateTransitionListener {
|
511
|
protected static final int INITIAL_DELAY = 250, STEP_DELAY = 40;
|
512
|
|
513
|
protected int stepDelay = INITIAL_DELAY, initialDelay = STEP_DELAY;
|
514
|
|
515
|
protected Timer timer;
|
516
|
|
517
|
public void pressed() {
|
518
|
fireActionPerformed();
|
519
|
if (!isEnabled())
|
520
|
return;
|
521
|
|
522
|
timer = new Timer();
|
523
|
TimerTask runAction = new Task(timer);
|
524
|
|
525
|
timer.scheduleAtFixedRate(runAction, INITIAL_DELAY, STEP_DELAY);
|
526
|
}
|
527
|
|
528
|
public void canceled() {
|
529
|
suspend();
|
530
|
}
|
531
|
|
532
|
public void released() {
|
533
|
suspend();
|
534
|
}
|
535
|
|
536
|
public void resume() {
|
537
|
timer = new Timer();
|
538
|
|
539
|
TimerTask runAction = new Task(timer);
|
540
|
|
541
|
timer.scheduleAtFixedRate(runAction, STEP_DELAY, STEP_DELAY);
|
542
|
}
|
543
|
|
544
|
public void suspend() {
|
545
|
if (timer == null)
|
546
|
return;
|
547
|
timer.cancel();
|
548
|
timer = null;
|
549
|
}
|
550
|
}
|
551
|
|
552
|
class Task extends TimerTask {
|
553
|
|
554
|
private Timer timer;
|
555
|
|
556
|
public Task(Timer timer) {
|
557
|
this.timer = timer;
|
558
|
}
|
559
|
|
560
|
public void run() {
|
561
|
org.eclipse.swt.widgets.Display.getDefault().syncExec(
|
562
|
new Runnable() {
|
563
|
public void run() {
|
564
|
if (!isEnabled())
|
565
|
timer.cancel();
|
566
|
fireActionPerformed();
|
567
|
}
|
568
|
});
|
569
|
}
|
570
|
}
|
571
|
|
572
|
}
|