Search in sources :

Example 26 with ActionEvent

use of com.codename1.ui.events.ActionEvent in project CodenameOne by codenameone.

the class BlackBerryOS5Implementation method captureVideo.

public void captureVideo(ActionListener response) {
    captureCallback = new EventDispatcher();
    captureCallback.addListener(response);
    UiApplication.getUiApplication().addFileSystemJournalListener(new FileSystemJournalListener() {

        private long lastUSN;

        private String videoPath;

        public void fileJournalChanged() {
            // next sequence number file system will use
            long USN = FileSystemJournal.getNextUSN();
            for (long i = USN - 1; i >= lastUSN && i < USN; --i) {
                FileSystemJournalEntry entry = FileSystemJournal.getEntry(i);
                if (entry == null) {
                    break;
                }
                String path = entry.getPath();
                if (entry.getEvent() == FileSystemJournalEntry.FILE_ADDED && videoPath == null) {
                    int index = path.indexOf(".3GP");
                    if (index != -1) {
                        videoPath = path;
                    }
                } else if (entry.getEvent() == FileSystemJournalEntry.FILE_RENAMED) {
                    if (path != null && path.equals(videoPath)) {
                        // close the camera
                        UiApplication.getUiApplication().removeFileSystemJournalListener(this);
                        try {
                            EventInjector.KeyEvent inject = new EventInjector.KeyEvent(EventInjector.KeyEvent.KEY_DOWN, Characters.ESCAPE, 0, 200);
                            inject.post();
                            inject.post();
                        } catch (Exception e) {
                        // try to close the camera
                        }
                        captureCallback.fireActionEvent(new ActionEvent("file://" + path));
                        captureCallback = null;
                        videoPath = null;
                        break;
                    }
                }
            }
            lastUSN = USN;
        }
    });
    app.setWaitingForReply(true);
    synchronized (UiApplication.getEventLock()) {
        Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA, new CameraArguments(CameraArguments.ARG_VIDEO_RECORDER));
    }
}
Also used : FileSystemJournalEntry(net.rim.device.api.io.file.FileSystemJournalEntry) CameraArguments(net.rim.blackberry.api.invoke.CameraArguments) EventDispatcher(com.codename1.ui.util.EventDispatcher) FileSystemJournalListener(net.rim.device.api.io.file.FileSystemJournalListener) ActionEvent(com.codename1.ui.events.ActionEvent) DatabaseIOException(net.rim.device.api.database.DatabaseIOException) DatabasePathException(net.rim.device.api.database.DatabasePathException) IOException(java.io.IOException) EventInjector(net.rim.device.api.system.EventInjector)

Example 27 with ActionEvent

use of com.codename1.ui.events.ActionEvent in project CodenameOne by codenameone.

the class BarCodeScanner method startScaning.

private void startScaning(ScanResult callback) {
    this.callback = callback;
    try {
        // Add the listener for scan and cancel
        Container cmdContainer = new Container(new FlowLayout(Component.CENTER));
        Button scanButton = new Button(new Command("Scan") {

            public void actionPerformed(ActionEvent evt) {
                cameraForm.repaint();
                if (snapshotThread != null) {
                    snapshotThread.continueRun();
                }
            }
        });
        Button cancelButton = new Button(new Command("Cancel") {

            public void actionPerformed(ActionEvent evt) {
                if (snapshotThread != null) {
                    snapshotThread.stop();
                    cancelScan();
                }
            }
        });
        cmdContainer.addComponent(scanButton);
        cmdContainer.addComponent(cancelButton);
        cameraForm = new Form();
        cameraForm.setScrollable(false);
        cameraForm.setLayout(new BorderLayout());
        cameraForm.addComponent(BorderLayout.CENTER, media.getVideoComponent());
        cameraForm.addComponent(BorderLayout.SOUTH, cmdContainer);
    } catch (Exception e) {
        // throw new AppException("Image/video capture not supported on this phone", e).setCode(97);
        e.printStackTrace();
    }
    startScan();
}
Also used : Container(com.codename1.ui.Container) FlowLayout(com.codename1.ui.layouts.FlowLayout) BorderLayout(com.codename1.ui.layouts.BorderLayout) Button(com.codename1.ui.Button) Command(com.codename1.ui.Command) Form(com.codename1.ui.Form) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 28 with ActionEvent

use of com.codename1.ui.events.ActionEvent in project CodenameOne by codenameone.

the class Component method pointerDragged.

/**
 * If this Component is focused, the pointer dragged event
 * will call this method
 *
 * @param x the pointer x coordinate
 * @param y the pointer y coordinate
 */
