Search in sources :

Example 26 with Command

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

the class FontImage method setMaterialIcon.

/**
 * <p>Applies a material design icon (one of the MATERIAL_* icon constants) to the given command using the
 * given UIID. Notice that the pressed/selected &amp; disabled states will be set appropriately.</p>
 *
 * @param c a command
 * @param icon one of the MATERIAL_* icons
 * @param uiid the UIID for the command (e.g. TitleCommand)
 * @param size the size of the icon in millimeters
 */
public static void setMaterialIcon(Command c, char icon, String uiid, float size) {
    if (Font.isTrueTypeFileSupported()) {
        UIManager uim = UIManager.getInstance();
        Style s = uim.getComponentStyle(uiid);
        s.setFont(getMaterialDesignFont().derive(rightSize(s, size), Font.STYLE_PLAIN));
        c.setIcon(FontImage.create("" + icon, s));
        Style sel = uim.getComponentSelectedStyle(uiid);
        Style pre = uim.getComponentCustomStyle(uiid, "press");
        Style dis = uim.getComponentCustomStyle(uiid, "dis");
        ;
        if (sel.getFgColor() != s.getFgColor() || (sel.getBgColor() != s.getBgColor()) || (sel.getBgTransparency() != s.getBgTransparency())) {
            sel = new Style(sel);
            sel.setFont(getMaterialDesignFont().derive(rightSize(sel, size), Font.STYLE_PLAIN));
            c.setRolloverIcon(FontImage.create("" + icon, sel));
        }
        if (pre.getFgColor() != s.getFgColor() || (pre.getBgColor() != s.getBgColor()) || (pre.getBgTransparency() != s.getBgTransparency())) {
            pre = new Style(pre);
            pre.setFont(getMaterialDesignFont().derive(rightSize(pre, size), Font.STYLE_PLAIN));
            c.setPressedIcon(FontImage.create("" + icon, pre));
        }
        if (dis.getFgColor() != s.getFgColor() || (dis.getBgColor() != s.getBgColor()) || (dis.getBgTransparency() != s.getBgTransparency())) {
            dis = new Style(dis);
            dis.setFont(getMaterialDesignFont().derive(rightSize(dis, size), Font.STYLE_PLAIN));
            c.setDisabledIcon(FontImage.create("" + icon, dis));
        }
    }
}
Also used : UIManager(com.codename1.ui.plaf.UIManager) Style(com.codename1.ui.plaf.Style)

Example 27 with Command

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

the class MenuBar method createMenuCancelCommand.

/**
 * Factory method that returns the Form Menu cancel Command.
 * This method can be overridden to customize the Command on the Form.
 *
 * @return Command
 */
protected Command createMenuCancelCommand() {
    UIManager manager = parent.getUIManager();
    LookAndFeel lf = manager.getLookAndFeel();
    return new Command(manager.localize("cancel", "Cancel"), lf.getMenuIcons()[1]);
}
Also used : UIManager(com.codename1.ui.plaf.UIManager) LookAndFeel(com.codename1.ui.plaf.LookAndFeel)

Example 28 with Command

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

the class MenuBar method addCommand.

/**
 * Adds a Command to the MenuBar
 *
 * @param cmd Command to add
 */
public void addCommand(Command cmd) {
    // with the select command
    if (commands.contains(cmd)) {
        return;
    }
    if (getBackCommand() == cmd && UIManager.getInstance().isThemeConstant("hideBackCommandBool", false)) {
        return;
    }
    // special case for default commands which are placed at the end and aren't overriden later
    if (soft.length > 2 && cmd == parent.getDefaultCommand()) {
        commands.addElement(cmd);
    } else {
        commands.insertElementAt(cmd, 0);
    }
    if (!(parent instanceof Dialog)) {
        int behavior = getCommandBehavior();
        if (behavior == Display.COMMAND_BEHAVIOR_BUTTON_BAR || behavior == Display.COMMAND_BEHAVIOR_BUTTON_BAR_TITLE_BACK || behavior == Display.COMMAND_BEHAVIOR_BUTTON_BAR_TITLE_RIGHT || behavior == Display.COMMAND_BEHAVIOR_ICS) {
            if (behavior == Display.COMMAND_BEHAVIOR_BUTTON_BAR_TITLE_BACK && (cmd == parent.getBackCommand() || findCommandComponent(cmd) != null)) {
                return;
            }
            if (parent.getBackCommand() != cmd) {
                if ((behavior == Display.COMMAND_BEHAVIOR_BUTTON_BAR_TITLE_BACK || behavior == Display.COMMAND_BEHAVIOR_ICS || behavior == Display.COMMAND_BEHAVIOR_SIDE_NAVIGATION) && parent.getTitle() != null && parent.getTitle().length() > 0) {
                    synchronizeCommandsWithButtonsInBackbutton();
                    return;
                }
                setLayout(new GridLayout(1, getCommandCount()));
                addComponent(createTouchCommandButton(cmd));
            } else {
                commands.removeElement(cmd);
            }
            return;
        }
    }
    updateCommands();
}
Also used : GridLayout(com.codename1.ui.layouts.GridLayout)

Example 29 with Command

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

the class MenuBar method actionPerformed.

/**
 * Invoked when a softbutton is pressed
 */
public void actionPerformed(ActionEvent evt) {
    if (evt.isConsumed()) {
        return;
    }
    Object src = evt.getSource();
    if (commandList == null) {
        Button source = (Button) src;
        for (int iter = 0; iter < soft.length; iter++) {
            if (source == soft[iter]) {
                if (softCommand[iter] == menuCommand) {
                    showMenu();
                    return;
                }
                if (softCommand[iter] != null) {
                    ActionEvent e = new ActionEvent(softCommand[iter], ActionEvent.Type.Command);
                    softCommand[iter].actionPerformed(e);
                    if (!e.isConsumed()) {
                        parent.actionCommandImpl(softCommand[iter]);
                    }
                }
                return;
            }
        }
    } else {
        // the list for the menu sent the event
        if (src instanceof Button) {
            for (int iter = 0; iter < soft.length; iter++) {
                if (src == soft[iter]) {
                    Container parent = commandList.getParent();
                    while (parent != null) {
                        if (parent instanceof Dialog) {
                            ((Dialog) parent).actionCommand(softCommand[iter]);
                            return;
                        }
                        parent = parent.getParent();
                    }
                }
            }
        }
        Command c = getComponentSelectedCommand(commandList);
        if (!c.isEnabled()) {
            return;
        }
        Container p = commandList.getParent();
        while (p != null) {
            if (p instanceof Dialog) {
                ((Dialog) p).actionCommand(c);
                return;
            }
            p = p.getParent();
        }
    }
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent)

Example 30 with Command

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

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