Example usage for com.google.gwt.user.client Timer Timer

List of usage examples for com.google.gwt.user.client Timer Timer

Introduction

In this page you can find the example usage for com.google.gwt.user.client Timer Timer.

Prototype

Timer

Source Link

Usage

From source file:ch.unifr.pai.twice.comm.serverPush.client.ServerPush.java

License:Apache License

private ServerPush(AtmosphereGWTSerializer serializer, final Command onConnected) {
    this.connectionTimeout = new Timer() {

        @Override//from   w  ww  .  j a  va  2s  .  com
        public void run() {
            if (!initialized) {
                onConnected.execute();
                initialized = true;
            }
        }
    };
    connectionTimeout.schedule(4000);
    this.atmosphereClient = new AtmosphereClient(
            GWT.getHostPageBaseURL() + Constants.BASEPATH + Constants.ATMOSPHERE, serializer,
            new AtmosphereListener() {

                @Override
                public void onConnected(int heartbeat, int connectionID) {
                    GWT.log("comet.connected [" + heartbeat + ", " + connectionID + "]");
                    connectionTimeout.cancel();
                    if (!initialized) {
                        onConnected.execute();
                        initialized = true;
                    }
                }

                @Override
                public void onBeforeDisconnected() {
                    GWT.log("comet.beforeDisconnected");
                }

                @Override
                public void onDisconnected() {
                    GWT.log("comet.disconnected");
                }

                @Override
                public void onError(Throwable exception, boolean connected) {
                    int statuscode = -1;
                    if (exception instanceof StatusCodeException) {
                        statuscode = ((StatusCodeException) exception).getStatusCode();
                    }
                    GWT.log("comet.error [connected=" + connected + "] (" + statuscode + ")"
                            + exception.getMessage());
                }

                @Override
                public void onHeartbeat() {
                    GWT.log("comet.heartbeat [" + atmosphereClient.getConnectionID() + "]");
                }

                @Override
                public void onRefresh() {
                    GWT.log("comet.refresh [" + atmosphereClient.getConnectionID() + "]");
                }

                @Override
                public void onAfterRefresh() {
                    GWT.log("comet.afterrefresh [" + atmosphereClient.getConnectionID() + "]");
                }

                @Override
                public void onMessage(List<?> messages) {
                    for (Object message : messages) {
                        if (message instanceof Serializable)
                            handleMessage((Serializable) message);
                    }
                }
            });
    this.atmosphereClient.start();
}

From source file:ch.unifr.pai.twice.dragndrop.client.MPDragNDrop.java

License:Apache License

