Search in sources :

Example 36 with Command

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

the class TestComponent method getComponentAt_int_int_browsercomponent.

private void getComponentAt_int_int_browsercomponent() {
    int w = Display.getInstance().getDisplayWidth();
    int h = Display.getInstance().getDisplayHeight();
    Form mapDemo = new Form("Maps", new LayeredLayout());
    Toolbar.setOnTopSideMenu(true);
    Toolbar tb = new Toolbar();
    mapDemo.setToolbar(tb);
    mapDemo.setTitle("Maps");
    tb.addCommandToSideMenu(new Command("Test") {

        @Override
        public void actionPerformed(ActionEvent e) {
        // testNestedScrollingLabels();
        }
    });
    BrowserComponent mc = new BrowserComponent();
    mapDemo.add(mc);
    mapDemo.show();
    TestUtils.waitForFormTitle("Maps", 2000);
    Component middleComponent = mapDemo.getComponentAt(w / 2, h / 2);
    assertTrue(mc == middleComponent || mc.contains(middleComponent), "Wrong component found in middle. Expected " + mc + " but found " + middleComponent);
    tb.openSideMenu();
    // wait for side menu to open
    TestUtils.waitFor(500);
    Component res = null;
    res = tb.getComponentAt(10, h / 2);
    // System.out.println("tb size = "+tb.getAbsoluteX()+", "+tb.getAbsoluteY()+", "+tb.getWidth()+", "+tb.getHeight());
    // System.out.println("mb size = "+tb.getMenuBar().getAbsoluteX()+", "+tb.getMenuBar().getAbsoluteY()+", "+tb.getMenuBar().getWidth()+", "+tb.getMenuBar().getHeight());
    // System.out.println("res is "+res);
    res = mapDemo.getComponentAt(10, h / 2);
    // Let's find the interaction dialog on the form
    Component interactionDialog = $("*", mapDemo).filter(c -> {
        return c instanceof InteractionDialog;
    }).asComponent();
    assertTrue(((InteractionDialog) interactionDialog).contains(res), "Toolbar is open so getComponentAt() should return something on the toolbar.  But received " + res + ".  Toolbar is " + tb);
}
Also used : Util(com.codename1.io.Util) BoxLayout(com.codename1.ui.layouts.BoxLayout) ActionEvent(com.codename1.ui.events.ActionEvent) NetworkManager(com.codename1.io.NetworkManager) LayeredLayout(com.codename1.ui.layouts.LayeredLayout) NetworkEvent(com.codename1.io.NetworkEvent) ComponentSelector.$(com.codename1.ui.ComponentSelector.$) ConnectionRequest(com.codename1.io.ConnectionRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) AbstractTest(com.codename1.testing.AbstractTest) TestUtils(com.codename1.testing.TestUtils) SimpleDateFormat(com.codename1.l10n.SimpleDateFormat) Cookie(com.codename1.io.Cookie) FileSystemStorage(com.codename1.io.FileSystemStorage) IOException(java.io.IOException) ActionListener(com.codename1.ui.events.ActionListener) JSONParser(com.codename1.io.JSONParser) Log(com.codename1.io.Log) BorderLayout(com.codename1.ui.layouts.BorderLayout) InputStreamReader(java.io.InputStreamReader) Style(com.codename1.ui.plaf.Style) Coord(com.codename1.maps.Coord) ToastBar(com.codename1.components.ToastBar) InteractionDialog(com.codename1.components.InteractionDialog) InteractionDialog(com.codename1.components.InteractionDialog) ActionEvent(com.codename1.ui.events.ActionEvent) LayeredLayout(com.codename1.ui.layouts.LayeredLayout)

Example 37 with Command

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

the class HTMLComponent method handleInput.

/**
 * Handles the INPUT tag
 *
 * @param element The input element
 * @param align The current aligment
 */
