Search in sources :

Example 61 with Command

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

the class Toolbar method constructOnTopSideMenu.

private void constructOnTopSideMenu() {
    if (sidemenuDialog == null) {
        permanentSideMenuContainer = constructSideNavigationComponent();
        final Form parent = getComponentForm();
        parent.addPointerPressedListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (Display.getInstance().getImplementation().isScrollWheeling()) {
                    return;
                }
                if (sidemenuDialog.isShowing()) {
                    if (evt.getX() > sidemenuDialog.getWidth()) {
                        parent.putClientProperty("cn1$sidemenuCharged", Boolean.FALSE);
                        closeSideMenu();
                        evt.consume();
                    } else {
                        if (evt.getX() + Display.getInstance().convertToPixels(8) > sidemenuDialog.getWidth()) {
                            parent.putClientProperty("cn1$sidemenuCharged", Boolean.TRUE);
                        } else {
                            parent.putClientProperty("cn1$sidemenuCharged", Boolean.FALSE);
                        }
                        if (!isComponentInOnTopSidemenu(parent.getComponentAt(evt.getX(), evt.getY()))) {
                            evt.consume();
                        }
                    }
                } else {
                    int displayWidth = Display.getInstance().getDisplayWidth();
                    final int sensitiveSection = displayWidth / getUIManager().getThemeConstant("sideSwipeSensitiveInt", 10);
                    if (evt.getX() < sensitiveSection) {
                        parent.putClientProperty("cn1$sidemenuCharged", Boolean.TRUE);
                        evt.consume();
                    } else {
                        parent.putClientProperty("cn1$sidemenuCharged", Boolean.FALSE);
                        permanentSideMenuContainer.pointerPressed(evt.getX(), evt.getY());
                    }
                }
            }
        });
        parent.addPointerDraggedListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (Display.getInstance().getImplementation().isScrollWheeling()) {
                    return;
                }
                Boolean b = (Boolean) parent.getClientProperty("cn1$sidemenuCharged");
                if (b != null && b.booleanValue()) {
                    parent.putClientProperty("cn1$sidemenuActivated", Boolean.TRUE);
                    showOnTopSidemenu(evt.getX(), false);
                    evt.consume();
                } else {
                    if (sidemenuDialog.isShowing()) {
                        permanentSideMenuContainer.pointerDragged(evt.getX(), evt.getY());
                        evt.consume();
                    }
                }
            }
        });
        parent.addPointerReleasedListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (Display.getInstance().getImplementation().isScrollWheeling()) {
                    return;
                }
                Boolean b = (Boolean) parent.getClientProperty("cn1$sidemenuActivated");
                if (b != null && b.booleanValue()) {
                    parent.putClientProperty("cn1$sidemenuActivated", null);
                    if (evt.getX() < parent.getWidth() / 4) {
                        closeSideMenu();
                    } else {
                        showOnTopSidemenu(-1, true);
                    }
                    evt.consume();
                } else {
                    if (sidemenuDialog.isShowing()) {
                        if (!isComponentInOnTopSidemenu(parent.getComponentAt(evt.getX(), evt.getY()))) {
                            evt.consume();
                        }
                        permanentSideMenuContainer.pointerReleased(evt.getX(), evt.getY());
                    }
                }
            }
        });
        sidemenuDialog = new InteractionDialog(new BorderLayout());
        sidemenuDialog.setFormMode(true);
        sidemenuDialog.setUIID("Container");
        sidemenuDialog.setDialogUIID("Container");
        sidemenuDialog.getTitleComponent().remove();
        sidemenuDialog.add(BorderLayout.CENTER, permanentSideMenuContainer);
        if (sidemenuSouthComponent != null) {
            sidemenuDialog.add(BorderLayout.SOUTH, sidemenuSouthComponent);
        }
        float size = 4.5f;
        try {
            size = Float.parseFloat(getUIManager().getThemeConstant("menuImageSize", "4.5"));
        } catch (Throwable t) {
            Log.e(t);
        }
        if (!parent.getUIManager().isThemeConstant("hideLeftSideMenuBool", false)) {
            Image i = (Image) parent.getUIManager().getThemeImageConstant("sideMenuImage");
            if (i != null) {
                Command cmd = addCommandToLeftBar("", i, new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        if (sidemenuDialog.isShowing()) {
                            closeSideMenu();
                            return;
                        }
                        showOnTopSidemenu(-1, false);
                    }
                });
                Image p = (Image) parent.getUIManager().getThemeImageConstant("sideMenuPressImage");
                if (p != null) {
                    findCommandComponent(cmd).setPressedIcon(p);
                }
            } else {
                addMaterialCommandToLeftBar("", FontImage.MATERIAL_MENU, size, new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        if (sidemenuDialog.isShowing()) {
                            closeSideMenu();
                            return;
                        }
                        showOnTopSidemenu(-1, false);
                    }
                });
            }
        }
    }
}
Also used : InteractionDialog(com.codename1.components.InteractionDialog) ActionListener(com.codename1.ui.events.ActionListener) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 62 with Command

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

