Example usage for com.google.gwt.dom.client NativeEvent preventDefault

List of usage examples for com.google.gwt.dom.client NativeEvent preventDefault

Introduction

In this page you can find the example usage for com.google.gwt.dom.client NativeEvent preventDefault.

Prototype

public final void preventDefault() 

Source Link

Document

Prevents the browser from taking its default action for the given event.

Usage

From source file:app.dnd.drag.DraggableCellDecorator.java

License:Apache License

public void onBrowserEvent(Context context, Element parent, final T value, NativeEvent event,
        ValueUpdater<T> valueUpdater) {
    if (MOUSE_DOWN.equals(event.getType())) {
        EventTarget eventTarget = event.getEventTarget();
        if (Element.is(eventTarget)) {
            Element target = eventTarget.cast();
            Element wrapper = target.getParentElement();
            if (wrapper != null && dragHandlerClass.equals(wrapper.getClassName())) {
                DNDContext dndContext = dragSource.startDragging(value);
                dragController.dragStart(dndContext, parent);
                event.stopPropagation();
                event.preventDefault();
                return;
            }//w  w w . jav a  2  s  .co m
        }
    }
    final Element cellParent = getCellParent(parent);
    cell.onBrowserEvent(context, cellParent, getValue(value), event, getValueUpdater());
}

From source file:client.application.ApplicationView.java

License:Apache License

void register() {
    if (!registred) {
        registred = true;/* www.  ja  v  a 2 s . co  m*/
        Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
            @Override
            public void onPreviewNativeEvent(Event.NativePreviewEvent event) {
                NativeEvent ne = event.getNativeEvent();
                if ("keypress".equals(ne.getType()) && ne.getKeyCode() != 0) {
                    logger.log(Level.INFO, String.valueOf((char) ne.getKeyCode()));
                    ServerEventBusService.App.getInstance().onSendKeyEvent(ne.getKeyCode(), counter++,
                            new VoidAsyncCallback());
                } else if ("mousedown".equals(ne.getType()) && ne.getButton() == NativeEvent.BUTTON_RIGHT) {
                    ne.preventDefault();
                }
            }
        });
    }
}

From source file:com.alkacon.geranium.client.ui.Popup.java

License:Open Source License

/**
 * @see com.google.gwt.user.client.ui.PopupPanel#onPreviewNativeEvent(com.google.gwt.user.client.Event.NativePreviewEvent)
 *///from ww w  . j a v  a 2  s  .co  m
@Override
protected void onPreviewNativeEvent(NativePreviewEvent event) {

    // We need to preventDefault() on mouseDown events (outside of the
    // DialogBox content) to keep text from being selected when it
    // is dragged.
    NativeEvent nativeEvent = event.getNativeEvent();

    if (!event.isCanceled() && (event.getTypeInt() == Event.ONMOUSEDOWN) && isCaptionEvent(nativeEvent)) {
        nativeEvent.preventDefault();
    }
    super.onPreviewNativeEvent(event);
}

From source file:com.badlogic.gdx.backends.gwt.GwtInput.java

License:Apache License

