use of com.codename1.ui.Command in project CodenameOne by codenameone.
the class SideMenuBar method installLeftCommands.
void installLeftCommands() {
if (leftCommands != null) {
for (int i = 0; i < leftCommands.size(); i++) {
Command leftCommand = (Command) leftCommands.get(leftCommands.size() - 1 - i);
String uiid = (String) leftCommand.getClientProperty("uiid");
if (uiid == null) {
uiid = "TitleCommand";
}
int txtPosition = Component.RIGHT;
Integer pos = (Integer) leftCommand.getClientProperty("textPosition");
if (pos != null) {
txtPosition = pos.intValue();
}
Layout l = getTitleAreaContainer().getLayout();
if (l instanceof BorderLayout) {
Button b = new Button(leftCommand);
b.setUIID(uiid);
b.putClientProperty("TitleCommand", Boolean.TRUE);
b.setTextPosition(txtPosition);
BorderLayout bl = (BorderLayout) l;
Component west = bl.getWest();
if (west == null) {
getTitleAreaContainer().addComponent(BorderLayout.WEST, b);
} else {
if (west instanceof Container) {
Container cnt = (Container) west;
// check if this command is already added
boolean shouldAdd = true;
for (int j = 0; j < cnt.getComponentCount(); j++) {
Component c = cnt.getComponentAt(j);
if (c instanceof Button) {
Command cc = ((Button) c).getCommand();
if (cc != null && cc.equals(b.getCommand())) {
shouldAdd = false;
break;
}
}
}
if (shouldAdd) {
cnt.addComponent(b);
}
} else {
if (west instanceof Button) {
Command cc = ((Button) west).getCommand();
if (cc != null && cc.equals(b.getCommand())) {
continue;
}
}
west.getParent().removeComponent(west);
Container buttons = new Container(new BoxLayout(BoxLayout.X_AXIS));
buttons.addComponent(west);
buttons.addComponent(b);
getTitleAreaContainer().addComponent(BorderLayout.WEST, buttons);
}
}
}
}
}
initTitleBarStatus();
}
use of com.codename1.ui.Command in project CodenameOne by codenameone.
the class SideMenuBar method createMenu.
private Form createMenu(final String placement) {
final Form m = new Form() {
private boolean pressedInRightPanel;
private boolean manualMotionLock;
protected boolean shouldSendPointerReleaseToOtherForm() {
return true;
}
void actionCommandImpl(Command cmd, ActionEvent ev) {
if (cmd instanceof SideMenuBar.CommandWrapper) {
cmd = ((SideMenuBar.CommandWrapper) cmd).cmd;
ev = new ActionEvent(cmd, ActionEvent.Type.Command);
}
final Command c = cmd;
final ActionEvent e = ev;
Display.getInstance().scheduleBackgroundTask(new Runnable() {
public void run() {
Display.getInstance().invokeAndBlock(new Runnable() {
public void run() {
while (Display.getInstance().getCurrent() != parent) {
try {
Thread.sleep(40);
} catch (Exception ex) {
}
}
}
});
Display.getInstance().callSerially(new Runnable() {
public void run() {
parent.actionCommandImpl(c, e);
}
});
}
});
}
protected void sizeChanged(int w, int h) {
Style formStyle = getStyle();
int width = w - (formStyle.getHorizontalMargins());
parent.sizeChangedInternal(w, h);
// close the menu
if (getWidth() != width) {
closeMenu();
}
super.sizeChanged(w, h);
}
public void pointerPressed(int x, int y) {
if (manualMotionLock) {
return;
}
super.pointerPressed(x, y);
if (rightPanel.contains(x, y)) {
pressedInRightPanel = true;
}
}
public void pointerDragged(int[] x, int[] y) {
if (manualMotionLock) {
return;
}
if (!transitionRunning && pressedInRightPanel) {
dragActivated = true;
pressedInRightPanel = false;
}
if (dragActivated) {
setMenuGlassPane(menu, placement);
draggedX = x[0];
repaint();
return;
}
super.pointerDragged(x, y);
}
public void pointerReleased(int x, int y) {
if (manualMotionLock) {
return;
}
super.pointerReleased(x, y);
boolean isRTLValue = isRTL();
if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
isRTLValue = !isRTLValue;
}
int displayWidth = Display.getInstance().getDisplayWidth();
if (isRTLValue) {
if (!transitionRunning && dragActivated && x < (displayWidth - rightPanel.getWidth()) / 2) {
final Motion motion = Motion.createEaseInOutMotion(draggedX, rightPanel.getWidth(), 200);
motion.start();
registerAnimated(new Animation() {
public boolean animate() {
draggedX = motion.getValue();
if (motion.isFinished()) {
dragActivated = false;
Display.getInstance().getCurrent().setGlassPane(null);
deregisterAnimated(this);
}
return true;
}
public void paint(Graphics g) {
repaint();
}
});
return;
}
} else {
if (!transitionRunning && dragActivated && x > (displayWidth - rightPanel.getWidth()) / 2) {
final Motion motion = Motion.createEaseInOutMotion(draggedX, Display.getInstance().getDisplayWidth() - rightPanel.getWidth(), 200);
motion.start();
registerAnimated(new Animation() {
public boolean animate() {
draggedX = motion.getValue();
if (motion.isFinished()) {
dragActivated = false;
Display.getInstance().getCurrent().setGlassPane(null);
deregisterAnimated(this);
}
return true;
}
public void paint(Graphics g) {
repaint();
}
});
return;
}
}
if (dragActivated || rightPanel.contains(x, y)) {
setMenuGlassPane(menu, placement);
draggedX = x;
int start = x;
int end = 0;
if (isRTLValue) {
end = getWidth();
}
final Motion motion = Motion.createEaseInOutMotion(start, end, getUIManager().getThemeConstant("sideMenuAnimSpeedInt", 300));
motion.start();
manualMotionLock = true;
sideSwipePotential = false;
rightSideSwipePotential = false;
topSwipePotential = false;
registerAnimated(new Animation() {
public boolean animate() {
draggedX = motion.getValue();
if (motion.isFinished()) {
dragActivated = false;
}
return true;
}
public void paint(Graphics g) {
repaint();
if (draggedX == motion.getDestinationValue() && motion.isFinished()) {
parent.setTransitionInAnimator(CommonTransitions.createEmpty());
parent.show();
deregisterAnimated(this);
Display.getInstance().callSerially(new Runnable() {
public void run() {
clean();
}
});
}
}
});
}
}
public void keyReleased(int keyCode) {
if (keyCode == leftSK) {
if (transitionRunning) {
return;
}
closeMenu();
return;
}
super.keyReleased(keyCode);
}
};
m.setScrollable(false);
m.removeComponentFromForm(m.getTitleArea());
m.putClientProperty("Menu", "true");
m.setTransitionInAnimator(CommonTransitions.createEmpty());
m.setTransitionOutAnimator(CommonTransitions.createEmpty());
m.setBackCommand(new Command("") {
public void actionPerformed(ActionEvent evt) {
if (transitionRunning) {
return;
}
closeMenu();
}
});
m.setLayout(new BorderLayout());
if (Display.getInstance().areMutableImagesFast()) {
rightPanel = new Container(new BorderLayout());
} else {
rightPanel = new Container(new BorderLayout()) {
public void paintBackground(Graphics g) {
}
public void paintBackgrounds(Graphics g) {
}
public void paint(Graphics g) {
Component c = (Component) rightPanel.getClientProperty("$parent");
// not sure why its happening: https://code.google.com/p/codenameone/issues/detail?id=1072
if (c != null) {
boolean b = c.isVisible();
c.setVisible(true);
int x = getAbsoluteX();
g.translate(x, 0);
Container.sidemenuBarTranslation = x;
c.paintComponent(g, true);
Container.sidemenuBarTranslation = 0;
g.translate(-x, 0);
c.setVisible(b);
}
}
};
}
if (placement == COMMAND_PLACEMENT_VALUE_TOP) {
int v = 0;
if (Display.getInstance().isPortrait()) {
if (Display.getInstance().isTablet()) {
v = getUIManager().getThemeConstant("topMenuSizeTabPortraitInt", -1);
if (v < 0) {
v = m.getHeight() * 2 / 3;
} else {
v = m.getHeight() / 100 * v;
}
} else {
v = getUIManager().getThemeConstant("topMenuSizePortraitInt", -1);
if (v < 0) {
v = openButton.getHeight();
} else {
v = m.getHeight() / 100 * v;
}
}
} else {
if (Display.getInstance().isTablet()) {
v = getUIManager().getThemeConstant("topMenuSizeTabLandscapeInt", -1);
if (v < 0) {
v = m.getHeight() * 3 / 4;
} else {
v = m.getWidth() / 100 * v;
}
} else {
v = getUIManager().getThemeConstant("topMenuSizeLandscapeInt", -1);
if (v < 0) {
v = m.getHeight() * 4 / 10;
} else {
v = m.getHeight() / 100 * v;
}
}
}
rightPanel.setPreferredH(v);
} else {
if (Display.getInstance().isPortrait()) {
int v = 0;
if (Display.getInstance().isTablet()) {
v = getUIManager().getThemeConstant("sideMenuSizeTabPortraitInt", -1);
if (v < 0) {
v = m.getWidth() * 2 / 3;
} else {
v = m.getWidth() / 100 * v;
}
} else {
v = getUIManager().getThemeConstant("sideMenuSizePortraitInt", -1);
if (v < 0) {
if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
if (isRTL()) {
v = openButton.getWidth();
} else {
v = rightSideButton.getWidth();
}
} else {
v = openButton.getWidth();
}
} else {
v = m.getWidth() / 100 * v;
}
}
rightPanel.setPreferredW(v);
} else {
int v = 0;
if (Display.getInstance().isTablet()) {
v = getUIManager().getThemeConstant("sideMenuSizeTabLandscapeInt", -1);
if (v < 0) {
v = m.getWidth() * 3 / 4;
} else {
v = m.getWidth() / 100 * v;
}
} else {
v = getUIManager().getThemeConstant("sideMenuSizeLandscapeInt", -1);
if (v < 0) {
v = m.getWidth() * 4 / 10;
} else {
v = m.getWidth() / 100 * v;
}
}
rightPanel.setPreferredW(v);
}
}
if (sidePanel != null) {
sidePanel.removeAll();
sidePanel = null;
}
sidePanel = createSideNavigationComponent(getCommands(), placement);
if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
m.addComponent(BorderLayout.WEST, rightPanel);
m.addComponent(BorderLayout.CENTER, sidePanel);
} else {
if (placement == COMMAND_PLACEMENT_VALUE_TOP) {
m.addComponent(BorderLayout.NORTH, rightPanel);
m.addComponent(BorderLayout.CENTER, sidePanel);
Button button = new Button(" ");
button.setUIID("Container");
button.setPreferredH(Display.getInstance().getDisplayHeight() / 10);
m.addComponent(BorderLayout.SOUTH, button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
closeMenu();
}
});
} else {
m.addComponent(BorderLayout.EAST, rightPanel);
m.addComponent(BorderLayout.CENTER, sidePanel);
}
}
m.putClientProperty("cn1$sideMenuParent", this);
return m;
}
use of com.codename1.ui.Command in project CodenameOne by codenameone.
the class MasterDetail method bindTabletLandscapeMaster.
/**
* @deprecated this was a half baked idea that made it into the public API
*/
public static void bindTabletLandscapeMaster(final Form rootForm, Container parentContainer, Component landscapeUI, final Component portraitUI, final String commandTitle, Image commandIcon) {
landscapeUI.setHideInPortrait(true);
parentContainer.addComponent(BorderLayout.WEST, landscapeUI);
final Command masterCommand = new Command(commandTitle, commandIcon) {
public void actionPerformed(ActionEvent ev) {
Dialog dlg = new Dialog();
dlg.setLayout(new BorderLayout());
dlg.setDialogUIID("Container");
dlg.getContentPane().setUIID("Container");
Container titleArea = new Container(new BorderLayout());
dlg.addComponent(BorderLayout.NORTH, titleArea);
titleArea.setUIID("TitleArea");
Label title = new Label(commandTitle);
titleArea.addComponent(BorderLayout.CENTER, title);
title.setUIID("Title");
Container body = new Container(new BorderLayout());
body.setUIID("Form");
body.addComponent(BorderLayout.CENTER, portraitUI);
dlg.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, false, 250));
dlg.setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 250));
dlg.addComponent(BorderLayout.CENTER, body);
dlg.setDisposeWhenPointerOutOfBounds(true);
dlg.showStetched(BorderLayout.WEST, true);
dlg.removeComponent(portraitUI);
}
};
if (Display.getInstance().isPortrait()) {
if (rootForm.getCommandCount() > 0) {
rootForm.addCommand(masterCommand, 1);
} else {
rootForm.addCommand(masterCommand);
}
}
rootForm.addOrientationListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (portraitUI.getParent() != null) {
Form f = Display.getInstance().getCurrent();
if (f instanceof Dialog) {
((Dialog) f).dispose();
}
}
if (Display.getInstance().isPortrait()) {
rootForm.addCommand(masterCommand, 1);
} else {
rootForm.removeCommand(masterCommand);
rootForm.revalidate();
}
}
});
}
use of com.codename1.ui.Command in project CodenameOne by codenameone.
the class MultiButton method setCommand.
/**
* Sets the command for the component, it doesn't affe
*
* @param c the command
*/
public void setCommand(Command c) {
Image img = emblem.getIcon();
emblem.setCommand(c);
emblem.setIcon(img);
emblem.setText("");
}
use of com.codename1.ui.Command in project CodenameOne by codenameone.
the class Progress method actionCommand.
/**
* {@inheritDoc}
*/
protected void actionCommand(Command cmd) {
if (Display.getInstance().isTouchScreenDevice() || getSoftButtonCount() < 2) {
for (int iter = 0; iter < getComponentCount(); iter++) {
Component c = getComponentAt(iter);
if (c instanceof Button) {
c.setEnabled(false);
}
}
} else {
removeAllCommands();
}
// cancel was pressed
request.kill();
dispose();
}
Aggregations