Search in sources :

Example 21 with Command

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

the class TestRecorder method eventPointerReleased.

void eventPointerReleased(int x, int y) {
    if (isRecording()) {
        com.codename1.ui.Component cmp = Display.getInstance().getCurrent().getComponentAt(x, y);
        if (isListComponent(cmp)) {
            return;
        }
        if (dragged) {
            if (dragToScroll.isSelected()) {
                com.codename1.ui.Component scrollTo;
                if (y > pointerPressedY) {
                    scrollTo = findLowestVisibleComponent();
                } else {
                    scrollTo = findHighestVisibleComponent();
                }
                if (scrollTo != null && scrollTo != Display.getInstance().getCurrent() && scrollTo != Display.getInstance().getCurrent().getContentPane()) {
                    String name = scrollTo.getName();
                    if (name != null) {
                        generatedCode += "        ensureVisible(\"" + name + "\");\n";
                    } else {
                        String pp = getPathToComponent(scrollTo);
                        if (pp == null) {
                            return;
                        }
                        generatedCode += "        ensureVisible(" + pp + ");\n";
                    }
                    updateTestCode();
                }
            } else {
                addWaitStatement();
                generatedCode += "        pointerRelease" + generatePointerEventArguments(x, y);
                updateTestCode();
            }
        } else {
            if (isToolbarComponent(cmp)) {
                if (cmp instanceof com.codename1.ui.Button) {
                    Command cmd = ((com.codename1.ui.Button) cmp).getCommand();
                    if (cmd != null) {
                        int offset = 0;
                        Command[] commands = TestUtils.getToolbarCommands();
                        for (Command c : commands) {
                            if (c == cmd) {
                                generatedCode += "        assertEqual(getToolbarCommands().length, " + commands.length + ");\n";
                                generatedCode += "        executeToolbarCommandAtOffset(" + offset + ");\n";
                                updateTestCode();
                                return;
                            }
                            offset++;
                        }
                    } else {
                        if (cmp.getUIID().equals("MenuButton")) {
                            // side menu button
                            generatedCode += "        showSidemenu();\n";
                            updateTestCode();
                            return;
                        }
                    }
                }
            }
            if (cmp instanceof com.codename1.ui.Button) {
                com.codename1.ui.Button btn = (com.codename1.ui.Button) cmp;
                // special case for back command on iOS
                if (btn.getCommand() != null && btn.getCommand() == Display.getInstance().getCurrent().getBackCommand()) {
                    generatedCode += "        goBack();\n";
                } else {
                    if (btn.getName() != null && btn.getName().length() > 0) {
                        generatedCode += "        clickButtonByName(\"" + btn.getName() + "\");\n";
                    } else {
                        if (btn.getText() != null && btn.getText().length() > 0) {
                            generatedCode += "        clickButtonByLabel(\"" + btn.getText() + "\");\n";
                        } else {
                            String pp = getPathToComponent(cmp);
                            if (pp == null || pp.equals("(String)null")) {
                                return;
                            }
                            generatedCode += "        clickButtonByPath(" + pp + ");\n";
                        }
                    }
                }
                updateTestCode();
                return;
            }
            if (cmp instanceof com.codename1.ui.TextArea) {
                // ignore this, its probably initiating edit which we will capture soon
                return;
            }
            generatedCode += "        pointerPress" + generatePointerEventArguments(pointerPressedX, pointerPressedY);
            addWaitStatement();
            generatedCode += "        pointerRelease" + generatePointerEventArguments(x, y);
            updateTestCode();
        }
    }
}
Also used : Command(com.codename1.ui.Command) Component(com.codename1.ui.Component)

Example 22 with Command

use of com.codename1.ui.Command 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 23 with Command

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

the class Dialog method placeButtonCommands.

/**
 * Places the given commands in the dialog command area, this is very useful for touch devices.
 *
 * @param cmds the commands to place
 * @deprecated this method shouldn't be invoked externally, it should have been private
 */
public void placeButtonCommands(Command[] cmds) {
    buttonCommands = cmds;
    Container buttonArea;
    if (getUIManager().isThemeConstant("dlgCommandGridBool", false)) {
        buttonArea = new Container(new GridLayout(1, cmds.length));
    } else {
        buttonArea = new Container(new FlowLayout(CENTER));
    }
    buttonArea.setUIID("DialogCommandArea");
    String uiid = getUIManager().getThemeConstant("dlgButtonCommandUIID", null);
    addButtonBar(buttonArea);
    if (cmds.length > 0) {
        String lineColor = getUIManager().getThemeConstant("dlgInvisibleButtons", null);
        if (cmds.length > 3) {
            lineColor = null;
        }
        int largest = Integer.parseInt(getUIManager().getThemeConstant("dlgCommandButtonSizeInt", "0"));
        for (int iter = 0; iter < cmds.length; iter++) {
            Button b = new Button(cmds[iter]);
            if (uiid != null) {
                b.setUIID(uiid);
            }
            // special case for dialog butons uppercase on Android
            if (Button.isCapsTextDefault()) {
                b.setCapsText(true);
            }
            largest = Math.max(b.getPreferredW(), largest);
            if (lineColor != null && lineColor.length() > 0) {
                int color = Integer.parseInt(lineColor, 16);
                Border brd = null;
                if (iter < cmds.length - 1) {
                    brd = Border.createCompoundBorder(Border.createLineBorder(1, color), null, null, Border.createLineBorder(1, color));
                } else {
                    brd = Border.createCompoundBorder(Border.createLineBorder(1, color), null, null, null);
                }
                b.getUnselectedStyle().setBorder(brd);
                b.getSelectedStyle().setBorder(brd);
                b.getPressedStyle().setBorder(brd);
            }
            buttonArea.addComponent(b);
        }
        for (int iter = 0; iter < cmds.length; iter++) {
            buttonArea.getComponentAt(iter).setPreferredW(largest);
        }
        buttonArea.getComponentAt(0).requestFocus();
    }
}
Also used : GridLayout(com.codename1.ui.layouts.GridLayout) FlowLayout(com.codename1.ui.layouts.FlowLayout) Border(com.codename1.ui.plaf.Border)

