Search in sources :

Example 76 with ActionEvent

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

the class MenuBar method keyReleased.

/**
 * {@inheritDoc}
 */
public void keyReleased(int keyCode) {
    int commandBehavior = getCommandBehavior();
    if (commandBehavior >= Display.COMMAND_BEHAVIOR_BUTTON_BAR && keyCode != backSK && keyCode != clearSK && keyCode != backspaceSK) {
        return;
    }
    if (getCommandCount() > 0) {
        int softkeyCount = Display.getInstance().getImplementation().getSoftkeyCount();
        if (softkeyCount < 2 && keyCode == leftSK) {
            if (commandList != null) {
                Container parent = commandList.getParent();
                while (parent != null) {
                    if (parent instanceof Dialog && ((Dialog) parent).isMenu()) {
                        return;
                    }
                    parent = parent.getParent();
                }
            }
            showMenu();
            return;
        } else {
            if (keyCode == leftSK) {
                if (left != null) {
                    left.released();
                }
                return;
            } else {
                // it might be a back command...
                if ((keyCode == rightSK || keyCode == rightSK2)) {
                    if (right != null) {
                        right.released();
                    }
                    return;
                } else {
                    if (Display.getInstance().getGameAction(keyCode) == Display.GAME_FIRE) {
                        main.released();
                        return;
                    }
                }
            }
        }
    }
    // allows a back/clear command to occur regardless of whether the
    // command was added to the form
    Command c = null;
    if (keyCode == backSK) {
        // the back command should be invoked
        c = parent.getBackCommand();
        if (c == null && minimizeOnBack) {
            Display.getInstance().minimizeApplication();
            return;
        }
    } else {
        if (keyCode == clearSK || keyCode == backspaceSK) {
            c = getClearCommand();
        }
    }
    if (c != null) {
        ActionEvent ev = new ActionEvent(c, keyCode);
        c.actionPerformed(ev);
        if (!ev.isConsumed()) {
            parent.actionCommandImpl(c);
        }
    }
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent)

Example 77 with ActionEvent

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

the class Toolbar method constructOnTopSideMenu.

private void constructOnTopSideMenu() {
    if (sidemenuDialog == null) {
        permanentSideMenuContainer = constructSideNavigationComponent();
        final Form parent = getComponentForm();
        parent.addPointerPressedListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (Display.getInstance().getImplementation().isScrollWheeling()) {
                    return;
                }
                if (sidemenuDialog.isShowing()) {
                    if (evt.getX() > sidemenuDialog.getWidth()) {
                        parent.putClientProperty("cn1$sidemenuCharged", Boolean.FALSE);
                        closeSideMenu();
                        evt.consume();
                    } else {
                        if (evt.getX() + Display.getInstance().convertToPixels(8) > sidemenuDialog.getWidth()) {
                            parent.putClientProperty("cn1$sidemenuCharged", Boolean.TRUE);
                        } else {
                            parent.putClientProperty("cn1$sidemenuCharged", Boolean.FALSE);
                        }
                        if (!isComponentInOnTopSidemenu(parent.getComponentAt(evt.getX(), evt.getY()))) {
                            evt.consume();
                        }
                    }
                } else {
                    int displayWidth = Display.getInstance().getDisplayWidth();
                    final int sensitiveSection = displayWidth / getUIManager().getThemeConstant("sideSwipeSensitiveInt", 10);
                    if (evt.getX() < sensitiveSection) {
                        parent.putClientProperty("cn1$sidemenuCharged", Boolean.TRUE);
                        evt.consume();
                    } else {
                        parent.putClientProperty("cn1$sidemenuCharged", Boolean.FALSE);
                        permanentSideMenuContainer.pointerPressed(evt.getX(), evt.getY());
                    }
                }
            }
        });
        parent.addPointerDraggedListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (Display.getInstance().getImplementation().isScrollWheeling()) {
                    return;
                }
                Boolean b = (Boolean) parent.getClientProperty("cn1$sidemenuCharged");
                if (b != null && b.booleanValue()) {
                    parent.putClientProperty("cn1$sidemenuActivated", Boolean.TRUE);
                    showOnTopSidemenu(evt.getX(), false);
                    evt.consume();
                } else {
                    if (sidemenuDialog.isShowing()) {
                        permanentSideMenuContainer.pointerDragged(evt.getX(), evt.getY());
                        evt.consume();
                    }
                }
            }
        });
        parent.addPointerReleasedListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (Display.getInstance().getImplementation().isScrollWheeling()) {
                    return;
                }
                Boolean b = (Boolean) parent.getClientProperty("cn1$sidemenuActivated");
                if (b != null && b.booleanValue()) {
                    parent.putClientProperty("cn1$sidemenuActivated", null);
                    if (evt.getX() < parent.getWidth() / 4) {
                        closeSideMenu();
                    } else {
                        showOnTopSidemenu(-1, true);
                    }
                    evt.consume();
                } else {
                    if (sidemenuDialog.isShowing()) {
                        if (!isComponentInOnTopSidemenu(parent.getComponentAt(evt.getX(), evt.getY()))) {
                            evt.consume();
                        }
                        permanentSideMenuContainer.pointerReleased(evt.getX(), evt.getY());
                    }
                }
            }
        });
        sidemenuDialog = new InteractionDialog(new BorderLayout());
        sidemenuDialog.setFormMode(true);
        sidemenuDialog.setUIID("Container");
        sidemenuDialog.setDialogUIID("Container");
        sidemenuDialog.getTitleComponent().remove();
        sidemenuDialog.add(BorderLayout.CENTER, permanentSideMenuContainer);
        if (sidemenuSouthComponent != null) {
            sidemenuDialog.add(BorderLayout.SOUTH, sidemenuSouthComponent);
        }
        float size = 4.5f;
        try {
            size = Float.parseFloat(getUIManager().getThemeConstant("menuImageSize", "4.5"));
        } catch (Throwable t) {
            Log.e(t);
        }
        if (!parent.getUIManager().isThemeConstant("hideLeftSideMenuBool", false)) {
            Image i = (Image) parent.getUIManager().getThemeImageConstant("sideMenuImage");
            if (i != null) {
                Command cmd = addCommandToLeftBar("", i, new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        if (sidemenuDialog.isShowing()) {
                            closeSideMenu();
                            return;
                        }
                        showOnTopSidemenu(-1, false);
                    }
                });
                Image p = (Image) parent.getUIManager().getThemeImageConstant("sideMenuPressImage");
                if (p != null) {
                    findCommandComponent(cmd).setPressedIcon(p);
                }
            } else {
                addMaterialCommandToLeftBar("", FontImage.MATERIAL_MENU, size, new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        if (sidemenuDialog.isShowing()) {
                            closeSideMenu();
                            return;
                        }
                        showOnTopSidemenu(-1, false);
                    }
                });
            }
        }
    }
}
Also used : InteractionDialog(com.codename1.components.InteractionDialog) ActionListener(com.codename1.ui.events.ActionListener) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 78 with ActionEvent

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

