Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

root/dwt/widgets/Listener.d

Revision 243:84629474b5ec, 3.0 kB (checked in by Frank Benoit <benoit@tionex.de>, 17 years ago)

Add dgListener template function for the Listener class

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 /// Helper class for the dgListener template function
61 private class _DgListenerT(Dg,T...) : Listener {
62
63     alias ParameterTupleOf!(Dg) DgArgs;
64     static assert( is(DgArgs == Tuple!(Event,T)),
65                 "Delegate args not correct" );
66
67     Dg dg;
68     T  t;
69
70     private this( Dg dg, T t ){
71         this.dg = dg;
72         static if( T.length > 0 ){
73             this.t = t;
74         }
75     }
76
77     void handleEvent( Event e ){
78         dg(e,t);
79     }
80 }
81
82 /++
83  + dgListener creates a class implementing the Listener interface and delegating the call to
84  + handleEvent to the users delegate. This template function will store also additional parameters.
85  +
86  + Examle of usage:
87  + ---
88  + void handleTextEvent (Event e, int inset ) {
89  +     // ...
90  + }
91  + text.addListener (DWT.FocusOut, dgListener( &handleTextEvent, inset ));
92  + ---
93  +/
94 Listener dgListener( Dg, T... )( Dg dg, T args ){
95     return new _DgListenerT!( Dg, T )( dg, args );
96 }
Note: See TracBrowser for help on using the browser.