Event Handling Event Issues Event Dispatch Bottomup TopDown
Event Handling
Event Issues • Event Dispatch – Bottom-up – Top-Down • Windowing system must handle events for any application • Windowing system may or may not assume compiler cooperation • Design-time vs. Runtime
Event Handling Properties Ease of programming Efficiency of event handling Code reliability Support for IDE (Interactive Design Environment) • Scalable to large user interfaces • •
Event Things to Know • For each approach – How it Works – Advantages – Disadvantages
Event Tables 0 Mouse. Up() 1 Mouse. Down() 0 Mouse. Up() 2 Mouse. Move() …… 1 Mouse. Down() 2 Mouse. Move() …… • Advantages – Simple to implement – Each window is its own system • Disadvantages – Debugging mistakes – No IDE help
Window. Proc • Advantages – Simple – Window modular – Primitive inheritance • Disadvantages – No IDE help – Debugging issues again void root. Window. Proc(Event, Window. Data) { switch(Event. type) {. . . } void a. Window. Proc(Event, Window. Data) } { switch(Event. type) {. . . } } void some. WProc(Event, Window. Data) { switch(Event. type) {. . . default: super. WProc(. . . ) } }
Class Inheritance public class Widget { public void mouse. Down(int x, int y, int button) { default behavior } public void mouse. Up(int x, int y, int button) { default behavior } public void key. Input(char key) { default behavior }. . . } public class Pig. Tickler extends Widget { public void mouse. Down(int x, int y, int button) { poke the pig } public void mouse. Up(int x, int y, int button) { wiggle your finger } public void key. Input(char key) { rename the pig}. . . }
Window -> Object : Widget Pigs: Dual. Pane 1 : Pig. Tickler Scroll 1 : Scroll. Bar Pane 2 : Pig. Tickler Scroll 2 : Scroll. Bar Mouse. Down -> selected. Window. my. Object. Mouse. Down( new Mouse. Event(…. . ) )
Class Inheritance Virtual Table 0: mouse. Down 1: mouse. Up 2: key. Input 3: . . . . public class Widget { public void mouse. Down(int x, int y, int button) { default behavior } public void mouse. Up(int x, int y, int button) { default behavior } public void key. Input(char key) { default behavior }. . . } public class Pig. Tickler extends Widget { public void mouse. Down(int x, int y, int button) { poke the pig } public void mouse. Up(int x, int y, int button) { wiggle your finger } public void key. Input(char key) { rename the pig}. . . }
Window -> Object : Widget Pigs: Dual. Pane 0: 1: Pane 1 : Pig. Tickler 0: 1: Pane 2 : Pig. Tickler Scroll 1 : Scroll. Bar 0: 1: Scroll 2 : Scroll. Bar Mouse. Down -> selected. Window. my. Object. Mouse. Down( new Mouse. Event(…. . ) )
Class Inheritance public class Widget { public void mouse. Down(int x, int y, int button) { default behavior } public void mouse. Up(int x, int y, int button) { default behavior } public void key. Input(char key) { default behavior }. . . } • Advantages – Language assistance – Debugging – Modular • Disadvantages – Virtual table size – Inheritance restrictions (see scroll bar handling)
Listeners class Cool. Event { information about the event } interface Cool. Listener { void cool. Happened(Cool. Event e); } • Advantages class Do. Cool. Stuff { private Vector<Cool. Listener> listeners; public void add. Cool. Listener(Cool. Listener c) { listeners. add(c); } public void rem. Cool. Listener(Cool. Listener c) { listeners. remove(c); } protected void cool. Happened(Cool. Event e) { for (Cool. Listener c: listeners) { c. cool. Happened(e); } } } – Modularity of event types – Separation from inheritance – Reflection in the IDE • Disadvantages – All events of a particular type go the same place
class Scroll. Bar extends Widget { private Vector<Scroll. Listener> listeners; public void add. Scroll. Listener(Scroll. Listener c) { listeners. add(c); } public void rem. Scroll. Listener(Scroll. Listener c) { listeners. remove(c); } protected void scroll. Happened(Scroll. Event e) { for (Scroll. Listener c: listeners) { c. scroll. Happened(e); } } }
class Scroll. Bar extends Widget { private Vector<Scroll. Listener> listeners; public void add. Scroll. Listener(Scroll. Listener c) { listeners. add(c); } public void rem. Scroll. Listener(Scroll. Listener c) { listeners. remove(c); } protected void scroll. Happened(Scroll. Event e) { for (Scroll. Listener c: listeners) { c. scroll. Happened(e); } } } class Browser extends Widget implements Scroll. Listener { public void scroll. Happened(Scroll. Event e) { if (e. is. Vertical() ) scroll vertical; else scroll horizontal; } }
class Scroll. Bar extends Widget { private Vector<Scroll. Listener> listeners; public void add. Scroll. Listener(Scroll. Listener c) { listeners. add(c); } public void rem. Scroll. Listener(Scroll. Listener c) { listeners. remove(c); } protected void scroll. Happened(Scroll. Event e) { for (Scroll. Listener c: listeners) { c. scroll. Happened(e); } } } class Firefox { public void main(String args[]) { h. Scroll=new Scroll. Bar(); v. Scroll=new Scroll. Bar(); browse = new Browser(); h. Scroll. add. Scroll. Listener(browse); v. Scroll. add. Scroll. Listener(browse); } } class Browser extends Widget implements Scroll. Listener { public void scroll. Happened(Scroll. Event e) { if (e. is. Vertical() ) scroll vertical; else scroll horizontal; } }
Listeners class Cool. Event { information about the event } interface Cool. Listener { void cool. Happened(Cool. Event e); } • Advantages class Do. Cool. Stuff { private Vector<Cool. Listener> listeners; public void add. Cool. Listener(Cool. Listener c) { listeners. add(c); } public void rem. Cool. Listener(Cool. Listener c) { listeners. remove(c); } protected void cool. Happened(Cool. Event e) { for (Cool. Listener c: listeners) { c. cool. Happened(e); } } } – Modularity of event types – Separation from inheritance – Reflection in the IDE • Disadvantages – All events of a particular type go the same place
class Scroll. Bar extends Widget { private Vector<Scroll. Listener> listeners; public void add. Scroll. Listener(Scroll. Listener c) { listeners. add(c); } public void rem. Scroll. Listener(Scroll. Listener c) { listeners. remove(c); } protected void scroll. Happened(Scroll. Event e) { for (Scroll. Listener c: listeners) { c. scroll. Happened(e); } } } class Firefox { public void main(String args[]) { h. Scroll=new Scroll. Bar(); v. Scroll=new Scroll. Bar(); browse = new Browser(); h. Scroll. add. Scroll. Listener(browse); v. Scroll. add. Scroll. Listener(browse); } } class Browser extends Widget implements Scroll. Listener { public void scroll. Happened(Scroll. Event e) { if ( e. is. Vertical() ) scroll vertical; else scroll horizontal; } }
Delegates public delegate void cool. Event. Handler(Cool. Event e) public class Do. Cool. Stuff { public cool. Event. Handler handler; public void process. Mouse. Event(Event e) { handler(new Cool. Event() ); . . . } } public class Bunch. Of. Coolness { public Bunch. Of. Coolness() { Do. Cool. Stuff cool 1; cool 1. handler = catch 1; Do. Cool. Stuff cool 2; coo 12. handler=catch 2; } private void catch 2(Cool. Event e) {. . . } private void catch 1(Cool. Event e) {. . . . } }
public delegate Scroll. Handler(Scroll. Event); class Scroll. Bar extends Widget { public Scroll. Handler scroll. Event; }
public delegate Scroll. Handler(Scroll. Event); class Scroll. Bar extends Widget { public Scroll. Handler scroll. Event; } class Browser extends Widget implements Scroll. Listener { public void scroll. Vertical(Scroll. Event e) {…} } public void scroll. Horizontal(Scroll. Event e) {…}
public delegate Scroll. Handler(Scroll. Event); class Scroll. Bar extends Widget { public Scroll. Handler scroll. Event; } class Firefox { public void main(String args[]) { h. Scroll=new Scroll. Bar(); v. Scroll=new Scroll. Bar(); browse = new Browser(); } } h. Scroll. scroll. Event = browse. scroll. Horizontal; v. Scroll. scroll. Event = browse. scroll. Vertical; class Browser extends Widget implements Scroll. Listener { public void scroll. Vertical(Scroll. Event e) {…} } public void scroll. Horizontal(Scroll. Event e) {…}
Delegates public delegate void cool. Event. Handler(Cool. Event e) public class Do. Cool. Stuff { public cool. Event. Handler handler; public void process. Mouse. Event(Event e) { handler(new Cool. Event() ); . . . } } • Advantages – Each event targeted – Some IDE help • Disadvantages –? public class Bunch. Of. Coolness { public Bunch. Of. Coolness() { Do. Cool. Stuff cool 1; cool 1. handler = catch 1; Do. Cool. Stuff cool 2; coo 12. handler=catch 2; } private void catch 2(Cool. Event e) {. . . } private void catch 1(Cool. Event e) {. . . . } }
Interpretive Language • <div onclick=“my. Div. Clicked(A+3)” > …</div> – eval(onclick) – Variable bindings
Anonymous Functions • <div onclick=function(X, Y){ return A+X; } > …</div> – Variables where function is created – onclick(X, Y) somewhere inside of <div>
Event Handling • • • Event Queue and switch on event type Event Tables Class Inheritance Listeners Delegates
- Slides: 26