/**
 * @see ch.unifr.pai.twice.dragndrop.client.DragNDrop.DragNDropIntf#makeDraggable(ch.unifr.pai.twice.dragndrop.client.intf.Draggable,
 *      ch.unifr.pai.twice.dragndrop.client.configuration.DragConfiguration, com.google.gwt.dom.client.Element)
 * /*from ww w  .  ja v a2s . c o  m*/
 *      Make the widget draggable by attaching a drag handler to it. If the drag handler is invoked, the logic checks if the widget is currently draggable
 *      (e.g. not locked by another user) and starts the drag by the invocation of
 *      {@link MPDragNDrop#startDrag(Widget, String, int, int, DragConfiguration, Element)} after making sure, that the delay defined by
 *      {@link MPDragNDrop#getDragDelayInMs()} is exceeded and it therefore is a valid drag
 * 
 */
@Override
public void makeDraggable(final Draggable w, final DragConfiguration conf, final Element dragProxyTemplate) {
    if (w instanceof Widget) {
        ((Widget) w).addStyleName("draggable");
        addDragHandler(w, new Callback<NativeEvent>() {
            @Override
            public void onDone(NativeEvent event) {
                if (w.isDraggable()) {
                    final String deviceId = getDeviceId(event);
                    final ValueHolder<Boolean> drag = new ValueHolder<Boolean>();
                    drag.setValue(true);
                    final HandlerRegistration end = registerEndHandler(w, new Callback<NativeEvent>() {

                        @Override
                        public void onDone(NativeEvent event) {

                            if (getDeviceId(event).equals(deviceId))
                                drag.setValue(false);
                        }
                    });
                    final int offsetX = getX(event) - ((Widget) w).getElement().getAbsoluteLeft();
                    final int offsetY = getY(event) - ((Widget) w).getElement().getAbsoluteTop();
                    DOM.eventPreventDefault((Event) event);
                    Timer t = new Timer() {

                        @Override
                        public void run() {
                            end.removeHandler();
                            if (drag.getValue() && !((Widget) w).getStyleName().contains(DRAGGINGSTYLENAME))
                                startDrag((Widget) w, deviceId, offsetX, offsetY, conf, dragProxyTemplate);
                        }
                    };
                    t.schedule(getDragDelayInMs());
                }
            }
        });
    }
}

From source file:ch.unifr.pai.twice.mousecontrol.client.TouchPadNintendoDS.java

License:Apache License

public TouchPadNintendoDS() {
    super(false);
    add(focus);/*  w ww  . j ava2  s  .c  o  m*/
    setWidth("2000px");
    setHeight("2000px");
    add(l);
    l.setWidth("200px");
    l.setHeight("200px");
    l.getElement().getStyle().setPosition(Position.ABSOLUTE);
    l.getElement().getStyle().setZIndex(200);
    updater = new Timer() {

        @Override
        public void run() {
            if (skipEvents == 0) {
                left = RootPanel.getBodyElement().getScrollLeft();
                top = RootPanel.getBodyElement().getScrollTop();
                if (currentX == -1 || currentY == -1) {
                    currentX = left;
                    currentY = top;
                } else if (left != currentX || top != currentY) {
                    // l.getElement().getStyle().setDisplay(Display.NONE);
                    // focus.setVisible(false);
                    int dX = currentX - left;
                    int dY = currentY - top;
                    currentX = left;
                    currentY = top;
                    updatePos(dX, dY);
                    lastUpdate = new Date().getTime();

                }
                // else if (lastUpdate != -1
                // && new Date().getTime() - lastUpdate > timerThreshold) {
                // lastUpdate = -1;
                // currentX = -1;
                // currentY = -1;
                // skipEvents = eventsAfterReset;
                // Window.scrollTo(1000, 1000);
                // l.getElement().getStyle().setDisplay(Display.BLOCK);
                // focus.setVisible(true);
                // }
            }
        }
    };

}

From source file:ch.unifr.pai.twice.mousecontrol.client.TouchPadNintendoDS.java

License:Apache License

@Override
public void start() {
    super.start();
    if (windowScrollHandler != null)
        windowScrollHandler.removeHandler();
    windowScrollHandler = Window.addWindowScrollHandler(new Window.ScrollHandler() {

        @Override//from   w  w  w. j  a v a 2  s . co  m
        public void onWindowScroll(ScrollEvent event) {
            if (skipEvents > 0)
                skipEvents--;
            l.getElement().getStyle().setLeft(RootPanel.getBodyElement().getScrollLeft(), Unit.PX);
            l.getElement().getStyle().setTop(RootPanel.getBodyElement().getScrollTop(), Unit.PX);
        }
    });
    Timer t2 = new Timer() {

        @Override
        public void run() {
            skipEvents = eventsAfterReset;
            Window.scrollTo(1000, 1000);
            updater.scheduleRepeating(50);
        }
    };
    t2.schedule(1000);
}

From source file:ch.unifr.pai.twice.mousecontrol.client.TouchPadScrollWidget.java

License:Apache License

public TouchPadScrollWidget() {
    super(false);
    scroller.setHeight("100%");
    scroller.setWidth("100%");
    add(scroller);//from   ww w .  java  2 s  .c  o m
    scroller.add(spacer);
    updater = new Timer() {

        @Override
        public void run() {
            currentScreenX = spacer.getOffsetWidth() - scroller.getHorizontalScrollPosition()
                    - scroller.getOffsetWidth();
            currentScreenY = spacer.getOffsetHeight() - scroller.getVerticalScrollPosition()
                    - scroller.getOffsetHeight();
        }
    };

}

From source file:ch.unifr.pai.twice.mousecontrol.client.TouchPadWidget.java

License:Apache License

/**
 * starts the execution of the component
 *//*from  w w w.ja v  a 2s  . c o  m*/
public void start() {
    if (!running) {
        label.setText("looking for available remote-clients");
        getAvailableClients(new Command() {

            @Override
            public void execute() {
                label.setText((availableClients == null ? "0" : availableClients.length) + " clients found");
                if (getCurrentClient() != null) {
                    label.setText("looking for cursor on client " + getCurrentClient());
                    lookForCursor = new Timer() {

                        @Override
                        public void run() {
                            try {
                                new RequestBuilder(RequestBuilder.GET, GWT.getHostPageBaseURL() + controlServlet
                                        + "?a=x"
                                        + (getCurrentClient() != null ? "&targetUUID=" + getCurrentClient()
                                                : "")
                                        + (uuid != null ? "&uuid=" + uuid : "")
                                        + (host != null ? "&host=" + host : "")
                                        + (port != null ? "&port=" + port : "")).sendRequest(null,
                                                new RequestCallback() {

                                                    @Override
                                                    public void onResponseReceived(Request request,
                                                            Response response) {
                                                        if (response.getStatusCode() > 400)
                                                            onError(request, null);
                                                        label.setText("GOT DATA: " + response.getText());
                                                        String color = extractColor(response);
                                                        if (color == null || color.isEmpty()
                                                                || color.equals("#null"))
                                                            color = null;
                                                        extractLastAction(response);

                                                        setScreenDimension(extractScreenDimensions(response));
                                                        if (color != null) {
                                                            setColor(color);
                                                            cursorAssigned();
                                                        } else {
                                                            noCursorAvailable();
                                                        }
                                                    }

                                                    @Override
                                                    public void onError(Request request, Throwable exception) {
                                                        noCursorAvailable();
                                                    }
                                                });
                            } catch (RequestException e) {
                                noCursorAvailable();
                            }
                        }
                    };
                    lookForCursor.run();
                }
            }
        });
    }
}

From source file:ch.unifr.pai.twice.multipointer.client.widgets.MultiFocusTextBox.java

License:Apache License

public MultiFocusTextBox() {
    blinkTimer = new Timer() {

        @Override/*w w w  .j  av  a2  s .co m*/
        public void run() {
            for (Cursor c : cursors.values()) {
                c.setVisible(cursorsVisible);
            }
            cursorsVisible = !cursorsVisible;
        }
    };
    blinkTimer.scheduleRepeating(cursorSpeed);
    p.getElement().getStyle().setDisplay(Display.INLINE_BLOCK);
    c = Canvas.createIfSupported();
    c.setCoordinateSpaceWidth(10000);
    c.addStyleName("multiFocusWidget");
    c.getElement().getStyle().setBorderWidth(0, Unit.PX);
    c.getElement().getStyle().setProperty("outline", "none");
    c.addKeyPressHandler(new KeyPressHandler() {

        @Override
        public void onKeyPress(KeyPressEvent event) {
            processInput(MultiCursorController.getUUID(event.getNativeEvent()), event.getCharCode());
        }
    });
    c.addKeyUpHandler(new KeyUpHandler() {

        @Override
        public void onKeyUp(KeyUpEvent event) {
            Cursor c = cursors.get(MultiCursorController.getUUID(event.getNativeEvent()));
            if (c != null) {
                switch (event.getNativeKeyCode()) {
                case KeyCodes.KEY_LEFT:
                    c.setPosition(Math.max(0, c.position - 1));
                    scrollIfNecessary();
                    break;
                case KeyCodes.KEY_RIGHT:
                    c.setPosition(Math.min(value.length(), c.position + 1));
                    scrollIfNecessary();
                    break;
                case KeyCodes.KEY_UP:
                    c.setPosition(0);
                    scrollIfNecessary();
                    break;
                case KeyCodes.KEY_DOWN:
                    c.setPosition(value != null ? value.length() : 0);
                    scrollIfNecessary();
                    break;
                case KeyCodes.KEY_DELETE:
                    if (value != null && c.position < value.length()) {
                        setValue(value.substring(0, c.position) + value.substring(c.position + 1));
                        for (Cursor cursor : cursors.values()) {
                            if (c.position < cursor.getPosition()) {
                                cursor.setPosition(cursor.getPosition() - 1);
                            }
                        }
                        scrollIfNecessary();
                    }
                    break;
                case KeyCodes.KEY_BACKSPACE:
                    if (value != null && c.position > 0 && c.position <= value.length()) {
                        setValue(value.substring(0, c.position - 1) + value.substring(c.position));
                        c.setPosition(c.position - 1);
                        for (Cursor cursor : cursors.values()) {
                            if (c.position < cursor.position) {
                                cursor.setPosition(cursor.getPosition() - 1);
                            }
                        }
                        scrollIfNecessary();
                    }
                    break;
                }
            }
        }
    });
    c.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            repositionCursor(MultiCursorController.getUUID(event.getNativeEvent()),
                    MultiCursorController.getColorNative(event.getNativeEvent()),
                    event.getRelativeX(c.getCanvasElement()), event.getRelativeY(c.getCanvasElement()));
        }
    });
    multiFocus.insert(c, 0, 0, 0);
    initWidget(multiFocus);
    context = c.getContext2d();
    context.setTextAlign(TextAlign.LEFT);
    context.setTextBaseline(TextBaseline.TOP);
    context.setFont("normal " + fontSize + "px sans-serif");
    c.getElement().getStyle().setPadding(padding, Unit.PX);
    setStyle();
    // TODO Auto-generated constructor stub
    // multiFocus.setVisible(false);

    multiFocus.setWidth("161px");
    multiFocus.setHeight("25px");

}