Example 24 with Command

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

the class Dialog method show.

/**
 * Shows a modal dialog with the given component as its "body" placed in the
 * center.
 *
 * @param title title for the dialog
 * @param body component placed in the center of the dialog
 * @param defaultCommand command to be assigned as the default command or null
 * @param cmds commands that are added to the form any click on any command
 * will dispose the form
 * @param type the type of the alert one of TYPE_WARNING, TYPE_INFO,
 * TYPE_ERROR, TYPE_CONFIRMATION or TYPE_ALARM
 * @param icon the icon for the dialog, can be null
 * @param timeout a timeout after which null would be returned if timeout is 0 inifinite time is used
 * @param transition the transition installed when the dialog enters/leaves
 * @return the command pressed by the user
 */
public static Command show(String title, Component body, Command defaultCommand, Command[] cmds, int type, Image icon, long timeout, Transition transition) {
    Dialog dialog = new Dialog(title);
    dialog.dialogType = type;
    dialog.setTransitionInAnimator(transition);
    dialog.setTransitionOutAnimator(transition);
    dialog.lastCommandPressed = null;
    dialog.setLayout(new BorderLayout());
    if (cmds != null) {
        if (commandsAsButtons) {
            dialog.placeButtonCommands(cmds);
        } else {
            for (int iter = 0; iter < cmds.length; iter++) {
                dialog.addCommand(cmds[iter]);
            }
        }
        // maps the first command to back
        if (cmds.length == 1 || cmds.length == 2) {
            dialog.setBackCommand(cmds[0]);
        }
    }
    if (defaultCommand != null) {
        dialog.setDefaultCommand(defaultCommand);
    }
    dialog.addComponent(BorderLayout.CENTER, body);
    if (icon != null) {
        dialog.addComponent(BorderLayout.EAST, new Label(icon));
    }
    if (timeout != 0) {
        dialog.setTimeout(timeout);
    }
    if (body.isScrollable() || disableStaticDialogScrolling) {
        dialog.setScrollable(false);
    }
    dialog.show();
    return dialog.lastCommandPressed;
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout)

Example 25 with Command

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

the class Dialog method showPopupDialog.

/**
 * A popup dialog is shown with the context of a component and  its selection, it is disposed seamlessly if the back button is pressed
 * or if the user touches outside its bounds. It can optionally provide an arrow in the theme to point at the context component. The popup
 * dialog has the PopupDialog style by default.
 *
 * @param rect the screen rectangle to which the popup should point
 * @return the command that might have been triggered by the user within the dialog if commands are placed in the dialog
 */