the class Form method keyReleased.

/**
 * {@inheritDoc}
 */
public void keyReleased(int keyCode) {
    int game = Display.getInstance().getGameAction(keyCode);
    if (menuBar.handlesKeycode(keyCode)) {
        menuBar.keyReleased(keyCode);
        return;
    }
    // Component focused = focusManager.getFocused();
    if (focused != null) {
        if (focused.getComponentForm() == this) {
            if (focused.isEnabled()) {
                focused.keyReleased(keyCode);
            }
        }
    }
    // prevent the default action from stealing the behavior from the popup/combo box...
    if (game == Display.GAME_FIRE) {
        Command defaultCmd = getDefaultCommand();
        if (defaultCmd != null) {
            defaultCmd.actionPerformed(new ActionEvent(defaultCmd, keyCode));
            actionCommandImpl(defaultCmd);
        }
    }
    fireKeyEvent(keyListeners, keyCode);
    fireKeyEvent(gameKeyListeners, game);
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent)

Example 63 with Command

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

the class SideMenuBar method installMenuBar.

/**
 * {@inheritDoc}
 */
protected void installMenuBar() {
    if (parent.getClientProperty("Menu") != null) {
        return;
    }
    super.installMenuBar();
    if (parent instanceof Dialog) {
        return;
    }
    openButton = createOpenButton();
    openButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            openMenu(null);
        }
    });
    addOpenButton(null, true);
    UIManager uim = parent.getUIManager();
    final boolean[] hasSideMenus = new boolean[2];
    if (uim.isThemeConstant("sideMenuFoldedSwipeBool", true) && parent.getClientProperty("sideMenuFoldedSwipeListeners") == null) {
        pointerDragged = new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (sideSwipePotential && hasSideMenus[0]) {
                    final int x = evt.getX();
                    final int y = evt.getY();
                    if (Math.abs(y - initialDragY) > x - initialDragX) {
                        sideSwipePotential = false;
                        return;
                    }
                    evt.consume();
                    if (x - initialDragX > Display.getInstance().getDisplayWidth() / getUIManager().getThemeConstant("sideSwipeActivationInt", 15)) {
                        draggedX = x;
                        dragActivated = true;
                        parent.pointerReleased(-1, -1);
                        openMenu(null, 0, draggedX, false);
                        initialDragX = 0;
                        initialDragY = 0;
                    }
                    return;
                }
                if (rightSideSwipePotential && (hasSideMenus[1] || (hasSideMenus[0] && isRTL()))) {
                    final int x = evt.getX();
                    final int y = evt.getY();
                    if (x < 0 || Math.abs(y - initialDragY) > initialDragX - x) {
                        rightSideSwipePotential = false;
                        return;
                    }
                    evt.consume();
                    if (initialDragX - x > Display.getInstance().getDisplayWidth() / getUIManager().getThemeConstant("sideSwipeActivationInt", 15)) {
                        draggedX = x;
                        dragActivated = true;
                        parent.pointerReleased(-1, -1);
                        if (isRTL()) {
                            openMenu(null, 0, draggedX, false);
                        } else {
                            openMenu(COMMAND_PLACEMENT_VALUE_RIGHT, 0, draggedX, false);
                        }
                        initialDragX = 0;
                        initialDragY = 0;
                    }
                }
                if (topSwipePotential) {
                    final int x = evt.getX();
                    final int y = evt.getY();
                    if (Math.abs(y - initialDragY) < x - initialDragX) {
                        topSwipePotential = false;
                        return;
                    }
                    evt.consume();
                    if (initialDragY - y > Display.getInstance().getDisplayHeight() / getUIManager().getThemeConstant("sideSwipeActivationInt", 15)) {
                        draggedX = y;
                        dragActivated = true;
                        parent.pointerReleased(-1, -1);
                        openMenu(COMMAND_PLACEMENT_VALUE_TOP, 0, draggedX, false);
                        initialDragX = 0;
                        initialDragY = 0;
                    }
                }
            }
        };
        parent.addPointerDraggedListener(pointerDragged);
        pointerPressed = new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                rightSideSwipePotential = false;
                topSwipePotential = false;
                sideSwipePotential = false;
                if (getCommandCount() == 0) {
                    return;
                }
                if (parent.getCommandCount() == 1) {
                    if (parent.getCommand(0) == parent.getBackCommand()) {
                        return;
                    }
                }
                for (int iter = 0; iter < getCommandCount(); iter++) {
                    Command c = getCommand(iter);
                    String p = (String) c.getClientProperty(COMMAND_PLACEMENT_KEY);
                    if (p == null) {
                        // has left menu
                        hasSideMenus[0] = true;
                    } else if (p.equals(COMMAND_PLACEMENT_VALUE_RIGHT)) {
                        // has right menu
                        hasSideMenus[1] = true;
                    }
                }
                int displayWidth = Display.getInstance().getDisplayWidth();
                if (rightSideButton != null || isRTL()) {
                    rightSideSwipePotential = !transitionRunning && evt.getX() > displayWidth - displayWidth / getUIManager().getThemeConstant("sideSwipeSensitiveInt", 10);
                }
                if (getTitleComponent() instanceof Button) {
                    topSwipePotential = !transitionRunning && evt.getY() < Display.getInstance().getDisplayHeight() / getUIManager().getThemeConstant("sideSwipeSensitiveInt", 10);
                }
                sideSwipePotential = !transitionRunning && evt.getX() < displayWidth / getUIManager().getThemeConstant("sideSwipeSensitiveInt", 10);
                initialDragX = evt.getX();
                initialDragY = evt.getY();
                if (sideSwipePotential || rightSideSwipePotential || topSwipePotential) {
                    Component c = Display.getInstance().getCurrent().getComponentAt(initialDragX, initialDragY);
                    if (c != null && c.shouldBlockSideSwipe()) {
                        sideSwipePotential = false;
                    }
                }
            }
        };
        parent.addPointerPressedListener(pointerPressed);
        parent.putClientProperty("sideMenuFoldedSwipeListeners", "true");
    }
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) UIManager(com.codename1.ui.plaf.UIManager)

