[java-gnome-hackers] Event Handling proposal
Brought to you by:
afcowie
From: Mark H. <mh...@ti...> - 2002-09-03 17:51:44
|
Hi, sorry for not suggesting this earlier - I was still learning how Java-gnome worked back then.=20 I don't like the way event type checking is done: if (event.getId() =3D=3D 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 =3D=3D source) throw new IllegalArgumentException("null source"); this.source =3D source; this.type =3D type; } public Object getSource() { return source; } public int getType() { return type; } public String toString() { return getClass().getName() + "[source=3D" + source + ",id=3D" + 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 =3D typeID; name =3D typeName; } } Sample Event (ButtonEvent.java): public class ButtonEvent extends GtkEvent {=09 public static class Type extends GtkEventType{ public static final ButtonEventType ACTIVATE =3D new ButtonEvent(1, "ACTIVATE"); public static final ButtonEventType CLICK =3D new ButtonEvent(2, "CLICK"); public static final ButtonEventType ENTER =3D new ButtonEvent(3, "ENTER"); public static final ButtonEventType LEAVE =3D new ButtonEvent(4, "Leave"); public static final ButtonEventType PRESS =3D new ButtonEvent(5, "PRESS"); public static final ButtonEventType RELEASE =3D 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 =3D=3D 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.=20 I think this should be done for the simple reason that=20 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.=20 Let me know your thoughts. --=20 +----------------------------------------------+ | Mark Howard cam.ac.uk mh344@ | | http://www.tildemh.com tildemh.com mh@ | +----------------------------------------------+ |