1
4
5 import org.aswing.*;
6 import org.aswing.plaf.*;
7
8
12 class org.aswing.AbstractButton extends Component{
13
14 public static var ON_SET_FOCUS:String = "onSetFocus";
15 public static var ON_KILL_FOCUS:String = "onKillFocus";
16
17
18
23 public static var ON_STATE_CHANGED:String = "onStateChanged";
24
25
30 public static var ON_SELECTION_CHANGED:String = "onSelectionChanged";
31
32
36 public static var CENTER:Number = ASWingConstants.CENTER;
37
41 public static var TOP:Number = ASWingConstants.TOP;
42
46 public static var LEFT:Number = ASWingConstants.LEFT;
47
51 public static var BOTTOM:Number = ASWingConstants.BOTTOM;
52
56 public static var RIGHT:Number = ASWingConstants.RIGHT;
57
61 public static var HORIZONTAL:Number = ASWingConstants.HORIZONTAL;
62
66 public static var VERTICAL:Number = ASWingConstants.VERTICAL;
67
68
69 private var model:ButtonModel;
70 private var modelStateListener:Object;
71 private var modelSelectionListener:Object;
72
73 private var text:String;
74 private var margin:Insets;
75 private var defaultMargin:Insets;
76
77
78 private var defaultIcon:Icon;
79 private var pressedIcon:Icon;
80 private var disabledIcon:Icon;
81
82 private var selectedIcon:Icon;
83 private var disabledSelectedIcon:Icon;
84
85 private var rolloverIcon:Icon;
86 private var rolloverSelectedIcon:Icon;
87
88
89 private var paintBorder:Boolean;
90 private var rolloverEnabled:Boolean;
91
92
93 private var verticalAlignment:Number;
94 private var horizontalAlignment:Number;
95
96 private var verticalTextPosition:Number;
97 private var horizontalTextPosition:Number;
98
99 private var iconTextGap:Number;
100
101
108 private function AbstractButton(text, icon:Icon){
109 super();
110 if(text instanceof Icon){
111 icon = Icon(text);
112 text = null;
113 }
114 setName("AbstractButton");
115 paintBorder = true;
116 rolloverEnabled = false;
117
118 verticalAlignment = CENTER;
119 horizontalAlignment = CENTER;
120 verticalTextPosition = CENTER;
121 horizontalTextPosition = RIGHT;
122
123 iconTextGap = 2;
124
125 setText(text);
126 setIcon(icon);
127 }
128
129
134 public function getModel():ButtonModel{
135 return model;
136 }
137
138
143 public function setModel(newModel:ButtonModel):Void {
144
145 var oldModel:ButtonModel = getModel();
146
147 if (oldModel != null) {
148 oldModel.removeEventListener(modelStateListener);
149 oldModel.removeEventListener(modelSelectionListener);
150 modelStateListener = null;
151 modelSelectionListener = null;
152 }
153
154 model = newModel;
155
156 if (newModel != null) {
157 modelStateListener = newModel.addChangeListener(__onModelStateChanged, this);
158 modelSelectionListener = newModel.addSelectionListener(__onModelSelectionChanged, this);
159 }
160
161 if (newModel != oldModel) {
162 revalidate();
163 repaint();
164 }
165 }
166
167 private function __onModelStateChanged(event:Event):Void{
168 fireStateChanged();
169 }
170
171 private function __onModelSelectionChanged(event:Event):Void{
172 dispatchEvent(ON_SELECTION_CHANGED, createEventObj(ON_SELECTION_CHANGED));
173 }
174
175
180 public function getUI():ButtonUI {
181 return ButtonUI(ui);
182 }
183
184
185
190 public function setUI(ui:ButtonUI):Void {
191 super.setUI(ui);
192 }
193
194
195
204 public function updateUI():Void{
205 }
206
207
219 public function addActionListener(fuc:Function, obj:Object):Object{
220 return addEventListener(ON_ACT, fuc, obj);
221 }
222
223
234 public function addSelectionListener(func:Function, obj:Object):Object{
235 return addEventListener(ON_SELECTION_CHANGED, func, obj);
236 }
237
238
242 public function setEnabled(b:Boolean):Void{
243 if (!b && model.isRollOver()) {
244 model.setRollOver(false);
245 }
246 super.setEnabled(b);
247 model.setEnabled(b);
248 }
249
250
255 public function isSelected():Boolean{
256 return model.isSelected();
257 }
258
259
266 public function setSelected(b:Boolean):Void{
267 model.setSelected(b);
268 }
269
270
281 public function setRollOverEnabled(b:Boolean):Void{
282 if(rolloverEnabled != b){
283 rolloverEnabled = b;
284 repaint();
285 }
286 }
287
288
294 public function isRollOverEnabled():Boolean{
295 return rolloverEnabled;
296 }
297
298
310 public function setMargin(m:Insets):Void{
311
312 if(m instanceof UIResource) {
313 defaultMargin = m;
314 }
315
316
317
318 if(m == null && defaultMargin != null) {
319 m = defaultMargin;
320 }
321
322 var old:Insets = margin;
323 margin = m;
324 if (old == null || !m.equals(old)) {
325 revalidate();
326
327 }
328 }
329
330 public function getMargin():Insets{
331 if(margin == null){
332 return defaultMargin;
333 }else{
334 return (new Insets()).addInsets(margin);
335 }
336 }
337
338
345 public function setContent(text:String, icon:Icon):Void{
346 if(this.text != text){
347 this.text = text;
348 }
349 if(this.defaultIcon != icon){
350 uninstallIconWhenNextPaint(this.defaultIcon);
351 this.defaultIcon = icon;
352 }
353 repaint();
354 invalidate();
355 }
356
357 public function setText(text:String):Void{
358 if(this.text != text){
359 this.text = text;
360 repaint();
361 invalidate();
362 }
363 }
364
365 public function getText():String{
366 return text;
367 }
368
369 public function setIcon(defaultIcon:Icon):Void{
370 if(this.defaultIcon != defaultIcon){
371 uninstallIconWhenNextPaint(this.defaultIcon);
372 this.defaultIcon = defaultIcon;
373 repaint();
374 invalidate();
375 }
376 }
377
378 public function getIcon():Icon{
379 return defaultIcon;
380 }
381
382
387 public function getPressedIcon():Icon {
388 return pressedIcon;
389 }
390
391
396 public function setPressedIcon(pressedIcon:Icon):Void {
397 var oldValue:Icon = this.pressedIcon;
398 this.pressedIcon = pressedIcon;
399 if (pressedIcon != oldValue) {
400 uninstallIconWhenNextPaint(oldValue);
401 if (getModel().isPressed()) {
402 repaint();
403 }
404 }
405 }
406
407
412 public function getSelectedIcon():Icon {
413 return selectedIcon;
414 }
415
416
421 public function setSelectedIcon(selectedIcon:Icon):Void {
422 var oldValue:Icon = this.selectedIcon;
423 this.selectedIcon = selectedIcon;
424 if (selectedIcon != oldValue) {
425 uninstallIconWhenNextPaint(oldValue);
426 if (isSelected()) {
427 repaint();
428 }
429 }
430 }
431
432
437 public function getRollOverIcon():Icon {
438 return rolloverIcon;
439 }
440
441
446 public function setRollOverIcon(rolloverIcon:Icon):Void {
447 var oldValue:Icon = this.rolloverIcon;
448 this.rolloverIcon = rolloverIcon;
449 setRollOverEnabled(true);
450 if (rolloverIcon != oldValue) {
451 uninstallIconWhenNextPaint(oldValue);
452 if(getModel().isRollOver()){
453 repaint();
454 }
455 }
456
457 }
458
459
464 public function getRollOverSelectedIcon():Icon {
465 return rolloverSelectedIcon;
466 }
467
468
474 public function setRollOverSelectedIcon(rolloverSelectedIcon:Icon):Void {
475 var oldValue:Icon = this.rolloverSelectedIcon;
476 this.rolloverSelectedIcon = rolloverSelectedIcon;
477 setRollOverEnabled(true);
478 if (rolloverSelectedIcon != oldValue) {
479 uninstallIconWhenNextPaint(oldValue);
480 if (isSelected()) {
481 repaint();
482 }
483 }
484 }
485
486
498 public function getDisabledIcon():Icon {
499 if(disabledIcon == null) {
500 if(defaultIcon != null) {
501 disabledIcon = new GrayFilteredIcon(defaultIcon);
502 }
503 }
504 return disabledIcon;
505 }
506
507
512 public function setDisabledIcon(disabledIcon:Icon):Void {
513 var oldValue:Icon = this.disabledIcon;
514 this.disabledIcon = disabledIcon;
515 if (disabledIcon != oldValue) {
516 uninstallIconWhenNextPaint(oldValue);
517 if (!isEnabled()) {
518 repaint();
519 }
520 }
521 }
522
523
535 public function getDisabledSelectedIcon():Icon {
536 if(disabledSelectedIcon == null) {
537 if(selectedIcon != null) {
538 disabledSelectedIcon = new GrayFilteredIcon(selectedIcon);
539 } else {
540 return getDisabledIcon();
541 }
542 }
543 return disabledSelectedIcon;
544 }
545
546
552 public function setDisabledSelectedIcon(disabledSelectedIcon:Icon):Void {
553 var oldValue:Icon = this.disabledSelectedIcon;
554 this.disabledSelectedIcon = disabledSelectedIcon;
555 if (disabledSelectedIcon != oldValue) {
556 uninstallIconWhenNextPaint(oldValue);
557 if (!isEnabled() && isSelected()) {
558 repaint();
559 revalidate();
560 }
561 }
562 }
563
564
575 public function getVerticalAlignment():Number {
576 return verticalAlignment;
577 }
578
579
588 public function setVerticalAlignment(alignment:Number):Void {
589 if (alignment == verticalAlignment){
590 return;
591 }else{
592 verticalAlignment = alignment;
593 repaint();
594 }
595 }
596
597
607 public function getHorizontalAlignment():Number{
608 return horizontalAlignment;
609 }
610
611
620 public function setHorizontalAlignment(alignment:Number):Void {
621 if (alignment == horizontalAlignment){
622 return;
623 }else{
624 horizontalAlignment = alignment;
625 repaint();
626 }
627 }
628
629
630
640 public function getVerticalTextPosition():Number{
641 return verticalTextPosition;
642 }
643
644
653 public function setVerticalTextPosition(textPosition:Number):Void {
654 if (textPosition == verticalTextPosition){
655 return;
656 }else{
657 verticalTextPosition = textPosition;
658 repaint();
659 }
660 }
661
662
672 public function getHorizontalTextPosition():Number {
673 return horizontalTextPosition;
674 }
675
676
685 public function setHorizontalTextPosition(textPosition:Number):Void {
686 if (textPosition == horizontalTextPosition){
687 return;
688 }else{
689 horizontalTextPosition = textPosition;
690 repaint();
691 }
692 }
693
694
702 public function getIconTextGap():Number {
703 return iconTextGap;
704 }
705
706
714 public function setIconTextGap(iconTextGap:Number):Void {
715 var oldValue:Number = this.iconTextGap;
716 this.iconTextGap = iconTextGap;
717 if (iconTextGap != oldValue) {
718 revalidate();
719 repaint();
720 }
721 }
722
723
724
725 private function __onPress():Void{
726 getModel().setPressed(true);
727 super.__onPress();
728 }
729
730 private function __onRelease():Void{
731 getModel().setReleased(true);
732 super.__onRelease();
733 dispatchEvent(ON_ACT, createEventObj(ON_ACT));
734 }
735
736 private function __onReleaseOutside():Void{
737 getModel().setRollOver(false);
738 getModel().setReleased(true);
739 super.__onReleaseOutside();
740 }
741
742 private function __onRollOver():Void{
743 getModel().setRollOver(true);
744 super.__onRollOver();
745 }
746
747 private function __onRollOut():Void{
748 getModel().setRollOver(false);
749 super.__onRollOut();
750 }
751 }
752