public void pointerDragged(final int x, final int y) {
    Form p = getComponentForm();
    if (p == null) {
        return;
    }
    if (pointerDraggedListeners != null && pointerDraggedListeners.hasListeners()) {
        pointerDraggedListeners.fireActionEvent(new ActionEvent(this, ActionEvent.Type.PointerDrag, x, y));
    }
    if (dragAndDropInitialized) {
        // keep call to pointerDragged to move the parent scroll if needed
        if (dragCallbacks < 2) {
            dragCallbacks++;
            Display.getInstance().callSerially(new Runnable() {

                public void run() {
                    if (dragActivated) {
                        pointerDragged(x, y);
                    }
                    dragCallbacks--;
                }
            });
        }
        if (!dragActivated) {
            dragActivated = true;
            setVisible(false);
            p.setDraggedComponent(this);
            oldx = x;
            oldy = y;
            draggedx = getAbsoluteX();
            draggedy = getAbsoluteY();
        }
        Component dropTo = findDropTarget(this, x, y);
        if (dropTo != null && dragOverListener != null) {
            ActionEvent ev = new ActionEvent(this, dropTo, x, y);
            dragOverListener.fireActionEvent(ev);
            if (ev.isConsumed()) {
                return;
            }
        }
        if (dropTargetComponent != dropTo) {
            if (dropTargetComponent != null) {
                dropTargetComponent.dragExit(this);
            }
            dropTargetComponent = dropTo;
            if (dropTargetComponent != null) {
                dropTargetComponent.dragEnter(this);
            }
        }
        // we repaint twice to create an intersection of the old and new position
        p.repaint(draggedx, draggedy, getWidth(), getHeight());
        draggedx = draggedx + (x - oldx);
        draggedy = draggedy + (y - oldy);
        oldx = x;
        oldy = y;
        p.repaint(draggedx, draggedy, getWidth(), getHeight());
        Container scrollParent = getParent();
        while (scrollParent != null && !scrollParent.isScrollable()) {
            scrollParent = scrollParent.getParent();
        }
        if (scrollParent != null) {
            Style s = getStyle();
            int w = getWidth() - s.getHorizontalPadding();
            int h = getHeight() - s.getVerticalPadding();
            Rectangle view;
            int invisibleAreaUnderVKB = getInvisibleAreaUnderVKB();
            view = new Rectangle(getScrollX(), getScrollY(), w, h - invisibleAreaUnderVKB);
            // if the dragging component is out of bounds move the scrollable parent
            if (!view.contains(draggedx - scrollParent.getAbsoluteX(), draggedy - scrollParent.getAbsoluteY(), getWidth(), getHeight())) {
                if ((scrollParent.isScrollableY() && scrollParent.getScrollY() >= 0 && scrollParent.getScrollY() + (draggedy + getHeight()) < scrollParent.getScrollDimension().getHeight()) || (scrollParent.isScrollableX() && scrollParent.getScrollX() >= 0 && scrollParent.getScrollX() + (draggedx + getWidth()) < scrollParent.getScrollDimension().getWidth())) {
                    int yposition = draggedy - scrollParent.getAbsoluteY() - 40;
                    if (yposition < 0) {
                        yposition = 0;
                    }
                    int xposition = draggedx - scrollParent.getAbsoluteX() - 40;
                    if (xposition < 0) {
                        xposition = 0;
                    }
                    int height = getHeight() + 80;
                    if (scrollParent.getScrollY() + draggedy + height >= scrollParent.getScrollDimension().getHeight()) {
                        yposition = draggedy - scrollParent.getAbsoluteY();
                        height = scrollParent.getScrollDimension().getHeight() - yposition;
                    }
                    int width = getWidth() + 80;
                    if (scrollParent.getScrollX() + draggedx + width >= scrollParent.getScrollDimension().getWidth()) {
                        xposition = draggedx - scrollParent.getAbsoluteX();
                        width = scrollParent.getScrollDimension().getWidth() - xposition;
                    }
                    scrollParent.scrollRectToVisible(xposition, yposition, width, height, scrollParent);
                }
            }
        }
        return;
    }
    if (dragActivated && p.getDraggedComponent() == null) {
        dragActivated = false;
    }
    if (!dragActivated) {
        boolean draggedOnX = Math.abs(p.initialPressX - x) > Math.abs(p.initialPressY - y);
        shouldGrabScrollEvents = (isScrollableX() && draggedOnX) || isScrollableY() && !draggedOnX;
    }
    if (isScrollable() && isSmoothScrolling() && shouldGrabScrollEvents) {
        if (!dragActivated) {
            dragActivated = true;
            lastScrollY = y;
            lastScrollX = x;
            p.setDraggedComponent(this);
            p.registerAnimatedInternal(this);
            Component fc = p.getFocused();
            if (fc != null && fc != this) {
                fc.dragInitiated();
            }
        }
        // and pulling it in the reverse direction of the drag
        if (isScrollableY()) {
            int tl;
            if (getTensileLength() > -1 && refreshTask == null) {
                tl = getTensileLength();
            } else {
                tl = getHeight() / 2;
            }
            if (!isSmoothScrolling() || !isTensileDragEnabled()) {
                tl = 0;
            }
            int scroll = getScrollY() + (lastScrollY - y);
            if (isAlwaysTensile() && getScrollDimension().getHeight() + getInvisibleAreaUnderVKB() <= getHeight()) {
                if (scroll >= -tl && scroll < getHeight() + tl) {
                    setScrollY(scroll);
                }
            } else {
                if (scroll >= -tl && scroll < getScrollDimension().getHeight() + getInvisibleAreaUnderVKB() - getHeight() + tl) {
                    setScrollY(scroll);
                }
            }
            updateTensileHighlightIntensity(lastScrollY, y, false);
        }
        if (isScrollableX()) {
            int tl;
            if (getTensileLength() > -1) {
                tl = getTensileLength();
            } else {
                tl = getWidth() / 2;
            }
            if (!isSmoothScrolling() || !isTensileDragEnabled()) {
                tl = 0;
            }
            int scroll = getScrollX() + (lastScrollX - x);
            if (scroll >= -tl && scroll < getScrollDimension().getWidth() - getWidth() + tl) {
                setScrollX(scroll);
            }
        }
        lastScrollY = y;
        lastScrollX = x;
    } else {
        // try to find a scrollable element until you reach the Form
        Component parent = getParent();
        if (!(parent instanceof Form)) {
            parent.pointerDragged(x, y);
        }
    }
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent) Rectangle(com.codename1.ui.geom.Rectangle) Style(com.codename1.ui.plaf.Style) Point(com.codename1.ui.geom.Point)

