1 /* AbstractButton.java -- Provides basic button functionality.
2    Copyright (C) 2002 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
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.
10 
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.
15 
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.
20 
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.
25 
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. */
37 
38 
39 package javax.swing;
40 
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;
65 
66 /**
67  * Provides basic button functionality
68  *
69  * @author Ronald Veldema (rveldema@cs.vu.nl)
70  */
71 public abstract class AbstractButton extends JComponent
72   implements ItemSelectable, SwingConstants
73 {
74   Icon default_icon, pressed_button, disabled_button,
75     selected_button, disabled_selected_button, current_icon;
76   String text;
77 
78   int vert_align = CENTER;
79   int hori_align = CENTER;
80   int hori_text_pos = CENTER;
81   int vert_text_pos = CENTER;
82 
83   boolean paint_border = true, paint_focus;
84   Action action_taken;
85   ButtonModel model;
86   Insets margin;
87 
88   public static final String FOCUS_PAINTED_CHANGED_PROPERTY = "focusPainted";
89 
90   /**
91    * AccessibleAbstractButton
92    */
93   protected abstract class AccessibleAbstractButton
94     extends AccessibleJComponent
95     implements AccessibleAction, AccessibleValue, AccessibleText {
96 
97     /**
98      * Constructor AccessibleAbstractButton
99      * @param component TODO
100      */
AccessibleAbstractButton(AbstractButton component)101     protected AccessibleAbstractButton(AbstractButton component) {
102       super(component);
103       // TODO
104     } // AccessibleAbstractButton()
105 
106     /**
107      * getAccessibleStateSet
108      * @returns AccessibleStateSet
109      */
getAccessibleStateSet()110     public AccessibleStateSet getAccessibleStateSet() {
111       return null; // TODO
112     } // getAccessibleStateSet()
113 
114     /**
115      * getAccessibleName
116      * @returns String
117      */
getAccessibleName()118     public String getAccessibleName() {
119       return null; // TODO
120     } // getAccessibleName()
121 
122     /**
123      * getAccessibleIcon
124      * @returns AccessibleIcon[]
125      */
getAccessibleIcon()126     public AccessibleIcon[] getAccessibleIcon() {
127       return null; // TODO
128     } // getAccessibleIcon()
129 
130     /**
131      * getAccessibleRelationSet
132      * @returns AccessibleRelationSet
133      */
getAccessibleRelationSet()134     public AccessibleRelationSet getAccessibleRelationSet() {
135       return null; // TODO
136     } // getAccessibleRelationSet()
137 
138     /**
139      * getAccessibleAction
140      * @returns AccessibleAction
141      */
getAccessibleAction()142     public AccessibleAction getAccessibleAction() {
143       return null; // TODO
144     } // getAccessibleAction()
145 
146     /**
147      * getAccessibleValue
148      * @returns AccessibleValue
149      */
getAccessibleValue()150     public AccessibleValue getAccessibleValue() {
151       return null; // TODO
152     } // getAccessibleValue()
153 
154     /**
155      * getAccessibleActionCount
156      * @returns int
157      */
getAccessibleActionCount()158     public int getAccessibleActionCount() {
159       return 0; // TODO
160     } // getAccessibleActionCount()
161 
162     /**
163      * getAccessibleActionDescription
164      * @param value0 TODO
165      * @returns String
166      */
getAccessibleActionDescription(int value0)167     public String getAccessibleActionDescription(int value0) {
168       return null; // TODO
169     } // getAccessibleActionDescription()
170 
171     /**
172      * doAccessibleAction
173      * @param value0 TODO
174      * @returns boolean
175      */
doAccessibleAction(int value0)176     public boolean doAccessibleAction(int value0) {
177       return false; // TODO
178     } // doAccessibleAction()
179 
180     /**
181      * getCurrentAccessibleValue
182      * @returns Number
183      */
getCurrentAccessibleValue()184     public Number getCurrentAccessibleValue() {
185       return null; // TODO
186     } // getCurrentAccessibleValue()
187 
188     /**
189      * setCurrentAccessibleValue
190      * @param value0 TODO
191      * @returns boolean
192      */
setCurrentAccessibleValue(Number value0)193     public boolean setCurrentAccessibleValue(Number value0) {
194       return false; // TODO
195     } // setCurrentAccessibleValue()
196 
197     /**
198      * getMinimumAccessibleValue
199      * @returns Number
200      */
getMinimumAccessibleValue()201     public Number getMinimumAccessibleValue() {
202       return null; // TODO
203     } // getMinimumAccessibleValue()
204 
205     /**
206      * getMaximumAccessibleValue
207      * @returns Number
208      */
getMaximumAccessibleValue()209     public Number getMaximumAccessibleValue() {
210       return null; // TODO
211     } // getMaximumAccessibleValue()
212 
213     /**
214      * getAccessibleText
215      * @returns AccessibleText
216      */
getAccessibleText()217     public AccessibleText getAccessibleText() {
218       return null; // TODO
219     } // getAccessibleText()
220 
221     /**
222      * getIndexAtPoint
223      * @param value0 TODO
224      * @returns int
225      */
getIndexAtPoint(Point value0)226     public int getIndexAtPoint(Point value0) {
227       return 0; // TODO
228     } // getIndexAtPoint()
229 
230     /**
231      * getCharacterBounds
232      * @param value0 TODO
233      * @returns Rectangle
234      */
getCharacterBounds(int value0)235     public Rectangle getCharacterBounds(int value0) {
236       return null; // TODO
237     } // getCharacterBounds()
238 
239     /**
240      * getCharCount
241      * @returns int
242      */
getCharCount()243     public int getCharCount() {
244       return 0; // TODO
245     } // getCharCount()
246 
247     /**
248      * getCaretPosition
249      * @returns int
250      */
getCaretPosition()251     public int getCaretPosition() {
252       return 0; // TODO
253     } // getCaretPosition()
254 
255     /**
256      * getAtIndex
257      * @param value0 TODO
258      * @param value1 TODO
259      * @returns String
260      */
getAtIndex(int value0, int value1)261     public String getAtIndex(int value0, int value1) {
262       return null; // TODO
263     } // getAtIndex()
264 
265     /**
266      * getAfterIndex
267      * @param value0 TODO
268      * @param value1 TODO
269      * @returns String
270      */
getAfterIndex(int value0, int value1)271     public String getAfterIndex(int value0, int value1) {
272       return null; // TODO
273     } // getAfterIndex()
274 
275     /**
276      * getBeforeIndex
277      * @param value0 TODO
278      * @param value1 TODO
279      * @returns String
280      */
getBeforeIndex(int value0, int value1)281     public String getBeforeIndex(int value0, int value1) {
282       return null; // TODO
283     } // getBeforeIndex()
284 
285     /**
286      * getCharacterAttribute
287      * @param value0 TODO
288      * @returns AttributeSet
289      */
getCharacterAttribute(int value0)290     public AttributeSet getCharacterAttribute(int value0) {
291       return null; // TODO
292     } // getCharacterAttribute()
293 
294     /**
295      * getSelectionStart
296      * @returns int
297      */
getSelectionStart()298     public int getSelectionStart() {
299       return 0; // TODO
300     } // getSelectionStart()
301 
302     /**
303      * getSelectionEnd
304      * @returns int
305      */
getSelectionEnd()306     public int getSelectionEnd() {
307       return 0; // TODO
308     } // getSelectionEnd()
309 
310     /**
311      * getSelectedText
312      * @returns String
313      */
getSelectedText()314     public String getSelectedText() {
315       return null; // TODO
316     } // getSelectedText()
317 
318     /**
319      * getTextRectangle
320      * @returns Rectangle
321      */
getTextRectangle()322     private Rectangle getTextRectangle() {
323       return null; // TODO
324     } // getTextRectangle()
325 
326 
327   } // AccessibleAbstractButton
328 
329 
330   static private class JFocusListener implements FocusListener
331   {
332     AbstractButton c;
333 
JFocusListener(AbstractButton c)334     JFocusListener(AbstractButton c)
335     {
336       this.c = c;
337     }
338 
focusLost(FocusEvent event)339     public void focusLost(FocusEvent event)
340     {
341       c.getModel().setArmed(false);
342 
343       System.out.println("LOST FOCUS");
344       if (c.isFocusPainted())
345         {
346           c.repaint();
347         }
348     }
focusGained(FocusEvent event)349     public void focusGained(FocusEvent event)
350     {
351       System.out.println("GAIN FOCUS");
352     }
353   }
354 
AbstractButton()355   AbstractButton()
356   {
357     this("",null);
358   }
359 
AbstractButton(String text, Icon icon)360   AbstractButton(String text,
361                  Icon icon)
362   {
363     this.text    = text;
364     setIcon(icon);
365 
366     setAlignmentX(LEFT_ALIGNMENT);
367     setAlignmentY(CENTER_ALIGNMENT);
368 
369     addFocusListener( new JFocusListener(this) );
370 
371     setModel(new DefaultButtonModel(this));
372 
373     updateUI(); // get a proper ui
374   }
375 
getModel()376   public ButtonModel getModel()
377   {	return model;    }
378 
setModel(ButtonModel newModel)379   public void setModel(ButtonModel newModel)
380   {	model = newModel;    }
381 
getActionCommand()382   public String getActionCommand()
383   {	return getModel().getActionCommand();    }
384 
setActionCommand(String aCommand)385   public void setActionCommand(String aCommand)
386   {   getModel().setActionCommand(aCommand);   }
387 
addActionListener(ActionListener l)388   public void addActionListener(ActionListener l)
389   {	getModel().addActionListener(l);    }
390 
removeActionListener(ActionListener l)391   public void removeActionListener(ActionListener l)
392   {	getModel().removeActionListener(l);    }
393 
addChangeListener(ChangeListener l)394   public void addChangeListener(ChangeListener l)
395   {   getModel().addChangeListener(l);     }
396 
removeChangeListener(ChangeListener l)397   public void removeChangeListener(ChangeListener l)
398   {  getModel().removeChangeListener(l);    }
399 
addItemListener(ItemListener l)400   public void addItemListener(ItemListener l)
401   {  getModel().addItemListener(l);    }
402 
removeItemListener(ItemListener l)403   public void removeItemListener(ItemListener l)
404   {  getModel().removeItemListener(l);  }
405 
getHorizontalAlignment()406   public int getHorizontalAlignment()
407   {	return hori_align;    }
408 
getHorizontalTextPosition()409   public int getHorizontalTextPosition()
410   {	return hori_text_pos;    }
411 
getVerticalAlignment()412   public int getVerticalAlignment()
413   {	return vert_align;   }
414 
getVerticalTextPosition()415   public int getVerticalTextPosition()
416   {	return vert_text_pos;  }
417 
fireItemStateChanged(ItemEvent event)418   protected  void fireItemStateChanged(ItemEvent event)
419   {
420   }
421 
fireStateChanged(ChangeEvent event)422   protected  void fireStateChanged(ChangeEvent event)
423   {
424   }
425 
fireActionPerformed(ActionEvent event)426   protected void fireActionPerformed(ActionEvent event)
427   {
428   }
429 
setVerticalAlignment(int alignment)430   public void setVerticalAlignment(int alignment)
431   {	vert_align = alignment;    }
432 
setHorizontalAlignment(int alignment)433   public void setHorizontalAlignment(int alignment)
434   {   hori_align = alignment;   }
435 
setVerticalTextPosition(int textPosition)436   public void setVerticalTextPosition(int textPosition)
437   {	vert_text_pos = textPosition;    }
438 
setHorizontalTextPosition(int textPosition)439   public void setHorizontalTextPosition(int textPosition)
440   {   hori_text_pos = textPosition;   }
441 
getMnemonic()442   public int getMnemonic()
443   {	return getModel().getMnemonic();    }
444 
setMnemonic(char mne)445   public void setMnemonic(char mne)
446   {	getModel().setMnemonic(mne);    }
447 
setMnemonic(int mne)448   public void setMnemonic(int mne)
449   {	getModel().setMnemonic(mne);    }
450 
setRolloverEnabled(boolean b)451   public void setRolloverEnabled(boolean b)
452   {    getModel().setRollover(b);    }
453 
isRolloverEnabled()454   public boolean isRolloverEnabled()
455   {    return getModel().isRollover();     }
456 
isBorderPainted()457   public boolean isBorderPainted()
458   {	return paint_border;    }
459 
setBorderPainted(boolean b)460   public void setBorderPainted(boolean b)
461   {
462     if (b != paint_border)
463       {
464         paint_border = b;
465         revalidate();
466         repaint();
467       }
468   }
469 
getAction()470   public Action getAction()
471   {	return action_taken;    }
472 
setAction(Action a)473   public void setAction(Action a)
474   {
475     action_taken = a;
476     revalidate();
477     repaint();
478   }
479 
setSelected(boolean b)480   public void setSelected(boolean b)
481   {	getModel().setSelected(b);    }
482 
isSelected()483   public boolean isSelected()
484   {	return getModel().isSelected();     }
485 
getIcon()486   public Icon getIcon()
487   {	return default_icon;    }
488 
setIcon(Icon defaultIcon)489   public void setIcon(Icon defaultIcon)
490   {
491     if (default_icon == defaultIcon)
492       return;
493 
494     default_icon = defaultIcon;
495     if (default_icon != null)
496       {
497         // XXX FIXME - icons do not know their parent
498         //  			default_icon.setParent(this);
499       }
500     revalidate();
501     repaint();
502   }
503 
getText()504   public String getText()
505   {	return text;    }
506 
setLabel(String label)507   public void setLabel(String label)
508   {	setText(label);    }
509 
getLabel()510   public String getLabel()
511   {	return getText();    }
512 
setText(String text)513   public void setText(String text)
514   {
515     this.text = text;
516     revalidate();
517     repaint();
518   }
519 
getMargin()520   public 	Insets getMargin()
521   {      return margin; }
522 
setMargin(Insets m)523   public void setMargin(Insets m)
524   {
525     margin = m;
526     revalidate();
527     repaint();
528   }
529 
setEnabled(boolean b)530   public void setEnabled(boolean b)
531   {
532     super.setEnabled(b);
533     getModel().setEnabled(b);
534     repaint();
535   }
536 
getPressedIcon()537   public Icon getPressedIcon()
538   {	return pressed_button;    }
539 
setPressedIcon(Icon pressedIcon)540   public void setPressedIcon(Icon pressedIcon)
541   {
542     pressed_button = pressedIcon;
543     revalidate();
544     repaint();
545   }
546 
getDisabledIcon()547   public Icon getDisabledIcon()
548   {	return disabled_button;    }
549 
setDisabledIcon(Icon disabledIcon)550   public void setDisabledIcon(Icon disabledIcon)
551   {
552     disabled_button = disabledIcon;
553     revalidate();
554     repaint();
555   }
556 
isFocusPainted()557   public boolean isFocusPainted()
558   {   return paint_focus;   }
559 
setFocusPainted(boolean b)560   public void setFocusPainted(boolean b)
561   {
562     boolean old = paint_focus;
563     paint_focus = b;
564 
565     firePropertyChange(FOCUS_PAINTED_CHANGED_PROPERTY,
566                        old,
567                        b);
568     if (hasFocus())
569       {
570         revalidate();
571         repaint();
572       }
573   }
574 
isFocusTraversable()575   public boolean isFocusTraversable()
576   {
577     //Identifies whether or not this component can receive the focus.
578     return true;
579   }
580 
581 
checkHorizontalKey(int key, String exception)582   protected  int checkHorizontalKey(int key, String exception)
583   {
584     //       Verify that key is a legal value for the horizontalAlignment properties.
585     return 0;
586   }
587 
checkVerticalKey(int key, String exception)588   protected  int checkVerticalKey(int key, String exception)
589   {
590     //       Ensures that the key is a valid.
591     return 0;
592   }
593 
configurePropertiesFromAction(Action a)594   protected  void configurePropertiesFromAction(Action a)
595   {
596     //Factory method which sets the ActionEvent source's properties according to values from the Action instance.
597   }
598 
createActionListener()599   protected  ActionListener createActionListener()
600   {
601     return new ActionListener()
602       {
603         public void actionPerformed(ActionEvent e) { }
604       };
605   }
606 
createActionPropertyChangeListener(Action a)607   protected  PropertyChangeListener createActionPropertyChangeListener(Action a)
608   {
609     //Factory method which creates the PropertyChangeListener used to update the ActionEvent source as properties change on its Action instance.
610     return null;
611   }
612 
createChangeListener()613   protected  ChangeListener createChangeListener()
614   {
615     //       Subclasses that want to handle ChangeEvents differently can override this to return another ChangeListener implementation.
616     return new ChangeListener()
617       {
618         public void stateChanged(ChangeEvent e) { }
619       };
620   }
621 
createItemListener()622   protected  ItemListener createItemListener()
623   {
624     return new ItemListener()
625       {
626         public void itemStateChanged(ItemEvent e) { }
627       };
628   }
629 
doClick()630   public void doClick()
631   {
632     doClick(100);
633   }
634 
doClick(int pressTime)635   public void doClick(int pressTime)
636   {
637     //Toolkit.tlkBeep ();
638     //Programmatically perform a "click".
639   }
640 
getDisabledSelectedIcon()641   public Icon getDisabledSelectedIcon()
642   {
643     //Returns the icon used by the button when it's disabled and selected.
644     return disabled_selected_button;
645   }
646 
getRolloverIcon()647   public Icon getRolloverIcon()
648   {
649     //       Returns the rollover icon for the button.
650     return null;
651   }
652 
getRolloverSelectedIcon()653   Icon getRolloverSelectedIcon()
654   {
655     //       Returns the rollover selection icon for the button.
656     return null;
657   }
658 
getSelectedIcon()659   Icon getSelectedIcon()
660   {
661     //       Returns the selected icon for the button.
662     return selected_button;
663   }
664 
getSelectedObjects()665   public Object[] getSelectedObjects()
666   {
667     //Returns an array (length 1) containing the label or null if the button is not selected.
668     return null;
669   }
670 
imageUpdate(Image img, int infoflags, int x, int y, int w, int h)671   public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h)
672   {
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;
675   }
676 
isContentAreaFilled()677   public boolean isContentAreaFilled()
678   {
679     //       Checks whether the "content area" of the button should be filled.
680     return false;
681   }
682 
paintBorder(Graphics g)683   protected  void paintBorder(Graphics g)
684   {
685     //       Paint the button's border if BorderPainted property is true.
686     if (isBorderPainted())
687       super.paintBorder(g);
688   }
689 
paramString()690   protected  String paramString()
691   {
692     //        Returns a string representation of this AbstractButton.
693     return "AbstractButton";
694   }
695 
setContentAreaFilled(boolean b)696   public void setContentAreaFilled(boolean b)
697   {
698     //Sets whether the button should paint the content area or leave it transparent.
699   }
700 
setDisabledSelectedIcon(Icon disabledSelectedIcon)701   public void setDisabledSelectedIcon(Icon disabledSelectedIcon)
702   {
703     //          Sets the disabled selection icon for the button.
704   }
705 
setRolloverIcon(Icon rolloverIcon)706   public void setRolloverIcon(Icon rolloverIcon)
707   {
708     //       Sets the rollover icon for the button.
709   }
setRolloverSelectedIcon(Icon rolloverSelectedIcon)710   public void setRolloverSelectedIcon(Icon rolloverSelectedIcon)
711   {
712     //       Sets the rollover selected icon for the button.
713   }
714 
setSelectedIcon(Icon selectedIcon)715   public void setSelectedIcon(Icon selectedIcon)
716   {
717     //       Sets the selected icon for the button.
718   }
719 
setUI(ButtonUI ui)720   public void setUI(ButtonUI ui)
721   {	//       Sets the L&F object that renders this component.
722     super.setUI(ui);
723   }
724 
getUI()725   public ButtonUI getUI()
726   {
727     //Returns the L&F object that renders this component.
728     return (ButtonUI) ui;
729   }
730 
updateUI()731   public void updateUI()
732   {
733     /*
734     //          Notification from the UIFactory that the L&F has changed.
735     if (getUI() == null)
736     {
737     setUI(getUI());
738     }
739     */
740   }
741 
processActionEvent(ActionEvent e)742   protected void processActionEvent(ActionEvent e)
743   {
744     System.out.println("PROCESS-ACTION-EVENT: " + e);
745   }
746 
processMouseEvent(MouseEvent e)747   protected void processMouseEvent(MouseEvent e)
748   {
749     //	System.out.println("PROCESS-MOUSE-EVENT: " + e + ", PRESSED-IN-MODEL="+getModel().isPressed());
750 
751     switch (e.getID())
752       {
753       case MouseEvent.MOUSE_MOVED:
754         {
755           break;
756         }
757       case MouseEvent.MOUSE_PRESSED:
758         {
759           if (! isEnabled())
760             {
761               System.out.println("button not enabled, ignoring press");
762             }
763           else
764             {
765               System.out.println("telling model:press: " + getModel());
766               getModel().setPressed(true);
767               repaint();
768             }
769           break;
770         }
771 
772       case MouseEvent.MOUSE_RELEASED:
773         {
774           if (! isEnabled())
775             {
776               System.out.println("button not enabled, ignoring release");
777             }
778           else
779             {
780               int flags = 0;
781 
782               System.out.println("        XXX--> " + getActionCommand());
783 
784               fireActionPerformed(new ActionEvent(this,
785                                                   ActionEvent.ACTION_PERFORMED,
786                                                   getActionCommand(),
787                                                   flags));
788 
789               //System.out.println("telling model:release");
790               getModel().setPressed(false);
791               repaint();
792             }
793           break;
794         }
795       case MouseEvent.MOUSE_CLICKED:
796         {
797           break;
798         }
799       }
800   }
801 }
802