2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / javax / swing / AbstractButton.java
blobd44d9775f3ac6fe2e10cb8dc6abd3313a2df66d2
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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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. */
39 package javax.swing;
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;
66 /**
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;
76 String text;
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;
84 Action action_taken;
85 ButtonModel model;
86 Insets margin;
88 public static final String FOCUS_PAINTED_CHANGED_PROPERTY = "focusPainted";
90 /**
91 * AccessibleAbstractButton
93 protected abstract class AccessibleAbstractButton
94 extends AccessibleJComponent
95 implements AccessibleAction, AccessibleValue, AccessibleText {
97 /**
98 * Constructor AccessibleAbstractButton
99 * @param component TODO
101 protected AccessibleAbstractButton(AbstractButton component) {
102 super(component);
103 // TODO
104 } // AccessibleAbstractButton()
107 * getAccessibleStateSet
108 * @returns AccessibleStateSet
110 public AccessibleStateSet getAccessibleStateSet() {
111 return null; // TODO
112 } // getAccessibleStateSet()
115 * getAccessibleName
116 * @returns String
118 public String getAccessibleName() {
119 return null; // TODO
120 } // getAccessibleName()
123 * getAccessibleIcon
124 * @returns AccessibleIcon[]
126 public AccessibleIcon[] getAccessibleIcon() {
127 return null; // TODO
128 } // getAccessibleIcon()
131 * getAccessibleRelationSet
132 * @returns AccessibleRelationSet
134 public AccessibleRelationSet getAccessibleRelationSet() {
135 return null; // TODO
136 } // getAccessibleRelationSet()
139 * getAccessibleAction
140 * @returns AccessibleAction
142 public AccessibleAction getAccessibleAction() {
143 return null; // TODO
144 } // getAccessibleAction()
147 * getAccessibleValue
148 * @returns AccessibleValue
150 public AccessibleValue getAccessibleValue() {
151 return null; // TODO
152 } // getAccessibleValue()
155 * getAccessibleActionCount
156 * @returns int
158 public int getAccessibleActionCount() {
159 return 0; // TODO
160 } // getAccessibleActionCount()
163 * getAccessibleActionDescription
164 * @param value0 TODO
165 * @returns String
167 public String getAccessibleActionDescription(int value0) {
168 return null; // TODO
169 } // getAccessibleActionDescription()
172 * doAccessibleAction
173 * @param value0 TODO
174 * @returns boolean
176 public boolean doAccessibleAction(int value0) {
177 return false; // TODO
178 } // doAccessibleAction()
181 * getCurrentAccessibleValue
182 * @returns Number
184 public Number getCurrentAccessibleValue() {
185 return null; // TODO
186 } // getCurrentAccessibleValue()
189 * setCurrentAccessibleValue
190 * @param value0 TODO
191 * @returns boolean
193 public boolean setCurrentAccessibleValue(Number value0) {
194 return false; // TODO
195 } // setCurrentAccessibleValue()
198 * getMinimumAccessibleValue
199 * @returns Number
201 public Number getMinimumAccessibleValue() {
202 return null; // TODO
203 } // getMinimumAccessibleValue()
206 * getMaximumAccessibleValue
207 * @returns Number
209 public Number getMaximumAccessibleValue() {
210 return null; // TODO
211 } // getMaximumAccessibleValue()
214 * getAccessibleText
215 * @returns AccessibleText
217 public AccessibleText getAccessibleText() {
218 return null; // TODO
219 } // getAccessibleText()
222 * getIndexAtPoint
223 * @param value0 TODO
224 * @returns int
226 public int getIndexAtPoint(Point value0) {
227 return 0; // TODO
228 } // getIndexAtPoint()
231 * getCharacterBounds
232 * @param value0 TODO
233 * @returns Rectangle
235 public Rectangle getCharacterBounds(int value0) {
236 return null; // TODO
237 } // getCharacterBounds()
240 * getCharCount
241 * @returns int
243 public int getCharCount() {
244 return 0; // TODO
245 } // getCharCount()
248 * getCaretPosition
249 * @returns int
251 public int getCaretPosition() {
252 return 0; // TODO
253 } // getCaretPosition()
256 * getAtIndex
257 * @param value0 TODO
258 * @param value1 TODO
259 * @returns String
261 public String getAtIndex(int value0, int value1) {
262 return null; // TODO
263 } // getAtIndex()
266 * getAfterIndex
267 * @param value0 TODO
268 * @param value1 TODO
269 * @returns String
271 public String getAfterIndex(int value0, int value1) {
272 return null; // TODO
273 } // getAfterIndex()
276 * getBeforeIndex
277 * @param value0 TODO
278 * @param value1 TODO
279 * @returns String
281 public String getBeforeIndex(int value0, int value1) {
282 return null; // TODO
283 } // getBeforeIndex()
286 * getCharacterAttribute
287 * @param value0 TODO
288 * @returns AttributeSet
290 public AttributeSet getCharacterAttribute(int value0) {
291 return null; // TODO
292 } // getCharacterAttribute()
295 * getSelectionStart
296 * @returns int
298 public int getSelectionStart() {
299 return 0; // TODO
300 } // getSelectionStart()
303 * getSelectionEnd
304 * @returns int
306 public int getSelectionEnd() {
307 return 0; // TODO
308 } // getSelectionEnd()
311 * getSelectedText
312 * @returns String
314 public String getSelectedText() {
315 return null; // TODO
316 } // getSelectedText()
319 * getTextRectangle
320 * @returns Rectangle
322 private Rectangle getTextRectangle() {
323 return null; // TODO
324 } // getTextRectangle()
327 } // AccessibleAbstractButton
330 static private class JFocusListener implements FocusListener
332 AbstractButton c;
334 JFocusListener(AbstractButton c)
336 this.c = c;
339 public void focusLost(FocusEvent event)
341 c.getModel().setArmed(false);
343 System.out.println("LOST FOCUS");
344 if (c.isFocusPainted())
346 c.repaint();
349 public void focusGained(FocusEvent event)
351 System.out.println("GAIN FOCUS");
355 AbstractButton()
357 this("",null);
360 AbstractButton(String text,
361 Icon icon)
363 this.text = text;
364 setIcon(icon);
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()
377 { return model; }
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)
464 paint_border = b;
465 revalidate();
466 repaint();
470 public Action getAction()
471 { return action_taken; }
473 public void setAction(Action a)
475 action_taken = a;
476 revalidate();
477 repaint();
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)
492 return;
494 default_icon = defaultIcon;
495 if (default_icon != null)
497 // XXX FIXME - icons do not know their parent
498 // default_icon.setParent(this);
500 revalidate();
501 repaint();
504 public String getText()
505 { return text; }
507 public void setLabel(String label)
508 { setText(label); }
510 public String getLabel()
511 { return getText(); }
513 public void setText(String text)
515 this.text = text;
516 revalidate();
517 repaint();
520 public Insets getMargin()
521 { return margin; }
523 public void setMargin(Insets m)
525 margin = m;
526 revalidate();
527 repaint();
530 public void setEnabled(boolean b)
532 super.setEnabled(b);
533 getModel().setEnabled(b);
534 repaint();
537 public Icon getPressedIcon()
538 { return pressed_button; }
540 public void setPressedIcon(Icon pressedIcon)
542 pressed_button = pressedIcon;
543 revalidate();
544 repaint();
547 public Icon getDisabledIcon()
548 { return disabled_button; }
550 public void setDisabledIcon(Icon disabledIcon)
552 disabled_button = disabledIcon;
553 revalidate();
554 repaint();
557 public boolean isFocusPainted()
558 { return paint_focus; }
560 public void setFocusPainted(boolean b)
562 boolean old = paint_focus;
563 paint_focus = b;
565 firePropertyChange(FOCUS_PAINTED_CHANGED_PROPERTY,
566 old,
568 if (hasFocus())
570 revalidate();
571 repaint();
575 public boolean isFocusTraversable()
577 //Identifies whether or not this component can receive the focus.
578 return true;
582 protected int checkHorizontalKey(int key, String exception)
584 // Verify that key is a legal value for the horizontalAlignment properties.
585 return 0;
588 protected int checkVerticalKey(int key, String exception)
590 // Ensures that the key is a valid.
591 return 0;
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.
610 return null;
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()
632 doClick(100);
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.
650 return null;
653 Icon getRolloverSelectedIcon()
655 // Returns the rollover selection icon for the button.
656 return null;
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.
668 return null;
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.
680 return false;
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.
722 super.setUI(ui);
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.
735 if (getUI() == null)
737 setUI(getUI());
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());
751 switch (e.getID())
753 case MouseEvent.MOUSE_MOVED:
755 break;
757 case MouseEvent.MOUSE_PRESSED:
759 if (! isEnabled())
761 System.out.println("button not enabled, ignoring press");
763 else
765 System.out.println("telling model:press: " + getModel());
766 getModel().setPressed(true);
767 repaint();
769 break;
772 case MouseEvent.MOUSE_RELEASED:
774 if (! isEnabled())
776 System.out.println("button not enabled, ignoring release");
778 else
780 int flags = 0;
782 System.out.println(" XXX--> " + getActionCommand());
784 fireActionPerformed(new ActionEvent(this,
785 ActionEvent.ACTION_PERFORMED,
786 getActionCommand(),
787 flags));
789 //System.out.println("telling model:release");
790 getModel().setPressed(false);
791 repaint();
793 break;
795 case MouseEvent.MOUSE_CLICKED:
797 break;