private void handleEvent(NativeEvent e) {
    if (e.getType().equals("mousedown")) {
        if (!e.getEventTarget().equals(canvas) || touched[0]) {
            float mouseX = getRelativeX(e, canvas);
            float mouseY = getRelativeY(e, canvas);
            if (mouseX < 0 || mouseX > Gdx.graphics.getWidth() || mouseY < 0
                    || mouseY > Gdx.graphics.getHeight()) {
                hasFocus = false;/*  ww w.  j  a  va  2 s  . c o m*/
            }
            return;
        }
        hasFocus = true;
        this.justTouched = true;
        this.touched[0] = true;
        this.pressedButtons.add(getButton(e.getButton()));
        this.deltaX[0] = 0;
        this.deltaY[0] = 0;
        if (isCursorCatched()) {
            this.touchX[0] += getMovementXJSNI(e);
            this.touchY[0] += getMovementYJSNI(e);
        } else {
            this.touchX[0] = getRelativeX(e, canvas);
            this.touchY[0] = getRelativeY(e, canvas);
        }
        this.currentEventTimeStamp = TimeUtils.nanoTime();
        if (processor != null)
            processor.touchDown(touchX[0], touchY[0], 0, getButton(e.getButton()));
    }

    if (e.getType().equals("mousemove")) {
        if (isCursorCatched()) {
            this.deltaX[0] = (int) getMovementXJSNI(e);
            this.deltaY[0] = (int) getMovementYJSNI(e);
            this.touchX[0] += getMovementXJSNI(e);
            this.touchY[0] += getMovementYJSNI(e);
        } else {
            this.deltaX[0] = getRelativeX(e, canvas) - touchX[0];
            this.deltaY[0] = getRelativeY(e, canvas) - touchY[0];
            this.touchX[0] = getRelativeX(e, canvas);
            this.touchY[0] = getRelativeY(e, canvas);
        }
        this.currentEventTimeStamp = TimeUtils.nanoTime();
        if (processor != null) {
            if (touched[0])
                processor.touchDragged(touchX[0], touchY[0], 0);
            else
                processor.mouseMoved(touchX[0], touchY[0]);
        }
    }

    if (e.getType().equals("mouseup")) {
        if (!touched[0])
            return;
        this.pressedButtons.remove(getButton(e.getButton()));
        this.touched[0] = pressedButtons.size > 0;
        if (isCursorCatched()) {
            this.deltaX[0] = (int) getMovementXJSNI(e);
            this.deltaY[0] = (int) getMovementYJSNI(e);
            this.touchX[0] += getMovementXJSNI(e);
            this.touchY[0] += getMovementYJSNI(e);
        } else {
            this.deltaX[0] = getRelativeX(e, canvas) - touchX[0];
            this.deltaY[0] = getRelativeY(e, canvas) - touchY[0];
            this.touchX[0] = getRelativeX(e, canvas);
            this.touchY[0] = getRelativeY(e, canvas);
        }
        this.currentEventTimeStamp = TimeUtils.nanoTime();
        this.touched[0] = false;
        if (processor != null)
            processor.touchUp(touchX[0], touchY[0], 0, getButton(e.getButton()));
    }
    if (e.getType().equals(getMouseWheelEvent())) {
        if (processor != null) {
            processor.scrolled((int) getMouseWheelVelocity(e));
        }
        this.currentEventTimeStamp = TimeUtils.nanoTime();
        e.preventDefault();
    }
    if (e.getType().equals("keydown") && hasFocus) {
        // System.out.println("keydown");
        int code = keyForCode(e.getKeyCode());
        if (code == 67) {
            e.preventDefault();
            if (processor != null) {
                processor.keyDown(code);
                processor.keyTyped('\b');
            }
        } else {
            if (!pressedKeys[code]) {
                pressedKeyCount++;
                pressedKeys[code] = true;
                keyJustPressed = true;
                justPressedKeys[code] = true;
                if (processor != null) {
                    processor.keyDown(code);
                }
            }
        }
    }

    if (e.getType().equals("keypress") && hasFocus) {
        // System.out.println("keypress");
        char c = (char) e.getCharCode();
        if (processor != null)
            processor.keyTyped(c);
    }

    if (e.getType().equals("keyup") && hasFocus) {
        // System.out.println("keyup");
        int code = keyForCode(e.getKeyCode());
        if (pressedKeys[code]) {
            pressedKeyCount--;
            pressedKeys[code] = false;
        }
        if (processor != null) {
            processor.keyUp(code);
        }
    }

    if (e.getType().equals("touchstart")) {
        this.justTouched = true;
        JsArray<Touch> touches = e.getChangedTouches();
        for (int i = 0, j = touches.length(); i < j; i++) {
            Touch touch = touches.get(i);
            int real = touch.getIdentifier();
            int touchId;
            touchMap.put(real, touchId = getAvailablePointer());
            touched[touchId] = true;
            touchX[touchId] = getRelativeX(touch, canvas);
            touchY[touchId] = getRelativeY(touch, canvas);
            deltaX[touchId] = 0;
            deltaY[touchId] = 0;
            if (processor != null) {
                processor.touchDown(touchX[touchId], touchY[touchId], touchId, Buttons.LEFT);
            }
        }
        this.currentEventTimeStamp = TimeUtils.nanoTime();
        e.preventDefault();
    }
    if (e.getType().equals("touchmove")) {
        JsArray<Touch> touches = e.getChangedTouches();
        for (int i = 0, j = touches.length(); i < j; i++) {
            Touch touch = touches.get(i);
            int real = touch.getIdentifier();
            int touchId = touchMap.get(real);
            deltaX[touchId] = getRelativeX(touch, canvas) - touchX[touchId];
            deltaY[touchId] = getRelativeY(touch, canvas) - touchY[touchId];
            touchX[touchId] = getRelativeX(touch, canvas);
            touchY[touchId] = getRelativeY(touch, canvas);
            if (processor != null) {
                processor.touchDragged(touchX[touchId], touchY[touchId], touchId);
            }
        }
        this.currentEventTimeStamp = TimeUtils.nanoTime();
        e.preventDefault();
    }
    if (e.getType().equals("touchcancel")) {
        JsArray<Touch> touches = e.getChangedTouches();
        for (int i = 0, j = touches.length(); i < j; i++) {
            Touch touch = touches.get(i);
            int real = touch.getIdentifier();
            int touchId = touchMap.get(real);
            touchMap.remove(real);
            touched[touchId] = false;
            deltaX[touchId] = getRelativeX(touch, canvas) - touchX[touchId];
            deltaY[touchId] = getRelativeY(touch, canvas) - touchY[touchId];
            touchX[touchId] = getRelativeX(touch, canvas);
            touchY[touchId] = getRelativeY(touch, canvas);
            if (processor != null) {
                processor.touchUp(touchX[touchId], touchY[touchId], touchId, Buttons.LEFT);
            }
        }
        this.currentEventTimeStamp = TimeUtils.nanoTime();
        e.preventDefault();
    }
    if (e.getType().equals("touchend")) {
        JsArray<Touch> touches = e.getChangedTouches();
        for (int i = 0, j = touches.length(); i < j; i++) {
            Touch touch = touches.get(i);
            int real = touch.getIdentifier();
            int touchId = touchMap.get(real);
            touchMap.remove(real);
            touched[touchId] = false;
            deltaX[touchId] = getRelativeX(touch, canvas) - touchX[touchId];
            deltaY[touchId] = getRelativeY(touch, canvas) - touchY[touchId];
            touchX[touchId] = getRelativeX(touch, canvas);
            touchY[touchId] = getRelativeY(touch, canvas);
            if (processor != null) {
                processor.touchUp(touchX[touchId], touchY[touchId], touchId, Buttons.LEFT);
            }
        }
        this.currentEventTimeStamp = TimeUtils.nanoTime();
        e.preventDefault();
    }
    // if(hasFocus) e.preventDefault();
}

