aboutsummaryrefslogtreecommitdiff
path: root/libjava/javax/swing/AbstractButton.java
blob: 951650dbdbd45e4feb1e0573da07b1a39f88e1fa (plain)
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/* AbstractButton.java -- Provides basic button functionality.
   Copyright (C) 2002 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package javax.swing;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.ItemSelectable;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeListener;
import javax.accessibility.AccessibleAction;
import javax.accessibility.AccessibleIcon;
import javax.accessibility.AccessibleStateSet;
import javax.accessibility.AccessibleRelationSet;
import javax.accessibility.AccessibleText;
import javax.accessibility.AccessibleValue;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.ButtonUI;
import javax.swing.text.AttributeSet;

/**
 * Provides basic button functionality
 *
 * @author Ronald Veldema (rveldema@cs.vu.nl)
 */
public abstract class AbstractButton extends JComponent
  implements ItemSelectable, SwingConstants
{
	Icon default_icon, pressed_button, disabled_button,
	selected_button, disabled_selected_button, current_icon;
	String text;

	int vert_align = CENTER;
	int hori_align = CENTER;
	int hori_text_pos = CENTER;
	int vert_text_pos = CENTER;

	boolean paint_border = true, paint_focus;
	Action action_taken;
	ButtonModel model;
	Insets margin;


	public static final String FOCUS_PAINTED_CHANGED_PROPERTY = "focusPainted";

	/**
	 * AccessibleAbstractButton
	 */
	protected abstract class AccessibleAbstractButton 
		extends AccessibleJComponent 
		implements AccessibleAction, AccessibleValue, AccessibleText {

		//-------------------------------------------------------------
		// Initialization ---------------------------------------------
		//-------------------------------------------------------------

		/**
		 * Constructor AccessibleAbstractButton
		 * @param component TODO
		 */
		protected AccessibleAbstractButton(AbstractButton component) {
			super(component);
			// TODO
		} // AccessibleAbstractButton()


		//-------------------------------------------------------------
		// Methods ----------------------------------------------------
		//-------------------------------------------------------------

		/**
		 * getAccessibleStateSet
		 * @returns AccessibleStateSet
		 */
		public AccessibleStateSet getAccessibleStateSet() {
			return null; // TODO
		} // getAccessibleStateSet()

		/**
		 * getAccessibleName
		 * @returns String
		 */
		public String getAccessibleName() {
			return null; // TODO
		} // getAccessibleName()

		/**
		 * getAccessibleIcon
		 * @returns AccessibleIcon[]
		 */
		public AccessibleIcon[] getAccessibleIcon() {
			return null; // TODO
		} // getAccessibleIcon()

		/**
		 * getAccessibleRelationSet
		 * @returns AccessibleRelationSet
		 */
		public AccessibleRelationSet getAccessibleRelationSet() {
			return null; // TODO
		} // getAccessibleRelationSet()

		/**
		 * getAccessibleAction
		 * @returns AccessibleAction
		 */
		public AccessibleAction getAccessibleAction() {
			return null; // TODO
		} // getAccessibleAction()

		/**
		 * getAccessibleValue
		 * @returns AccessibleValue
		 */
		public AccessibleValue getAccessibleValue() {
			return null; // TODO
		} // getAccessibleValue()

		/**
		 * getAccessibleActionCount
		 * @returns int
		 */
		public int getAccessibleActionCount() {
			return 0; // TODO
		} // getAccessibleActionCount()

		/**
		 * getAccessibleActionDescription
		 * @param value0 TODO
		 * @returns String
		 */
		public String getAccessibleActionDescription(int value0) {
			return null; // TODO
		} // getAccessibleActionDescription()

		/**
		 * doAccessibleAction
		 * @param value0 TODO
		 * @returns boolean
		 */
		public boolean doAccessibleAction(int value0) {
			return false; // TODO
		} // doAccessibleAction()

		/**
		 * getCurrentAccessibleValue
		 * @returns Number
		 */
		public Number getCurrentAccessibleValue() {
			return null; // TODO
		} // getCurrentAccessibleValue()

		/**
		 * setCurrentAccessibleValue
		 * @param value0 TODO
		 * @returns boolean
		 */
		public boolean setCurrentAccessibleValue(Number value0) {
			return false; // TODO
		} // setCurrentAccessibleValue()

		/**
		 * getMinimumAccessibleValue
		 * @returns Number
		 */
		public Number getMinimumAccessibleValue() {
			return null; // TODO
		} // getMinimumAccessibleValue()

		/**
		 * getMaximumAccessibleValue
		 * @returns Number
		 */
		public Number getMaximumAccessibleValue() {
			return null; // TODO
		} // getMaximumAccessibleValue()

		/**
		 * getAccessibleText
		 * @returns AccessibleText
		 */
		public AccessibleText getAccessibleText() {
			return null; // TODO
		} // getAccessibleText()

		/**
		 * getIndexAtPoint
		 * @param value0 TODO
		 * @returns int
		 */
		public int getIndexAtPoint(Point value0) {
			return 0; // TODO
		} // getIndexAtPoint()

		/**
		 * getCharacterBounds
		 * @param value0 TODO
		 * @returns Rectangle
		 */
		public Rectangle getCharacterBounds(int value0) {
			return null; // TODO
		} // getCharacterBounds()

		/**
		 * getCharCount
		 * @returns int
		 */
		public int getCharCount() {
			return 0; // TODO
		} // getCharCount()

		/**
		 * getCaretPosition
		 * @returns int
		 */
		public int getCaretPosition() {
			return 0; // TODO
		} // getCaretPosition()

		/**
		 * getAtIndex
		 * @param value0 TODO
		 * @param value1 TODO
		 * @returns String
		 */
		public String getAtIndex(int value0, int value1) {
			return null; // TODO
		} // getAtIndex()

		/**
		 * getAfterIndex
		 * @param value0 TODO
		 * @param value1 TODO
		 * @returns String
		 */
		public String getAfterIndex(int value0, int value1) {
			return null; // TODO
		} // getAfterIndex()

		/**
		 * getBeforeIndex
		 * @param value0 TODO
		 * @param value1 TODO
		 * @returns String
		 */
		public String getBeforeIndex(int value0, int value1) {
			return null; // TODO
		} // getBeforeIndex()

		/**
		 * getCharacterAttribute
		 * @param value0 TODO
		 * @returns AttributeSet
		 */
		public AttributeSet getCharacterAttribute(int value0) {
			return null; // TODO
		} // getCharacterAttribute()

		/**
		 * getSelectionStart
		 * @returns int
		 */
		public int getSelectionStart() {
			return 0; // TODO
		} // getSelectionStart()

		/**
		 * getSelectionEnd
		 * @returns int
		 */
		public int getSelectionEnd() {
			return 0; // TODO
		} // getSelectionEnd()

		/**
		 * getSelectedText
		 * @returns String
		 */
		public String getSelectedText() {
			return null; // TODO
		} // getSelectedText()

		/**
		 * getTextRectangle
		 * @returns Rectangle
		 */
		private Rectangle getTextRectangle() {
			return null; // TODO
		} // getTextRectangle()


	} // AccessibleAbstractButton


	static private class JFocusListener implements FocusListener
	{
		AbstractButton c;

		JFocusListener(AbstractButton c)
		{
			this.c = c;
		}

		public void focusLost(FocusEvent event)
		{
			c.getModel().setArmed(false);

			System.out.println("LOST FOCUS");
			if (c.isFocusPainted())
			{
				c.repaint();
			}
		}
		public void focusGained(FocusEvent event)
		{
			System.out.println("GAIN FOCUS");
		}
	}


	/**********************************************
	 *
	 * 
	 *       Constructors
	 *
	 *
	 ****************/

	AbstractButton()
	{
		this("",null);
	}

	AbstractButton(String text,
	               Icon icon)
	{
		this.text    = text;
		setIcon(icon);

		setAlignmentX(LEFT_ALIGNMENT);
		setAlignmentY(CENTER_ALIGNMENT);

		addFocusListener( new JFocusListener(this) );

		setModel(new DefaultButtonModel(this));

		updateUI(); // get a proper ui
	}


	/**********************************************
	 *
	 * 
	 *       Actions etc
	 *
	 *
	 ****************/

	public ButtonModel getModel()
	{	return model;    }
	public void setModel(ButtonModel newModel)
	{	model = newModel;    }

	public String getActionCommand()
	{	return getModel().getActionCommand();    }
	public void setActionCommand(String aCommand)
	{   getModel().setActionCommand(aCommand);   }

	public void addActionListener(ActionListener l)
	{	getModel().addActionListener(l);    }
	public void removeActionListener(ActionListener l)
	{	getModel().removeActionListener(l);    }

	public void addChangeListener(ChangeListener l)
	{   getModel().addChangeListener(l);     }
	public void removeChangeListener(ChangeListener l)
	{  getModel().removeChangeListener(l);    }

        public void addItemListener(ItemListener l)
	{  getModel().addItemListener(l);    }
        public void removeItemListener(ItemListener l)
	{  getModel().removeItemListener(l);  }

	public int getHorizontalAlignment()
	{	return hori_align;    }
	public int getHorizontalTextPosition()
	{	return hori_text_pos;    }
	public int getVerticalAlignment()
	{	return vert_align;   }
	public int getVerticalTextPosition()
	{	return vert_text_pos;  }


	protected  void fireItemStateChanged(ItemEvent event)
	{    getModel().fireItemStateChanged(event);    }
	protected  void fireStateChanged(ChangeEvent event)
	{	 getModel().fireStateChanged(event);    }
	protected void fireActionPerformed(ActionEvent event)
	{	getModel().fireActionPerformed(event);    }

	public void setVerticalAlignment(int alignment)
	{	vert_align = alignment;    }
	public void setHorizontalAlignment(int alignment)
	{   hori_align = alignment;   }
	public void setVerticalTextPosition(int textPosition)
	{	vert_text_pos = textPosition;    }
	public void setHorizontalTextPosition(int textPosition)
	{   hori_text_pos = textPosition;   }

	public int getMnemonic()
	{	return getModel().getMnemonic();    }
	public void setMnemonic(char mne)
	{	getModel().setMnemonic(mne);    }
	public void setMnemonic(int mne)
	{	getModel().setMnemonic(mne);    }

	public void setRolloverEnabled(boolean b)
	{    getModel().setRollover(b);    }
	public boolean isRolloverEnabled()
	{    return getModel().isRollover();     }


	public boolean isBorderPainted()
	{	return paint_border;    }
	public void setBorderPainted(boolean b)
	{
		if (b != paint_border)
		{
			paint_border = b;
			revalidate();
			repaint();
		}
	}

	public Action getAction()
	{	return action_taken;    }
	public void setAction(Action a)
	{
		action_taken = a;
		revalidate();
		repaint();
	}

	public void setSelected(boolean b)
	{	getModel().setSelected(b);    }
	public boolean isSelected()
	{	return getModel().isSelected();     }


        public Icon getIcon()
	{	return default_icon;    }
	public void setIcon(Icon defaultIcon)
	{
		if (default_icon == defaultIcon)
			return;

		default_icon = defaultIcon;
		if (default_icon != null)
		{
                  // XXX FIXME - icons do not know their parent
//  			default_icon.setParent(this);
		}
		revalidate();
		repaint();
	}

	public String getText()
	{	return text;    }
	public void setLabel(String label)
	{	setText(label);    }
	public String getLabel()
	{	return getText();    }
	public void setText(String text)
	{
		this.text = text;
		revalidate();
		repaint();
	}


        public 	Insets getMargin()
	{      return margin; }
	public void setMargin(Insets m)
	{
		margin = m;
		revalidate();
		repaint();
	}

	public void setEnabled(boolean b)
	{
		super.setEnabled(b);
		getModel().setEnabled(b);
		repaint();
	}

	public Icon getPressedIcon()
	{	return pressed_button;    }
	public void setPressedIcon(Icon pressedIcon)
	{
		pressed_button = pressedIcon;
		revalidate();
		repaint();
	}


	public Icon getDisabledIcon()
	{	return disabled_button;    }
	public void setDisabledIcon(Icon disabledIcon)
	{
		disabled_button = disabledIcon;
		revalidate();
		repaint();
	}

	public boolean isFocusPainted()
	{   return paint_focus;   }
	public void setFocusPainted(boolean b)
	{
		boolean old = paint_focus;
		paint_focus = b;

		firePropertyChange(FOCUS_PAINTED_CHANGED_PROPERTY,
		                   old,
		                   b);
		if (hasFocus())
		{
			revalidate();
			repaint();
		}
	}

	public boolean isFocusTraversable()
	{
		//Identifies whether or not this component can receive the focus.
		return true;
	}


	protected  int checkHorizontalKey(int key, String exception)
	{
		//       Verify that key is a legal value for the horizontalAlignment properties.
		return 0;
	}
	protected  int checkVerticalKey(int key, String exception)
	{
		//       Ensures that the key is a valid.
		return 0;
	}
	protected  void configurePropertiesFromAction(Action a)
	{
		//Factory method which sets the ActionEvent source's properties according to values from the Action instance.
	}

	protected  ActionListener createActionListener()
	{
		return new ActionListener()
		       {
			       public void actionPerformed(ActionEvent e) { }
		       };
	}

	protected  PropertyChangeListener createActionPropertyChangeListener(Action a)
	{
		//Factory method which creates the PropertyChangeListener used to update the ActionEvent source as properties change on its Action instance.
		return null;
	}
	protected  ChangeListener createChangeListener()
	{
		//       Subclasses that want to handle ChangeEvents differently can override this to return another ChangeListener implementation.
		return new ChangeListener()
		       {
			       public void stateChanged(ChangeEvent e) { }
		       };
	}

	protected  ItemListener createItemListener()
	{
		return new ItemListener()
		       {
			       public void itemStateChanged(ItemEvent e) { }
		       };
	}


	public void doClick()
	{
		doClick(100);
	}
	public void doClick(int pressTime)
	{
	    //Toolkit.tlkBeep ();
		//Programmatically perform a "click".
	}


	public Icon getDisabledSelectedIcon()
	{
		//Returns the icon used by the button when it's disabled and selected.
		return disabled_selected_button;
	}


	public Icon getRolloverIcon()
	{
		//       Returns the rollover icon for the button.
		return null;
	}

	Icon getRolloverSelectedIcon()
	{
		//       Returns the rollover selection icon for the button.
		return null;
	}
	Icon getSelectedIcon()
	{
		//       Returns the selected icon for the button.
		return selected_button;
	}


	public Object[] getSelectedObjects()
	{
		//Returns an array (length 1) containing the label or null if the button is not selected.
		return null;
	}


	public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h)
	{
		//This is overridden to return false if the current Icon's Image is not equal to the passed in Image img.
		return current_icon == img;
	}

        public boolean isContentAreaFilled()
	{
		//       Checks whether the "content area" of the button should be filled.
		return false;
	}



	protected  void paintBorder(Graphics g)
	{
		//       Paint the button's border if BorderPainted property is true.
		if (isBorderPainted())
			super.paintBorder(g);
	}
	protected  String paramString()
	{
		//        Returns a string representation of this AbstractButton.
		return "AbstractButton";
	}


	public void setContentAreaFilled(boolean b)
	{
		//Sets whether the button should paint the content area or leave it transparent.
	}


	public void setDisabledSelectedIcon(Icon disabledSelectedIcon)
	{
		//          Sets the disabled selection icon for the button.
	}

	public void setRolloverIcon(Icon rolloverIcon)
	{
		//       Sets the rollover icon for the button.
	}
        public void setRolloverSelectedIcon(Icon rolloverSelectedIcon)
	{
		//       Sets the rollover selected icon for the button.
	}


	public void setSelectedIcon(Icon selectedIcon)
	{
		//       Sets the selected icon for the button.
	}


	public void setUI(ButtonUI ui)
	{	//       Sets the L&F object that renders this component.
		super.setUI(ui);
	}

	public ButtonUI getUI()
	{
		//Returns the L&F object that renders this component.
		return (ButtonUI) ui;
	}

        public void updateUI()
	{
		/*
		  //          Notification from the UIFactory that the L&F has changed.
		  if (getUI() == null)
		  {
		  setUI(getUI());
		  }
		*/
	}

	protected void processActionEvent(ActionEvent e)
	{
		System.out.println("PROCESS-ACTION-EVENT: " + e);
	}


	protected void processMouseEvent(MouseEvent e)
	{
		//	System.out.println("PROCESS-MOUSE-EVENT: " + e + ", PRESSED-IN-MODEL="+getModel().isPressed());

		switch (e.getID())
		{
		case MouseEvent.MOUSE_MOVED:
			{
				break;
			}
		case MouseEvent.MOUSE_PRESSED:
			{
				if (! isEnabled())
				{
					System.out.println("button not enabled, ignoring press");
				}
				else
				{
					System.out.println("telling model:press: " + getModel());
					getModel().setPressed(true);
					repaint();
				}
				break;
			}

		case MouseEvent.MOUSE_RELEASED:
			{
				if (! isEnabled())
				{
					System.out.println("button not enabled, ignoring release");
				}
				else
				{
					int flags = 0;

					System.out.println("        XXX--> " + getActionCommand());

					fireActionPerformed(new ActionEvent(this,
					                                    ActionEvent.ACTION_PERFORMED,
					                                    getActionCommand(),
					                                    flags));

					//System.out.println("telling model:release");
					getModel().setPressed(false);
					repaint();
				}
				break;
			}
		case MouseEvent.MOUSE_CLICKED:
			{
				break;
			}
		}
	}
}