1 /* AbstractButton.java -- Provides basic button functionality.
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java
.awt
.Graphics
;
42 import java
.awt
.Image
;
43 import java
.awt
.Insets
;
44 import java
.awt
.ItemSelectable
;
45 import java
.awt
.Point
;
46 import java
.awt
.Rectangle
;
47 import java
.awt
.event
.ActionEvent
;
48 import java
.awt
.event
.ActionListener
;
49 import java
.awt
.event
.FocusEvent
;
50 import java
.awt
.event
.FocusListener
;
51 import java
.awt
.event
.ItemEvent
;
52 import java
.awt
.event
.ItemListener
;
53 import java
.awt
.event
.MouseEvent
;
54 import java
.beans
.PropertyChangeListener
;
55 import javax
.accessibility
.AccessibleAction
;
56 import javax
.accessibility
.AccessibleIcon
;
57 import javax
.accessibility
.AccessibleStateSet
;
58 import javax
.accessibility
.AccessibleRelationSet
;
59 import javax
.accessibility
.AccessibleText
;
60 import javax
.accessibility
.AccessibleValue
;
61 import javax
.swing
.event
.ChangeEvent
;
62 import javax
.swing
.event
.ChangeListener
;
63 import javax
.swing
.plaf
.ButtonUI
;
64 import javax
.swing
.text
.AttributeSet
;
67 * Provides basic button functionality
69 * @author Ronald Veldema (rveldema@cs.vu.nl)
71 public abstract class AbstractButton
extends JComponent
72 implements ItemSelectable
, SwingConstants
74 Icon default_icon
, pressed_button
, disabled_button
,
75 selected_button
, disabled_selected_button
, current_icon
;
78 int vert_align
= CENTER
;
79 int hori_align
= CENTER
;
80 int hori_text_pos
= CENTER
;
81 int vert_text_pos
= CENTER
;
83 boolean paint_border
= true, paint_focus
;
88 public static final String FOCUS_PAINTED_CHANGED_PROPERTY
= "focusPainted";
91 * AccessibleAbstractButton
93 protected abstract class AccessibleAbstractButton
94 extends AccessibleJComponent
95 implements AccessibleAction
, AccessibleValue
, AccessibleText
{
98 * Constructor AccessibleAbstractButton
99 * @param component TODO
101 protected AccessibleAbstractButton(AbstractButton component
) {
104 } // AccessibleAbstractButton()
107 * getAccessibleStateSet
108 * @returns AccessibleStateSet
110 public AccessibleStateSet
getAccessibleStateSet() {
112 } // getAccessibleStateSet()
118 public String
getAccessibleName() {
120 } // getAccessibleName()
124 * @returns AccessibleIcon[]
126 public AccessibleIcon
[] getAccessibleIcon() {
128 } // getAccessibleIcon()
131 * getAccessibleRelationSet
132 * @returns AccessibleRelationSet
134 public AccessibleRelationSet
getAccessibleRelationSet() {
136 } // getAccessibleRelationSet()
139 * getAccessibleAction
140 * @returns AccessibleAction
142 public AccessibleAction
getAccessibleAction() {
144 } // getAccessibleAction()
148 * @returns AccessibleValue
150 public AccessibleValue
getAccessibleValue() {
152 } // getAccessibleValue()
155 * getAccessibleActionCount
158 public int getAccessibleActionCount() {
160 } // getAccessibleActionCount()
163 * getAccessibleActionDescription
167 public String
getAccessibleActionDescription(int value0
) {
169 } // getAccessibleActionDescription()
176 public boolean doAccessibleAction(int value0
) {
177 return false; // TODO
178 } // doAccessibleAction()
181 * getCurrentAccessibleValue
184 public Number
getCurrentAccessibleValue() {
186 } // getCurrentAccessibleValue()
189 * setCurrentAccessibleValue
193 public boolean setCurrentAccessibleValue(Number value0
) {
194 return false; // TODO
195 } // setCurrentAccessibleValue()
198 * getMinimumAccessibleValue
201 public Number
getMinimumAccessibleValue() {
203 } // getMinimumAccessibleValue()
206 * getMaximumAccessibleValue
209 public Number
getMaximumAccessibleValue() {
211 } // getMaximumAccessibleValue()
215 * @returns AccessibleText
217 public AccessibleText
getAccessibleText() {
219 } // getAccessibleText()
226 public int getIndexAtPoint(Point value0
) {
228 } // getIndexAtPoint()
235 public Rectangle
getCharacterBounds(int value0
) {
237 } // getCharacterBounds()
243 public int getCharCount() {
251 public int getCaretPosition() {
253 } // getCaretPosition()
261 public String
getAtIndex(int value0
, int value1
) {
271 public String
getAfterIndex(int value0
, int value1
) {
281 public String
getBeforeIndex(int value0
, int value1
) {
283 } // getBeforeIndex()
286 * getCharacterAttribute
288 * @returns AttributeSet
290 public AttributeSet
getCharacterAttribute(int value0
) {
292 } // getCharacterAttribute()
298 public int getSelectionStart() {
300 } // getSelectionStart()
306 public int getSelectionEnd() {
308 } // getSelectionEnd()
314 public String
getSelectedText() {
316 } // getSelectedText()
322 private Rectangle
getTextRectangle() {
324 } // getTextRectangle()
327 } // AccessibleAbstractButton
330 static private class JFocusListener
implements FocusListener
334 JFocusListener(AbstractButton c
)
339 public void focusLost(FocusEvent event
)
341 c
.getModel().setArmed(false);
343 System
.out
.println("LOST FOCUS");
344 if (c
.isFocusPainted())
349 public void focusGained(FocusEvent event
)
351 System
.out
.println("GAIN FOCUS");
360 AbstractButton(String text
,
366 setAlignmentX(LEFT_ALIGNMENT
);
367 setAlignmentY(CENTER_ALIGNMENT
);
369 addFocusListener( new JFocusListener(this) );
371 setModel(new DefaultButtonModel(this));
373 updateUI(); // get a proper ui
376 public ButtonModel
getModel()
379 public void setModel(ButtonModel newModel
)
380 { model
= newModel
; }
382 public String
getActionCommand()
383 { return getModel().getActionCommand(); }
385 public void setActionCommand(String aCommand
)
386 { getModel().setActionCommand(aCommand
); }
388 public void addActionListener(ActionListener l
)
389 { getModel().addActionListener(l
); }
391 public void removeActionListener(ActionListener l
)
392 { getModel().removeActionListener(l
); }
394 public void addChangeListener(ChangeListener l
)
395 { getModel().addChangeListener(l
); }
397 public void removeChangeListener(ChangeListener l
)
398 { getModel().removeChangeListener(l
); }
400 public void addItemListener(ItemListener l
)
401 { getModel().addItemListener(l
); }
403 public void removeItemListener(ItemListener l
)
404 { getModel().removeItemListener(l
); }
406 public int getHorizontalAlignment()
407 { return hori_align
; }
409 public int getHorizontalTextPosition()
410 { return hori_text_pos
; }
412 public int getVerticalAlignment()
413 { return vert_align
; }
415 public int getVerticalTextPosition()
416 { return vert_text_pos
; }
418 protected void fireItemStateChanged(ItemEvent event
)
422 protected void fireStateChanged(ChangeEvent event
)
426 protected void fireActionPerformed(ActionEvent event
)
430 public void setVerticalAlignment(int alignment
)
431 { vert_align
= alignment
; }
433 public void setHorizontalAlignment(int alignment
)
434 { hori_align
= alignment
; }
436 public void setVerticalTextPosition(int textPosition
)
437 { vert_text_pos
= textPosition
; }
439 public void setHorizontalTextPosition(int textPosition
)
440 { hori_text_pos
= textPosition
; }
442 public int getMnemonic()
443 { return getModel().getMnemonic(); }
445 public void setMnemonic(char mne
)
446 { getModel().setMnemonic(mne
); }
448 public void setMnemonic(int mne
)
449 { getModel().setMnemonic(mne
); }
451 public void setRolloverEnabled(boolean b
)
452 { getModel().setRollover(b
); }
454 public boolean isRolloverEnabled()
455 { return getModel().isRollover(); }
457 public boolean isBorderPainted()
458 { return paint_border
; }
460 public void setBorderPainted(boolean b
)
462 if (b
!= paint_border
)
470 public Action
getAction()
471 { return action_taken
; }
473 public void setAction(Action a
)
480 public void setSelected(boolean b
)
481 { getModel().setSelected(b
); }
483 public boolean isSelected()
484 { return getModel().isSelected(); }
486 public Icon
getIcon()
487 { return default_icon
; }
489 public void setIcon(Icon defaultIcon
)
491 if (default_icon
== defaultIcon
)
494 default_icon
= defaultIcon
;
495 if (default_icon
!= null)
497 // XXX FIXME - icons do not know their parent
498 // default_icon.setParent(this);
504 public String
getText()
507 public void setLabel(String label
)
510 public String
getLabel()
511 { return getText(); }
513 public void setText(String text
)
520 public Insets
getMargin()
523 public void setMargin(Insets m
)
530 public void setEnabled(boolean b
)
533 getModel().setEnabled(b
);
537 public Icon
getPressedIcon()
538 { return pressed_button
; }
540 public void setPressedIcon(Icon pressedIcon
)
542 pressed_button
= pressedIcon
;
547 public Icon
getDisabledIcon()
548 { return disabled_button
; }
550 public void setDisabledIcon(Icon disabledIcon
)
552 disabled_button
= disabledIcon
;
557 public boolean isFocusPainted()
558 { return paint_focus
; }
560 public void setFocusPainted(boolean b
)
562 boolean old
= paint_focus
;
565 firePropertyChange(FOCUS_PAINTED_CHANGED_PROPERTY
,
575 public boolean isFocusTraversable()
577 //Identifies whether or not this component can receive the focus.
582 protected int checkHorizontalKey(int key
, String exception
)
584 // Verify that key is a legal value for the horizontalAlignment properties.
588 protected int checkVerticalKey(int key
, String exception
)
590 // Ensures that the key is a valid.
594 protected void configurePropertiesFromAction(Action a
)
596 //Factory method which sets the ActionEvent source's properties according to values from the Action instance.
599 protected ActionListener
createActionListener()
601 return new ActionListener()
603 public void actionPerformed(ActionEvent e
) { }
607 protected PropertyChangeListener
createActionPropertyChangeListener(Action a
)
609 //Factory method which creates the PropertyChangeListener used to update the ActionEvent source as properties change on its Action instance.
613 protected ChangeListener
createChangeListener()
615 // Subclasses that want to handle ChangeEvents differently can override this to return another ChangeListener implementation.
616 return new ChangeListener()
618 public void stateChanged(ChangeEvent e
) { }
622 protected ItemListener
createItemListener()
624 return new ItemListener()
626 public void itemStateChanged(ItemEvent e
) { }
630 public void doClick()
635 public void doClick(int pressTime
)
637 //Toolkit.tlkBeep ();
638 //Programmatically perform a "click".
641 public Icon
getDisabledSelectedIcon()
643 //Returns the icon used by the button when it's disabled and selected.
644 return disabled_selected_button
;
647 public Icon
getRolloverIcon()
649 // Returns the rollover icon for the button.
653 Icon
getRolloverSelectedIcon()
655 // Returns the rollover selection icon for the button.
659 Icon
getSelectedIcon()
661 // Returns the selected icon for the button.
662 return selected_button
;
665 public Object
[] getSelectedObjects()
667 //Returns an array (length 1) containing the label or null if the button is not selected.
671 public boolean imageUpdate(Image img
, int infoflags
, int x
, int y
, int w
, int h
)
673 //This is overridden to return false if the current Icon's Image is not equal to the passed in Image img.
674 return current_icon
== img
;
677 public boolean isContentAreaFilled()
679 // Checks whether the "content area" of the button should be filled.
683 protected void paintBorder(Graphics g
)
685 // Paint the button's border if BorderPainted property is true.
686 if (isBorderPainted())
687 super.paintBorder(g
);
690 protected String
paramString()
692 // Returns a string representation of this AbstractButton.
693 return "AbstractButton";
696 public void setContentAreaFilled(boolean b
)
698 //Sets whether the button should paint the content area or leave it transparent.
701 public void setDisabledSelectedIcon(Icon disabledSelectedIcon
)
703 // Sets the disabled selection icon for the button.
706 public void setRolloverIcon(Icon rolloverIcon
)
708 // Sets the rollover icon for the button.
710 public void setRolloverSelectedIcon(Icon rolloverSelectedIcon
)
712 // Sets the rollover selected icon for the button.
715 public void setSelectedIcon(Icon selectedIcon
)
717 // Sets the selected icon for the button.
720 public void setUI(ButtonUI ui
)
721 { // Sets the L&F object that renders this component.
725 public ButtonUI
getUI()
727 //Returns the L&F object that renders this component.
728 return (ButtonUI
) ui
;
731 public void updateUI()
734 // Notification from the UIFactory that the L&F has changed.
742 protected void processActionEvent(ActionEvent e
)
744 System
.out
.println("PROCESS-ACTION-EVENT: " + e
);
747 protected void processMouseEvent(MouseEvent e
)
749 // System.out.println("PROCESS-MOUSE-EVENT: " + e + ", PRESSED-IN-MODEL="+getModel().isPressed());
753 case MouseEvent
.MOUSE_MOVED
:
757 case MouseEvent
.MOUSE_PRESSED
:
761 System
.out
.println("button not enabled, ignoring press");
765 System
.out
.println("telling model:press: " + getModel());
766 getModel().setPressed(true);
772 case MouseEvent
.MOUSE_RELEASED
:
776 System
.out
.println("button not enabled, ignoring release");
782 System
.out
.println(" XXX--> " + getActionCommand());
784 fireActionPerformed(new ActionEvent(this,
785 ActionEvent
.ACTION_PERFORMED
,
789 //System.out.println("telling model:release");
790 getModel().setPressed(false);
795 case MouseEvent
.MOUSE_CLICKED
: