use of com.codename1.ui.util.xml.comps.ComponentEntry in project CodenameOne by codenameone.
the class UIBuilderOverride method createInstance.
/**
* Create a component instance from XML
*/
public Container createInstance(ComponentEntry root, EditableResources res) {
ArrayList<Runnable> postCreateTasks = new ArrayList<Runnable>();
Container c = (Container) createInstance(root, res, null, null, postCreateTasks);
// execute tasks that must have the entire hierarchy constructed in order to work
for (Runnable r : postCreateTasks) {
r.run();
}
return c;
}
use of com.codename1.ui.util.xml.comps.ComponentEntry in project CodenameOne by codenameone.
the class UIBuilderOverride method createInstance.
/**
* Create a component instance from XML
*/
private Component createInstance(final ComponentEntry root, final EditableResources res, Container rootCnt, final Container parentContainer, final ArrayList<Runnable> postCreateTasks) {
try {
final Component c = createComponentType(root.getType());
if (rootCnt == null) {
rootCnt = (Container) c;
}
final Container rootContainer = rootCnt;
if (root.getBaseForm() != null) {
c.putClientProperty("%base_form%", root.getBaseForm());
}
c.putClientProperty(TYPE_KEY, root.getType());
c.setName(root.getName());
String clientProps = root.getClientProperties();
if (clientProps != null && clientProps.length() > 0) {
String[] props = clientProps.split(",");
StringBuilder b = new StringBuilder();
for (String p : props) {
String[] keyVal = p.split("=");
c.putClientProperty(keyVal[0], keyVal[1]);
if (b.length() > 0) {
b.append(",");
}
b.append(keyVal[0]);
}
c.putClientProperty("cn1$Properties", b.toString());
}
rootContainer.putClientProperty("%" + root.getName() + "%", c);
// layout must be first since we might need to rely on it later on with things such as constraints
if (root.getLayout() != null) {
modifyingProperty(c, PROPERTY_LAYOUT);
Layout l;
if (root.getLayout().equals("BorderLayout")) {
l = new BorderLayout();
if (root.isBorderLayoutAbsoluteCenter() != null) {
((BorderLayout) l).setAbsoluteCenter(root.isBorderLayoutAbsoluteCenter().booleanValue());
}
if (root.getBorderLayoutSwapCenter() != null) {
((BorderLayout) l).defineLandscapeSwap(BorderLayout.CENTER, root.getBorderLayoutSwapCenter());
}
if (root.getBorderLayoutSwapNorth() != null) {
((BorderLayout) l).defineLandscapeSwap(BorderLayout.NORTH, root.getBorderLayoutSwapNorth());
}
if (root.getBorderLayoutSwapSouth() != null) {
((BorderLayout) l).defineLandscapeSwap(BorderLayout.SOUTH, root.getBorderLayoutSwapSouth());
}
if (root.getBorderLayoutSwapEast() != null) {
((BorderLayout) l).defineLandscapeSwap(BorderLayout.EAST, root.getBorderLayoutSwapEast());
}
if (root.getBorderLayoutSwapWest() != null) {
((BorderLayout) l).defineLandscapeSwap(BorderLayout.WEST, root.getBorderLayoutSwapWest());
}
} else {
if (root.getLayout().equals("FlowLayout")) {
l = new FlowLayout();
((FlowLayout) l).setFillRows(root.isFlowLayoutFillRows());
((FlowLayout) l).setAlign(root.getFlowLayoutAlign());
((FlowLayout) l).setValign(root.getFlowLayoutValign());
} else {
if (root.getLayout().equals("GridLayout")) {
l = new GridLayout(root.getGridLayoutRows().intValue(), root.getGridLayoutColumns().intValue());
} else {
if (root.getLayout().equals("BoxLayout")) {
if (root.getBoxLayoutAxis().equals("X")) {
l = new BoxLayout(BoxLayout.X_AXIS);
} else {
l = new BoxLayout(BoxLayout.Y_AXIS);
}
} else {
if (root.getLayout().equals("TableLayout")) {
l = new TableLayout(root.getTableLayoutRows(), root.getTableLayoutColumns());
} else {
l = new LayeredLayout();
}
}
}
}
}
((Container) c).setLayout(l);
}
if (parentContainer != null && root.getLayoutConstraint() != null) {
modifyingProperty(c, PROPERTY_LAYOUT_CONSTRAINT);
if (parentContainer.getLayout() instanceof BorderLayout) {
c.putClientProperty("layoutConstraint", root.getLayoutConstraint().getValue());
} else {
TableLayout tl = (TableLayout) parentContainer.getLayout();
TableLayout.Constraint con = tl.createConstraint(root.getLayoutConstraint().getRow(), root.getLayoutConstraint().getColumn());
con.setHeightPercentage(root.getLayoutConstraint().getHeight());
con.setWidthPercentage(root.getLayoutConstraint().getWidth());
con.setHorizontalAlign(root.getLayoutConstraint().getAlign());
con.setHorizontalSpan(root.getLayoutConstraint().getSpanHorizontal());
con.setVerticalAlign(root.getLayoutConstraint().getValign());
con.setVerticalSpan(root.getLayoutConstraint().getSpanVertical());
c.putClientProperty("layoutConstraint", con);
}
}
if (root.getEmbed() != null && root.getEmbed().length() > 0) {
modifyingProperty(c, PROPERTY_EMBED);
rootContainer.putClientProperty(EMBEDDED_FORM_FLAG, "");
((EmbeddedContainer) c).setEmbed(root.getEmbed());
Container embed = createContainer(res, root.getEmbed(), (EmbeddedContainer) c);
if (embed != null) {
if (embed instanceof Form) {
embed = formToContainer((Form) embed);
}
((EmbeddedContainer) c).addComponent(BorderLayout.CENTER, embed);
// this isn't exactly the "right thing" but its the best we can do to make all
// use cases work
beforeShowContainer(embed);
postShowContainer(embed);
}
}
if (root.isToggle() != null) {
modifyingProperty(c, PROPERTY_TOGGLE_BUTTON);
((Button) c).setToggle(root.isToggle().booleanValue());
}
if (root.getGroup() != null) {
modifyingProperty(c, PROPERTY_RADIO_GROUP);
((RadioButton) c).setGroup(root.getGroup());
}
if (root.isSelected() != null) {
modifyingProperty(c, PROPERTY_SELECTED);
if (c instanceof RadioButton) {
((RadioButton) c).setSelected(root.isSelected().booleanValue());
} else {
((CheckBox) c).setSelected(root.isSelected().booleanValue());
}
}
if (root.isScrollableX() != null) {
modifyingProperty(c, PROPERTY_SCROLLABLE_X);
((Container) c).setScrollableX(root.isScrollableX().booleanValue());
}
if (root.isScrollableY() != null) {
modifyingProperty(c, PROPERTY_SCROLLABLE_Y);
((Container) c).setScrollableY(root.isScrollableY().booleanValue());
}
if (root.isTensileDragEnabled() != null) {
modifyingProperty(c, PROPERTY_TENSILE_DRAG_ENABLED);
c.setTensileDragEnabled(root.isTensileDragEnabled().booleanValue());
}
if (root.isTactileTouch() != null) {
modifyingProperty(c, PROPERTY_TACTILE_TOUCH);
c.setTactileTouch(root.isTactileTouch().booleanValue());
}
if (root.isSnapToGrid() != null) {
modifyingProperty(c, PROPERTY_SNAP_TO_GRID);
c.setSnapToGrid(root.isSnapToGrid().booleanValue());
}
if (root.isFlatten() != null) {
modifyingProperty(c, PROPERTY_FLATTEN);
c.setFlatten(root.isFlatten().booleanValue());
}
if (root.getText() != null) {
modifyingProperty(c, PROPERTY_TEXT);
if (c instanceof Label) {
((Label) c).setText(root.getText());
} else {
((TextArea) c).setText(root.getText());
}
}
if (root.getMaxSize() != null) {
modifyingProperty(c, PROPERTY_TEXT_MAX_LENGTH);
((TextArea) c).setMaxSize(root.getMaxSize().intValue());
}
if (root.getConstraint() != null) {
modifyingProperty(c, PROPERTY_TEXT_CONSTRAINT);
((TextArea) c).setConstraint(root.getConstraint().intValue());
}
if (root.getAlignment() != null) {
modifyingProperty(c, PROPERTY_ALIGNMENT);
if (c instanceof Label) {
((Label) c).setAlignment(root.getAlignment().intValue());
} else {
((TextArea) c).setAlignment(root.getAlignment().intValue());
}
}
if (root.isGrowByContent() != null) {
modifyingProperty(c, PROPERTY_TEXT_AREA_GROW);
((TextArea) c).setGrowByContent(root.isGrowByContent().booleanValue());
}
if (root.getTabPlacement() != null) {
modifyingProperty(c, PROPERTY_TAB_PLACEMENT);
((Tabs) c).setTabPlacement(root.getTabPlacement().intValue());
}
if (root.getTabTextPosition() != null) {
modifyingProperty(c, PROPERTY_TAB_TEXT_POSITION);
((Tabs) c).setTabTextPosition(root.getTabTextPosition().intValue());
}
if (root.getUiid() != null) {
modifyingProperty(c, PROPERTY_UIID);
c.setUIID(root.getUiid());
}
if (root.getDialogUIID() != null) {
modifyingProperty(c, PROPERTY_DIALOG_UIID);
((Dialog) c).setDialogUIID(root.getDialogUIID());
}
if (root.isDisposeWhenPointerOutOfBounds() != null) {
modifyingProperty(c, PROPERTY_DISPOSE_WHEN_POINTER_OUT);
((Dialog) c).setDisposeWhenPointerOutOfBounds(root.isDisposeWhenPointerOutOfBounds());
}
if (root.getCloudBoundProperty() != null) {
modifyingProperty(c, PROPERTY_CLOUD_BOUND_PROPERTY);
c.setCloudBoundProperty(root.getCloudBoundProperty());
}
if (root.getCloudDestinationProperty() != null) {
modifyingProperty(c, PROPERTY_CLOUD_DESTINATION_PROPERTY);
c.setCloudDestinationProperty(root.getCloudDestinationProperty());
}
if (root.getDialogPosition() != null && root.getDialogPosition().length() > 0) {
modifyingProperty(c, PROPERTY_DIALOG_POSITION);
((Dialog) c).setDialogPosition(root.getDialogPosition());
}
if (root.isFocusable() != null) {
modifyingProperty(c, PROPERTY_FOCUSABLE);
c.setFocusable(root.isFocusable().booleanValue());
}
if (root.isEnabled() != null) {
modifyingProperty(c, PROPERTY_ENABLED);
c.setEnabled(root.isEnabled().booleanValue());
}
if (root.isScrollVisible() != null) {
modifyingProperty(c, PROPERTY_SCROLL_VISIBLE);
c.setScrollVisible(root.isScrollVisible().booleanValue());
}
if (root.getIcon() != null) {
modifyingProperty(c, PROPERTY_ICON);
((Label) c).setIcon(res.getImage(root.getIcon()));
}
if (root.getRolloverIcon() != null) {
modifyingProperty(c, PROPERTY_ROLLOVER_ICON);
((Button) c).setRolloverIcon(res.getImage(root.getRolloverIcon()));
}
if (root.getPressedIcon() != null) {
modifyingProperty(c, PROPERTY_PRESSED_ICON);
((Button) c).setPressedIcon(res.getImage(root.getPressedIcon()));
}
if (root.getDisabledIcon() != null) {
modifyingProperty(c, PROPERTY_DISABLED_ICON);
((Button) c).setDisabledIcon(res.getImage(root.getDisabledIcon()));
}
if (root.getGap() != null) {
modifyingProperty(c, PROPERTY_GAP);
((Label) c).setGap(root.getGap().intValue());
}
if (root.getVerticalAlignment() != null) {
modifyingProperty(c, PROPERTY_VERTICAL_ALIGNMENT);
if (c instanceof Label) {
((Label) c).setVerticalAlignment(root.getVerticalAlignment().intValue());
} else {
((TextArea) c).setVerticalAlignment(root.getVerticalAlignment().intValue());
}
}
if (root.getTextPosition() != null) {
modifyingProperty(c, PROPERTY_TEXT_POSITION);
((Label) c).setTextPosition(root.getTextPosition().intValue());
}
if (root.getTitle() != null) {
modifyingProperty(c, PROPERTY_TITLE);
((Form) c).setTitle(root.getTitle());
}
// components should be added when we've set everything else up
if (root.getComponent() != null) {
modifyingProperty(c, PROPERTY_COMPONENTS);
if (c instanceof Tabs) {
for (ComponentEntry ent : root.getComponent()) {
Component newCmp = createInstance(ent, res, rootContainer, (Container) c, postCreateTasks);
((Tabs) c).addTab(ent.getTabTitle(), newCmp);
}
} else {
for (ComponentEntry ent : root.getComponent()) {
Component newCmp = createInstance(ent, res, rootContainer, (Container) c, postCreateTasks);
Object cons = newCmp.getClientProperty("layoutConstraint");
if (cons != null) {
modifyingProperty(c, PROPERTY_LAYOUT_CONSTRAINT);
((Container) c).addComponent(cons, newCmp);
} else {
((Container) c).addComponent(newCmp);
}
}
}
}
if (root.getColumns() != null) {
modifyingProperty(c, PROPERTY_COLUMNS);
((TextArea) c).setColumns(root.getColumns().intValue());
}
if (root.getRows() != null) {
modifyingProperty(c, PROPERTY_ROWS);
((TextArea) c).setRows(root.getRows().intValue());
}
if (root.getHint() != null) {
modifyingProperty(c, PROPERTY_HINT);
if (c instanceof List) {
((List) c).setHint(root.getHint());
} else {
((TextArea) c).setHint(root.getHint());
}
}
if (root.getHintIcon() != null) {
modifyingProperty(c, PROPERTY_HINT_ICON);
if (c instanceof List) {
((List) c).setHintIcon(res.getImage(root.getHint()));
} else {
((TextArea) c).setHintIcon(res.getImage(root.getHint()));
}
}
if (root.getItemGap() != null) {
modifyingProperty(c, PROPERTY_ITEM_GAP);
((List) c).setItemGap(root.getItemGap().intValue());
}
if (root.getFixedSelection() != null) {
modifyingProperty(c, PROPERTY_LIST_FIXED);
((List) c).setFixedSelection(root.getFixedSelection().intValue());
}
if (root.getOrientation() != null) {
modifyingProperty(c, PROPERTY_LIST_ORIENTATION);
((List) c).setOrientation(root.getOrientation().intValue());
}
if (c instanceof com.codename1.ui.List && !(c instanceof com.codename1.components.RSSReader)) {
modifyingProperty(c, PROPERTY_LIST_ITEMS);
if (root.getStringItem() != null && root.getStringItem().length > 0) {
String[] arr = new String[root.getStringItem().length];
for (int iter = 0; iter < arr.length; iter++) {
arr[iter] = root.getStringItem()[iter].getValue();
}
((List) c).setModel(new DefaultListModel<String>(arr));
} else {
if (root.getMapItems() != null && root.getMapItems().length > 0) {
Hashtable[] arr = new Hashtable[root.getMapItems().length];
for (int iter = 0; iter < arr.length; iter++) {
arr[iter] = new Hashtable();
if (root.getMapItems()[iter].getActionItem() != null) {
for (Val v : root.getMapItems()[iter].getActionItem()) {
Command cmd = createCommandImpl((String) v.getValue(), null, -1, v.getValue(), false, "");
cmd.putClientProperty(COMMAND_ACTION, (String) v.getValue());
arr[iter].put(v.getKey(), cmd);
}
}
if (root.getMapItems()[iter].getStringItem() != null) {
for (Val v : root.getMapItems()[iter].getActionItem()) {
arr[iter].put(v.getKey(), v.getValue());
}
}
if (root.getMapItems()[iter].getImageItem() != null) {
for (Val v : root.getMapItems()[iter].getActionItem()) {
arr[iter].put(v.getKey(), res.getImage(v.getValue()));
}
}
}
((List) c).setModel(new DefaultListModel<java.util.Map>(arr));
}
}
}
if (root.getSelectedRenderer() != null) {
modifyingProperty(c, PROPERTY_LIST_RENDERER);
GenericListCellRenderer g;
if (root.getSelectedRendererEven() == null) {
Component selected = createContainer(res, root.getSelectedRenderer());
Component unselected = createContainer(res, root.getUnselectedRenderer());
g = new GenericListCellRenderer(selected, unselected);
g.setFisheye(!root.getSelectedRenderer().equals(root.getUnselectedRenderer()));
} else {
Component selected = createContainer(res, root.getSelectedRenderer());
Component unselected = createContainer(res, root.getUnselectedRenderer());
Component even = createContainer(res, root.getSelectedRendererEven());
Component evenU = createContainer(res, root.getUnselectedRendererEven());
g = new GenericListCellRenderer(selected, unselected, even, evenU);
g.setFisheye(!root.getSelectedRenderer().equals(root.getUnselectedRenderer()));
}
if (c instanceof ContainerList) {
((ContainerList) c).setRenderer(g);
} else {
((List) c).setRenderer(g);
}
}
if (root.getNextForm() != null && root.getNextForm().length() > 0) {
modifyingProperty(c, PROPERTY_NEXT_FORM);
setNextForm(c, root.getNextForm(), res, rootContainer);
}
if (root.getCommand() != null) {
modifyingProperty(c, PROPERTY_COMMANDS);
for (CommandEntry cmd : root.getCommand()) {
Command currentCommand = createCommandImpl(cmd.getName(), res.getImage(cmd.getIcon()), cmd.getId(), cmd.getAction(), cmd.isBackCommand(), cmd.getArgument());
if (cmd.getRolloverIcon() != null && cmd.getRolloverIcon().length() > 0) {
currentCommand.setRolloverIcon(res.getImage(cmd.getRolloverIcon()));
}
if (cmd.getPressedIcon() != null && cmd.getPressedIcon().length() > 0) {
currentCommand.setPressedIcon(res.getImage(cmd.getPressedIcon()));
}
if (cmd.getDisabledIcon() != null && cmd.getDisabledIcon().length() > 0) {
currentCommand.setDisabledIcon(res.getImage(cmd.getDisabledIcon()));
}
if (cmd.isBackCommand()) {
((Form) c).setBackCommand(currentCommand);
}
((Form) c).addCommand(currentCommand);
currentCommand.putClientProperty(COMMAND_ARGUMENTS, cmd.getArgument());
currentCommand.putClientProperty(COMMAND_ACTION, cmd.getAction());
}
}
if (root.isCyclicFocus() != null) {
modifyingProperty(c, PROPERTY_CYCLIC_FOCUS);
((Form) c).setCyclicFocus(root.isCyclicFocus().booleanValue());
}
if (root.isRtl() != null) {
modifyingProperty(c, PROPERTY_RTL);
c.setRTL(root.isRtl().booleanValue());
}
if (root.getThumbImage() != null) {
modifyingProperty(c, PROPERTY_SLIDER_THUMB);
((Slider) c).setThumbImage(res.getImage(root.getThumbImage()));
}
if (root.isInfinite() != null) {
modifyingProperty(c, PROPERTY_INFINITE);
((Slider) c).setInfinite(root.isInfinite().booleanValue());
}
if (root.getProgress() != null) {
modifyingProperty(c, PROPERTY_PROGRESS);
((Slider) c).setProgress(root.getProgress().intValue());
}
if (root.isVertical() != null) {
modifyingProperty(c, PROPERTY_VERTICAL);
((Slider) c).setVertical(root.isVertical().booleanValue());
}
if (root.isEditable() != null) {
modifyingProperty(c, PROPERTY_EDITABLE);
if (c instanceof TextArea) {
((TextArea) c).setEditable(root.isEditable().booleanValue());
} else {
((Slider) c).setEditable(root.isEditable().booleanValue());
}
}
if (root.getIncrements() != null) {
modifyingProperty(c, PROPERTY_INCREMENTS);
((Slider) c).setIncrements(root.getIncrements().intValue());
}
if (root.isRenderPercentageOnTop() != null) {
modifyingProperty(c, PROPERTY_RENDER_PERCENTAGE_ON_TOP);
((Slider) c).setRenderPercentageOnTop(root.isRenderPercentageOnTop().booleanValue());
}
if (root.getMaxValue() != null) {
modifyingProperty(c, PROPERTY_MAX_VALUE);
((Slider) c).setMaxValue(root.getMaxValue().intValue());
}
if (root.getMinValue() != null) {
modifyingProperty(c, PROPERTY_MIN_VALUE);
((Slider) c).setMinValue(root.getMinValue().intValue());
}
if (root.getCommandName() != null) {
modifyingProperty(c, PROPERTY_COMMAND);
postCreateTasks.add(new Runnable() {
public void run() {
Command cmd = createCommandImpl(root.getCommandName(), res.getImage(root.getCommandIcon()), root.getCommandId().intValue(), root.getCommandAction(), root.isCommandBack().booleanValue(), root.getCommandArgument());
if (c instanceof Container) {
Button b = (Button) ((Container) c).getLeadComponent();
b.setCommand(cmd);
return;
}
((Button) c).setCommand(cmd);
}
});
}
if (root.getLabelFor() != null) {
modifyingProperty(c, PROPERTY_LABEL_FOR);
postCreateTasks.add(new Runnable() {
public void run() {
((Label) c).setLabelForComponent((Label) findByName(root.getLabelFor(), rootContainer));
}
});
}
if (root.getLeadComponent() != null) {
modifyingProperty(c, PROPERTY_LEAD_COMPONENT);
postCreateTasks.add(new Runnable() {
public void run() {
((Container) c).setLeadComponent(findByName(root.getLeadComponent(), rootContainer));
}
});
}
if (root.getNextFocusUp() != null) {
modifyingProperty(c, PROPERTY_NEXT_FOCUS_UP);
postCreateTasks.add(new Runnable() {
public void run() {
c.setNextFocusUp(findByName(root.getNextFocusUp(), rootContainer));
}
});
}
if (root.getNextFocusDown() != null) {
modifyingProperty(c, PROPERTY_NEXT_FOCUS_DOWN);
postCreateTasks.add(new Runnable() {
public void run() {
c.setNextFocusDown(findByName(root.getNextFocusDown(), rootContainer));
}
});
}
if (root.getNextFocusLeft() != null) {
modifyingProperty(c, PROPERTY_NEXT_FOCUS_LEFT);
postCreateTasks.add(new Runnable() {
public void run() {
c.setNextFocusLeft(findByName(root.getNextFocusLeft(), rootContainer));
}
});
}
if (root.getNextFocusRight() != null) {
modifyingProperty(c, PROPERTY_NEXT_FOCUS_RIGHT);
postCreateTasks.add(new Runnable() {
public void run() {
c.setNextFocusRight(findByName(root.getNextFocusRight(), rootContainer));
}
});
}
// custom settings are always last after all other properties
if (root.getCustom() != null && root.getCustom().length > 0) {
modifyingProperty(c, PROPERTY_CUSTOM);
for (Custom cust : root.getCustom()) {
modifyingCustomProperty(c, cust.getName());
Object value = null;
Class customType = UserInterfaceEditor.getPropertyCustomType(c, cust.getName());
if (customType.isArray()) {
if (customType == String[].class) {
if (cust.getStr() != null) {
String[] arr = new String[cust.getStr().length];
for (int iter = 0; iter < arr.length; iter++) {
arr[iter] = cust.getStr()[iter].getValue();
}
c.setPropertyValue(cust.getName(), arr);
} else {
c.setPropertyValue(cust.getName(), null);
}
continue;
}
if (customType == String[][].class) {
if (cust.getArr() != null) {
String[][] arr = new String[cust.getArr().length][];
for (int iter = 0; iter < arr.length; iter++) {
if (cust.getArr()[iter] != null && cust.getArr()[iter].getValue() != null) {
arr[iter] = new String[cust.getArr()[iter].getValue().length];
for (int inter = 0; inter < arr[iter].length; inter++) {
arr[iter][inter] = cust.getArr()[iter].getValue()[inter].getValue();
}
}
}
c.setPropertyValue(cust.getName(), arr);
} else {
c.setPropertyValue(cust.getName(), null);
}
continue;
}
if (customType == com.codename1.ui.Image[].class) {
if (cust.getStr() != null) {
com.codename1.ui.Image[] arr = new com.codename1.ui.Image[cust.getStr().length];
for (int iter = 0; iter < arr.length; iter++) {
arr[iter] = res.getImage(cust.getStr()[iter].getValue());
}
c.setPropertyValue(cust.getName(), arr);
} else {
c.setPropertyValue(cust.getName(), null);
}
continue;
}
if (customType == Object[].class) {
if (cust.getStringItem() != null) {
String[] arr = new String[cust.getStringItem().length];
for (int iter = 0; iter < arr.length; iter++) {
arr[iter] = cust.getStringItem()[iter].getValue();
}
c.setPropertyValue(cust.getName(), arr);
continue;
} else {
if (cust.getMapItems() != null) {
Hashtable[] arr = new Hashtable[cust.getMapItems().length];
for (int iter = 0; iter < arr.length; iter++) {
arr[iter] = new Hashtable();
if (cust.getMapItems()[iter].getActionItem() != null) {
for (Val v : cust.getMapItems()[iter].getActionItem()) {
Command cmd = createCommandImpl(v.getValue(), null, -1, v.getValue(), false, "");
cmd.putClientProperty(COMMAND_ACTION, v.getValue());
value = cmd;
arr[iter].put(v.getKey(), cmd);
}
}
if (cust.getMapItems()[iter].getStringItem() != null) {
for (Val v : cust.getMapItems()[iter].getActionItem()) {
arr[iter].put(v.getKey(), v.getValue());
}
}
if (cust.getMapItems()[iter].getImageItem() != null) {
for (Val v : cust.getMapItems()[iter].getActionItem()) {
arr[iter].put(v.getKey(), res.getImage(v.getValue()));
}
}
}
c.setPropertyValue(cust.getName(), arr);
continue;
}
}
c.setPropertyValue(cust.getName(), null);
continue;
}
}
if (customType == String.class) {
c.setPropertyValue(cust.getName(), cust.getValue());
continue;
}
if (customType == Integer.class) {
if (cust.getValue() != null) {
c.setPropertyValue(cust.getName(), Integer.valueOf(cust.getValue()));
} else {
c.setPropertyValue(cust.getName(), null);
}
continue;
}
if (customType == Long.class) {
if (cust.getValue() != null) {
c.setPropertyValue(cust.getName(), Long.valueOf(cust.getValue()));
} else {
c.setPropertyValue(cust.getName(), null);
}
continue;
}
if (customType == Double.class) {
if (cust.getValue() != null) {
c.setPropertyValue(cust.getName(), Double.valueOf(cust.getValue()));
} else {
c.setPropertyValue(cust.getName(), null);
}
continue;
}
if (customType == Date.class) {
if (cust.getValue() != null) {
c.setPropertyValue(cust.getName(), new Date(Long.parseLong(cust.getValue())));
} else {
c.setPropertyValue(cust.getName(), null);
}
continue;
}
if (customType == Float.class) {
if (cust.getValue() != null) {
c.setPropertyValue(cust.getName(), Float.valueOf(cust.getValue()));
} else {
c.setPropertyValue(cust.getName(), null);
}
continue;
}
if (customType == Byte.class) {
if (cust.getValue() != null) {
c.setPropertyValue(cust.getName(), Byte.valueOf(cust.getValue()));
} else {
c.setPropertyValue(cust.getName(), null);
}
continue;
}
if (customType == Character.class) {
if (cust.getValue() != null && ((String) cust.getValue()).length() > 0) {
c.setPropertyValue(cust.getName(), new Character(((String) cust.getValue()).charAt(0)));
} else {
c.setPropertyValue(cust.getName(), null);
}
continue;
}
if (customType == Boolean.class) {
if (cust.getValue() != null) {
c.setPropertyValue(cust.getName(), Boolean.valueOf(cust.getValue()));
} else {
c.setPropertyValue(cust.getName(), null);
}
continue;
}
if (customType == com.codename1.ui.Image.class) {
if (cust.getValue() != null) {
c.setPropertyValue(cust.getName(), res.getImage(cust.getValue()));
} else {
c.setPropertyValue(cust.getName(), null);
}
continue;
}
if (customType == com.codename1.ui.Container.class) {
// resource might have been removed we need to fail gracefully
String[] uiNames = res.getUIResourceNames();
for (int iter = 0; iter < uiNames.length; iter++) {
if (uiNames[iter].equals(cust.getName())) {
c.setPropertyValue(cust.getName(), createContainer(res, cust.getName()));
continue;
}
}
c.setPropertyValue(cust.getName(), null);
continue;
}
if (customType == com.codename1.ui.list.CellRenderer.class) {
if (cust.getUnselectedRenderer() != null) {
GenericListCellRenderer g;
if (cust.getSelectedRendererEven() == null) {
Component selected = createContainer(res, cust.getSelectedRenderer());
Component unselected = createContainer(res, cust.getUnselectedRenderer());
g = new GenericListCellRenderer(selected, unselected);
g.setFisheye(!cust.getSelectedRenderer().equals(cust.getUnselectedRenderer()));
} else {
Component selected = createContainer(res, cust.getSelectedRenderer());
Component unselected = createContainer(res, cust.getUnselectedRenderer());
Component even = createContainer(res, cust.getSelectedRendererEven());
Component evenU = createContainer(res, cust.getUnselectedRendererEven());
g = new GenericListCellRenderer(selected, unselected, even, evenU);
g.setFisheye(!cust.getSelectedRenderer().equals(cust.getUnselectedRenderer()));
}
c.setPropertyValue(cust.getName(), g);
continue;
}
c.setPropertyValue(cust.getName(), null);
continue;
}
}
}
return c;
} catch (Throwable t) {
t.printStackTrace();
JOptionPane.showMessageDialog(java.awt.Frame.getFrames()[0], "Error creating component: " + root.getName() + "\n" + t.toString() + "\ntrying to recover...", "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
}
use of com.codename1.ui.util.xml.comps.ComponentEntry in project CodenameOne by codenameone.
the class EditableResources method openFileWithXMLSupport.
public void openFileWithXMLSupport(File f) throws IOException {
if (xmlEnabled && f.getParentFile().getName().equals("src")) {
File res = new File(f.getParentFile().getParentFile(), "res");
if (res.exists()) {
File xml = new File(res, f.getName().substring(0, f.getName().length() - 3) + "xml");
if (xml.exists()) {
loadingMode = true;
com.codename1.ui.Font.clearBitmapCache();
clear();
try {
File resDir = new File(res, f.getName().substring(0, f.getName().length() - 4));
// open the XML file...
JAXBContext ctx = JAXBContext.newInstance(ResourceFileXML.class);
ResourceFileXML xmlData = (ResourceFileXML) ctx.createUnmarshaller().unmarshal(xml);
boolean normalize = xmlData.getMajorVersion() > 1 || xmlData.getMinorVersion() > 5;
majorVersion = (short) xmlData.getMajorVersion();
minorVersion = (short) xmlData.getMinorVersion();
xmlUI = xmlData.isUseXmlUI();
if (xmlData.getData() != null) {
for (Data d : xmlData.getData()) {
setResource(d.getName(), MAGIC_DATA, readFile(resDir, d.getName(), normalize));
}
}
if (xmlData.getLegacyFont() != null) {
for (LegacyFont d : xmlData.getLegacyFont()) {
String name = d.getName();
if (normalize) {
name = normalizeFileName(name);
}
DataInputStream fi = new DataInputStream(new FileInputStream(new File(resDir, name)));
setResource(d.getName(), MAGIC_FONT, loadFont(fi, d.getName(), false));
fi.close();
}
}
if (xmlData.getImage() != null) {
for (com.codename1.ui.util.xml.Image d : xmlData.getImage()) {
if (d.getType() == null) {
// standara JPG or PNG
String name = d.getName();
if (normalize) {
name = normalizeFileName(name);
}
FileInputStream fi = new FileInputStream(new File(resDir, name));
EncodedImage e = EncodedImage.create(fi);
fi.close();
setResource(d.getName(), MAGIC_IMAGE, e);
continue;
}
if ("svg".equals(d.getType())) {
setResource(d.getName(), MAGIC_IMAGE, Image.createSVG(d.getType(), false, readFile(resDir, d.getName(), normalize)));
continue;
}
if ("timeline".equals(d.getType())) {
String name = d.getName();
if (normalize) {
name = normalizeFileName(name);
}
DataInputStream fi = new DataInputStream(new FileInputStream(new File(resDir, name)));
setResource(d.getName(), MAGIC_IMAGE, readTimeline(fi));
fi.close();
continue;
}
if ("multi".equals(d.getType())) {
String name = d.getName();
if (normalize) {
name = normalizeFileName(name);
}
File multiImageDir = new File(resDir, name);
File hd4k = new File(multiImageDir, "4k.png");
File hd2 = new File(multiImageDir, "2hd.png");
File hd560 = new File(multiImageDir, "560.png");
File hd = new File(multiImageDir, "hd.png");
File veryhigh = new File(multiImageDir, "veryhigh.png");
File high = new File(multiImageDir, "high.png");
File medium = new File(multiImageDir, "medium.png");
File low = new File(multiImageDir, "low.png");
File veryLow = new File(multiImageDir, "verylow.png");
Map<Integer, EncodedImage> images = new HashMap<Integer, EncodedImage>();
if (hd4k.exists()) {
images.put(new Integer(Display.DENSITY_4K), EncodedImage.create(readFileNoNormal(hd4k)));
}
if (hd2.exists()) {
images.put(new Integer(Display.DENSITY_2HD), EncodedImage.create(readFileNoNormal(hd2)));
}
if (hd560.exists()) {
images.put(new Integer(Display.DENSITY_560), EncodedImage.create(readFileNoNormal(hd560)));
}
if (hd.exists()) {
images.put(new Integer(Display.DENSITY_HD), EncodedImage.create(readFileNoNormal(hd)));
}
if (veryhigh.exists()) {
images.put(new Integer(Display.DENSITY_VERY_HIGH), EncodedImage.create(readFileNoNormal(veryhigh)));
}
if (high.exists()) {
images.put(new Integer(Display.DENSITY_HIGH), EncodedImage.create(readFileNoNormal(high)));
}
if (medium.exists()) {
images.put(new Integer(Display.DENSITY_MEDIUM), EncodedImage.create(readFileNoNormal(medium)));
}
if (low.exists()) {
images.put(new Integer(Display.DENSITY_LOW), EncodedImage.create(readFileNoNormal(low)));
}
if (veryLow.exists()) {
images.put(new Integer(Display.DENSITY_VERY_LOW), EncodedImage.create(readFileNoNormal(veryLow)));
}
int[] dpis = new int[images.size()];
EncodedImage[] imageArray = new EncodedImage[images.size()];
int count = 0;
for (Map.Entry<Integer, EncodedImage> m : images.entrySet()) {
dpis[count] = m.getKey().intValue();
imageArray[count] = m.getValue();
count++;
}
MultiImage result = new MultiImage();
result.setDpi(dpis);
result.setInternalImages(imageArray);
setResource(d.getName(), MAGIC_IMAGE, result);
continue;
}
}
}
if (xmlData.getL10n() != null) {
for (L10n d : xmlData.getL10n()) {
Hashtable<String, Hashtable<String, String>> l10n = new Hashtable<String, Hashtable<String, String>>();
for (Lang l : d.getLang()) {
Hashtable<String, String> language = new Hashtable<String, String>();
if (l != null && l.getEntry() != null) {
for (Entry e : l.getEntry()) {
language.put(e.getKey(), e.getValue());
}
}
l10n.put(l.getName(), language);
}
setResource(d.getName(), MAGIC_L10N, l10n);
}
}
if (xmlData.getTheme() != null) {
for (Theme d : xmlData.getTheme()) {
Hashtable<String, Object> theme = new Hashtable<String, Object>();
theme.put("uninitialized", Boolean.TRUE);
if (d.getVal() != null) {
for (Val v : d.getVal()) {
String key = v.getKey();
if (key.endsWith("align") || key.endsWith("textDecoration")) {
theme.put(key, Integer.valueOf(v.getValue()));
continue;
}
if (key.endsWith(Style.BACKGROUND_TYPE) || key.endsWith(Style.BACKGROUND_ALIGNMENT)) {
theme.put(key, Byte.valueOf(v.getValue()));
continue;
}
// padding and or margin type
if (key.endsWith("Unit")) {
String[] s = v.getValue().split(",");
theme.put(key, new byte[] { Byte.parseByte(s[0]), Byte.parseByte(s[1]), Byte.parseByte(s[2]), Byte.parseByte(s[3]) });
continue;
}
theme.put(key, v.getValue());
}
}
if (d.getBorder() != null) {
for (com.codename1.ui.util.xml.Border b : d.getBorder()) {
if ("empty".equals(b.getType())) {
theme.put(b.getKey(), Border.createEmpty());
continue;
}
if ("round".equals(b.getType())) {
RoundBorder rb = RoundBorder.create();
rb = rb.color(b.getRoundBorderColor());
rb = rb.rectangle(b.isRectangle());
rb = rb.shadowBlur(b.getShadowBlur());
rb = rb.shadowOpacity(b.getShadowOpacity());
rb = rb.shadowSpread((int) b.getShadowSpread(), b.isShadowMM());
rb = rb.shadowX(b.getShadowX());
rb = rb.shadowY(b.getShadowY());
rb = rb.stroke(b.getStrokeThickness(), b.isStrokeMM());
rb = rb.strokeColor(b.getStrokeColor());
rb = rb.strokeOpacity(b.getStrokeOpacity());
theme.put(b.getKey(), rb);
continue;
}
if ("roundRect".equals(b.getType())) {
RoundRectBorder rb = RoundRectBorder.create();
rb = rb.shadowBlur(b.getShadowBlur());
rb = rb.shadowOpacity(b.getShadowOpacity());
rb = rb.shadowSpread(b.getShadowSpread());
rb = rb.shadowX(b.getShadowX());
rb = rb.shadowY(b.getShadowY());
rb = rb.stroke(b.getStrokeThickness(), b.isStrokeMM());
rb = rb.strokeColor(b.getStrokeColor());
rb = rb.strokeOpacity(b.getStrokeOpacity());
rb = rb.bezierCorners(b.isBezierCorners());
rb = rb.bottomOnlyMode(b.isBottomOnlyMode());
rb = rb.topOnlyMode(b.isTopOnlyMode());
rb = rb.cornerRadius(b.getCornerRadius());
theme.put(b.getKey(), rb);
continue;
}
if ("line".equals(b.getType())) {
if (b.getColor() == null) {
if (b.isMillimeters()) {
theme.put(b.getKey(), Border.createLineBorder(b.getThickness().floatValue()));
} else {
theme.put(b.getKey(), Border.createLineBorder(b.getThickness().intValue()));
}
} else {
if (b.isMillimeters()) {
theme.put(b.getKey(), Border.createLineBorder(b.getThickness().floatValue(), b.getColor().intValue()));
} else {
theme.put(b.getKey(), Border.createLineBorder(b.getThickness().intValue(), b.getColor().intValue()));
}
}
continue;
}
if ("underline".equals(b.getType())) {
if (b.getColor() == null) {
theme.put(b.getKey(), Border.createUndelineBorder(b.getThickness().intValue()));
} else {
theme.put(b.getKey(), Border.createUnderlineBorder(b.getThickness().intValue(), b.getColor().intValue()));
}
continue;
}
if ("rounded".equals(b.getType())) {
if (b.getColor() == null) {
theme.put(b.getKey(), Border.createRoundBorder(b.getArcW().intValue(), b.getArcH().intValue()));
} else {
theme.put(b.getKey(), Border.createRoundBorder(b.getArcW().intValue(), b.getArcH().intValue(), b.getColor().intValue()));
}
continue;
}
if ("etchedRaised".equals(b.getType())) {
if (b.getColor() == null) {
theme.put(b.getKey(), Border.createEtchedRaised());
} else {
theme.put(b.getKey(), Border.createEtchedRaised(b.getColor().intValue(), b.getColorB().intValue()));
}
continue;
}
if ("etchedLowered".equals(b.getType())) {
if (b.getColor() == null) {
theme.put(b.getKey(), Border.createEtchedLowered());
} else {
theme.put(b.getKey(), Border.createEtchedLowered(b.getColor().intValue(), b.getColorB().intValue()));
}
continue;
}
if ("bevelLowered".equals(b.getType())) {
if (b.getColor() == null) {
theme.put(b.getKey(), Border.createBevelLowered());
} else {
theme.put(b.getKey(), Border.createBevelLowered(b.getColor().intValue(), b.getColorB().intValue(), b.getColorC().intValue(), b.getColorD().intValue()));
}
continue;
}
if ("bevelRaised".equals(b.getType())) {
if (b.getColor() == null) {
theme.put(b.getKey(), Border.createBevelRaised());
} else {
theme.put(b.getKey(), Border.createBevelRaised(b.getColor().intValue(), b.getColorB().intValue(), b.getColorC().intValue(), b.getColorD().intValue()));
}
continue;
}
if ("image".equals(b.getType())) {
int imageCount = 2;
if (b.getI9() != null) {
imageCount = 9;
} else {
if (b.getI8() != null) {
imageCount = 8;
} else {
if (b.getI3() != null) {
imageCount = 3;
}
}
}
String[] borderInstance;
switch(imageCount) {
case 2:
borderInstance = new String[] { b.getI1(), b.getI2() };
break;
case 3:
borderInstance = new String[] { b.getI1(), b.getI2(), b.getI3() };
break;
case 8:
borderInstance = new String[] { b.getI1(), b.getI2(), b.getI3(), b.getI4(), b.getI5(), b.getI6(), b.getI7(), b.getI8() };
break;
default:
borderInstance = new String[] { b.getI1(), b.getI2(), b.getI3(), b.getI4(), b.getI5(), b.getI6(), b.getI7(), b.getI8(), b.getI9() };
break;
}
theme.put(b.getKey(), borderInstance);
continue;
}
if ("imageH".equals(b.getType())) {
theme.put(b.getKey(), new String[] { "h", b.getI1(), b.getI2(), b.getI3() });
continue;
}
if ("imageV".equals(b.getType())) {
theme.put(b.getKey(), new String[] { "v", b.getI1(), b.getI2(), b.getI3() });
continue;
}
}
}
if (d.getFont() != null) {
for (com.codename1.ui.util.xml.Font b : d.getFont()) {
if ("ttf".equals(b.getType())) {
com.codename1.ui.Font system = com.codename1.ui.Font.createSystemFont(b.getFace().intValue(), b.getStyle().intValue(), b.getSize().intValue());
EditorTTFFont t;
if (b.getName().startsWith("native:")) {
t = new EditorTTFFont(b.getName(), b.getSizeSettings().intValue(), b.getActualSize().floatValue(), system);
} else {
t = new EditorTTFFont(new File(f.getParentFile(), b.getName()), b.getSizeSettings().intValue(), b.getActualSize().floatValue(), system);
}
theme.put(b.getKey(), t);
continue;
}
if ("system".equals(b.getType())) {
com.codename1.ui.Font system = com.codename1.ui.Font.createSystemFont(b.getFace().intValue(), b.getStyle().intValue(), b.getSize().intValue());
theme.put(b.getKey(), system);
continue;
}
// bitmap fonts aren't supported right now
}
}
if (d.getGradient() != null) {
for (com.codename1.ui.util.xml.Gradient b : d.getGradient()) {
theme.put(b.getKey(), new Object[] { b.getColor1(), b.getColor2(), b.getPosX(), b.getPosY(), b.getRadius() });
}
}
setResource(d.getName(), MAGIC_THEME, theme);
}
}
// we load the UI last since it might depend on images or other elements in the future
if (xmlData.getUi() != null) {
if (xmlData.isUseXmlUI()) {
ArrayList<ComponentEntry> guiElements = new ArrayList<ComponentEntry>();
// place renderers first
final ArrayList<String> renderers = new ArrayList<String>();
for (Ui d : xmlData.getUi()) {
JAXBContext componentContext = JAXBContext.newInstance(ComponentEntry.class);
File uiFile = new File(resDir, normalizeFileName(d.getName()) + ".ui");
ComponentEntry uiXMLData = (ComponentEntry) componentContext.createUnmarshaller().unmarshal(uiFile);
guiElements.add(uiXMLData);
uiXMLData.findRendererers(renderers);
}
Collections.sort(guiElements, new Comparator<ComponentEntry>() {
private final ArrayList<String> entries1 = new ArrayList<String>();
private final ArrayList<String> entries2 = new ArrayList<String>();
@Override
public int compare(ComponentEntry o1, ComponentEntry o2) {
if (renderers.contains(o1.getName())) {
return -1;
}
if (renderers.contains(o2.getName())) {
return 1;
}
entries1.clear();
entries2.clear();
o1.findEmbeddedDependencies(entries1);
o2.findEmbeddedDependencies(entries2);
if (entries1.size() == 0) {
if (entries2.size() == 0) {
return 0;
}
return -1;
} else {
if (entries2.size() == 0) {
return 1;
}
}
for (String e : entries1) {
if (e.equals(o2.getName())) {
return 1;
}
}
for (String e : entries2) {
if (e.equals(o1.getName())) {
return -1;
}
}
return 0;
}
});
for (ComponentEntry uiXMLData : guiElements) {
UIBuilderOverride uib = new UIBuilderOverride();
com.codename1.ui.Container cnt = uib.createInstance(uiXMLData, this);
// encountered an error loading the component fallback to loading with the binary types
if (cnt == null) {
for (Ui ui : xmlData.getUi()) {
setResource(uiXMLData.getName(), MAGIC_UI, readFile(resDir, ui.getName(), normalize));
}
break;
} else {
byte[] data = UserInterfaceEditor.persistContainer(cnt, this);
setResource(uiXMLData.getName(), MAGIC_UI, data);
}
}
} else {
for (Ui d : xmlData.getUi()) {
setResource(d.getName(), MAGIC_UI, readFile(resDir, d.getName(), normalize));
}
}
}
loadingMode = false;
modified = false;
updateModified();
// can occure when a resource file is opened via the constructor
if (undoQueue != null) {
undoQueue.clear();
redoQueue.clear();
}
return;
} catch (JAXBException err) {
err.printStackTrace();
}
}
}
}
openFile(new FileInputStream(f));
}
Aggregations