Example 64 with Command

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

the class AndroidImplementation method captureAudio.

public void captureAudio(final ActionListener response) {
    if (!checkForPermission(Manifest.permission.RECORD_AUDIO, "This is required to record the audio")) {
        return;
    }
    try {
        final Form current = Display.getInstance().getCurrent();
        final File temp = File.createTempFile("mtmp", ".3gpp");
        temp.deleteOnExit();
        if (recorder != null) {
            recorder.release();
        }
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
        recorder.setOutputFile(temp.getAbsolutePath());
        final Form recording = new Form("Recording");
        recording.setTransitionInAnimator(CommonTransitions.createEmpty());
        recording.setTransitionOutAnimator(CommonTransitions.createEmpty());
        recording.setLayout(new BorderLayout());
        recorder.prepare();
        recorder.start();
        final Label time = new Label("00:00");
        time.getAllStyles().setAlignment(Component.CENTER);
        Font f = Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
        f = f.derive(getDisplayHeight() / 10, Font.STYLE_PLAIN);
        time.getAllStyles().setFont(f);
        recording.addComponent(BorderLayout.CENTER, time);
        recording.registerAnimated(new Animation() {

            long current = System.currentTimeMillis();

            long zero = current;

            int sec = 0;

            public boolean animate() {
                long now = System.currentTimeMillis();
                if (now - current > 1000) {
                    current = now;
                    sec++;
                    return true;
                }
                return false;
            }

            public void paint(Graphics g) {
                int seconds = sec % 60;
                int minutes = sec / 60;
                String secStr = seconds < 10 ? "0" + seconds : "" + seconds;
                String minStr = minutes < 10 ? "0" + minutes : "" + minutes;
                String txt = minStr + ":" + secStr;
                time.setText(txt);
            }
        });
        Container south = new Container(new com.codename1.ui.layouts.GridLayout(1, 2));
        Command cancel = new Command("Cancel") {

            @Override
            public void actionPerformed(ActionEvent evt) {
                if (recorder != null) {
                    recorder.stop();
                    recorder.release();
                    recorder = null;
                }
                current.showBack();
                response.actionPerformed(null);
            }
        };
        recording.setBackCommand(cancel);
        south.add(new com.codename1.ui.Button(cancel));
        south.add(new com.codename1.ui.Button(new Command("Save") {

            @Override
            public void actionPerformed(ActionEvent evt) {
                if (recorder != null) {
                    recorder.stop();
                    recorder.release();
                    recorder = null;
                }
                current.showBack();
                response.actionPerformed(new ActionEvent(temp.getAbsolutePath()));
            }
        }));
        recording.addComponent(BorderLayout.SOUTH, south);
        recording.show();
    } catch (IOException ex) {
        ex.printStackTrace();
        throw new RuntimeException("failed to start audio recording");
    }
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent) IOException(java.io.IOException) Font(com.codename1.ui.Font) Paint(android.graphics.Paint) BorderLayout(com.codename1.ui.layouts.BorderLayout) com.codename1.ui(com.codename1.ui) Animation(com.codename1.ui.animations.Animation) MediaRecorder(android.media.MediaRecorder) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 65 with Command

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

