org.tbee.swing
Class SwingEventDispatcher

java.lang.Object
  extended by org.tbee.swing.SwingEventDispatcher
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.event.FocusListener, java.awt.event.ItemListener, java.awt.event.MouseListener, java.beans.PropertyChangeListener, java.beans.VetoableChangeListener, java.util.EventListener, javax.swing.event.ChangeListener, javax.swing.event.ListSelectionListener, javax.swing.event.TreeSelectionListener

public class SwingEventDispatcher
extends java.lang.Object
implements java.awt.event.ActionListener, java.awt.event.FocusListener, java.awt.event.MouseListener, javax.swing.event.ListSelectionListener, javax.swing.event.ChangeListener, java.beans.PropertyChangeListener, java.beans.VetoableChangeListener, javax.swing.event.TreeSelectionListener, java.awt.event.ItemListener

This class implements a event handling behaviour of Swing components similar to VisualBasic.

Details

This class examines the component specified in the constructor for add*Listener methods and if it find them, examines the listener for the corresponding handler methodes and if it find these, it will call these handler methods automatically. Every handler method is optional, leaving it out means it simply won't be called. (So you wont need to implement all mouse listener methods, just the ones that are required.) The component must have its name set (setName), this is used for the handler methods. Optionally the constructor with the additional name parameter can be used to set the name AND setup the listeners. For example, if the components name is "doit", possible handler methods are:

public void doit_actionPerformed(ActionEvent e, Map v) // regular
public void doit_click(ActionEvent e, Map v) // alternative, for readibility

public void doit_focusGained(FocusEvent e, Map values)
public void doit_focusLost(FocusEvent e, Map values)

public void doit_mouseClicked(MouseEvent e, Map v)
public void doit_mouseEntered(MouseEvent e, Map v)
public void doit_mouseExited(MouseEvent e, Map v)
public void doit_mousePressed(MouseEvent e, Map v)
public void doit_mouseReleased(MouseEvent e, Map v)
--
public void doit_mouseSingleClicked(MouseEvent e, Map v)
public void doit_mouseDoubleClicked(MouseEvent e, Map v)
public void doit_mouseTripleClicked(MouseEvent e, Map v)
public void doit_mouseManyClicked(MouseEvent e, Map v)
Normally a triple click also invokes double clicked and clicked, this means there cannot be separated logics assigned to the N-clicks. (e.g. I cannot do an "add" on single click and a "remove" on double click, because single click is always executed before double.) However, by delaying firing of a click event and waiting a small amount of time if the followup click also comes, we can make sure that only 1 event is fired; either the single, double, triple ... many click.
The way this is implemented also eliminates some of the focussing issues around multiple clicks.

public void doit_valueChanged(ListSelectionEvent e, Map v) // regular
public void doit_selectionChanged(ListSelectionEvent e, Map v) // alternative, for readibility

public void doit_stateChanged(ChangeEvent e, Map v) // regular
public void doit_changed(ChangeEvent e, Map v) // alternative, for readibility

public void doit_propertyChange(PropertyChangeEvent e, Map v) // regular
public void doit_changed(PropertyChangeEvent e, Map v) // alternative, for readibility

public void doit_vetoableChange(PropertyChangeEvent e, Map v) throws SwingEventDispatcher.PropertyVetoException // regular,
public void doit_validate(PropertyChangeEvent e, Map v) throws SwingEventDispatcher.PropertyVetoException // alternative, for readibility
The vetoable related methods must throw a SwingEventDispatcher.PropertyVetoException instead of a regular PropertyVetoException, this has all to do with checked vs uncheck exceptions. The actual setter throws a regular PropertyVetoException.

public void mytree_valueChanged(TreeSelectionEvent e, Map v)
public void mytree_selectionChanged(TreeSelectionEvent e, Map v) // alternative, for readibility


Every method can be written with 2, 1 or no parameters:
public void doit_click(ActionEvent e, Map v)
public void doit_click(Map v)
public void doit_click()
When multiple methods exists, the one with the most detail (=number of parameters) is called.

Version:
$Revision: 1.25 $

Nested Class Summary
static class SwingEventDispatcher.PropertyVetoException
          For handling VetoableChange correctly.
 
Field Summary
static java.lang.String SOURCECODE_VERSION
          Standard variable for determining version of a class file.
 
Constructor Summary
SwingEventDispatcher(java.awt.Component component, java.lang.Object listener)
          Initialize
SwingEventDispatcher(java.awt.Component component, java.lang.Object listener, java.util.Map values)
          Initialize
SwingEventDispatcher(java.awt.Component component, java.lang.String name, java.lang.Object listener)
          Convenience constructor: set the name of the component and then initialize.
SwingEventDispatcher(java.awt.Component component, java.lang.String name, java.lang.Object listener, java.util.Map values)
          Convenience constructor: set the name of the component and then initialize.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
           
 void focusGained(java.awt.event.FocusEvent e)
           
 void focusLost(java.awt.event.FocusEvent e)
           
 void itemStateChanged(java.awt.event.ItemEvent e)
           
 void mouseClicked(java.awt.event.MouseEvent e)
          this method calls mouseClicked and schedules a task to call either single, double, triple or many click
 void mouseEntered(java.awt.event.MouseEvent e)
           
 void mouseExited(java.awt.event.MouseEvent e)
           
 void mousePressed(java.awt.event.MouseEvent e)
           
 void mouseReleased(java.awt.event.MouseEvent e)
           
 void propertyChange(java.beans.PropertyChangeEvent e)
           
 void stateChanged(javax.swing.event.ChangeEvent e)
           
 void valueChanged(javax.swing.event.ListSelectionEvent e)
           
 void valueChanged(javax.swing.event.TreeSelectionEvent e)
           
 void vetoableChange(java.beans.PropertyChangeEvent e)
           