From source file:com.chinarewards.gwt.license.client.ui.DialogBox.java

@Override
protected void onPreviewNativeEvent(NativePreviewEvent event) {
    // We need to preventDefault() on mouseDown events (outside of the
    // DialogBox content) to keep text from being selected when it
    // is dragged.
    NativeEvent nativeEvent = event.getNativeEvent();

    if (!event.isCanceled() && (event.getTypeInt() == Event.ONMOUSEDOWN) && isCaptionEvent(nativeEvent)) {
        nativeEvent.preventDefault();
    }/*from   ww w  . ja  v a  2s . c  o  m*/

    super.onPreviewNativeEvent(event);
}

From source file:com.edgenius.wiki.gwt.client.widgets.DialogBox.java

License:Open Source License

@Override
protected void onPreviewNativeEvent(NativePreviewEvent event) {
    // We need to preventDefault() on mouseDown events (outside of the
    // DialogBox content) to keep text from being selected when it
    // is dragged.
    NativeEvent nativeEvent = event.getNativeEvent();

    if (!event.isCanceled() && (event.getTypeInt() == Event.ONMOUSEDOWN) && isCaptionEvent(nativeEvent)) {
        nativeEvent.preventDefault();

    } else if (!event.isCanceled() && event.getTypeInt() == Event.ONKEYDOWN) {
        int keyCode = event.getNativeEvent().getKeyCode();
        // ESC clicked
        if (keyCode == KeyCodes.KEY_ESCAPE) {
            DialogBox.this.hidebox();
        }// w w w .  j a v  a  2  s . co m
    }

    //check if event is from adopted elements, if so, avoid these events cancelled by consume() method. 
    if (adoptedElementIds != null && adoptedElementIds.size() > 0) {
        EventTarget target = event.getNativeEvent().getEventTarget();
        if (Element.is(target)) {
            for (String id : adoptedElementIds) {
                Element ele = DOM.getElementById(id);
                if (ele != null && ele.isOrHasChild(Element.as(target))) {
                    event.consume();
                    break;
                }
            }
        }
    }
    super.onPreviewNativeEvent(event);
}