Example 29 with ActionEvent

use of com.codename1.ui.events.ActionEvent in project CodenameOne by codenameone.

the class Form method pointerPressed.

/**
 * {@inheritDoc}
 */
public void pointerPressed(int x, int y) {
    pressedCmp = null;
    stickyDrag = null;
    dragStopFlag = false;
    dragged = null;
    boolean isScrollWheeling = Display.INSTANCE.impl.isScrollWheeling();
    if (pointerPressedListeners != null && pointerPressedListeners.hasListeners()) {
        pointerPressedListeners.fireActionEvent(new ActionEvent(this, ActionEvent.Type.PointerPressed, x, y));
    }
    // check if the click is relevant to the menu bar.
    /*
        if (menuBar.contains(x, y)) {
            Component cmp = menuBar.getComponentAt(x, y);
            while (cmp != null && cmp.isIgnorePointerEvents()) {
                cmp = cmp.getParent();
            }
            if (cmp != null && cmp.isEnabled()) {
                cmp.pointerPressed(x, y);
                tactileTouchVibe(x, y, cmp);
                initRippleEffect(x, y, cmp);
            }
            return;
        }
        */
    Container actual = getActualPane(formLayeredPane, x, y);
    if (y >= actual.getY() && x >= actual.getX()) {
        Component cmp = actual.getComponentAt(x, y);
        while (cmp != null && cmp.isIgnorePointerEvents()) {
            cmp = cmp.getParent();
        }
        if (cmp != null) {
            cmp.initDragAndDrop(x, y);
            if (cmp.hasLead) {
                if (isCurrentlyScrolling(cmp)) {
                    dragStopFlag = true;
                    cmp.clearDrag();
                    return;
                }
                Container leadParent;
                if (cmp instanceof Container) {
                    leadParent = ((Container) cmp).getLeadParent();
                } else {
                    leadParent = cmp.getParent().getLeadParent();
                }
                leadParent.repaint();
                if (!isScrollWheeling) {
                    setFocused(leadParent);
                }
                pressedCmp = cmp.getLeadComponent();
                cmp.getLeadComponent().pointerPressed(x, y);
            } else {
                if (isCurrentlyScrolling(cmp)) {
                    dragStopFlag = true;
                    cmp.clearDrag();
                    return;
                }
                if (cmp.isEnabled()) {
                    if (!isScrollWheeling && cmp.isFocusable()) {
                        setFocused(cmp);
                    }
                    pressedCmp = cmp;
                    cmp.pointerPressed(x, y);
                    tactileTouchVibe(x, y, cmp);
                    initRippleEffect(x, y, cmp);
                }
            }
        }
    } else {
        if (y < actual.getY()) {
            Component cmp = getTitleArea().getComponentAt(x, y);
            while (cmp != null && cmp.isIgnorePointerEvents()) {
                cmp = cmp.getParent();
            }
            if (cmp != null && cmp.isEnabled() && cmp.isFocusable()) {
                pressedCmp = cmp;
                cmp.pointerPressed(x, y);
                tactileTouchVibe(x, y, cmp);
                initRippleEffect(x, y, cmp);
            }
        } else {
            Component cmp = ((BorderLayout) super.getLayout()).getWest();
            if (cmp != null) {
                cmp = ((Container) cmp).getComponentAt(x, y);
                while (cmp != null && cmp.isIgnorePointerEvents()) {
                    cmp = cmp.getParent();
                }
                if (cmp != null && cmp.isEnabled() && cmp.isFocusable()) {
                    if (cmp.hasLead) {
                        Container leadParent;
                        if (cmp instanceof Container) {
                            leadParent = ((Container) cmp).getLeadParent();
                        } else {
                            leadParent = cmp.getParent().getLeadParent();
                        }
                        if (!isScrollWheeling) {
                            setFocused(leadParent);
                        }
                        cmp = cmp.getLeadComponent();
                    }
                    cmp.initDragAndDrop(x, y);
                    pressedCmp = cmp;
                    cmp.pointerPressed(x, y);
                    tactileTouchVibe(x, y, cmp);
                    initRippleEffect(x, y, cmp);
                }
            }
        }
    }
    initialPressX = x;
    initialPressY = y;
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 30 with ActionEvent

use of com.codename1.ui.events.ActionEvent in project CodenameOne by codenameone.

the class Form method pointerDragged.

@Override
public void pointerDragged(int[] x, int[] y) {
    // disable the drag stop flag if we are dragging again
    boolean isScrollWheeling = Display.INSTANCE.impl.isScrollWheeling();
    if (dragStopFlag) {
        pointerPressed(x, y);
    }
    autoRelease(x[0], y[0]);
    if (pointerDraggedListeners != null && pointerDraggedListeners.hasListeners()) {
        ActionEvent av = new ActionEvent(this, ActionEvent.Type.PointerDrag, x[0], y[0]);
        pointerDraggedListeners.fireActionEvent(av);
        if (av.isConsumed()) {
            return;
        }
    }
    rippleMotion = null;
    if (dragged != null) {
        dragged.pointerDragged(x, y);
        return;
    }
    if (pressedCmp != null && pressedCmp.isStickyDrag()) {
        stickyDrag = pressedCmp;
    }
    if (stickyDrag != null) {
        stickyDrag.pointerDragged(x, y);
        repaint();
        return;
    }
    Container actual = getActualPane(formLayeredPane, x[0], y[0]);
    if (x[0] < actual.getX()) {
        // special case for sidemenu
        Component cmp = ((BorderLayout) super.getLayout()).getWest();
        if (cmp != null) {
            cmp = ((Container) cmp).getComponentAt(x[0], y[0]);
            while (cmp != null && cmp.isIgnorePointerEvents()) {
                cmp = cmp.getParent();
            }
            if (cmp != null && cmp.isEnabled()) {
                cmp.pointerDragged(x, y);
                cmp.repaint();
                if (cmp == pressedCmp && cmp.isStickyDrag()) {
                    stickyDrag = cmp;
                }
            }
        }
        return;
    }
    Component cmp = actual.getComponentAt(x[0], y[0]);
    while (cmp != null && cmp.isIgnorePointerEvents()) {
        cmp = cmp.getParent();
    }
    if (cmp != null) {
        if (!isScrollWheeling && cmp.isFocusable() && cmp.isEnabled()) {
            setFocused(cmp);
        }
        cmp.pointerDragged(x, y);
        cmp.repaint();
        if (cmp == pressedCmp && cmp.isStickyDrag()) {
            stickyDrag = cmp;
        }
    }
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionEvent(com.codename1.ui.events.ActionEvent)

Aggregations

ActionEvent (com.codename1.ui.events.ActionEvent)98 ActionListener (com.codename1.ui.events.ActionListener)55 IOException (java.io.IOException)30 BorderLayout (com.codename1.ui.layouts.BorderLayout)28 Form (com.codename1.ui.Form)18 Hashtable (java.util.Hashtable)14 Vector (java.util.Vector)11 NetworkEvent (com.codename1.io.NetworkEvent)10 Container (com.codename1.ui.Container)9 ActionEvent (java.awt.event.ActionEvent)9 Command (com.codename1.ui.Command)8 ActionListener (java.awt.event.ActionListener)8 Button (com.codename1.ui.Button)7 Style (com.codename1.ui.plaf.Style)7 Rectangle (com.codename1.ui.geom.Rectangle)6 File (java.io.File)6 ConnectionNotFoundException (javax.microedition.io.ConnectionNotFoundException)6 MediaException (javax.microedition.media.MediaException)6 RecordStoreException (javax.microedition.rms.RecordStoreException)6 BufferedOutputStream (com.codename1.io.BufferedOutputStream)5