Search in sources :

Example 31 with ActionEvent

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

the class Form method pointerReleased.

/**
 * {@inheritDoc}
 */
public void pointerReleased(int x, int y) {
    rippleMotion = null;
    pressedCmp = null;
    boolean isScrollWheeling = Display.INSTANCE.impl.isScrollWheeling();
    Container actual = getActualPane(formLayeredPane, x, y);
    if (buttonsAwatingRelease != null && buttonsAwatingRelease.size() == 1) {
        // special case allowing drag within a button
        Component atXY = actual.getComponentAt(x, y);
        Component pendingButton = (Component) buttonsAwatingRelease.get(0);
        if (atXY == pendingButton) {
            buttonsAwatingRelease = null;
            if (dragged == pendingButton) {
                if (pendingButton.isDragAndDropInitialized()) {
                    pendingButton.dragFinishedImpl(x, y);
                } else {
                    pendingButton.pointerReleased(x, y);
                }
                dragged = null;
            } else {
                pendingButton.pointerReleased(x, y);
                if (dragged != null) {
                    if (dragged.isDragAndDropInitialized()) {
                        dragged.dragFinishedImpl(x, y);
                        dragged = null;
                    } else {
                        dragged.pointerReleased(x, y);
                        dragged = null;
                    }
                }
            }
            return;
        }
        if (pendingButton instanceof Button) {
            Button b = (Button) pendingButton;
            int relRadius = b.getReleaseRadius();
            if (relRadius > 0 || b.contains(x, y)) {
                Rectangle r = new Rectangle(b.getAbsoluteX() - relRadius, b.getAbsoluteY() - relRadius, b.getWidth() + relRadius * 2, b.getHeight() + relRadius * 2);
                if (r.contains(x, y)) {
                    buttonsAwatingRelease = null;
                    pointerReleased(b.getAbsoluteX() + 1, b.getAbsoluteY() + 1);
                    return;
                }
            }
        }
    }
    if (pointerReleasedListeners != null && pointerReleasedListeners.hasListeners()) {
        ActionEvent ev = new ActionEvent(this, ActionEvent.Type.PointerReleased, x, y);
        pointerReleasedListeners.fireActionEvent(ev);
        if (ev.isConsumed()) {
            if (dragged != null) {
                if (dragged.isDragAndDropInitialized()) {
                    dragged.dragFinishedImpl(x, y);
                }
                dragged = null;
            }
            return;
        }
    }
    if (dragStopFlag) {
        if (dragged != null) {
            if (dragged.isDragAndDropInitialized()) {
                dragged.dragFinishedImpl(x, y);
            }
            dragged = null;
        }
        dragStopFlag = false;
        return;
    }
    if (dragged == null) {
        // soft button.
        if (menuBar.contains(x, y)) {
            Component cmp = menuBar.getComponentAt(x, y);
            if (cmp != null && cmp.isEnabled()) {
                cmp.pointerReleased(x, y);
            }
            return;
        }
        if (stickyDrag != null) {
            stickyDrag.pointerReleased(x, y);
            repaint();
        } else {
            // Container actual = getActualPane();
            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.isEnabled()) {
                    if (cmp.hasLead) {
                        Container leadParent;
                        if (cmp instanceof Container) {
                            leadParent = ((Container) cmp).getLeadParent();
                        } else {
                            leadParent = cmp.getParent().getLeadParent();
                        }
                        leadParent.repaint();
                        if (!isScrollWheeling) {
                            setFocused(leadParent);
                        }
                        cmp.getLeadComponent().pointerReleased(x, y);
                    } else {
                        if (cmp.isEnabled()) {
                            if (!isScrollWheeling && cmp.isFocusable()) {
                                setFocused(cmp);
                            }
                            cmp.pointerReleased(x, y);
                        }
                    }
                }
            } 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.pointerReleased(x, y);
                    }
                } 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()) {
                            if (cmp.hasLead) {
                                Container leadParent;
                                if (cmp instanceof Container) {
                                    leadParent = ((Container) cmp).getLeadParent();
                                } else {
                                    leadParent = cmp.getParent().getLeadParent();
                                }
                                leadParent.repaint();
                                if (!isScrollWheeling) {
                                    setFocused(leadParent);
                                }
                                cmp = cmp.getLeadComponent();
                                cmp.pointerReleased(x, y);
                            } else {
                                cmp.pointerReleased(x, y);
                            }
                        }
                    }
                }
            }
        }
    } else {
        if (dragged.isDragAndDropInitialized()) {
            dragged.dragFinishedImpl(x, y);
            dragged = null;
        } else {
            dragged.pointerReleased(x, y);
            dragged = null;
        }
    }
    stickyDrag = null;
    if (buttonsAwatingRelease != null && !Display.getInstance().isRecursivePointerRelease()) {
        for (int iter = 0; iter < buttonsAwatingRelease.size(); iter++) {
            Button b = (Button) buttonsAwatingRelease.get(iter);
            b.setState(Button.STATE_DEFAULT);
            b.repaint();
        }
        buttonsAwatingRelease = null;
    }
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionEvent(com.codename1.ui.events.ActionEvent) Rectangle(com.codename1.ui.geom.Rectangle)