public Command showPopupDialog(Rectangle rect) {
    if (getDialogUIID().equals("Dialog")) {
        setDialogUIID("PopupDialog");
        if (getTitleComponent().getUIID().equals("DialogTitle")) {
            getTitleComponent().setUIID("PopupDialogTitle");
        }
        getContentPane().setUIID("PopupContentPane");
    }
    disposeOnRotation = true;
    disposeWhenPointerOutOfBounds = true;
    Command backCommand = null;
    if (getBackCommand() == null) {
        backCommand = new Command("Back");
        setBackCommand(backCommand);
    }
    Component contentPane = super.getContentPane();
    Label title = super.getTitleComponent();
    int menuHeight = calcMenuHeight();
    UIManager manager = getUIManager();
    // preferred size logic of the dialog won't work with large title borders
    if (dialogTitle != null && manager.isThemeConstant("hideEmptyTitleBool", false)) {
        boolean b = getTitle().length() > 0;
        getTitleArea().setVisible(b);
        getTitleComponent().setVisible(b);
        if (!b && manager.isThemeConstant("shrinkPopupTitleBool", true)) {
            getTitleComponent().setPreferredSize(new Dimension(0, 0));
            getTitleComponent().getStyle().setBorder(null);
            getTitleArea().setPreferredSize(new Dimension(0, 0));
            if (getContentPane().getClientProperty("$ENLARGED_POP") == null) {
                getContentPane().putClientProperty("$ENLARGED_POP", Boolean.TRUE);
                int cpPaddingTop = getContentPane().getStyle().getPaddingTop();
                int titlePT = getTitleComponent().getStyle().getPaddingTop();
                byte[] pu = getContentPane().getStyle().getPaddingUnit();
                if (pu == null) {
                    pu = new byte[4];
                }
                pu[0] = Style.UNIT_TYPE_PIXELS;
                getContentPane().getStyle().setPaddingUnit(pu);
                int pop = Display.getInstance().convertToPixels(manager.getThemeConstant("popupNoTitleAddPaddingInt", 1), false);
                getContentPane().getStyle().setPadding(TOP, pop + cpPaddingTop + titlePT);
            }
        }
    }
    // allows a text area to recalculate its preferred size if embedded within a dialog
    revalidate();
    Style contentPaneStyle = getDialogStyle();
    boolean restoreArrow = false;
    if (manager.isThemeConstant(getDialogUIID() + "ArrowBool", false)) {
        Image t = manager.getThemeImageConstant(getDialogUIID() + "ArrowTopImage");
        Image b = manager.getThemeImageConstant(getDialogUIID() + "ArrowBottomImage");
        Image l = manager.getThemeImageConstant(getDialogUIID() + "ArrowLeftImage");
        Image r = manager.getThemeImageConstant(getDialogUIID() + "ArrowRightImage");
        Border border = contentPaneStyle.getBorder();
        if (border != null) {
            border.setImageBorderSpecialTile(t, b, l, r, rect);
            restoreArrow = true;
        }
    }
    int prefHeight = contentPane.getPreferredH();
    int prefWidth = contentPane.getPreferredW();
    if (contentPaneStyle.getBorder() != null) {
        prefWidth = Math.max(contentPaneStyle.getBorder().getMinimumWidth(), prefWidth);
        prefHeight = Math.max(contentPaneStyle.getBorder().getMinimumHeight(), prefHeight);
    }
    prefWidth += getUIManager().getLookAndFeel().getVerticalScrollWidth();
    int availableHeight = Display.getInstance().getDisplayHeight() - menuHeight - title.getPreferredH();
    int availableWidth = Display.getInstance().getDisplayWidth();
    int width = Math.min(availableWidth, prefWidth);
    int x = 0;
    int y = 0;
    Command result;
    boolean showPortrait;
    if (popupDirectionBiasPortrait != null) {
        showPortrait = popupDirectionBiasPortrait.booleanValue();
    } else {
        showPortrait = Display.getInstance().isPortrait();
    }
    // if we don't have enough space then disregard device orientation
    if (showPortrait) {
        if (availableHeight < (availableWidth - rect.getWidth()) / 2) {
            showPortrait = false;
        }
    } else {
        if (availableHeight / 2 > availableWidth - rect.getWidth()) {
            showPortrait = true;
        }
    }
    if (showPortrait) {
        if (width < availableWidth) {
            int idealX = rect.getX() - width / 2 + rect.getSize().getWidth() / 2;
            // if the ideal position is less than 0 just use 0
            if (idealX > 0) {
                // if the idealX is too far to the right just align to the right
                if (idealX + width > availableWidth) {
                    x = availableWidth - width;
                } else {
                    x = idealX;
                }
            }
        }
        if (rect.getY() < availableHeight / 2) {
            // popup downwards
            y = rect.getY() + rect.getSize().getHeight();
            int height = Math.min(prefHeight, availableHeight - y);
            result = show(y, availableHeight - height - y, x, availableWidth - width - x, true, true);
        } else {
            // popup upwards
            int height = Math.min(prefHeight, availableHeight - (availableHeight - rect.getY()));
            y = rect.getY() - height;
            result = show(y, availableHeight - height - y, x, availableWidth - width - x, true, true);
        }
    } else {
        int height = Math.min(prefHeight, availableHeight);
        if (height < availableHeight) {
            int idealY = rect.getY() - height / 2 + rect.getSize().getHeight() / 2;
            // if the ideal position is less than 0 just use 0
            if (idealY > 0) {
                // if the idealY is too far up just align to the top
                if (idealY + height > availableHeight) {
                    y = availableHeight - height;
                } else {
                    y = idealY;
                }
            }
        }
        if (prefWidth > rect.getX()) {
            // popup right
            x = rect.getX() + rect.getSize().getWidth();
            if (x + prefWidth > availableWidth) {
                x = availableWidth - prefWidth;
            }
            width = Math.min(prefWidth, availableWidth - x);
            result = show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x), true, true);
        } else {
            // popup left
            width = Math.min(prefWidth, availableWidth - (availableWidth - rect.getX()));
            x = rect.getX() - width;
            result = show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x), true, true);
        }
    }
    if (restoreArrow) {
        contentPaneStyle.getBorder().clearImageBorderSpecialTile();
    }
    if (result == backCommand) {
        return null;
    }
    return result;
}
Also used : UIManager(com.codename1.ui.plaf.UIManager) Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension) Border(com.codename1.ui.plaf.Border)

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