From source file:com.ephesoft.dcma.gwt.core.client.view.ExternalAppDialogBox.java

License:Open Source License

@Override
protected void onPreviewNativeEvent(NativePreviewEvent preview) {
    super.onPreviewNativeEvent(preview);

    NativeEvent evt = preview.getNativeEvent();

    if (evt.getCtrlKey()) {
        switch (evt.getKeyCode()) {
        case 'a':
        case 'A':
            evt.preventDefault();
            okButton.click();/*  w w w.ja  v a 2s  .c om*/
        case 'z':
        case 'Z':
            evt.preventDefault();
            closeButton.click();
            break;
        }
    }

}

From source file:com.ephesoft.dcma.gwt.rv.client.view.ExternalAppDialogBox.java

License:Open Source License

@Override
protected void onPreviewNativeEvent(NativePreviewEvent preview) {
    super.onPreviewNativeEvent(preview);

    NativeEvent evt = preview.getNativeEvent();

    if (evt.getCtrlKey()) {
        switch (evt.getKeyCode()) {
        case 'a':
        case 'A':
            evt.preventDefault();
            okButton.click();/*from   w  ww.j a  v  a2 s.c o m*/
        case 'z':
        case 'Z':
            evt.preventDefault();
            closeButton.click();
            break;
        default:
            break;
        }
    }

}

From source file:com.facebook.tsdb.tsdash.client.ApplicationController.java

License:Apache License

private void bindKeyShortcuts() {
    Event.addNativePreviewHandler(new NativePreviewHandler() {
        @Override/*  w  ww  .  j  a  v a2 s.c o  m*/
        public void onPreviewNativeEvent(NativePreviewEvent event) {
            NativeEvent nativeEvent = event.getNativeEvent();
            if (nativeEvent.getCtrlKey() && nativeEvent.getKeyCode() == ' ') {
                nativeEvent.preventDefault();
                Scheduler.get().scheduleDeferred(new ScheduledCommand() {
                    @Override
                    public void execute() {
                        long now = (new Date()).getTime();
                        if (now - keyShortcutTs < 500) {
                            return;
                        }
                        keyShortcutTs = now;
                        if (appState.needsAutoreload()) {
                            eventBus.fireEvent(
                                    new KeyboardShortcutEvent(KeyboardShortcutEvent.Shortcut.CTRL_SPACE));
                        }
                    }
                });
            } else if (nativeEvent.getCtrlKey() && nativeEvent.getKeyCode() == 'F') {
                nativeEvent.preventDefault();
                Scheduler.get().scheduleDeferred(new ScheduledCommand() {
                    @Override
                    public void execute() {
                        long now = (new Date()).getTime();
                        if (now - keyShortcutTs < 500) {
                            return;
                        }
                        keyShortcutTs = now;
                        eventBus.fireEvent(new KeyboardShortcutEvent(KeyboardShortcutEvent.Shortcut.CTRL_F));
                    }
                });
            }
        }
    });
}

From source file:com.google.gerrit.client.change.FileTable.java

License:Apache License

private static boolean onRestore(NativeEvent e, int idx) {
    MyTable t = getMyTable(e);/*ww w  .j  av  a2s  .  com*/
    if (t != null) {
        t.onRestore(idx);
        e.preventDefault();
        e.stopPropagation();
        return false;
    }
    return true;
}