Example 32 with ActionEvent

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

the class MenuBar method actionPerformed.

/**
 * Invoked when a softbutton is pressed
 */
public void actionPerformed(ActionEvent evt) {
    if (evt.isConsumed()) {
        return;
    }
    Object src = evt.getSource();
    if (commandList == null) {
        Button source = (Button) src;
        for (int iter = 0; iter < soft.length; iter++) {
            if (source == soft[iter]) {
                if (softCommand[iter] == menuCommand) {
                    showMenu();
                    return;
                }
                if (softCommand[iter] != null) {
                    ActionEvent e = new ActionEvent(softCommand[iter], ActionEvent.Type.Command);
                    softCommand[iter].actionPerformed(e);
                    if (!e.isConsumed()) {
                        parent.actionCommandImpl(softCommand[iter]);
                    }
                }
                return;
            }
        }
    } else {
        // the list for the menu sent the event
        if (src instanceof Button) {
            for (int iter = 0; iter < soft.length; iter++) {
                if (src == soft[iter]) {
                    Container parent = commandList.getParent();
                    while (parent != null) {
                        if (parent instanceof Dialog) {
                            ((Dialog) parent).actionCommand(softCommand[iter]);
                            return;
                        }
                        parent = parent.getParent();
                    }
                }
            }
        }
        Command c = getComponentSelectedCommand(commandList);
        if (!c.isEnabled()) {
            return;
        }
        Container p = commandList.getParent();
        while (p != null) {
            if (p instanceof Dialog) {
                ((Dialog) p).actionCommand(c);
                return;
            }
            p = p.getParent();
        }
    }
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent)

Example 33 with ActionEvent

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

the class MenuBar method showMenu.

/**
 * This method shows the menu on the Form.
 * The method creates a Dialog with the commands and calls showMenuDialog.
 * The method blocks until the user dispose the dialog.
 */
public void showMenu() {
    final Dialog d = new Dialog("Menu", "");
    d.setDisposeWhenPointerOutOfBounds(true);
    d.setMenu(true);
    d.addOrientationListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            d.dispose();
        }
    });
    d.setTransitionInAnimator(transitionIn);
    d.setTransitionOutAnimator(transitionOut);
    d.setLayout(new BorderLayout());
    d.setScrollable(false);
    // calling parent.createCommandComponent is done only for backward
    // compatability reasons, in the next version this call be replaced with
    // calling directly to createCommandComponent
    ((Form) d).getMenuBar().commandList = createCommandComponent(commands);
    if (menuCellRenderer != null && ((Form) d).getMenuBar().commandList instanceof List) {
        ((List) ((Form) d).getMenuBar().commandList).setListCellRenderer(menuCellRenderer);
    }
    d.getContentPane().getStyle().setMargin(0, 0, 0, 0);
    d.addComponent(BorderLayout.CENTER, ((Form) d).getMenuBar().commandList);
    if (thirdSoftButton) {
        d.addCommand(selectMenuItem);
        d.addCommand(cancelMenuItem);
    } else {
        d.addCommand(cancelMenuItem);
        if (soft.length > 1) {
            d.addCommand(selectMenuItem);
        }
    }
    d.setClearCommand(cancelMenuItem);
    d.setBackCommand(cancelMenuItem);
    if (((Form) d).getMenuBar().commandList instanceof List) {
        ((List) ((Form) d).getMenuBar().commandList).addActionListener(((Form) d).getMenuBar());
    }
    menuDisplaying = true;
    Command result = showMenuDialog(d);
    menuDisplaying = false;
    if (result != cancelMenuItem) {
        Command c = null;
        if (result == selectMenuItem) {
            c = getComponentSelectedCommand(((Form) d).getMenuBar().commandList);
            if (c != null) {
                ActionEvent e = new ActionEvent(c, ActionEvent.Type.Command);
                c.actionPerformed(e);
            }
        } else {
            c = result;
            // a touch menu will always send its commands on its own...
            if (!isTouchMenus()) {
                c = result;
                if (c != null) {
                    ActionEvent e = new ActionEvent(c, ActionEvent.Type.Command);
                    c.actionPerformed(e);
                }
            }
        }
        // menu item was handled internally in a touch interface that is not a touch menu
        if (c != null) {
            parent.actionCommandImpl(c);
        }
    }
    if (((Form) d).getMenuBar().commandList instanceof List) {
        ((List) ((Form) d).getMenuBar().commandList).removeActionListener(((Form) d).getMenuBar());
    }
    Form upcoming = Display.getInstance().getCurrentUpcoming();
    if (upcoming == parent) {
        d.disposeImpl();
    } else {
        parent.tint = (upcoming instanceof Dialog);
    }
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 34 with ActionEvent

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

