javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/OnController.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 24 Feb 2015 11:12:53 +0100
changeset 1787 ea12a3bb4b33
parent 813 2fa85847ccf7
permissions -rw-r--r--
Using year range 2012-2015 in copyright header
jaroslav@435
     1
/**
jaroslav@435
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1787
     3
 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@435
     4
 *
jaroslav@435
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@435
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@435
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@435
     8
 *
jaroslav@435
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@435
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@435
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@435
    12
 * GNU General Public License for more details.
jaroslav@435
    13
 *
jaroslav@435
    14
 * You should have received a copy of the GNU General Public License
jaroslav@435
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@435
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@435
    17
 */
jaroslav@435
    18
package org.apidesign.bck2brwsr.htmlpage.api;
jaroslav@435
    19
jaroslav@435
    20
/** Controller created via {@link OnEvent#of(org.apidesign.bck2brwsr.htmlpage.api.Element[])}.
jaroslav@435
    21
 *
jaroslav@435
    22
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@435
    23
 */
jaroslav@435
    24
public final class OnController {
jaroslav@435
    25
    private final Element[] arr;
jaroslav@435
    26
    private final OnEvent event;
jaroslav@435
    27
    
jaroslav@435
    28
    OnController(OnEvent event, Element[] arr) {
jaroslav@435
    29
        this.event = event;
jaroslav@435
    30
        this.arr = arr;
jaroslav@435
    31
    }
jaroslav@435
    32
    
jaroslav@813
    33
    /** Registers an event handler on associated {@link OnEvent}
jaroslav@813
    34
     * and {@link Element}
jaroslav@813
    35
     * 
jaroslav@813
    36
     * @param handler the handler to be called when the event occurs
jaroslav@813
    37
     */
jaroslav@813
    38
    public void perform(final OnHandler handler) {
jaroslav@813
    39
        for (Element e : arr) {
jaroslav@813
    40
            e.on(event, handler);
jaroslav@813
    41
        }
jaroslav@813
    42
    }
jaroslav@813
    43
    
jaroslav@435
    44
    /** Registers a runnable to be performed on associated {@link OnEvent} 
jaroslav@435
    45
     * and {@link Element}.
jaroslav@435
    46
     * 
jaroslav@435
    47
     * @see OnEvent#of
jaroslav@435
    48
     */
jaroslav@813
    49
    public void perform(final Runnable r) {
jaroslav@813
    50
        class W implements OnHandler {
jaroslav@813
    51
            @Override
jaroslav@813
    52
            public void onEvent(Object event) throws Exception {
jaroslav@813
    53
                r.run();
jaroslav@813
    54
            }
jaroslav@813
    55
        }
jaroslav@813
    56
        perform(new W());
jaroslav@813
    57
        OnHandler w = new W();
jaroslav@435
    58
        for (Element e : arr) {
jaroslav@813
    59
            e.on(event, w);
jaroslav@435
    60
        }
jaroslav@435
    61
    }
jaroslav@435
    62
}