From source file:ch.unifr.pai.twice.multipointer.provider.client.widgets.MultiFocusTextBox.java

License:Apache License

public MultiFocusTextBox() {

    blinkTimer = new Timer() {

        @Override/*from  w  w w.  j ava2s  .  c  o m*/
        public void run() {
            for (Cursor c : cursors.values()) {
                c.setVisible(cursorsVisible);
            }
            cursorsVisible = !cursorsVisible;
        }
    };

    blinkTimer.scheduleRepeating(cursorSpeed);
    p.getElement().getStyle().setDisplay(Display.INLINE_BLOCK);
    c = Canvas.createIfSupported();
    c.setCoordinateSpaceWidth(10000);
    c.addStyleName("multiFocusWidget");
    c.getElement().getStyle().setBorderWidth(0, Unit.PX);
    c.getElement().getStyle().setProperty("outline", "none");
    c.addKeyPressHandler(new KeyPressHandler() {

        @Override
        public void onKeyPress(KeyPressEvent event) {
            processInput(NoMultiCursorController.getUUID(event.getNativeEvent()), event.getCharCode());
            String textcolor = NoMultiCursorController.getColorNative(event.getNativeEvent());
            setTextColor(textcolor);

        }
    });

    c.addKeyUpHandler(new KeyUpHandler() {

        @Override
        public void onKeyUp(KeyUpEvent event) {
            Cursor c = cursors.get(NoMultiCursorController.getUUID(event.getNativeEvent()));

            if (c != null) {

                switch (event.getNativeKeyCode()) {
                case KeyCodes.KEY_LEFT:
                    c.setPosition(Math.max(0, c.position - 1));
                    scrollIfNecessary();
                    break;
                case KeyCodes.KEY_RIGHT:
                    c.setPosition(Math.min(value.length(), c.position + 1));
                    scrollIfNecessary();
                    break;
                case KeyCodes.KEY_UP:
                    c.setPosition(0);
                    scrollIfNecessary();
                    break;
                case KeyCodes.KEY_DOWN:
                    c.setPosition(value != null ? value.length() : 0);
                    scrollIfNecessary();
                    break;
                case KeyCodes.KEY_DELETE:
                    if (value != null && c.position < value.length()) {
                        setValue(value.substring(0, c.position) + value.substring(c.position + 1));
                        for (Cursor cursor : cursors.values()) {
                            if (c.position < cursor.getPosition()) {
                                cursor.setPosition(cursor.getPosition() - 1);
                            }
                        }
                        scrollIfNecessary();
                    }
                    break;
                case KeyCodes.KEY_BACKSPACE:
                    if (value != null && c.position > 0 && c.position <= value.length()) {
                        setValue(value.substring(0, c.position - 1) + value.substring(c.position));
                        c.setPosition(c.position - 1);
                        for (Cursor cursor : cursors.values()) {
                            if (c.position < cursor.position) {
                                cursor.setPosition(cursor.getPosition() - 1);
                            }
                        }
                        scrollIfNecessary();
                    }
                    break;
                case KeyCodes.KEY_ENTER: //ICEExperiments-TextEdit
                    setValue("");
                    c.setPosition(0);

                    break;
                }
            }
        }
    });

    c.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            repositionCursor(NoMultiCursorController.getUUID(event.getNativeEvent()),
                    NoMultiCursorController.getColorNative(event.getNativeEvent()),
                    event.getRelativeX(c.getCanvasElement()), event.getRelativeY(c.getCanvasElement()));
        }
    });
    multiFocus.insert(c, 0, 0, 0);
    initWidget(multiFocus);
    context = c.getContext2d();
    context.setTextAlign(TextAlign.LEFT);
    context.setTextBaseline(TextBaseline.TOP);
    context.setFont("normal " + fontSize + "px sans-serif");
    c.getElement().getStyle().setPadding(padding, Unit.PX);
    setStyle();
    // TODO Auto-generated constructor stub
    // multiFocus.setVisible(false);

    multiFocus.setWidth("161px");
    multiFocus.setHeight("25px");

}

