elemental.events.EventListener Java Examples

The following examples show how to use elemental.events.EventListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Connection.java    From ThinkMap with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a connect to the plugin at the address. Calls the callback once the connection succeeds.
 *
 * @param address
 *         The address to connect to, may include the port
 * @param handler
 *         The handler to handle received events
 * @param callback
 *         The Runnable to call once the connection is completed
 */
public Connection(String address, ServerPacketHandler handler, final Runnable callback) {
    this.address = address;
    this.handler = handler;
    webSocket = Browser.getWindow().newWebSocket("ws://" + address + "/server/ws");
    // Work in binary instead of strings
    webSocket.setBinaryType("arraybuffer");
    webSocket.setOnopen(new EventListener() {
        @Override
        public void handleEvent(Event evt) {
            System.out.println("Connected to server");
            send(new InitConnection());
            if (callback != null) callback.run();
        }
    });
    webSocket.setOnmessage(this);
}
 
Example #2
Source File: Renderer.java    From vertxui with GNU General Public License v3.0 5 votes vote down vote up
private static <T, V> void compareApplyRemove(Node element, T name, V value, What what) {
	// Fluent.console.log("removing " + name + " with " + value);
	switch (what) {
	case Attributes:
		switch ((Att) name) {
		case checked:
			((InputElement) element).setChecked(false);
			break;
		case value:
			((InputElement) element).setValue(null);
			break;
		case selectedIndex:
			// nothing to do
			// ((SelectElement) element).);
			break;
		default:
			((Element) element).removeAttribute(((Att) name).nameValid());
			break;
		}
		break;
	case Styles:
		((Element) element).getStyle().removeProperty(((Css) name).nameValid());
		break;
	case Listeners:
		element.removeEventListener((String) name, (EventListener) value);
		break;
	default:
		throw new IllegalArgumentException("Not possible");
	}
}
 
Example #3
Source File: Renderer.java    From vertxui with GNU General Public License v3.0 5 votes vote down vote up
private static <T, V> void compareApplySet(Node element, T name, V value, What what) {
	// Fluent.console.log("setting " + name + " with " + value);
	switch (what) {
	case Attributes:
		switch ((Att) name) {
		case checked:
			((InputElement) element).setChecked(true);
			break;
		case value:
			((InputElement) element).setValue((String) value);
			break;
		case selectedIndex:
			((SelectElement) element).setSelectedIndex(Integer.parseInt((String) value));
		default:
			((Element) element).setAttribute(((Att) name).nameValid(), (String) value);
			break;
		}
		break;
	case Styles:
		((Element) element).getStyle().setProperty(((Css) name).nameValid(), (String) value);
		break;
	case Listeners:
		element.addEventListener((String) name, (EventListener) value);
		break;
	default:
		throw new IllegalArgumentException("Not possible");
	}
}
 
Example #4
Source File: FluentBase.java    From vertxui with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Add or remove (by value null) an eventlistener.
 * 
 * @param name
 *            the name that should be listening too
 * @param value
 *            the low level eventlistener
 * @return this
 */
public Fluent listen(String name, EventListener value) {
	if (listeners == null) {
		listeners = new TreeMap<>();
	}
	EventListener oldValue = listeners.get(name);

	if (value != null) { // set it

		// if does not exist yet or has a different value
		if (oldValue == null || !oldValue.equals(value)) {
			listeners.put(name, value);
			if (element != null) { // if visual
				element.addEventListener(name, value);
			}
		}

	} else { // remove it

		// if old value exists
		if (oldValue != null) {
			listeners.remove(name);
			if (element != null) { // if visual
				element.removeEventListener(name, oldValue);
			}
		}
	}
	return (Fluent) this;
}
 
Example #5
Source File: ClientWorld.java    From ThinkMap with Apache License 2.0 5 votes vote down vote up
/**
 * Triggers an async request to load the chunk. The chunk is forwarded to all workers to be processed before being returned to the client
 *
 * @param x
 *         The chunk x position
 * @param z
 *         The chunk z position
 */
