Search in sources :

Example 66 with Command

use of com.codename1.ui.Command in project CodenameOne by codenameone.

the class SMSShare method share.

/**
 * {@inheritDoc}
 */
public void share(final String toShare) {
    final Form currentForm = Display.getInstance().getCurrent();
    final Form contactsForm = new Form("Contacts");
    contactsForm.setScrollable(false);
    contactsForm.setLayout(new BorderLayout());
    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];
                            final Hashtable contact = (Hashtable) contacts.getSelectedItem();
                            f[0] = new ShareForm(contactsForm, "Send SMS", (String) contact.get("phone"), toShare, new ActionListener() {

                                public void actionPerformed(ActionEvent evt) {
                                    try {
                                        Display.getInstance().sendSMS(f[0].getTo(), f[0].getMessage());
                                    } catch (IOException ex) {
                                        Log.e(ex);
                                        System.out.println("failed to send sms to " + (String) contact.get("phone"));
                                    }
                                    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();
                }
            });
        }
    }, "SMS Thread").start();
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent) Hashtable(java.util.Hashtable) IOException(java.io.IOException) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) ContactsModel(com.codename1.contacts.ContactsModel)

Example 67 with Command

use of com.codename1.ui.Command in project CodenameOne by codenameone.

the class TestUtils method getToolbarCommands.

/**
 * Returns all the command objects from the toolbar in the order of left, right, overflow & sidemenu
 * @return the set of commands
 */
public static Command[] getToolbarCommands() {
    Form f = Display.getInstance().getCurrent();
    Toolbar tb = f.getToolbar();
    ArrayList<Command> result = new ArrayList<Command>();
    addAllCommands(tb.getLeftBarCommands(), result);
    addAllCommands(tb.getRightBarCommands(), result);
    addAllCommands(tb.getOverflowCommands(), result);
    addAllCommands(tb.getSideMenuCommands(), result);
    Command[] carr = new Command[result.size()];
    result.toArray(carr);
    return carr;
}
Also used : Form(com.codename1.ui.Form) Command(com.codename1.ui.Command) ArrayList(java.util.ArrayList) Toolbar(com.codename1.ui.Toolbar)

Example 68 with Command

use of com.codename1.ui.Command in project CodenameOne by codenameone.

the class TestUtils method goBack.

/**
 * Executes the back command for the current form, similarly to pressing the back button
 */
public static void goBack() {
    if (verbose) {
        log("goBack()");
    }
    Form f = Display.getInstance().getCurrent();
    Command c = f.getBackCommand();
    assertBool(c != null, "The current form doesn't have a back command at this moment! for form name " + f.getName());
    f.dispatchCommand(c, new ActionEvent(c, ActionEvent.Type.Command));
    waitFor(20);
}
Also used : Form(com.codename1.ui.Form) Command(com.codename1.ui.Command) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 69 with Command

use of com.codename1.ui.Command in project CodenameOne by codenameone.

the class LazyValueC method createForm.

Form createForm(Form f) {
    Form currentForm = Display.getInstance().getCurrent();
    if (currentForm != null && currentForm instanceof Dialog) {
        ((Dialog) Display.getInstance().getCurrent()).dispose();
        currentForm = Display.getInstance().getCurrent();
    }
    Vector formNavigationStack = baseFormNavigationStack;
    if (formNavigationStack != null && !(f instanceof Dialog) && !f.getName().equals(homeForm)) {
        if (currentForm != null) {
            String nextForm = (String) f.getClientProperty("%next_form%");
            // we are in the sidemenu view we should really be using the parent form
            SideMenuBar b = (SideMenuBar) currentForm.getClientProperty("cn1$sideMenuParent");
            if (b != null) {
                currentForm = b.getParentForm();
            }
            // don't add back commands to transitional forms
            if (nextForm == null) {
                String commandAction = currentForm.getName();
                if (allowBackTo(commandAction) && f.getBackCommand() == null) {
                    Command backCommand;
                    if (isSameBackDestination(currentForm, f)) {
                        backCommand = currentForm.getBackCommand();
                    } else {
                        backCommand = createCommandImpl(getBackCommandText(currentForm.getTitle()), null, BACK_COMMAND_ID, commandAction, true, "");
                        backCommand.putClientProperty(COMMAND_ARGUMENTS, "");
                        backCommand.putClientProperty(COMMAND_ACTION, commandAction);
                    }
                    if (backCommand != null) {
                        setBackCommand(f, backCommand);
                    }
                    // trigger listener creation if this is the only command in the form
                    getFormListenerInstance(f, null);
                    formNavigationStack.addElement(getFormState(currentForm));
                }
            }
        }
    }
    return f;
}
Also used : Form(com.codename1.ui.Form) Command(com.codename1.ui.Command) Dialog(com.codename1.ui.Dialog) SideMenuBar(com.codename1.ui.SideMenuBar) Vector(java.util.Vector)