private void handleInput(HTMLElement element, int align) {
    String type = element.getAttributeById(HTMLElement.ATTR_TYPE);
    if (type == null) {
        return;
    }
    int typeID = INPUT_TYPES.indexOf(type.toLowerCase());
    if (typeID == -1) {
        if (htmlCallback != null) {
            if (!htmlCallback.parsingError(HTMLCallback.ERROR_ATTIBUTE_VALUE_INVALID, element.getTagName(), element.getAttributeName(new Integer(HTMLElement.ATTR_TYPE)), type, "Unsupported input type '" + type + "'. Supported types: text, password, checkbox, radio, submit, reset, hidden, image")) {
                cancel();
            }
        }
        return;
    }
    String name = element.getAttributeById(HTMLElement.ATTR_NAME);
    String id = element.getAttributeById(HTMLElement.ATTR_ID);
    String value = element.getAttributeById(HTMLElement.ATTR_VALUE);
    if (value == null) {
        value = "";
    }
    Component cmp = null;
    switch(typeID) {
        case INPUT_CHECKBOX:
            CheckBox cb = new CheckBox();
            if (element.getAttributeById(HTMLElement.ATTR_CHECKED) != null) {
                cb.setSelected(true);
            }
            cmp = cb;
            if (curForm != null) {
                curForm.addCheckBox(name, cb, value);
            }
            break;
        case INPUT_HIDDEN:
            if (curForm != null) {
                curForm.addInput(name, value, null);
            }
            break;
        case INPUT_EMAIL:
        case INPUT_TEXT:
        case INPUT_PASSWORD:
            TextField tf = new TextField(value);
            tf.setLeftAndRightEditingTrigger(false);
            if (typeID == INPUT_PASSWORD) {
                tf.setConstraint(TextField.PASSWORD);
            }
            if (typeID == INPUT_EMAIL) {
                tf.setConstraint(TextField.EMAILADDR);
            }
            if (SUPPORT_INPUT_FORMAT) {
                HTMLInputFormat inputFormat = HTMLInputFormat.getInputFormat(element.getAttributeById(HTMLElement.ATTR_FORMAT));
                if (inputFormat != null) {
                    tf = (TextField) inputFormat.applyConstraints(tf);
                    if (curForm != null) {
                        curForm.setInputFormat(tf, inputFormat);
                    }
                }
                String emptyOk = element.getAttributeById(HTMLElement.ATTR_EMPTYOK);
                if ((emptyOk != null) && (curForm != null)) {
                    if (emptyOk.equalsIgnoreCase("true")) {
                        curForm.setEmptyOK(tf, true);
                    } else if (emptyOk.equalsIgnoreCase("false")) {
                        curForm.setEmptyOK(tf, false);
                    }
                }
            }
            int size = getInt(element.getAttributeById(HTMLElement.ATTR_SIZE));
            int maxlen = getInt(element.getAttributeById(HTMLElement.ATTR_MAXLENGTH));
            if (size == 0) {
                size = DEFAULT_TEXTFIELD_SIZE;
            }
            if (maxlen != 0) {
                tf.setMaxSize(maxlen);
                if (size > maxlen) {
                    size = maxlen;
                }
            }
            tf.setPreferredW(tf.getStyle().getFont().stringWidth("W") * size);
            tf.getSelectedStyle().setFont(font.getFont());
            tf.getUnselectedStyle().setFont(font.getFont());
            if ((!PROCESS_HTML_MP1_ONLY) && (element.getAttributeById(HTMLElement.ATTR_READONLY) != null)) {
                tf.setEditable(false);
            }
            cmp = tf;
            if (curForm != null) {
                curForm.addInput(name, cmp, value);
                textfieldsToForms.put(tf, curForm);
            }
            break;
        case INPUT_RADIO:
            RadioButton rb = new RadioButton(" ");
            if (element.getAttributeById(HTMLElement.ATTR_CHECKED) != null) {
                rb.setSelected(true);
            }
            cmp = rb;
            if (curForm != null) {
                curForm.addRadioButton(name, rb, value);
            }
            break;
        case INPUT_RESET:
            Command resetCmd = null;
            if (curForm != null) {
                resetCmd = curForm.createResetCommand(value);
            }
            if (resetCmd == null) {
                // dummy command - no form so it won't do anything
                resetCmd = new Command(getUIManager().localize("html.reset", HTMLForm.DEFAULT_RESET_TEXT));
            }
            Button resetButton = new Button(resetCmd);
            cmp = resetButton;
            break;
        case INPUT_BUTTON:
        case INPUT_SUBMIT:
            Command submitCmd = null;
            if (curForm != null) {
                submitCmd = curForm.createSubmitCommand(name, value);
            }
            if (submitCmd == null) {
                // dummy command - no form so it won't do anything
                submitCmd = new Command(value.equals("") ? value = getUIManager().localize("html.submit", HTMLForm.DEFAULT_SUBMIT_TEXT) : value);
            }
            Button submitButton = new Button(submitCmd);
            cmp = submitButton;
            break;
        case // Image submit is not officially supported in XHTML-MP 1.0 but was added anyway, but pixel data submission is not supported (i.e. name.x=xx&name.y=yy)
        INPUT_IMAGE:
            submitCmd = null;
            if (curForm != null) {
                submitCmd = curForm.createSubmitCommand(name, value);
            }
            handleImage(element, align, submitCmd);
            break;
    }
    if (cmp != null) {
        if ((!PROCESS_HTML_MP1_ONLY) && (element.getAttributeById(HTMLElement.ATTR_DISABLED) != null)) {
            cmp.setEnabled(false);
        }
        String aKey = element.getAttributeById(HTMLElement.ATTR_ACCESSKEY);
        if ((aKey != null) && (aKey.length() == 1)) {
            // accessKeys.put(new Integer(aKey.charAt(0)), cmp);
            addAccessKey(aKey.charAt(0), cmp, false);
        }
        if (eventsListener != null) {
            eventsListener.registerComponent(cmp, element);
        }
        // Even if CSS is off, we need to associate it for HTMLElement.getCurentValue
        element.setAssociatedComponents(cmp);
        if ((curForm != null) && (curForm.action == null)) {
            // Form that submits to a forbidden link
            cmp.setEnabled(false);
        } else if (firstFocusable == null) {
            firstFocusable = cmp;
        }
        if (id != null) {
            inputFields.put(id, cmp);
        }
    }
    addCmp(cmp, align);
}
Also used : Command(com.codename1.ui.Command) Button(com.codename1.ui.Button) RadioButton(com.codename1.ui.RadioButton) CheckBox(com.codename1.ui.CheckBox) TextField(com.codename1.ui.TextField) RadioButton(com.codename1.ui.RadioButton) Component(com.codename1.ui.Component)

