org.tbee.swing
Class SwingEventDispatcher

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

public class SwingEventDispatcher
extends Object
implements ActionListener, FocusListener, MouseListener, ListSelectionListener, ChangeListener, PropertyChangeListener, VetoableChangeListener, TreeSelectionListener, 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 String SOURCECODE_VERSION
          Standard variable for determining version of a class file.
 
Constructor Summary
SwingEventDispatcher(Component component, Object listener)
          Initialize
SwingEventDispatcher(Component component, Object listener, Map values)
          Initialize
SwingEventDispatcher(Component component, String name, Object listener)
          Convenience constructor: set the name of the component and then initialize.
SwingEventDispatcher(Component component, String name, Object listener, Map values)
          Convenience constructor: set the name of the component and then initialize.
 
Method Summary
 void actionPerformed(ActionEvent e)
           
 void focusGained(FocusEvent e)
           
 void focusLost(FocusEvent e)
           
 void itemStateChanged(ItemEvent e)
           
 void mouseClicked(MouseEvent e)
          this method calls mouseClicked and schedules a task to call either single, double, triple or many click
 void mouseEntered(MouseEvent e)
           
 void mouseExited(MouseEvent e)
           
 void mousePressed(MouseEvent e)
           
 void mouseReleased(MouseEvent e)
           
 void propertyChange(PropertyChangeEvent e)
           
 void stateChanged(ChangeEvent e)
           
 void valueChanged(ListSelectionEvent e)
           
 void valueChanged(TreeSelectionEvent e)
           
 void vetoableChange(PropertyChangeEvent e)
           
static Component wrap(Component component, String name, 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 Component wrap(Component component, String name, Object listener, 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 String SOURCECODE_VERSION
Standard variable for determining version of a class file.

See Also:
Constant Field Values
Constructor Detail

SwingEventDispatcher

public SwingEventDispatcher(Component component,
                            String name,
                            Object listener,
                            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(Component component,
                            String name,
                            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(Component component,
                            Object listener,
                            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(Component component,
                            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 Component wrap(Component component,
                             String name,
                             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 Component wrap(Component component,
                             String name,
                             Object listener,
                             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(ActionEvent e)
Specified by:
actionPerformed in interface ActionListener

focusGained

public void focusGained(FocusEvent e)
Specified by:
focusGained in interface FocusListener

focusLost

public void focusLost(FocusEvent e)
Specified by:
focusLost in interface FocusListener

mouseClicked

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

Specified by:
mouseClicked in interface MouseListener

mouseEntered

public void mouseEntered(MouseEvent e)
Specified by:
mouseEntered in interface MouseListener

mouseExited

public void mouseExited(MouseEvent e)
Specified by:
mouseExited in interface MouseListener

mousePressed

public void mousePressed(MouseEvent e)
Specified by:
mousePressed in interface MouseListener

mouseReleased

public void mouseReleased(MouseEvent e)
Specified by:
mouseReleased in interface MouseListener

valueChanged

public void valueChanged(ListSelectionEvent e)
Specified by:
valueChanged in interface ListSelectionListener

stateChanged

public void stateChanged(ChangeEvent e)
Specified by:
stateChanged in interface ChangeListener

propertyChange

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

vetoableChange

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

valueChanged

public void valueChanged(TreeSelectionEvent e)
Specified by:
valueChanged in interface TreeSelectionListener

itemStateChanged

public void itemStateChanged(ItemEvent e)
Specified by:
itemStateChanged in interface ItemListener


Copyright © 2012 KnowledgePlaza. All Rights Reserved.