Example 70 with Command

use of com.codename1.ui.Command in project CodenameOne by codenameone.

the class LazyValueC method showForm.

private void showForm(Form f, Command sourceCommand, Component sourceComponent) {
    Form currentForm = Display.getInstance().getCurrent();
    if (currentForm != null && currentForm instanceof Dialog) {
        ((Dialog) Display.getInstance().getCurrent()).dispose();
        currentForm = Display.getInstance().getCurrent();
    }
    Vector formNavigationStack = baseFormNavigationStack;
    if (sourceCommand != null && currentForm != null && currentForm.getBackCommand() == sourceCommand) {
        if (currentForm != null) {
            exitForm(currentForm);
        }
        if (formNavigationStack != null && formNavigationStack.size() > 0) {
            String name = f.getName();
            if (name != null && name.equals(homeForm)) {
                if (formNavigationStack.size() > 0) {
                    setFormState(f, (Hashtable) formNavigationStack.elementAt(formNavigationStack.size() - 1));
                }
                formNavigationStack.clear();
            } else {
                initBackForm(f);
            }
        }
        onBackNavigation();
        beforeShow(f);
        f.showBack();
        postShowImpl(f);
    } else {
        if (currentForm != null) {
            exitForm(currentForm);
        }
        if (formNavigationStack != null && !(f instanceof Dialog) && !f.getName().equals(homeForm)) {
            if (currentForm != null) {
                String nextForm = (String) f.getClientProperty("%next_form%");
                // we are in the sidemenu view we should really be using the parent form
                SideMenuBar b = (SideMenuBar) currentForm.getClientProperty("cn1$sideMenuParent");
                if (b != null) {
                    currentForm = b.getParentForm();
                }
                // don't add back commands to transitional forms
                if (nextForm == null) {
                    String commandAction = currentForm.getName();
                    if (allowBackTo(commandAction) && f.getBackCommand() == null) {
                        Command backCommand;
                        if (isSameBackDestination(currentForm, f)) {
                            backCommand = currentForm.getBackCommand();
                        } else {
                            backCommand = createCommandImpl(getBackCommandText(currentForm.getTitle()), null, BACK_COMMAND_ID, commandAction, true, "");
                            backCommand.putClientProperty(COMMAND_ARGUMENTS, "");
                            backCommand.putClientProperty(COMMAND_ACTION, commandAction);
                        }
                        if (backCommand != null) {
                            setBackCommand(f, backCommand);
                        }
                        // trigger listener creation if this is the only command in the form
                        getFormListenerInstance(f, null);
                        formNavigationStack.addElement(getFormState(currentForm));
                    }
                }
            }
        }
        if (f instanceof Dialog) {
            beforeShow(f);
            if (sourceComponent != null) {
                // we are cheating with the post show here since we are using a modal
                // dialog to prevent the "double clicking button" problem by using
                // a modal dialog
                sourceComponent.setEnabled(false);
                postShowImpl(f);
                f.show();
                sourceComponent.setEnabled(true);
                exitForm(f);
            } else {
                ((Dialog) f).showModeless();
                postShowImpl(f);
            }
        } else {
            beforeShow(f);
            f.show();
            postShowImpl(f);
        }
    }
}
Also used : Form(com.codename1.ui.Form) Command(com.codename1.ui.Command) Dialog(com.codename1.ui.Dialog) SideMenuBar(com.codename1.ui.SideMenuBar) Vector(java.util.Vector)

Aggregations

BorderLayout (com.codename1.ui.layouts.BorderLayout)25 Command (com.codename1.ui.Command)24 ActionEvent (com.codename1.ui.events.ActionEvent)22 Form (com.codename1.ui.Form)20 Hashtable (java.util.Hashtable)13 UIManager (com.codename1.ui.plaf.UIManager)11 Container (com.codename1.ui.Container)10 ActionListener (com.codename1.ui.events.ActionListener)10 GridLayout (com.codename1.ui.layouts.GridLayout)9 Button (com.codename1.ui.Button)8 Style (com.codename1.ui.plaf.Style)8 IOException (java.io.IOException)7 Vector (java.util.Vector)7 Component (com.codename1.ui.Component)6 Dialog (com.codename1.ui.Dialog)6 BoxLayout (com.codename1.ui.layouts.BoxLayout)6 ArrayList (java.util.ArrayList)6 RadioButton (com.codename1.ui.RadioButton)5 LayeredLayout (com.codename1.ui.layouts.LayeredLayout)5 CheckBox (com.codename1.ui.CheckBox)4