Search in sources :

Example 11 with Result

use of com.codename1.rad.processing.Result 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;
        }
    } else {
        Border border = contentPaneStyle.getBorder();
        if (border != null) {
            border.setTrackComponent(rect);
        }
    }
    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
            if (getDialogUIID().equals("PopupDialog") && isUIIDByPopupPosition) {
                getContentPane().setUIID("PopupContentPaneDownwards");
            }
            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
            if (getDialogUIID().equals("PopupDialog") && isUIIDByPopupPosition) {
                getContentPane().setUIID("PopupContentPaneUpwards");
            }
            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
            if (getDialogUIID().equals("PopupDialog") && isUIIDByPopupPosition) {
                getContentPane().setUIID("PopupContentPaneRight");
            }
            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
            if (getDialogUIID().equals("PopupDialog") && isUIIDByPopupPosition) {
                getContentPane().setUIID("PopupContentPaneLeft");
            }
            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)

Example 12 with Result

use of com.codename1.rad.processing.Result in project CodenameOne by codenameone.

the class TextArea method keyPressed.

/**
 * {@inheritDoc}
 */
public void keyPressed(int keyCode) {
    super.keyPressed(keyCode);
    setSuppressActionEvent(false);
    int action = com.codename1.ui.Display.getInstance().getGameAction(keyCode);
    // this works around a bug where fire is also a softkey on devices such as newer Nokia
    // series 40's (e.g. the Nokia emulator). It closes its native text box on fire then
    // as a result of a Nokia bug we get the key released of that closing and assume the
    // users wants to edit the text... When means the only way to exit the native text box
    // is via the cancel option (after pressing OK once).
    triggerClose = action == Display.GAME_FIRE;
    // scroll the TextArea
    Rectangle rect = new Rectangle(getScrollX(), getScrollY(), getWidth(), getHeight());
    Font textFont = getStyle().getFont();
    if (action == Display.GAME_DOWN) {
        if ((getScrollY() + getHeight()) < (rowsGap + getStyle().getFont().getHeight()) * getLines()) {
            rect.setY(rect.getY() + (textFont.getHeight() + rowsGap) * linesToScroll);
            scrollRectToVisible(rect, this);
        } else {
            setHandlesInput(false);
        }
    } else {
        if (action == Display.GAME_UP) {
            if (getScrollY() > 0) {
                rect.setY(Math.max(0, rect.getY() - (textFont.getHeight() + rowsGap) * linesToScroll));
                scrollRectToVisible(rect, this);
            } else {
                setHandlesInput(false);
            }
        }
    }
    if (action == Display.GAME_RIGHT || action == Display.GAME_LEFT) {
        setHandlesInput(false);
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle)

Example 13 with Result

use of com.codename1.rad.processing.Result 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 14 with Result

use of com.codename1.rad.processing.Result in project CodenameOne by codenameone.

the class ArrayTests2768 method start.

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    Form hi = new Form("Hi World", BoxLayout.y());
    Button b = new Button("Run Test");
    SpanLabel result = new SpanLabel();
    b.addActionListener(e -> {
        try {
            if (new MyTest().runTest()) {
                result.setText("Success");
            } else {
                result.setText("Failed");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setText("Failed: " + ex.getMessage());
        }
        hi.revalidateWithAnimationSafety();
    });
    hi.addAll(b, result);
    hi.show();
}
Also used : Form(com.codename1.ui.Form) Button(com.codename1.ui.Button) SpanLabel(com.codename1.components.SpanLabel) IOException(java.io.IOException)

Example 15 with Result

use of com.codename1.rad.processing.Result in project CodenameOne by codenameone.

the class FingerprintScannerSample method start.

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    Form hi = new Form("Hi World", BoxLayout.y());
    TextField keyName = new TextField();
    TextField keyValue = new TextField();
    Button addItem = new Button("Add Item");
    Button getItem = new Button("Get Item");
    Button deleteItem = new Button("Delete Item");
    Button checkAvailable = new Button("Check Available");
    checkAvailable.addActionListener(evt -> {
        Container cnt = BoxLayout.encloseY(new Label("TouchID: " + Fingerprint.isTouchIDAvailable()), new Label("FaceID: " + Fingerprint.isFaceIDAvailable()));
        Dialog.show("Capabilities", cnt, new Command("OK"));
    });
    addItem.addActionListener(evt -> {
        if (!Fingerprint.isAvailable()) {
            ToastBar.showErrorMessage("Fingerprint not avaiable on this platform");
            return;
        }
        Fingerprint.addPassword("Adding secure item to keystore", keyName.getText(), keyValue.getText()).onResult((res, err) -> {
            if (err != null) {
                if (AsyncResource.isCancelled(err)) {
                    Log.p("addPassword request was cancelled");
                    return;
                }
                Log.e(err);
                ToastBar.showErrorMessage(err.getMessage());
            } else {
                ToastBar.showInfoMessage("Result: " + res);
            }
        });
    });
    getItem.addActionListener(evt -> {
        Fingerprint.getPassword("Getting secure item", keyName.getText()).onResult((res, err) -> {
            if (err != null) {
                if (AsyncResource.isCancelled(err)) {
                    Log.p("getPassword request was cancelled");
                    return;
                }
                Log.e(err);
                if (err instanceof KeyRevokedException) {
                    ToastBar.showErrorMessage("Your key has been invalidated, likely due to adding new fingerprints");
                } else {
                    ToastBar.showErrorMessage(err.getMessage());
                }
            } else {
                keyValue.setText(res);
                hi.revalidateWithAnimationSafety();
            }
        });
    });
    deleteItem.addActionListener(evt -> {
        Fingerprint.deletePassword("Getting secure item", keyName.getText()).onResult((res, err) -> {
            if (err != null) {
                if (AsyncResource.isCancelled(err)) {
                    Log.p("deletePassword request was cancelled");
                }
                Log.e(err);
                ToastBar.showErrorMessage(err.getMessage());
            } else {
                keyValue.setText("");
                hi.revalidateWithAnimationSafety();
            }
        });
    });
    Button checkHardware = new Button("Check Hardware");
    checkHardware.addActionListener(evt -> {
        Fingerprint.isAvailable();
        Dialog.show("Hardware", CN.getProperty("Fingerprint.types", "None"), "OK", null);
    });
    hi.addAll(checkHardware, new Label("Key:"), keyName, new Label("Value: "), keyValue, addItem, getItem, deleteItem, checkAvailable);
    hi.show();
}
Also used : Container(com.codename1.ui.Container) KeyRevokedException(com.codename1.fingerprint.KeyRevokedException) Form(com.codename1.ui.Form) Button(com.codename1.ui.Button) Command(com.codename1.ui.Command) Label(com.codename1.ui.Label) TextField(com.codename1.ui.TextField)

Aggregations

IOException (java.io.IOException)19 ArrayList (java.util.ArrayList)14 Form (com.codename1.ui.Form)12 Button (com.codename1.ui.Button)11 Map (java.util.Map)9 Date (java.util.Date)8 HashMap (java.util.HashMap)8 Label (com.codename1.ui.Label)7 BorderLayout (com.codename1.ui.layouts.BorderLayout)7 List (java.util.List)7 Container (com.codename1.ui.Container)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 OutputStream (java.io.OutputStream)6 JSONParser (com.codename1.io.JSONParser)5 Result (com.codename1.rad.processing.Result)5 ActionEvent (com.codename1.ui.events.ActionEvent)5 File (java.io.File)5 Cursor (com.codename1.db.Cursor)4 ConnectionRequest (com.codename1.io.ConnectionRequest)4 Entity (com.codename1.rad.models.Entity)4