static java.awt.Component wrap(java.awt.Component component, java.lang.String name, java.lang.Object listener)
          This method is only for convenience, so you can write: JButton b = new JButton("y"); b.setName("z"); new SwingEventDispatcher(b, this); panel.add(b); as: panel.add( SwingEventDispatcher.wrap( new JButton("y"), "z", this) );
static java.awt.Component wrap(java.awt.Component component, java.lang.String name, java.lang.Object listener, java.util.Map values)
          This method is only for convenience, so you can write: JButton b = new JButton("y"); b.setName("z"); new SwingEventDispatcher(b, this, null); panel.add(b); as: panel.add( SwingEventDispatcher.wrap( new JButton("y"), "z", this, null) );
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SOURCECODE_VERSION

public static final java.lang.String SOURCECODE_VERSION
Standard variable for determining version of a class file.

See Also:
Constant Field Values
Constructor Detail

SwingEventDispatcher

public SwingEventDispatcher(java.awt.Component component,
                            java.lang.String name,
                            java.lang.Object listener,
                            java.util.Map values)
Convenience constructor: set the name of the component and then initialize.

Parameters:
component - the component of which the events must be dispatched
name - the name that the component gets (is used in finding the event methods)
listener - which class / instance contains the event methods
values - a convenience map for passing values

SwingEventDispatcher

public SwingEventDispatcher(java.awt.Component component,
                            java.lang.String name,
                            java.lang.Object listener)
Convenience constructor: set the name of the component and then initialize.

Parameters:
component - the component of which the events must be dispatched
name - the name that the component gets (is used in finding the event methods)
listener - which class / instance contains the event methods

SwingEventDispatcher

public SwingEventDispatcher(java.awt.Component component,
                            java.lang.Object listener,
                            java.util.Map values)
Initialize

Parameters:
component - the component of which the events must be dispatched
listener - which class / instance contains the event methods
values - a convenience map for passing values

SwingEventDispatcher

public SwingEventDispatcher(java.awt.Component component,
                            java.lang.Object listener)
Initialize

Parameters:
component - the component of which the events must be dispatched
listener - which class / instance contains the event methods
Method Detail

wrap

public static java.awt.Component wrap(java.awt.Component component,
                                      java.lang.String name,
                                      java.lang.Object listener)
This method is only for convenience, so you can write: JButton b = new JButton("y"); b.setName("z"); new SwingEventDispatcher(b, this); panel.add(b); as: panel.add( SwingEventDispatcher.wrap( new JButton("y"), "z", this) );

Parameters:
component - the component of which the events must be dispatched
name - the name that the component gets (is used in finding the event methods)
listener - which class / instance contains the event methods
Returns:
the component that is specified in the parameter

wrap

public static java.awt.Component wrap(java.awt.Component component,
                                      java.lang.String name,
                                      java.lang.Object listener,
                                      java.util.Map values)
This method is only for convenience, so you can write: JButton b = new JButton("y"); b.setName("z"); new SwingEventDispatcher(b, this, null); panel.add(b); as: panel.add( SwingEventDispatcher.wrap( new JButton("y"), "z", this, null) );

Parameters:
component - the component of which the events must be dispatched
name - the name that the component gets (is used in finding the event methods)
listener - which class / instance contains the event methods
values - a convenience map for passing values
Returns:
the component that is specified in the parameter

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Specified by:
actionPerformed in interface java.awt.event.ActionListener

focusGained

public void focusGained(java.awt.event.FocusEvent e)
Specified by:
focusGained in interface java.awt.event.FocusListener

focusLost

public void focusLost(java.awt.event.FocusEvent e)
Specified by:
focusLost in interface java.awt.event.FocusListener

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
this method calls mouseClicked and schedules a task to call either single, double, triple or many click

Specified by:
mouseClicked in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Specified by:
mouseExited in interface java.awt.event.MouseListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Specified by:
mousePressed in interface java.awt.event.MouseListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Specified by:
mouseReleased in interface java.awt.event.MouseListener

valueChanged

public void valueChanged(javax.swing.event.ListSelectionEvent e)
Specified by:
valueChanged in interface javax.swing.event.ListSelectionListener

stateChanged

public void stateChanged(javax.swing.event.ChangeEvent e)
Specified by:
stateChanged in interface javax.swing.event.ChangeListener

propertyChange

public void propertyChange(java.beans.PropertyChangeEvent e)
Specified by:
propertyChange in interface java.beans.PropertyChangeListener

vetoableChange

public void vetoableChange(java.beans.PropertyChangeEvent e)
                    throws java.beans.PropertyVetoException
Specified by:
vetoableChange in interface java.beans.VetoableChangeListener
Throws:
java.beans.PropertyVetoException

valueChanged

public void valueChanged(javax.swing.event.TreeSelectionEvent e)
Specified by:
valueChanged in interface javax.swing.event.TreeSelectionListener

itemStateChanged

public void itemStateChanged(java.awt.event.ItemEvent e)
Specified by:
itemStateChanged in interface java.awt.event.ItemListener


Copyright © 2010 KnowledgePlaza. All Rights Reserved.