Example 38 with Command

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

the class Toolbar method createOverflowCommandList.

/**
 * Creates the list component containing the commands within the given
 * vector used for showing the menu dialog
 *
 * @param commands list of command objects
 * @return List object
 */
protected List createOverflowCommandList(Vector commands) {
    List l = new List(commands);
    l.setUIID("CommandList");
    Component c = (Component) l.getRenderer();
    c.setUIID("Command");
    c = l.getRenderer().getListFocusComponent(l);
    c.setUIID("CommandFocus");
    l.setFixedSelection(List.FIXED_NONE_CYCLIC);
    l.setScrollVisible(false);
    ((DefaultListCellRenderer) l.getRenderer()).setShowNumbers(false);
    return l;
}
Also used : DefaultListCellRenderer(com.codename1.ui.list.DefaultListCellRenderer) ArrayList(java.util.ArrayList)

Example 39 with Command

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

the class SideMenuBar method createSideNavigationPanel.

Container createSideNavigationPanel(Vector commands, String placement) {
    Container menu = constructSideNavigationComponent();
    if (getUIManager().isThemeConstant("paintsTitleBarBool", false)) {
        Container bar = new Container();
        bar.setUIID("StatusBarSideMenu");
        addComponentToSideMenu(menu, bar);
    }
    if (!getUIManager().isThemeConstant("sideMenuTensileDragBool", true)) {
        menu.setTensileDragEnabled(false);
    }
    for (int iter = commands.size() - 1; iter > -1; iter--) {
        Command c = (Command) commands.elementAt(iter);
        if (c.getClientProperty(COMMAND_PLACEMENT_KEY) != placement) {
            continue;
        }
        Component cmp = (Component) c.getClientProperty(COMMAND_SIDE_COMPONENT);
        if (cmp != null) {
            if (cmp.getParent() != null) {
                cmp.getParent().removeAll();
            }
            if (c.getClientProperty(COMMAND_ACTIONABLE) != null && c.getClientProperty(COMMAND_ACTIONABLE).equals(Boolean.TRUE)) {
                Container cnt = new Container(new BorderLayout());
                cnt.addComponent(BorderLayout.CENTER, cmp);
                Button btn = createTouchCommandButton(c);
                btn.setParent(cnt);
                cnt.setLeadComponent(btn);
                addComponentToSideMenu(menu, cnt);
            } else {
                addComponentToSideMenu(menu, cmp);
            }
            initTitleBarStatus();
        } else {
            // special case: hide back button that doesn't have text, icon or a side component entry
            if (parent.getBackCommand() == c && (c.getCommandName() == null || c.getCommandName().length() == 0) && c.getIcon() == null) {
                continue;
            }
            addComponentToSideMenu(menu, createTouchCommandButton(c));
        }
    }
    boolean isRTLValue = isRTL();
    if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
        isRTLValue = !isRTLValue;
    }
    UIManager uim = menu.getUIManager();
    boolean shadowEnabled = uim.isThemeConstant("sideMenuShadowBool", true);
    Image sh = (Image) uim.getThemeImageConstant("sideMenuShadowImage");
    if (sh == null && shadowEnabled) {
        sh = Resources.getSystemResource().getImage("sidemenu-shadow.png");
    }
    if (isRTLValue && sh != null) {
        sh = sh.flipHorizontally(true);
    }
    final Image shadow = sh;
    if (shadow == null) {
        return menu;
    } else {
        Container main = new Container(new LayeredLayout());
        Label shadowLabel = new Label();
        shadowLabel.getStyle().setBackgroundType(Style.BACKGROUND_IMAGE_TILE_VERTICAL_ALIGN_CENTER);
        shadowLabel.getStyle().setBgImage(shadow);
        shadowLabel.getStyle().setPadding(0, 0, 0, 0);
        shadowLabel.getStyle().setMargin(0, 0, 0, 0);
        shadowLabel.getStyle().setBgTransparency(0);
        Container c = new Container(new BorderLayout());
        if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
            shadowLabel.setPreferredW(shadow.getWidth());
            c.addComponent(BorderLayout.WEST, shadowLabel);
            shadowLabel.getStyle().setBgImage(shadow.rotate180Degrees(true));
        } else {
            if (placement == COMMAND_PLACEMENT_VALUE_TOP) {
            // shadowLabel.setPreferredH(shadow.getHeight());
            // c.addComponent(BorderLayout.SOUTH, shadowLabel);
            // shadowLabel.getStyle().setBgImage(shadow.rotate90Degrees(true));
            } else {
                shadowLabel.setPreferredW(shadow.getWidth());
                c.addComponent(BorderLayout.EAST, shadowLabel);
            }
        }
        main.addComponent(menu);
        main.addComponent(c);
        return main;
    }
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) UIManager(com.codename1.ui.plaf.UIManager) LayeredLayout(com.codename1.ui.layouts.LayeredLayout)