the class ComboBox method showPopupDialog.

/**
 * Shows the popup dialog for the combo box and returns the resulting command.
 * This method can be overriden by subclasses to modify the behavior of the class.
 *
 * @param popupDialog the popup dialog
 * @param l the list within
 * @return the selected command
 */
protected Command showPopupDialog(Dialog popupDialog, List l) {
    if (getUIManager().isThemeConstant("popupTitleBool", false)) {
        if (getLabelForComponent() != null) {
            popupDialog.setTitle(getLabelForComponent().getText());
        }
    }
    if (includeSelectCancel) {
        popupDialog.setBackCommand(popupDialog.getMenuBar().getCancelMenuItem());
        if (Display.getInstance().isTouchScreenDevice()) {
            if (getUIManager().isThemeConstant("popupCancelBodyBool", false)) {
                popupDialog.placeButtonCommands(new Command[] { popupDialog.getMenuBar().getCancelMenuItem() });
            }
        } else {
            if (Display.getInstance().isThirdSoftButton()) {
                popupDialog.addCommand(popupDialog.getMenuBar().getSelectMenuItem());
                popupDialog.addCommand(popupDialog.getMenuBar().getCancelMenuItem());
            } else {
                popupDialog.addCommand(popupDialog.getMenuBar().getCancelMenuItem());
                popupDialog.addCommand(popupDialog.getMenuBar().getSelectMenuItem());
            }
        }
    }
    if (actAsSpinnerDialog) {
        l.setFixedSelection(List.FIXED_CENTER);
        l.setUIID("Spinner");
        l.spinnerOverlay = getUIManager().getComponentStyle("SpinnerOverlay");
        l.spinnerOverlay.setMargin(0, 0, 0, 0);
        l.setAlwaysTensile(false);
        l.installDefaultPainter(l.spinnerOverlay);
        popupDialog.setDialogUIID("Container");
        popupDialog.setUIID("Container");
        popupDialog.getTitleComponent().setUIID("Container");
        popupDialog.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, true, 200));
        popupDialog.setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, 200));
        return popupDialog.show(Display.getInstance().getDisplayHeight() - popupDialog.getDialogComponent().getPreferredH(), 0, 0, 0, true, true);
    }
    if (getUIManager().isThemeConstant("centeredPopupBool", false)) {
        return popupDialog.showPacked(BorderLayout.CENTER, true);
    } else {
        int top, bottom, left, right;
        Form parentForm = getComponentForm();
        int listW = Math.max(getWidth(), l.getPreferredW());
        listW = Math.min(listW + l.getSideGap(), parentForm.getContentPane().getWidth());
        Component content = popupDialog.getDialogComponent();
        Style contentStyle = content.getStyle();
        int listH = content.getPreferredH() + contentStyle.getVerticalMargins();
        Component title = popupDialog.getTitleArea();
        listH += title.getPreferredH() + title.getStyle().getVerticalMargins();
        bottom = 0;
        top = getAbsoluteY();
        int formHeight = parentForm.getHeight();
        if (parentForm.getSoftButtonCount() > 1) {
            Component c = parentForm.getSoftButton(0).getParent();
            formHeight -= c.getHeight();
            Style s = c.getStyle();
            formHeight -= (s.getVerticalMargins());
        }
        if (listH < formHeight) {
            // pop up or down?
            if (top > formHeight / 2) {
                bottom = formHeight - top;
                top = top - listH;
            } else {
                top += getHeight();
                bottom = formHeight - top - listH;
            }
        } else {
            top = 0;
        }
        left = getAbsoluteX();
        right = parentForm.getWidth() - left - listW;
        if (right < 0) {
            left += right;
            right = 0;
        }
        popupDialog.setBackCommand(popupDialog.getMenuBar().getCancelMenuItem());
        return popupDialog.show(Math.max(top, 0), Math.max(bottom, 0), Math.max(left, 0), Math.max(right, 0), false, true);
    }
}
Also used : Style(com.codename1.ui.plaf.Style)

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