json/src/main/java/org/apidesign/html/json/impl/RcvrJSON.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 25 Aug 2013 17:41:21 +0200
changeset 262 30b03f2c82af
parent 255 62f7dafef4cf
child 358 80702021b851
permissions -rw-r--r--
All callback calls are rescheduled to browser thread automatically
     1 /**
     2  * HTML via Java(tm) Language Bindings
     3  * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details. apidesign.org
    13  * designates this particular file as subject to the
    14  * "Classpath" exception as provided by apidesign.org
    15  * in the License file that accompanied this code.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program. Look for COPYING file in the top folder.
    19  * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    20  */
    21 package org.apidesign.html.json.impl;
    22 
    23 import net.java.html.BrwsrCtx;
    24 
    25 /** Super type for those who wish to receive JSON messages.
    26  *
    27  * @author Jaroslav Tulach <jtulach@netbeans.org>
    28  */
    29 public abstract class RcvrJSON {
    30     protected void onOpen(MsgEvnt msg) {}
    31     protected abstract void onMessage(MsgEvnt msg);
    32     protected void onClose(MsgEvnt msg) {}
    33     protected abstract void onError(MsgEvnt msg);
    34     
    35     public abstract static class MsgEvnt {
    36         MsgEvnt() {
    37         }
    38         
    39         public Throwable getError() {
    40             return null;
    41         }
    42         
    43         public final Exception getException() {
    44             Throwable t = getError();
    45             if (t instanceof Exception) {
    46                 return (Exception)t;
    47             }
    48             if (t == null) {
    49                 return null;
    50             }
    51             return new Exception(t);
    52         }
    53         
    54         public int dataSize() {
    55             return -1;
    56         }
    57         
    58         public <Data> void dataRead(BrwsrCtx ctx, Class<? extends Data> type, Data[] fillTheArray) {
    59         }
    60 
    61         public abstract void dispatch(RcvrJSON r);
    62         
    63         public static MsgEvnt createError(final Throwable t) {
    64             return new MsgEvnt() {
    65                 @Override
    66                 public Throwable getError() {
    67                     return t;
    68                 }
    69 
    70                 @Override
    71                 public void dispatch(RcvrJSON r) {
    72                     r.onError(this);
    73                 }
    74             };
    75         }
    76         
    77         public static MsgEvnt createMessage(final Object value) {
    78             return new MsgEvnt() {
    79                 @Override
    80                 public int dataSize() {
    81                     if (value instanceof Object[]) {
    82                         return ((Object[])value).length;
    83                     } else {
    84                         return 1;
    85                     }
    86                 }
    87                 
    88                 @Override
    89                 public <Data> void dataRead(BrwsrCtx context, Class<? extends Data> type, Data[] arr) {
    90                     if (value instanceof Object[]) {
    91                         Object[] data = ((Object[]) value);
    92                         for (int i = 0; i < data.length && i < arr.length; i++) {
    93                             arr[i] = org.apidesign.html.json.impl.JSON.read(context, type, data[i]);
    94                         }
    95                     } else {
    96                         if (arr.length > 0) {
    97                             arr[0] = org.apidesign.html.json.impl.JSON.read(context, type, value);
    98                         }
    99                     }
   100                 }
   101                 
   102                 @Override
   103                 public void dispatch(RcvrJSON r) {
   104                     r.onMessage(this);
   105                 }
   106             };
   107         }
   108         
   109         public static MsgEvnt createOpen() {
   110             return new MsgEvnt() {
   111                 @Override
   112                 public void dispatch(RcvrJSON r) {
   113                     r.onOpen(this);
   114                 }
   115             };
   116         }
   117 
   118         public static MsgEvnt createClose() {
   119             return new MsgEvnt() {
   120                 @Override
   121                 public void dispatch(RcvrJSON r) {
   122                     r.onClose(this);
   123                 }
   124             };
   125         }
   126     } // end MsgEvnt
   127 }