Example 40 with Command

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

the class SideMenuBar method installRightCommands.

void installRightCommands() {
    if (rightCommands != null) {
        for (int i = 0; i < rightCommands.size(); i++) {
            Command rightCommand = (Command) rightCommands.get(rightCommands.size() - 1 - i);
            String uiid = (String) rightCommand.getClientProperty("uiid");
            if (uiid == null) {
                uiid = "TitleCommand";
            }
            int txtPosition = Component.RIGHT;
            Integer pos = (Integer) rightCommand.getClientProperty("textPosition");
            if (pos != null) {
                txtPosition = pos.intValue();
            }
            Layout l = getTitleAreaContainer().getLayout();
            if (l instanceof BorderLayout) {
                final Button b = new Button(rightCommand);
                b.setUIID(uiid);
                b.putClientProperty("TitleCommand", Boolean.TRUE);
                b.setTextPosition(txtPosition);
                BorderLayout bl = (BorderLayout) l;
                final Component east = bl.getEast();
                if (east == null) {
                    getTitleAreaContainer().addComponent(BorderLayout.EAST, b);
                } else {
                    if (east instanceof Container) {
                        Container cnt = (Container) east;
                        // check if this command is already added
                        boolean shouldAdd = true;
                        for (int j = 0; j < cnt.getComponentCount(); j++) {
                            Component c = cnt.getComponentAt(j);
                            if (c instanceof Button) {
                                Command cc = ((Button) c).getCommand();
                                if (cc != null && cc.equals(b.getCommand())) {
                                    shouldAdd = false;
                                    break;
                                }
                            }
                        }
                        if (shouldAdd) {
                            cnt.addComponent(b);
                        }
                    } else {
                        if (east instanceof Button) {
                            Command cc = ((Button) east).getCommand();
                            if (cc != null && cc.equals(b.getCommand())) {
                                continue;
                            }
                        }
                        east.getParent().removeComponent(east);
                        Container buttons = new Container(new BoxLayout(BoxLayout.X_AXIS));
                        buttons.addComponent(east);
                        buttons.addComponent(b);
                        getTitleAreaContainer().addComponent(BorderLayout.EAST, buttons);
                    }
                }
            }
        }
    }
    initTitleBarStatus();
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) Layout(com.codename1.ui.layouts.Layout) BoxLayout(com.codename1.ui.layouts.BoxLayout) LayeredLayout(com.codename1.ui.layouts.LayeredLayout) BorderLayout(com.codename1.ui.layouts.BorderLayout) BoxLayout(com.codename1.ui.layouts.BoxLayout)

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