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 org.eclipse.swt.widgets.Listener;
14 
15 import java.lang.all;
16 
17 import org.eclipse.swt.widgets.Event;
18 
19 version(Tango){
20     import tango.core.Traits;
21     import tango.core.Tuple;
22 } else { // Phobos
23     import std.traits;
24     import std.typetuple;
25 }
26 
27 /**
28  * Implementers of <code>Listener</code> provide a simple
29  * <code>handleEvent()</code> method that is used internally
30  * by SWT to dispatch events.
31  * <p>
32  * After creating an instance of a class that implements this interface
33  * it can be added to a widget using the
34  * <code>addListener(int eventType, Listener handler)</code> method and
35  * removed using the
36  * <code>removeListener (int eventType, Listener handler)</code> method.
37  * When the specified event occurs, <code>handleEvent(...)</code> will
38  * be sent to the instance.
39  * </p>
40  * <p>
41  * Classes which implement this interface are described within SWT as
42  * providing the <em>untyped listener</em> API. Typically, widgets will
43  * also provide a higher-level <em>typed listener</em> API, that is based
44  * on the standard <code>java.util.EventListener</code> pattern.
45  * </p>
46  * <p>
47  * Note that, since all internal SWT event dispatching is based on untyped
48  * listeners, it is simple to build subsets of SWT for use on memory
49  * constrained, small footprint devices, by removing the classes and
50  * methods which implement the typed listener API.
51  * </p>
52  *
53  * @see Widget#addListener
54  * @see java.util.EventListener
55  * @see org.eclipse.swt.events
56  */
57 public interface Listener {
58 
59 /**
60  * Sent when an event that the receiver has registered for occurs.
61  *
62  * @param event the event which occurred
63  */
64 void handleEvent (Event event);
65 }
66 
67 
68 /// Helper class for the dgListener template function
69 private class _DgListenerT(Dg,T...) : Listener {
70 
71     version(Tango){
72         alias ParameterTupleOf!(Dg) DgArgs;
73         static assert( is(DgArgs == Tuple!(Event,T)),
74             "Delegate args not correct: delegate args: ("~
75             DgArgs.stringof~
76             ") vs. passed args: ("~
77             Tuple!(Event,T).stringof~")" );
78     } else { // Phobos
79         alias ParameterTypeTuple!(Dg) DgArgs;
80         static assert( is(DgArgs == TypeTuple!(Event,T)),
81             "Delegate args not correct: delegate args: ("~
82             DgArgs.stringof~
83             ") vs. passed args: ("~
84             TypeTuple!(Event,T).stringof~")" );
85     }
86 
87 
88     Dg dg;
89     T  t;
90 
91     private this( Dg dg, T t ){
92         this.dg = dg;
93         static if( T.length > 0 ){
94             this.t = t;
95         }
96     }
97 
98     void handleEvent( Event e ){
99         dg(e,t);
100     }
101 }
102 
103 /++
104  + dgListener creates a class implementing the Listener interface and delegating the call to
105  + handleEvent to the users delegate. This template function will store also additional parameters.
106  +
107  + Examle of usage:
108  + ---
109  + void handleTextEvent (Event e, int inset ) {
110  +     // ...
111  + }
112  + text.addListener (SWT.FocusOut, dgListener( &handleTextEvent, inset ));
113  + ---
114  +/
115 Listener dgListener( Dg, T... )( Dg dg, T args ){
116     return new _DgListenerT!( Dg, T )( dg, args );
117 }
118 
119 
120