the class URLImage method fetch.

/**
 * Images are normally fetched from storage or network only as needed,
 * however if the download must start before the image is drawn this method
 * can be invoked. Notice that "immediately" doesn't mean synchronously, it just
 * means that the image will be added to the queue right away but probably won't be
 * available by the time the method completes.
 */
public void fetch() {
    if (fetching || imageData != null) {
        return;
    }
    fetching = true;
    try {
        locked = super.isLocked();
        if (storageFile != null) {
            if (Storage.getInstance().exists(storageFile)) {
                super.unlock();
                imageData = new byte[Storage.getInstance().entrySize(storageFile)];
                InputStream is = Storage.getInstance().createInputStream(storageFile);
                Util.readFully(is, imageData);
                resetCache();
                fetching = false;
                repaintImage = true;
                return;
            }
            if (adapter != null) {
                Util.downloadImageToStorage(url, storageFile + IMAGE_SUFFIX, new SuccessCallback<Image>() {

                    public void onSucess(Image value) {
                        DownloadCompleted onComplete = new DownloadCompleted();
                        onComplete.setSourceImage(value);
                        onComplete.actionPerformed(new ActionEvent(value));
                    }
                });
            } else {
                Util.downloadImageToStorage(url, storageFile, new SuccessCallback<Image>() {

                    public void onSucess(Image value) {
                        DownloadCompleted onComplete = new DownloadCompleted();
                        onComplete.setSourceImage(value);
                        onComplete.actionPerformed(new ActionEvent(value));
                    }
                });
            }
        } else {
            if (FileSystemStorage.getInstance().exists(fileSystemFile)) {
                super.unlock();
                imageData = new byte[(int) FileSystemStorage.getInstance().getLength(fileSystemFile)];
                InputStream is = FileSystemStorage.getInstance().openInputStream(fileSystemFile);
                Util.readFully(is, imageData);
                resetCache();
                fetching = false;
                repaintImage = true;
                return;
            }
            if (adapter != null) {
                Util.downloadImageToFileSystem(url, fileSystemFile + IMAGE_SUFFIX, new SuccessCallback<Image>() {

                    public void onSucess(Image value) {
                        DownloadCompleted onComplete = new DownloadCompleted();
                        onComplete.setSourceImage(value);
                        onComplete.actionPerformed(new ActionEvent(value));
                    }
                });
            } else {
                Util.downloadImageToFileSystem(url, fileSystemFile, new SuccessCallback<Image>() {

                    public void onSucess(Image value) {
                        DownloadCompleted onComplete = new DownloadCompleted();
                        onComplete.setSourceImage(value);
                        onComplete.actionPerformed(new ActionEvent(value));
                    }
                });
            }
        }
    } catch (IOException ioErr) {
        throw new RuntimeException(ioErr.toString());
    }
}
Also used : InputStream(java.io.InputStream) ActionEvent(com.codename1.ui.events.ActionEvent) IOException(java.io.IOException)

Example 79 with ActionEvent

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

the class Form method pointerDragged.

/**
 * {@inheritDoc}
 */
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, y);
    if (pointerDraggedListeners != null) {
        ActionEvent av = new ActionEvent(this, ActionEvent.Type.PointerDrag, x, y);
        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, y);
    if (x < actual.getX()) {
        // special case for sidemenu
        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.pointerDragged(x, y);
                cmp.repaint();
                if (cmp == pressedCmp && cmp.isStickyDrag()) {
                    stickyDrag = cmp;
                }
            }
        }
        return;
    }
    Component cmp = actual.getComponentAt(x, y);
    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)

Example 80 with ActionEvent

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

the class Form method onShowCompletedImpl.

void onShowCompletedImpl() {
    setLightweightMode(false);
    onShowCompleted();
    if (showListener != null) {
        showListener.fireActionEvent(new ActionEvent(this, ActionEvent.Type.Show));
    }
    if (editOnShow != null) {
        editOnShow.startEditingAsync();
    }
}
Also used : 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