RE: [java-gnome-hackers] Event Handling proposal
Brought to you by:
afcowie
From: Jeffrey M. <Jef...@Br...> - 2002-09-03 18:02:39
|
I like this idea. -Jeff > Hi, > sorry for not suggesting this earlier - I was still learning how > Java-gnome worked back then. > > I don't like the way event type checking is done: > if (event.getId() == KeyEvent.PRESSED){ > I think this would be better done something like: > if (event.isOfType(KeyEvent.Type.PRESSED) ){ > > This: > - ensures a type is used, not any arbitrary integer > - is immediately obvious to the developer > - is readable > - has better toString support (see below) > > > I have created the following sources to show how I propose it > should be > done: > > GtkEvent.java: > public class GtkEvent implements Serializable { > protected GtkEventType type; > protected Object source; > > public GtkEvent(Object source, GtkEventType type) { > if (null == source) > throw new IllegalArgumentException("null source"); > this.source = source; > this.type = type; > } > public Object getSource() { > return source; > } > public int getType() { > return type; > } > public String toString() { > return getClass().getName() + "[source=" + source + ",id=" + > type.getName() + "]"; > } > } > > GtkEventType.java: > public class GtkEventType{ > private int id; > private String name; > protected int getID(){ > return id; > } > protected String getName(){ > return name; > } > protected GtkEventType(int typeID, String typeName){ > id = typeID; > name = typeName; > } > } > > Sample Event (ButtonEvent.java): > public class ButtonEvent extends GtkEvent { > public static class Type extends GtkEventType{ > public static final ButtonEventType ACTIVATE = new ButtonEvent(1, > "ACTIVATE"); > public static final ButtonEventType CLICK = new ButtonEvent(2, > "CLICK"); > public static final ButtonEventType ENTER = new ButtonEvent(3, > "ENTER"); > public static final ButtonEventType LEAVE = new ButtonEvent(4, > "Leave"); > public static final ButtonEventType PRESS = new ButtonEvent(5, > "PRESS"); > public static final ButtonEventType RELEASE = new ButtonEvent(6, > "RELEASE"); > // the strings were defined separately using the old method. > } > public interface Listener{ > public void buttonEvent(ButtonEvent event ); > } > public ButtonEvent(Object source, ButtonEvent.Type type) { > super(source, type); > } > public boolean ofType(ButtonEvent.Type type){ > return ( id == type.getID()); > } > } > > I have not done extensive testing of this (as it would > require changing > all the events and methods which use them), but I see no reason why it > shouldn't work - we are still dealing with exactly the same data. > > I think this should be done for the simple reason that > event.isOfType( ButtonEvent.Type.CLICK ) > is far more obvious to the developer (and it is readable) > > The question of Type and Listener being done as inner classes is > debatable. Perhaps listener should be separate. > > I am prepared to make the necessary changes to all the currently > implemented events and classes which use these. > > Let me know your thoughts. > -- > > +----------------------------------------------+ > | Mark Howard cam.ac.uk mh344@ | > | http://www.tildemh.com tildemh.com mh@ | > +----------------------------------------------+ > |