use of com.codename1.ui.Command in project CodenameOne by codenameone.
the class MenuBar method keyReleased.
/**
* {@inheritDoc}
*/
public void keyReleased(int keyCode) {
int commandBehavior = getCommandBehavior();
if (commandBehavior >= Display.COMMAND_BEHAVIOR_BUTTON_BAR && keyCode != backSK && keyCode != clearSK && keyCode != backspaceSK) {
return;
}
if (getCommandCount() > 0) {
int softkeyCount = Display.getInstance().getImplementation().getSoftkeyCount();
if (softkeyCount < 2 && keyCode == leftSK) {
if (commandList != null) {
Container parent = commandList.getParent();
while (parent != null) {
if (parent instanceof Dialog && ((Dialog) parent).isMenu()) {
return;
}
parent = parent.getParent();
}
}
showMenu();
return;
} else {
if (keyCode == leftSK) {
if (left != null) {
left.released();
}
return;
} else {
// it might be a back command...
if ((keyCode == rightSK || keyCode == rightSK2)) {
if (right != null) {
right.released();
}
return;
} else {
if (Display.getInstance().getGameAction(keyCode) == Display.GAME_FIRE) {
main.released();
return;
}
}
}
}
}
// allows a back/clear command to occur regardless of whether the
// command was added to the form
Command c = null;
if (keyCode == backSK) {
// the back command should be invoked
c = parent.getBackCommand();
if (c == null && minimizeOnBack) {
Display.getInstance().minimizeApplication();
return;
}
} else {
if (keyCode == clearSK || keyCode == backspaceSK) {
c = getClearCommand();
}
}
if (c != null) {
ActionEvent ev = new ActionEvent(c, keyCode);
c.actionPerformed(ev);
if (!ev.isConsumed()) {
parent.actionCommandImpl(c);
}
}
}
use of com.codename1.ui.Command in project CodenameOne by codenameone.
the class MenuBar method initMenuBar.
/**
* Initialize the MenuBar
*
* @param parent the associated Form
*/
protected void initMenuBar(Form parent) {
this.parent = parent;
selectMenuItem = createMenuSelectCommand();
cancelMenuItem = createMenuCancelCommand();
UIManager manager = parent.getUIManager();
LookAndFeel lf = manager.getLookAndFeel();
// don't minimize the app if it's a Dialog
minimizeOnBack = manager.isThemeConstant("minimizeOnBackBool", true) && !(parent instanceof Dialog);
hideEmptyCommands = manager.isThemeConstant("hideEmptyCommandsBool", false);
menuStyle = manager.getComponentStyle("Menu");
setUIID("SoftButton");
menuCommand = new Command(manager.localize("menu", "Menu"), lf.getMenuIcons()[2]);
// use the slide transition by default
if (lf.getDefaultMenuTransitionIn() != null || lf.getDefaultMenuTransitionOut() != null) {
transitionIn = lf.getDefaultMenuTransitionIn();
transitionOut = lf.getDefaultMenuTransitionOut();
} else {
transitionIn = CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, true, 300, true);
transitionOut = CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, 300, true);
}
menuCellRenderer = lf.getMenuRenderer();
int softkeyCount = Display.getInstance().getImplementation().getSoftkeyCount();
thirdSoftButton = Display.getInstance().isThirdSoftButton();
int commandBehavior = getCommandBehavior();
if (softkeyCount > 1 && commandBehavior < Display.COMMAND_BEHAVIOR_BUTTON_BAR) {
if (thirdSoftButton) {
setLayout(new GridLayout(1, 3));
soft = new Button[] { createSoftButton("SoftButtonCenter"), createSoftButton("SoftButtonLeft"), createSoftButton("SoftButtonRight") };
main = soft[0];
left = soft[1];
right = soft[2];
if (parent.isRTL()) {
right.setUIID("SoftButtonLeft");
left.setUIID("SoftButtonRight");
addComponent(right);
addComponent(main);
addComponent(left);
} else {
addComponent(left);
addComponent(main);
addComponent(right);
}
if (isReverseSoftButtons()) {
Button b = soft[1];
soft[1] = soft[2];
soft[2] = b;
}
} else {
setLayout(new GridLayout(1, 2));
soft = new Button[] { createSoftButton("SoftButtonLeft"), createSoftButton("SoftButtonRight") };
main = soft[0];
left = soft[0];
right = soft[1];
if (parent.isRTL()) {
right.setUIID("SoftButtonLeft");
left.setUIID("SoftButtonRight");
addComponent(right);
addComponent(left);
} else {
addComponent(left);
addComponent(right);
}
if (isReverseSoftButtons()) {
Button b = soft[0];
soft[0] = soft[1];
soft[1] = b;
}
}
// It doesn't make sense for softbuttons to have ... at the end
for (int iter = 0; iter < soft.length; iter++) {
soft[iter].setEndsWith3Points(false);
}
} else {
// special case for touch screens we still want the 3 softbutton areas...
if (thirdSoftButton) {
setLayout(new GridLayout(1, 3));
soft = new Button[] { createSoftButton("SoftButtonCenter"), createSoftButton("SoftButtonLeft"), createSoftButton("SoftButtonRight") };
main = soft[0];
left = soft[1];
right = soft[2];
addComponent(left);
addComponent(main);
addComponent(right);
if (isReverseSoftButtons()) {
Button b = soft[1];
soft[1] = soft[2];
soft[2] = b;
}
} else {
soft = new Button[] { createSoftButton("SoftButtonCenter") };
}
}
softCommand = new Command[soft.length];
}
use of com.codename1.ui.Command in project CodenameOne by codenameone.
the class MenuBar method createCommandComponent.
/**
* Creates the component containing the commands within the given vector
* used for showing the menu dialog, this method calls the createCommandList
* method by default however it allows more elaborate menu creation.
*
* @param commands list of command objects
* @return Component that will result in the parent menu dialog recieving a command event
*/
protected Component createCommandComponent(Vector commands) {
UIManager manager = parent.getUIManager();
// Create a touch based menu interface
if (manager.getLookAndFeel().isTouchMenus()) {
Container menu = new Container();
menu.setScrollableY(true);
for (int iter = 0; iter < commands.size(); iter++) {
Command c = (Command) commands.elementAt(iter);
menu.addComponent(createTouchCommandButton(c));
}
if (!manager.isThemeConstant("touchCommandFlowBool", false)) {
int cols = calculateTouchCommandGridColumns(menu);
if (cols > getCommandCount()) {
cols = getCommandCount();
}
int rows = Math.max(1, getCommandCount() / cols + (getCommandCount() % cols != 0 ? 1 : 0));
if (rows > 1) {
// try to prevent too many columns concentraiting within a single row
int remainingColumns = (rows * cols) % getCommandCount();
int newCols = cols;
int newRows = rows;
while (remainingColumns != 0 && remainingColumns > 1 && newCols >= 2) {
newCols--;
newRows = Math.max(1, getCommandCount() / newCols + (getCommandCount() % newCols != 0 ? 1 : 0));
if (newRows != rows) {
break;
}
remainingColumns = (newRows * newCols) % getCommandCount();
}
if (newRows == rows) {
cols = newCols;
rows = newRows;
}
}
GridLayout g = new GridLayout(rows, cols);
g.setFillLastRow(manager.isThemeConstant("touchCommandFillBool", true));
menu.setLayout(g);
} else {
((FlowLayout) menu.getLayout()).setFillRows(true);
}
menu.setPreferredW(Display.getInstance().getDisplayWidth());
return menu;
}
return createCommandList(commands);
}
use of com.codename1.ui.Command in project CodenameOne by codenameone.
the class MenuBar method removeCommand.
/**
* Removes a Command from the MenuBar
*
* @param cmd Command to remove
*/
protected void removeCommand(Command cmd) {
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) {
int i = commands.indexOf(cmd);
if (i > -1) {
commands.removeElementAt(i);
Button b = findCommandComponent(cmd);
if (b != null && b.getParent() != null) {
b.getParent().removeComponent(b);
}
if (getCommandCount() > 0) {
setLayout(new GridLayout(1, getCommandCount()));
}
}
return;
}
commands.removeElement(cmd);
updateCommands();
}
use of com.codename1.ui.Command in project CodenameOne by codenameone.
the class MenuBar method showMenuDialog.
/*private void fixCommandAlignment() {
if (left != null) {
if (parent.isRTL()) {
left.setAlignment(Label.RIGHT);
right.setAlignment(Label.LEFT);
} else {
left.setAlignment(Label.LEFT);
right.setAlignment(Label.RIGHT);
}
if(main != null && main != left && main != right) {
main.setAlignment(CENTER);
}
}
}*/
/**
* A menu is implemented as a dialog, this method allows you to override dialog
* display in order to customize the dialog menu in various ways
*
* @param menu a dialog containing menu options that can be customized
* @return the command selected by the user in the dialog (not menu) Select
* or Cancel
*/
protected Command showMenuDialog(Dialog menu) {
UIManager manager = parent.getUIManager();
boolean pref = manager.isThemeConstant("menuPrefSizeBool", false);
int height;
int marginLeft;
int marginRight = 0;
if (pref) {
Container dialogContentPane = menu.getDialogComponent();
marginLeft = parent.getWidth() - (dialogContentPane.getPreferredW() + menu.getStyle().getHorizontalPadding());
marginLeft = Math.max(0, marginLeft);
if (parent.getSoftButtonCount() > 1) {
height = parent.getHeight() - parent.getSoftButton(0).getParent().getPreferredH() - dialogContentPane.getPreferredH();
} else {
height = parent.getHeight() - dialogContentPane.getPreferredH();
}
height = Math.max(0, height);
} else {
float menuWidthPercent = 1 - Float.parseFloat(manager.getThemeConstant("menuWidthPercent", "75")) / 100;
float menuHeightPercent = 1 - Float.parseFloat(manager.getThemeConstant("menuHeightPercent", "50")) / 100;
height = (int) (parent.getHeight() * menuHeightPercent);
marginLeft = (int) (parent.getWidth() * menuWidthPercent);
}
if (isReverseSoftButtons()) {
marginRight = marginLeft;
marginLeft = 0;
}
if (getCommandBehavior() == Display.COMMAND_BEHAVIOR_ICS) {
menu.setTransitionOutAnimator(transitionIn);
menu.setTransitionInAnimator(transitionOut);
int th = getTitleAreaContainer().getHeight();
return menu.show(th, height - th, marginLeft, marginRight, true);
} else {
if (manager.getLookAndFeel().isTouchMenus() && manager.isThemeConstant("PackTouchMenuBool", true)) {
return menu.showPacked(BorderLayout.SOUTH, true);
} else {
return menu.show(height, 0, marginLeft, marginRight, true);
}
}
}
Aggregations