From source file:ch.unifr.pai.twice.utils.experiment.workflow.client.ExperimentWorkflow.java

License:Apache License

/**
 * stops the current task, disables the "next" button (to prevent users skipping the current tasks) for a short moment and show the next task if available.
 * Also start the timer if the task has a specified timeout that interrupts the execution
 *///from   w w  w  .j av  a 2 s  .c  om
private void switchToNextTask() {
    int nextTaskIndex;
    if (taskTimer != null) {
        taskTimer.cancel();
        taskTimer = null;
    }
    if (currentTaskIndex >= experiments.size() - 1) {
        if (loopExecution) {
            nextTaskIndex = 0;
        } else {
            stop();
            return;
        }
    } else {
        nextTaskIndex = currentTaskIndex + 1;
    }
    Task<?> newTask = experiments.get(nextTaskIndex);
    if (currentTask instanceof HasStartAndStop)
        ((HasStartAndStop) currentTask).stop();
    // if (currentTask instanceof HasLog)
    // service.log(((HasLog) currentTask).getLog(), false,
    // new DummyAsyncCallback<Void>());
    if (newTask != null) {
        currentTask = newTask;
        if (newTask instanceof Widget) {
            // taskContainerPanel.setWidget(((Widget) newTask));
        }
    }
    if (newTask.hasNextButton()) {
        nextButton.setVisible(true);
        nextButton.setEnabled(false);
        Timer t = new Timer() {
            @Override
            public void run() {
                nextButton.setEnabled(true);
            }
        };
        t.schedule(ENABLENEXTBUTTONINMS);
    } else {
        nextButton.setVisible(false);
    }
    //
    // if (newTask instanceof EnforceInputDevice)
    // changeInputDevice(((EnforceInputDevice) newTask).useInputDevice());
    if (newTask instanceof HasStartAndStop) {
        ((HasStartAndStop) newTask).start();
    }
    currentTaskIndex++;
    if (newTask.getTimeout() != null) {
        taskTimer = new Timer() {
            @Override
            public void run() {
                switchToNextTask();
            }
        };
        taskTimer.schedule(newTask.getTimeout().intValue());

    }
}

