javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/KOFunction.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 15 Apr 2013 18:30:30 +0200
branchfx
changeset 990 9ddce13e8ff9
permissions -rw-r--r--
Can call @OnFunction
jaroslav@990
     1
/**
jaroslav@990
     2
 * Back 2 Browser Bytecode Translator
jaroslav@990
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@990
     4
 *
jaroslav@990
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@990
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@990
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@990
     8
 *
jaroslav@990
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@990
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@990
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@990
    12
 * GNU General Public License for more details.
jaroslav@990
    13
 *
jaroslav@990
    14
 * You should have received a copy of the GNU General Public License
jaroslav@990
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@990
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@990
    17
 */
jaroslav@990
    18
package org.apidesign.bck2brwsr.htmlpage;
jaroslav@990
    19
jaroslav@990
    20
import java.lang.reflect.Method;
jaroslav@990
    21
import java.util.logging.Level;
jaroslav@990
    22
import java.util.logging.Logger;
jaroslav@990
    23
jaroslav@990
    24
/** Represents callback from Knockout.js to Java.
jaroslav@990
    25
 *
jaroslav@990
    26
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@990
    27
 */
jaroslav@990
    28
public final class KOFunction {
jaroslav@990
    29
    private static final Logger LOG = Logger.getLogger(KOFunction.class.getName());
jaroslav@990
    30
    private final Object model;
jaroslav@990
    31
    private final Method method;
jaroslav@990
    32
    
jaroslav@990
    33
    KOFunction(Object model, String method) throws NoSuchMethodException {
jaroslav@990
    34
        this.method = model.getClass().getDeclaredMethod(method, Object.class, Object.class);
jaroslav@990
    35
        this.method.setAccessible(true);
jaroslav@990
    36
        this.model = model;
jaroslav@990
    37
    }
jaroslav@990
    38
    
jaroslav@990
    39
    public void call(Object data, Object ev) {
jaroslav@990
    40
        try {
jaroslav@990
    41
            this.method.invoke(model, data, ev);
jaroslav@990
    42
        } catch (Throwable ex) {
jaroslav@990
    43
            LOG.log(Level.SEVERE, "Can't call " + method + " on " + model, ex);
jaroslav@990
    44
        }
jaroslav@990
    45
    }
jaroslav@990
    46
}