the class EmailShare method share.

/**
 * {@inheritDoc}
 */
public void share(final String toShare, final String image, final String mimeType) {
    final Form currentForm = Display.getInstance().getCurrent();
    final Form contactsForm = new Form("Contacts");
    contactsForm.setLayout(new BorderLayout());
    contactsForm.setScrollable(false);
    contactsForm.addComponent(BorderLayout.CENTER, new Label("Please wait..."));
    contactsForm.show();
    Display.getInstance().startThread(new Runnable() {

        public void run() {
            String[] ids = ContactsManager.getAllContacts();
            if (ids == null || ids.length == 0) {
                Display.getInstance().callSerially(new Runnable() {

                    public void run() {
                        Dialog.show("Failed to Share", "No Contacts Found", "Ok", null);
                        currentForm.showBack();
                    }
                });
                return;
            }
            ContactsModel model = new ContactsModel(ids);
            final List contacts = new List(model);
            contacts.setRenderer(createListRenderer());
            Display.getInstance().callSerially(new Runnable() {

                public void run() {
                    contacts.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            final ShareForm[] f = new ShareForm[1];
                            Hashtable contact = (Hashtable) contacts.getSelectedItem();
                            if (image == null) {
                                f[0] = new ShareForm(contactsForm, "Send Email", (String) contact.get("email"), toShare, new ActionListener() {

                                    public void actionPerformed(ActionEvent evt) {
                                        String[] recieptents = new String[1];
                                        recieptents[0] = f[0].getTo();
                                        Message msg = new Message(toShare);
                                        Message.sendMessage(recieptents, "share", msg);
                                        finish();
                                    }
                                });
                                f[0].show();
                            } else {
                                f[0] = new ShareForm(contactsForm, "Send Email", (String) contact.get("email"), toShare, image, new ActionListener() {

                                    public void actionPerformed(ActionEvent evt) {
                                        String[] recieptents = new String[1];
                                        recieptents[0] = f[0].getTo();
                                        Message msg = new Message(toShare);
                                        msg.setAttachment(image);
                                        msg.setMimeType(mimeType);
                                        Message.sendMessage(recieptents, "share", msg);
                                        finish();
                                    }
                                });
                                f[0].show();
                            }
                        }
                    });
                    contactsForm.addComponent(BorderLayout.CENTER, contacts);
                    Command back = new Command("Back") {

                        public void actionPerformed(ActionEvent evt) {
                            currentForm.showBack();
                        }
                    };
                    contactsForm.addCommand(back);
                    contactsForm.setBackCommand(back);
                    contactsForm.revalidate();
                }
            });
        }
    }, "Email Thread").start();
}
Also used : Message(com.codename1.messaging.Message) ActionEvent(com.codename1.ui.events.ActionEvent) Hashtable(java.util.Hashtable) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) ContactsModel(com.codename1.contacts.ContactsModel)

Example 35 with ActionEvent

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

the class TestUtils method executeToolbarCommandAtOffset.

/**
 * Executes a command from the offset returned by {@link #getToolbarCommands()}
 *
 * @param offset the offset of the command we want to execute
 */
public static void executeToolbarCommandAtOffset(final int offset) {
    Form f = Display.getInstance().getCurrent();
    if (!Display.getInstance().isEdt()) {
        Display.getInstance().callSerially(new Runnable() {

            public void run() {
                executeToolbarCommandAtOffset(offset);
            }
        });
        return;
    }
    Command cmd = getToolbarCommands()[offset];
    f.dispatchCommand(cmd, new ActionEvent(cmd));
}
Also used : Form(com.codename1.ui.Form) Command(com.codename1.ui.Command) 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