From source file:ch.unifr.pai.twice.widgets.client.MultiFocusTextBox.java

License:Apache License

public MultiFocusTextBox() {
    blinkTimer = new Timer() {

        @Override/*from w w  w.j  av a  2 s  .c om*/
        public void run() {
            for (Cursor c : cursors.values()) {
                c.setVisible(cursorsVisible);
            }
            cursorsVisible = !cursorsVisible;
        }
    };
    blinkTimer.scheduleRepeating(cursorSpeed);
    p.getElement().getStyle().setDisplay(Display.INLINE_BLOCK);
    c = Canvas.createIfSupported();
    c.getElement().getStyle().setBorderWidth(0, Unit.PX);
    c.getElement().getStyle().setProperty("outline", "none");
    c.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            //TODO if it is a new device, create a new cursor with the 
            repositionCursor(null, event.getRelativeX(c.getCanvasElement()),
                    event.getRelativeY(c.getCanvasElement()));
        }
    });
    multiFocus.insert(c, 0, 0, 0);
    initWidget(multiFocus);
    getElement().getStyle().setBorderStyle(BorderStyle.SOLID);
    getElement().getStyle().setBorderWidth(1, Unit.PX);
    c.getElement().getStyle().setMargin(5, Unit.PX);
    context = c.getContext2d();
    context.setTextAlign(TextAlign.LEFT);
    context.setTextBaseline(TextBaseline.TOP);
    context.setFont("13px sans-serif;");

    // TODO Auto-generated constructor stub
    //      multiFocus.setVisible(false);

    multiFocus.setWidth("161px");
    multiFocus.setHeight("28px");

}