use of com.codename1.ui.Command in project CodenameOne by codenameone.
the class LazyValueC method createBackLazyValue.
/**
* This is useful for swipe back navigation behavior
* @param f the form from which we should go back
* @return a lazy value that will return the back form
*/
public LazyValue<Form> createBackLazyValue(final Form f) {
Vector formNavigationStack = baseFormNavigationStack;
Hashtable p = null;
Command cmd = null;
if (formNavigationStack.size() > 1) {
p = (Hashtable) formNavigationStack.elementAt(formNavigationStack.size() - 2);
String backTitle = getBackCommandText((String) p.get(FORM_STATE_KEY_TITLE));
String commandAction = (String) p.get(FORM_STATE_KEY_NAME);
cmd = createCommandImpl(backTitle, null, BACK_COMMAND_ID, commandAction, true, "");
cmd.putClientProperty(COMMAND_ARGUMENTS, "");
cmd.putClientProperty(COMMAND_ACTION, commandAction);
}
return new LazyValueC(f, p, cmd, this);
}
use of com.codename1.ui.Command in project CodenameOne by codenameone.
the class LazyValueC method showContainerImpl.
private Container showContainerImpl(String resourceName, Command sourceCommand, Component sourceComponent, boolean forceBack) {
if (sourceComponent != null) {
Form currentForm = sourceComponent.getComponentForm();
// avoid the overhead of searching if no embedding is used
if (currentForm.getClientProperty(EMBEDDED_FORM_FLAG) != null) {
Container destContainer = sourceComponent.getParent();
while (!(destContainer instanceof EmbeddedContainer || destContainer instanceof Form)) {
// race condition, container was already removed by someone else
if (destContainer == null) {
return null;
}
destContainer = destContainer.getParent();
}
if (destContainer instanceof EmbeddedContainer) {
Container cnt = createContainer(fetchResourceFile(), resourceName, (EmbeddedContainer) destContainer);
if (cnt instanceof Form) {
// Form f = (Form)cnt;
// cnt = formToContainer(f);
showForm((Form) cnt, sourceCommand, sourceComponent);
return cnt;
}
Component fromCmp = destContainer.getComponentAt(0);
// This seems to be no longer necessary now that we have the replaceAndWait version that drops events
// block the user from the ability to press the button twice by mistake
// fromCmp.setEnabled(false);
boolean isBack = forceBack;
Transition t = fromCmp.getUIManager().getLookAndFeel().getDefaultFormTransitionOut();
if (forceBack) {
initBackContainer(cnt, destContainer.getComponentForm(), getFormNavigationStackForComponent(sourceComponent));
t = t.copy(true);
} else {
if (sourceCommand != null) {
if (t != null && backCommands != null && backCommands.contains(sourceCommand) || Display.getInstance().getCurrent().getBackCommand() == sourceCommand) {
isBack = true;
t = t.copy(true);
}
}
}
// create a back command if supported
String commandAction = cnt.getName();
Vector formNavigationStack = getFormNavigationStackForComponent(fromCmp);
if (formNavigationStack != null && !isBack && allowBackTo(commandAction) && !isSameBackDestination((Container) fromCmp, cnt)) {
// trigger listener creation if this is the only command in the form
getFormListenerInstance(destContainer.getComponentForm(), null);
formNavigationStack.addElement(getContainerState((com.codename1.ui.Container) fromCmp));
}
beforeShowContainer(cnt);
destContainer.replaceAndWait(fromCmp, cnt, t, true);
postShowContainer(cnt);
return cnt;
} else {
Container cnt = createContainer(fetchResourceFile(), resourceName);
showForm((Form) cnt, sourceCommand, sourceComponent);
return cnt;
}
}
}
Container cnt = createContainer(fetchResourceFile(), resourceName);
if (cnt instanceof Form) {
showForm((Form) cnt, sourceCommand, sourceComponent);
} else {
Form f = new Form();
f.setLayout(new BorderLayout());
f.addComponent(BorderLayout.CENTER, cnt);
f.setName("Form" + cnt.getName());
showForm(f, sourceCommand, sourceComponent);
}
return cnt;
}
use of com.codename1.ui.Command in project CodenameOne by codenameone.
the class LazyValueC method showForm.
/**
* This method is equivalent to the internal navigation behavior, it adds
* functionality such as the back command into the given form resource and
* shows it. If the source command is the back command the showBack() method
* will run.
*
* @param resourceName the name of the resource for the form to show
* @param sourceCommand the command of the resource (may be null)
* @return the form thats being shown, notice that you can still manipulate
* some states of the form before it actually appears
*/
public Form showForm(String resourceName, Command sourceCommand) {
Form f = (Form) createContainer(fetchResourceFile(), resourceName);
showForm(f, sourceCommand, null);
return f;
}
use of com.codename1.ui.Command in project CodenameOne by codenameone.
the class LazyValueC method readCommand.
private void readCommand(DataInputStream in, Component c, Container parent, Resources res, boolean legacy) throws IOException {
String commandName = in.readUTF();
String commandImageName = in.readUTF();
String rollover = null;
String pressed = null;
String disabled = null;
if (!legacy) {
rollover = in.readUTF();
pressed = in.readUTF();
disabled = in.readUTF();
}
int commandId = in.readInt();
String commandAction = in.readUTF();
String commandArgument = "";
if (commandAction.equals("$Execute")) {
commandArgument = in.readUTF();
}
boolean isBack = in.readBoolean();
Command cmd = createCommandImpl(commandName, res.getImage(commandImageName), commandId, commandAction, isBack, commandArgument);
if (rollover != null && rollover.length() > 0) {
cmd.setRolloverIcon(res.getImage(rollover));
}
if (pressed != null && pressed.length() > 0) {
cmd.setPressedIcon(res.getImage(pressed));
}
if (disabled != null && disabled.length() > 0) {
cmd.setPressedIcon(res.getImage(pressed));
}
if (isBack) {
Form f = c.getComponentForm();
if (f != null) {
setBackCommand(f, cmd);
} else {
if (backCommands == null) {
backCommands = new Vector();
}
backCommands.addElement(cmd);
}
}
Button btn;
if (c instanceof Container) {
btn = (Button) ((Container) c).getLeadComponent();
} else {
btn = ((Button) c);
}
boolean e = btn.isEnabled();
btn.setCommand(cmd);
// special case since setting the command implicitly gets the enabled state from the command
if (!e) {
btn.setEnabled(false);
}
// no menu
if (c.getComponentForm() != null) {
btn.removeActionListener(getFormListenerInstance(parent, null));
}
cmd.putClientProperty(COMMAND_ARGUMENTS, commandArgument);
cmd.putClientProperty(COMMAND_ACTION, commandAction);
if (commandAction.length() > 0 && resourceFilePath == null || isKeepResourcesInRam()) {
resourceFile = res;
}
}
use of com.codename1.ui.Command in project CodenameOne by codenameone.
the class LazyValueC method readObjectArrayForListModel.
private Object[] readObjectArrayForListModel(DataInputStream in, Resources res) throws IOException {
Object[] elements = new Object[in.readInt()];
int elen = elements.length;
for (int iter = 0; iter < elen; iter++) {
switch(in.readByte()) {
case // String
1:
elements[iter] = in.readUTF();
break;
case // hashtable of Strings
2:
int hashSize = in.readInt();
Hashtable val = new Hashtable();
elements[iter] = val;
for (int i = 0; i < hashSize; i++) {
int type = in.readInt();
if (type == 1) {
// String
String key = in.readUTF();
Object value = in.readUTF();
if (key.equals("$navigation")) {
Command cmd = createCommandImpl((String) value, null, -1, (String) value, false, "");
cmd.putClientProperty(COMMAND_ACTION, (String) value);
value = cmd;
}
val.put(key, value);
} else {
val.put(in.readUTF(), res.getImage(in.readUTF()));
}
}
break;
}
}
return elements;
}
Aggregations