root/dwt/widgets/Listener.d

Revision 311:02332a154347, 3.1 kB (checked in by Frank Benoit <benoit@tionex.de>, 3 months ago)

Improved Listeners access functions.

Line 
1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  * Port to the D programming language:
11  *     Frank Benoit <benoit@tionex.de>
12  *******************************************************************************/
13 module dwt.widgets.Listener;
14
15 import dwt.widgets.Event;
16
17 import tango.core.Traits;
18 import tango.core.Tuple;
19
20 /**
21  * Implementers of <code>Listener</code> provide a simple
22  * <code>handleEvent()</code> method that is used internally
23  * by DWT to dispatch events.
24  * <p>
25  * After creating an instance of a class that implements this interface
26  * it can be added to a widget using the
27  * <code>addListener(int eventType, Listener handler)</code> method and
28  * removed using the
29  * <code>removeListener (int eventType, Listener handler)</code> method.
30  * When the specified event occurs, <code>handleEvent(...)</code> will
31  * be sent to the instance.
32  * </p>
33  * <p>
34  * Classes which implement this interface are described within DWT as
35  * providing the <em>untyped listener</em> API. Typically, widgets will
36  * also provide a higher-level <em>typed listener</em> API, that is based
37  * on the standard <code>java.util.EventListener</code> pattern.
38  * </p>
39  * <p>
40  * Note that, since all internal DWT event dispatching is based on untyped
41  * listeners, it is simple to build subsets of DWT for use on memory
42  * constrained, small footprint devices, by removing the classes and
43  * methods which implement the typed listener API.
44  * </p>
45  *
46  * @see Widget#addListener
47  * @see java.util.EventListener
48  * @see dwt.events
49  */
50 public interface Listener {
51
52 /**
53  * Sent when an event that the receiver has registered for occurs.
54  *
55  * @param event the event which occurred
56  */
57 void handleEvent (Event event);
58 }
59
60
61 /// Helper class for the dgListener template function
62 private class _DgListenerT(Dg,T...) : Listener {
63
64     alias ParameterTupleOf!(Dg) DgArgs;
65     static assert( is(DgArgs == Tuple!(Event,T)),
66                 "Delegate args not correct: delegate args: ("~DgArgs.stringof~") vs. passed args: ("~Tuple!(Event,T).stringof~")" );
67
68     Dg dg;
69     T  t;
70
71     private this( Dg dg, T t ){
72         this.dg = dg;
73         static if( T.length > 0 ){
74             this.t = t;
75         }
76     }
77
78     void handleEvent( Event e ){
79         dg(e,t);
80     }
81 }
82
83 /++
84  + dgListener creates a class implementing the Listener interface and delegating the call to
85  + handleEvent to the users delegate. This template function will store also additional parameters.
86  +
87  + Examle of usage:
88  + ---
89  + void handleTextEvent (Event e, int inset ) {
90  +     // ...
91  + }
92  + text.addListener (DWT.FocusOut, dgListener( &handleTextEvent, inset ));
93  + ---
94  +/
95 Listener dgListener( Dg, T... )( Dg dg, T args ){
96     return new _DgListenerT!( Dg, T )( dg, args );
97 }
Note: See TracBrowser for help on using the browser.