private void loadChunk(final int x, final int z) {
    final String key = chunkKey(x, z);
    if (loadingChunks.contains(key) || isLoaded(x, z)) {
        return;
    }
    final XMLHttpRequest xmlHttpRequest = Browser.getWindow().newXMLHttpRequest();
    xmlHttpRequest.open("POST", "http://" + mapViewer.getConnection().getAddress() + "/server/chunk", true);
    xmlHttpRequest.setResponseType("arraybuffer");
    xmlHttpRequest.setOnreadystatechange(new EventListener() {
        @Override
        public void handleEvent(Event evt) {
            if (xmlHttpRequest.getReadyState() != 4) return;
            if (xmlHttpRequest.getStatus() == 200) {
                // Got the chunk successfully, move on
                // to processing the chunk
                ArrayBuffer data = (ArrayBuffer) xmlHttpRequest.getResponse();
                ViewBuffer dataStream = JavascriptViewBuffer.create(data, false, 0, data.getByteLength());
                if (dataStream.getInt8(0) == 0) {
                    loadingChunks.remove(key);
                    return;
                }
                UByteBuffer sendableData = JavascriptUByteBuffer.create(data, 0, data.getByteLength());
                mapViewer.getWorkerPool().sendMessage(new ChunkLoadMessage(x, z, sendableData), true);
            } else {
                // Request failed (e.g. non-existing chunk)
                // remove from the loadingChunks set so
                // that it may be tried again
                loadingChunks.remove(key);
            }
        }
    });
    xmlHttpRequest.send(key);
}
 
Example #6
Source File: Elements.java    From core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Adds the given event listener to the the last added element.
 */
public Builder on(EventType type, EventListener listener) {
    assertCurrent();

    Element element = elements.peek().element;
    type.register(element, listener);
    return this;
}
 
Example #7
Source File: EventBus.java    From vertxui with GNU General Public License v3.0 4 votes vote down vote up
public final native void onopen(EventListener listener)/*-{
this.onopen = @elemental.js.dom.JsElementalMixinBase::getHandlerFor(Lelemental/events/EventListener;)(listener);		
}-*/;
 
Example #8
Source File: EventBus.java    From vertxui with GNU General Public License v3.0 4 votes vote down vote up
public final native void onclose(EventListener listener) /*-{
this.onclose = @elemental.js.dom.JsElementalMixinBase::getHandlerFor(Lelemental/events/EventListener;)(listener);
}-*/;
 
Example #9
Source File: EventBus.java    From vertxui with GNU General Public License v3.0 4 votes vote down vote up
public final native void onerror(EventListener listener) /*-{
this.onerror = @elemental.js.dom.JsElementalMixinBase::getHandlerFor(Lelemental/events/EventListener;)(listener);
}-*/;
 
Example #10
Source File: Worker.java    From ThinkMap with Apache License 2.0 4 votes vote down vote up
private native void setOnMessage(EventListener eventListener)/*-{
    self.onmessage = @elemental.js.dom.JsElementalMixinBase::getHandlerFor(Lelemental/events/EventListener;)(eventListener);
}-*/;
 
Example #11
Source File: FluentBase.java    From vertxui with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Get the eventlistener for the given eventname.
 * 
 * @param event
 *            event name
 * @return the event listener
 */
public EventListener listen(String event) {
	if (listeners == null) {
		return null;
	}
	return listeners.get(event);
}
 
Example #12
Source File: FluentBase.java    From vertxui with GNU General Public License v3.0 2 votes vote down vote up
/**
 * A convenient helper method for this event listener.
 * 
 * @param listener
 *            the listener
 * @return this
 */
public Fluent load(EventListener listener) {
	return listen("LOAD", listener);
}
 
Example #13
Source File: FluentBase.java    From vertxui with GNU General Public License v3.0 2 votes vote down vote up
/**
 * A convenient helper method for this event listener.
 * 
 * @param listener
 *            the listener
 * @return this
 */
public Fluent focus(EventListener listener) {
	return listen(Event.FOCUS, listener);
}
 
Example #14
Source File: EventRegistrar.java    From core with GNU Lesser General Public License v2.1 votes vote down vote up
void register(Element element, EventListener listener);