Search in sources :

Example 1 with EditableResources

use of com.codename1.ui.util.EditableResources in project CodenameOne by codenameone.

the class UserInterfaceEditor method persistToXML.

private static void persistToXML(com.codename1.ui.Container containerInstance, com.codename1.ui.Component cmp, StringBuilder build, EditableResources res, String indent, String tabTitle) {
    build.append(indent);
    build.append("<component type=\"");
    build.append((String) cmp.getClientProperty(TYPE_KEY));
    build.append("\" name=\"");
    if (cmp.getName() != null) {
        build.append(xmlize(cmp.getName()));
    }
    build.append("\" ");
    if (exportToNewGuiBuilderMode) {
        String cmpName = cmp.getName();
        Class cls = componentNames.get(cmpName);
        if (cls == null) {
            componentNames.put(cmpName, cmp.getClass());
        } else {
            if (cls != cmp.getClass()) {
                componentNames.put(cmpName, com.codename1.ui.Component.class);
            }
        }
        if (cmp instanceof List) {
            listNames.add(cmp.getName());
        }
        if (cmp instanceof com.codename1.ui.Button || cmp instanceof com.codename1.ui.TextArea || cmp instanceof com.codename1.ui.Slider || cmp instanceof com.codename1.ui.List || cmp instanceof com.codename1.components.MultiButton || cmp instanceof com.codename1.components.SpanButton || cmp instanceof com.codename1.components.OnOffSwitch || cmp instanceof com.codename1.ui.Calendar || cmp instanceof com.codename1.ui.list.ContainerList) {
            // add action listener XML
            actionEventNames.add(cmp.getName());
            build.append(" actionEvent=\"true\" ");
        }
    }
    if (cmp.getClientProperty("cn1$Properties") != null) {
        String[] p = ((String) cmp.getClientProperty("cn1$Properties")).split(",");
        if (p.length > 0) {
            build.append("clientProperties=\"");
            boolean first = true;
            for (String c : p) {
                if (!first) {
                    build.append(",");
                }
                first = false;
                build.append(c);
                build.append("=");
                build.append((String) cmp.getClientProperty(c));
            }
            build.append("\" ");
        }
    }
    if (tabTitle != null) {
        build.append("tabTitle=\"");
        build.append(xmlize(tabTitle));
        build.append("\" ");
    }
    if (cmp.getClientProperty("%base_form%") != null) {
        build.append("baseForm=\"");
        build.append(xmlize((String) cmp.getClientProperty("%base_form%")));
        build.append("\" ");
    }
    if (cmp.getCloudBoundProperty() != null) {
        build.append("cloudBoundProperty=\"");
        build.append(xmlize(cmp.getCloudBoundProperty()));
        build.append("\" ");
    }
    if (cmp.getCloudDestinationProperty() != null) {
        build.append("cloudDestinationProperty=\"");
        build.append(xmlize(cmp.getCloudDestinationProperty()));
        build.append("\" ");
    }
    if (isPropertyModified(cmp, PROPERTY_COMMAND) || isPropertyModified(cmp, PROPERTY_COMMAND_LEGACY)) {
        ActionCommand cmd;
        if (cmp instanceof com.codename1.ui.Container) {
            cmd = (ActionCommand) ((com.codename1.ui.Button) ((com.codename1.ui.Container) cmp).getLeadComponent()).getCommand();
        } else {
            cmd = (ActionCommand) ((com.codename1.ui.Button) cmp).getCommand();
        }
        build.append("commandName=\"");
        build.append(xmlize(cmd.getCommandName()));
        build.append("\" ");
        if (cmd.getIcon() != null) {
            build.append("commandIcon=\"");
            build.append(xmlize(res.findId(cmd.getIcon())));
            build.append("\" ");
        }
        if (cmd.getRolloverIcon() != null) {
            build.append("commandRolloverIcon=\"");
            build.append(xmlize(res.findId(cmd.getRolloverIcon())));
            build.append("\" ");
        }
        if (cmd.getPressedIcon() != null) {
            build.append("commandPressedIcon=\"");
            build.append(xmlize(res.findId(cmd.getPressedIcon())));
            build.append("\" ");
        }
        if (cmd.getDisabledIcon() != null) {
            build.append("commandDisabledIcon=\"");
            build.append(xmlize(res.findId(cmd.getDisabledIcon())));
            build.append("\" ");
        }
        build.append("commandId=\"");
        build.append(cmd.getId());
        build.append("\" ");
        if (cmd.getAction() != null) {
            build.append("commandAction=\"");
            build.append(xmlize(cmd.getAction()));
            build.append("\" ");
            if (cmd.getAction().equals("$Execute")) {
                build.append("commandArgument=\"");
                build.append(xmlize(cmd.getArgument()));
                build.append("\" ");
            }
        }
        build.append("commandBack=\"");
        build.append(cmp.getComponentForm().getBackCommand() == cmd);
        build.append("\" ");
        if (exportToNewGuiBuilderMode) {
            build.append(" varName=\"");
            String varName;
            if (cmd.getCommandName() == null || cmd.getCommandName().length() == 0) {
                varName = ResourceEditorView.normalizeFormName(cmd.getCommandName());
                build.append(varName);
            } else {
                varName = "Command" + commandCounter;
                build.append(varName);
                commandCounter++;
            }
            build.append("\" ");
            String action = cmd.getAction();
            if (action != null) {
                if (cmp.getComponentForm() != null) {
                    commandList.add(cmd);
                    cmd.putClientProperty("FORMNAME", cmp.getComponentForm().getName());
                }
            }
        }
    }
    if (isPropertyModified(cmp, PROPERTY_LABEL_FOR)) {
        if (cmp.getLabelForComponent() != null) {
            build.append("labelFor=\"");
            build.append(xmlize(cmp.getLabelForComponent().getName()));
            build.append("\" ");
        }
    }
    if (isPropertyModified(cmp, PROPERTY_LEAD_COMPONENT) && ((com.codename1.ui.Container) cmp).getLeadComponent() != null) {
        build.append("leadComponent=\"");
        build.append(xmlize(((com.codename1.ui.Container) cmp).getLeadComponent().getName()));
        build.append("\" ");
    }
    if (isPropertyModified(cmp, PROPERTY_NEXT_FOCUS_DOWN) && cmp.getNextFocusDown() != null) {
        build.append("nextFocusDown=\"");
        build.append(xmlize(cmp.getNextFocusDown().getName()));
        build.append("\" ");
    }
    if (isPropertyModified(cmp, PROPERTY_NEXT_FOCUS_UP) && cmp.getNextFocusUp() != null) {
        build.append("nextFocusUp=\"");
        build.append(xmlize(cmp.getNextFocusUp().getName()));
        build.append("\" ");
    }
    if (isPropertyModified(cmp, PROPERTY_NEXT_FOCUS_LEFT) && cmp.getNextFocusLeft() != null) {
        build.append("nextFocusLeft=\"");
        build.append(xmlize(cmp.getNextFocusLeft().getName()));
        build.append("\" ");
    }
    if (isPropertyModified(cmp, PROPERTY_NEXT_FOCUS_RIGHT) && cmp.getNextFocusRight() != null) {
        build.append("nextFocusRight=\"");
        build.append(xmlize(cmp.getNextFocusRight().getName()));
        build.append("\" ");
    }
    if (isPropertyModified(cmp, PROPERTY_EMBED)) {
        build.append("embed=\"");
        build.append(xmlize(((EmbeddedContainer) cmp).getEmbed()));
        build.append("\" ");
    }
    if (isPropertyModified(cmp, PROPERTY_UIID)) {
        build.append("uiid=\"");
        build.append(cmp.getUIID());
        build.append("\" ");
    }
    if (isPropertyModified(cmp, PROPERTY_FOCUSABLE)) {
        build.append("focusable=\"");
        build.append(cmp.isFocusable());
        build.append("\" ");
    }
    if (isPropertyModified(cmp, PROPERTY_ENABLED)) {
        build.append("enabled=\"");
        build.append(cmp.isEnabled());
        build.append("\" ");
    }
    if (isPropertyModified(cmp, PROPERTY_RTL)) {
        build.append("rtl=\"");
        build.append(cmp.isRTL());
        build.append("\" ");
    }
    if (isPropertyModified(cmp, PROPERTY_SCROLL_VISIBLE)) {
        build.append("scrollVisible=\"");
        build.append(cmp.isScrollVisible());
        build.append("\" ");
    }
    /*if(isPropertyModified(cmp, PROPERTY_PREFERRED_WIDTH)) {
            out.writeInt(PROPERTY_PREFERRED_WIDTH);
            out.writeInt(cmp.getPreferredW());
        }

        if(isPropertyModified(cmp, PROPERTY_PREFERRED_HEIGHT)) {
            out.writeInt(PROPERTY_PREFERRED_HEIGHT);
            out.writeInt(cmp.getPreferredH());
        }*/
    if (isPropertyModified(cmp, PROPERTY_TENSILE_DRAG_ENABLED)) {
        build.append("tensileDragEnabled=\"");
        build.append(cmp.isTensileDragEnabled());
        build.append("\" ");
    }
    if (isPropertyModified(cmp, PROPERTY_TACTILE_TOUCH)) {
        build.append("tactileTouch=\"");
        build.append(cmp.isTactileTouch());
        build.append("\" ");
    }
    if (isPropertyModified(cmp, PROPERTY_SNAP_TO_GRID)) {
        build.append("snapToGrid=\"");
        build.append(cmp.isSnapToGrid());
        build.append("\" ");
    }
    if (isPropertyModified(cmp, PROPERTY_FLATTEN)) {
        build.append("flatten=\"");
        build.append(cmp.isFlatten());
        build.append("\" ");
    }
    if (isActualContainer(cmp) || cmp instanceof com.codename1.ui.list.ContainerList) {
        com.codename1.ui.Container cnt = (com.codename1.ui.Container) cmp;
        if (isPropertyModified(cnt, PROPERTY_SCROLLABLE_X)) {
            build.append("scrollableX=\"");
            build.append(CodenameOneAccessor.isScrollableX(cnt));
            build.append("\" ");
        }
        if (isPropertyModified(cnt, PROPERTY_SCROLLABLE_Y)) {
            build.append("scrollableY=\"");
            build.append(CodenameOneAccessor.isScrollableY(cnt));
            build.append("\" ");
        }
        if (cmp instanceof com.codename1.ui.Tabs) {
            com.codename1.ui.Tabs tab = (com.codename1.ui.Tabs) cmp;
            if (isPropertyModified(cmp, PROPERTY_TAB_PLACEMENT)) {
                build.append("tabPlacement=\"");
                build.append(((com.codename1.ui.Tabs) cmp).getTabPlacement());
                build.append("\" ");
            }
            if (isPropertyModified(cmp, PROPERTY_TAB_TEXT_POSITION)) {
                build.append("tabTextPosition=\"");
                build.append(((com.codename1.ui.Tabs) cmp).getTabTextPosition());
                build.append("\" ");
            }
            build.append(">\n");
            appendComponentXMLBody(containerInstance, cmp, build, res, indent + "  ");
            for (int iter = 0; iter < tab.getTabCount(); iter++) {
                persistToXML(containerInstance, tab.getTabComponentAt(iter), build, res, indent + "  ", tab.getTabTitle(iter));
            }
            build.append(indent);
            build.append("</component>\n");
        } else {
            if (isPropertyModified(cmp, PROPERTY_LAYOUT)) {
                com.codename1.ui.layouts.Layout l = cnt.getLayout();
                build.append("layout=\"");
                if (l instanceof com.codename1.ui.layouts.FlowLayout) {
                    com.codename1.ui.layouts.FlowLayout f = (com.codename1.ui.layouts.FlowLayout) l;
                    build.append("FlowLayout\" flowLayoutFillRows=\"");
                    build.append(f.isFillRows());
                    build.append("\" flowLayoutAlign=\"");
                    build.append(f.getAlign());
                    build.append("\" flowLayoutValign=\"");
                    build.append(f.getValign());
                    build.append("\" ");
                } else {
                    if (l instanceof com.codename1.ui.layouts.BorderLayout) {
                        com.codename1.ui.layouts.BorderLayout b = (com.codename1.ui.layouts.BorderLayout) l;
                        build.append("BorderLayout\" borderLayoutAbsoluteCenter=\"");
                        build.append(b.isAbsoluteCenter());
                        build.append("\" ");
                        String north = b.getLandscapeSwap(com.codename1.ui.layouts.BorderLayout.NORTH);
                        String east = b.getLandscapeSwap(com.codename1.ui.layouts.BorderLayout.EAST);
                        String west = b.getLandscapeSwap(com.codename1.ui.layouts.BorderLayout.WEST);
                        String south = b.getLandscapeSwap(com.codename1.ui.layouts.BorderLayout.SOUTH);
                        String center = b.getLandscapeSwap(com.codename1.ui.layouts.BorderLayout.CENTER);
                        if (north != null) {
                            build.append("borderLayoutSwapNorth=\"");
                            build.append(north);
                            build.append("\" ");
                        }
                        if (east != null) {
                            build.append("borderLayoutSwapEast=\"");
                            build.append(east);
                            build.append("\" ");
                        }
                        if (west != null) {
                            build.append("borderLayoutSwapWest=\"");
                            build.append(west);
                            build.append("\" ");
                        }
                        if (south != null) {
                            build.append("borderLayoutSwapSouth=\"");
                            build.append(south);
                            build.append("\" ");
                        }
                        if (center != null) {
                            build.append("borderLayoutSwapCenter=\"");
                            build.append(center);
                            build.append("\" ");
                        }
                    } else {
                        if (l instanceof com.codename1.ui.layouts.GridLayout) {
                            build.append("GridLayout\" gridLayoutRows=\"");
                            build.append(((com.codename1.ui.layouts.GridLayout) l).getRows());
                            build.append("\" gridLayoutColumns=\"");
                            build.append(((com.codename1.ui.layouts.GridLayout) l).getColumns());
                            build.append("\" ");
                        } else {
                            if (l instanceof com.codename1.ui.layouts.BoxLayout) {
                                if (getInt("axis", l.getClass(), l) == com.codename1.ui.layouts.BoxLayout.X_AXIS) {
                                    build.append("BoxLayout\" boxLayoutAxis=\"X\" ");
                                } else {
                                    build.append("BoxLayout\" boxLayoutAxis=\"Y\" ");
                                }
                            } else {
                                if (l instanceof com.codename1.ui.table.TableLayout) {
                                    build.append("TableLayout\" tableLayoutRows=\"");
                                    build.append(((com.codename1.ui.table.TableLayout) l).getRows());
                                    build.append("\" tableLayoutColumns=\"");
                                    build.append(((com.codename1.ui.table.TableLayout) l).getColumns());
                                    build.append("\" ");
                                } else {
                                    if (l instanceof com.codename1.ui.layouts.LayeredLayout) {
                                        build.append("LayeredLayout\" ");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (cmp instanceof com.codename1.ui.Form) {
                com.codename1.ui.Form frm = (com.codename1.ui.Form) cmp;
                if (isPropertyModified(cmp, PROPERTY_NEXT_FORM) && frm.getClientProperty("%next_form%") != null) {
                    build.append("nextForm=\"");
                    build.append(xmlize((String) frm.getClientProperty("%next_form%")));
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_TITLE)) {
                    build.append("title=\"");
                    build.append(xmlize(frm.getTitle()));
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_CYCLIC_FOCUS)) {
                    build.append("cyclicFocus=\"");
                    build.append(frm.isCyclicFocus());
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_DIALOG_UIID) && cmp instanceof com.codename1.ui.Dialog) {
                    com.codename1.ui.Dialog dlg = (com.codename1.ui.Dialog) cmp;
                    build.append("dialogUIID=\"");
                    build.append(dlg.getDialogUIID());
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_DISPOSE_WHEN_POINTER_OUT) && cmp instanceof com.codename1.ui.Dialog) {
                    com.codename1.ui.Dialog dlg = (com.codename1.ui.Dialog) cmp;
                    build.append("disposeWhenPointerOutOfBounds=\"");
                    build.append(dlg.isDisposeWhenPointerOutOfBounds());
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_DIALOG_POSITION) && cmp instanceof com.codename1.ui.Dialog) {
                    com.codename1.ui.Dialog dlg = (com.codename1.ui.Dialog) cmp;
                    if (dlg.getDialogPosition() != null) {
                        build.append("dialogPosition=\"");
                        build.append(dlg.getDialogPosition());
                        build.append("\" ");
                    }
                }
                build.append(">\n");
                appendComponentXMLBody(containerInstance, cmp, build, res, indent + "  ");
                if (frm.getCommandCount() > 0 || frm.getBackCommand() != null) {
                    if (isPropertyModified(cmp, PROPERTY_COMMANDS) || isPropertyModified(cmp, PROPERTY_COMMANDS_LEGACY)) {
                        if (frm.getBackCommand() != null && !hasBackCommand(frm, frm.getBackCommand())) {
                            build.append("<command name=\"");
                            ActionCommand cmd = (ActionCommand) frm.getBackCommand();
                            if (exportToNewGuiBuilderMode) {
                                if (cmp.getComponentForm() != null) {
                                    commandList.add(cmd);
                                    cmd.putClientProperty("FORMNAME", cmp.getComponentForm().getName());
                                }
                            }
                            build.append(xmlize(cmd.getCommandName()));
                            build.append("\" ");
                            if (cmd.getIcon() != null) {
                                build.append("icon=\"");
                                build.append(xmlize(res.findId(cmd.getIcon())));
                                build.append("\" ");
                            }
                            if (cmd.getRolloverIcon() != null) {
                                build.append("rolloverIcon=\"");
                                build.append(xmlize(res.findId(cmd.getRolloverIcon())));
                                build.append("\" ");
                            }
                            if (cmd.getPressedIcon() != null) {
                                build.append("pressedIcon=\"");
                                build.append(xmlize(res.findId(cmd.getPressedIcon())));
                                build.append("\" ");
                            }
                            if (cmd.getDisabledIcon() != null) {
                                build.append("disabledIcon=\"");
                                build.append(xmlize(res.findId(cmd.getDisabledIcon())));
                                build.append("\" ");
                            }
                            build.append("id=\"");
                            build.append(cmd.getId());
                            build.append("\" ");
                            if (cmd.getAction() != null) {
                                build.append("action=\"");
                                build.append(xmlize(cmd.getAction()));
                                build.append("\" ");
                                if (cmd.getAction().equals("$Execute")) {
                                    build.append("argument=\"");
                                    build.append(xmlize(cmd.getArgument()));
                                    build.append("\" ");
                                }
                            }
                            build.append("backCommand=\"");
                            build.append(frm.getBackCommand() == cmd);
                            build.append("\" />");
                        }
                        for (int iter = frm.getCommandCount() - 1; iter >= 0; iter--) {
                            ActionCommand cmd = (ActionCommand) frm.getCommand(iter);
                            if (exportToNewGuiBuilderMode) {
                                if (cmp.getComponentForm() != null) {
                                    commandList.add(cmd);
                                    cmd.putClientProperty("FORMNAME", cmp.getComponentForm().getName());
                                }
                            }
                            build.append("<command name=\"");
                            build.append(xmlize(cmd.getCommandName()));
                            build.append("\" ");
                            if (cmd.getIcon() != null) {
                                build.append("icon=\"");
                                build.append(xmlize(res.findId(cmd.getIcon())));
                                build.append("\" ");
                            }
                            if (cmd.getRolloverIcon() != null) {
                                build.append("rolloverIcon=\"");
                                build.append(xmlize(res.findId(cmd.getRolloverIcon())));
                                build.append("\" ");
                            }
                            if (cmd.getPressedIcon() != null) {
                                build.append("pressedIcon=\"");
                                build.append(xmlize(res.findId(cmd.getPressedIcon())));
                                build.append("\" ");
                            }
                            if (cmd.getDisabledIcon() != null) {
                                build.append("disabledIcon=\"");
                                build.append(xmlize(res.findId(cmd.getDisabledIcon())));
                                build.append("\" ");
                            }
                            build.append("id=\"");
                            build.append(cmd.getId());
                            build.append("\" ");
                            if (cmd.getAction() != null) {
                                build.append("action=\"");
                                build.append(xmlize(cmd.getAction()));
                                build.append("\" ");
                                if (cmd.getAction().equals("$Execute")) {
                                    build.append("argument=\"");
                                    build.append(xmlize(cmd.getArgument()));
                                    build.append("\" ");
                                }
                            }
                            build.append("backCommand=\"");
                            build.append(frm.getBackCommand() == cmd);
                            build.append("\" />");
                        }
                    }
                }
                build.append(indent);
                build.append("</component>\n");
            } else {
                if (!(cmp instanceof com.codename1.ui.list.ContainerList)) {
                    build.append(">\n");
                    appendComponentXMLBody(containerInstance, cmp, build, res, indent + "  ");
                    build.append(indent);
                    build.append("</component>\n");
                } else {
                    com.codename1.ui.list.ContainerList lst = ((com.codename1.ui.list.ContainerList) cmp);
                    if (isPropertyModified(cmp, PROPERTY_LIST_RENDERER) && lst.getRenderer() instanceof com.codename1.ui.list.GenericListCellRenderer) {
                        com.codename1.ui.list.GenericListCellRenderer g = (com.codename1.ui.list.GenericListCellRenderer) lst.getRenderer();
                        if (g.getSelectedEven() == null) {
                            build.append(" selectedRenderer=\"");
                            build.append(xmlize(g.getSelected().getName()));
                            build.append("\" unselectedRenderer=\"");
                            build.append(xmlize(g.getUnselected().getName()));
                            build.append("\" ");
                        } else {
                            build.append(" selectedRenderer=\"");
                            build.append(xmlize(g.getSelected().getName()));
                            build.append("\" unselectedRenderer=\"");
                            build.append(xmlize(g.getUnselected().getName()));
                            build.append(" selectedRendererEven=\"");
                            build.append(xmlize(g.getSelectedEven().getName()));
                            build.append("\" unselectedRendererEven=\"");
                            build.append(xmlize(g.getUnselectedEven().getName()));
                            build.append("\" ");
                        }
                    }
                    build.append(">\n");
                    appendComponentXMLBody(containerInstance, cmp, build, res, indent + "  ");
                    build.append(indent);
                    build.append("</component>\n");
                }
            }
        }
    } else {
        if (cmp instanceof com.codename1.ui.Label) {
            com.codename1.ui.Label lbl = (com.codename1.ui.Label) cmp;
            build.append("text=\"");
            build.append(xmlize(lbl.getText()));
            build.append("\" ");
            if (isPropertyModified(cmp, PROPERTY_ALIGNMENT)) {
                build.append("alignment=\"");
                build.append(lbl.getAlignment());
                build.append("\" ");
            }
            if (isPropertyModified(cmp, PROPERTY_ICON) && lbl.getIcon() != null) {
                build.append("icon=\"");
                build.append(xmlize(res.findId(lbl.getIcon())));
                build.append("\" ");
            }
            if (lbl instanceof com.codename1.ui.Button) {
                com.codename1.ui.Button button = (com.codename1.ui.Button) lbl;
                if (isPropertyModified(cmp, PROPERTY_ROLLOVER_ICON) && button.getRolloverIcon() != null) {
                    build.append("rolloverIcon=\"");
                    build.append(xmlize(res.findId(button.getRolloverIcon())));
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_PRESSED_ICON) && button.getPressedIcon() != null) {
                    build.append("pressedIcon=\"");
                    build.append(xmlize(res.findId(button.getPressedIcon())));
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_DISABLED_ICON) && button.getDisabledIcon() != null) {
                    build.append("disabledIcon=\"");
                    build.append(xmlize(res.findId(button.getDisabledIcon())));
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_TOGGLE_BUTTON)) {
                    build.append("toggle=\"");
                    build.append(button.isToggle());
                    build.append("\" ");
                }
            } else {
                if (lbl instanceof com.codename1.ui.Slider) {
                    com.codename1.ui.Slider sld = (com.codename1.ui.Slider) lbl;
                    if (isPropertyModified(cmp, PROPERTY_EDITABLE)) {
                        build.append("editable=\"");
                        build.append(sld.isEditable());
                        build.append("\" ");
                    }
                    if (isPropertyModified(cmp, PROPERTY_INFINITE)) {
                        build.append("infinite=\"");
                        build.append(sld.isInfinite());
                        build.append("\" ");
                    }
                    if (isPropertyModified(cmp, PROPERTY_SLIDER_THUMB) && sld.getThumbImage() != null) {
                        build.append("thumbImage=\"");
                        build.append(xmlize(res.findId(sld.getThumbImage())));
                        build.append("\" ");
                    }
                    if (isPropertyModified(cmp, PROPERTY_PROGRESS)) {
                        build.append("progress=\"");
                        build.append(sld.getProgress());
                        build.append("\" ");
                    }
                    if (isPropertyModified(cmp, PROPERTY_VERTICAL)) {
                        build.append("vertical=\"");
                        build.append(sld.isVertical());
                        build.append("\" ");
                    }
                    if (isPropertyModified(cmp, PROPERTY_INCREMENTS)) {
                        build.append("increments=\"");
                        build.append(sld.getIncrements());
                        build.append("\" ");
                    }
                    if (isPropertyModified(cmp, PROPERTY_MAX_VALUE)) {
                        build.append("maxValue=\"");
                        build.append(sld.getMaxValue());
                        build.append("\" ");
                    }
                    if (isPropertyModified(cmp, PROPERTY_MIN_VALUE)) {
                        build.append("minValue=\"");
                        build.append(sld.getMinValue());
                        build.append("\" ");
                    }
                    if (isPropertyModified(cmp, PROPERTY_RENDER_PERCENTAGE_ON_TOP)) {
                        build.append("renderPercentageOnTop=\"");
                        build.append(sld.isRenderPercentageOnTop());
                        build.append("\" ");
                    }
                }
            }
            if (isPropertyModified(cmp, PROPERTY_RADIO_GROUP)) {
                build.append("group=\"");
                build.append(xmlize(((com.codename1.ui.RadioButton) cmp).getGroup()));
                build.append("\" ");
            }
            if (isPropertyModified(cmp, PROPERTY_SELECTED)) {
                build.append("selected=\"");
                build.append(((com.codename1.ui.Button) cmp).isSelected());
                build.append("\" ");
            }
            if (isPropertyModified(cmp, PROPERTY_GAP)) {
                build.append("gap=\"");
                build.append(lbl.getGap());
                build.append("\" ");
            }
            if (isPropertyModified(cmp, PROPERTY_VERTICAL_ALIGNMENT)) {
                build.append("verticalAlignment=\"");
                build.append(lbl.getVerticalAlignment());
                build.append("\" ");
            }
            if (isPropertyModified(cmp, PROPERTY_TEXT_POSITION)) {
                build.append("textPosition=\"");
                build.append(lbl.getTextPosition());
                build.append("\" ");
            }
        } else {
            if (cmp instanceof com.codename1.ui.TextArea) {
                com.codename1.ui.TextArea txt = (com.codename1.ui.TextArea) cmp;
                if (isPropertyModified(cmp, PROPERTY_VERTICAL_ALIGNMENT)) {
                    build.append("verticalAlignment=\"");
                    build.append(txt.getVerticalAlignment());
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_TEXT)) {
                    build.append("text=\"");
                    build.append(xmlize(txt.getText()));
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_TEXT_AREA_GROW)) {
                    build.append("growByContent=\"");
                    build.append(txt.isGrowByContent());
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_TEXT_CONSTRAINT)) {
                    build.append("constraint=\"");
                    build.append(txt.getConstraint());
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_TEXT_MAX_LENGTH)) {
                    build.append("maxSize=\"");
                    build.append(txt.getMaxSize());
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_EDITABLE)) {
                    build.append("editable=\"");
                    build.append(txt.isEditable());
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_ALIGNMENT)) {
                    build.append("alignment=\"");
                    build.append(txt.getAlignment());
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_HINT)) {
                    build.append("hint=\"");
                    build.append(xmlize(txt.getHint()));
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_HINT_ICON) && txt.getHintIcon() != null) {
                    build.append("hintIcon=\"");
                    build.append(xmlize(res.findId(txt.getHintIcon())));
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_COLUMNS)) {
                    build.append("columns=\"");
                    build.append(txt.getColumns());
                    build.append("\" ");
                }
                if (isPropertyModified(cmp, PROPERTY_ROWS)) {
                    build.append("rows=\"");
                    build.append(txt.getRows());
                    build.append("\" ");
                }
            } else {
                if (cmp instanceof com.codename1.ui.List) {
                    com.codename1.ui.List lst = (com.codename1.ui.List) cmp;
                    if (isPropertyModified(cmp, PROPERTY_ITEM_GAP)) {
                        build.append("itemGap=\"");
                        build.append(lst.getItemGap());
                        build.append("\" ");
                    }
                    if (isPropertyModified(cmp, PROPERTY_LIST_FIXED)) {
                        build.append("fixedSelection=\"");
                        build.append(lst.getFixedSelection());
                        build.append("\" ");
                    }
                    if (isPropertyModified(cmp, PROPERTY_LIST_ORIENTATION)) {
                        build.append("orientation=\"");
                        build.append(lst.getOrientation());
                        build.append("\" ");
                    }
                    if (isPropertyModified(cmp, PROPERTY_HINT)) {
                        build.append("hint=\"");
                        build.append(xmlize(lst.getHint()));
                        build.append("\" ");
                    }
                    if (isPropertyModified(cmp, PROPERTY_HINT_ICON) && lst.getHintIcon() != null) {
                        build.append("hintIcon=\"");
                        build.append(xmlize(res.findId(lst.getHintIcon())));
                        build.append("\" ");
                    }
                    if (isPropertyModified(cmp, PROPERTY_LIST_RENDERER) && lst.getRenderer() instanceof com.codename1.ui.list.GenericListCellRenderer) {
                        com.codename1.ui.list.GenericListCellRenderer g = (com.codename1.ui.list.GenericListCellRenderer) lst.getRenderer();
                        if (g.getSelectedEven() == null) {
                            build.append(" selectedRenderer=\"");
                            build.append(xmlize(g.getSelected().getName()));
                            build.append("\" unselectedRenderer=\"");
                            build.append(xmlize(g.getUnselected().getName()));
                            build.append("\" ");
                        } else {
                            build.append(" selectedRenderer=\"");
                            build.append(xmlize(g.getSelected().getName()));
                            build.append("\" unselectedRenderer=\"");
                            build.append(xmlize(g.getUnselected().getName()));
                            build.append(" selectedRendererEven=\"");
                            build.append(xmlize(g.getSelectedEven().getName()));
                            build.append("\" unselectedRendererEven=\"");
                            build.append(xmlize(g.getUnselectedEven().getName()));
                            build.append("\" ");
                        }
                    }
                }
            }
        }
        build.append(">\n");
        appendComponentXMLBody(containerInstance, cmp, build, res, indent + "  ");
        build.append(indent);
        build.append("</component>\n");
    }
}
Also used : JLabel(javax.swing.JLabel) EmbeddedContainer(com.codename1.ui.util.EmbeddedContainer) BorderLayout(com.codename1.ui.layouts.BorderLayout) JButton(javax.swing.JButton) ArrayList(java.util.ArrayList) List(java.util.List) JList(javax.swing.JList) EmbeddedContainer(com.codename1.ui.util.EmbeddedContainer) BorderLayout(com.codename1.ui.layouts.BorderLayout) TableLayout(com.codename1.ui.table.TableLayout) Hashtable(java.util.Hashtable) Point(java.awt.Point)

Example 2 with EditableResources

use of com.codename1.ui.util.EditableResources in project CodenameOne by codenameone.

the class UserInterfaceEditor method postCreateComponent.

public static void postCreateComponent(com.codename1.ui.Component cmp, DataOutputStream out, EditableResources res) throws IOException {
    if (isPropertyModified(cmp, PROPERTY_COMMAND) || isPropertyModified(cmp, PROPERTY_COMMAND_LEGACY)) {
        out.writeUTF(cmp.getName());
        out.writeInt(PROPERTY_COMMAND);
        ActionCommand cmd;
        if (cmp instanceof com.codename1.ui.Container) {
            cmd = (ActionCommand) ((com.codename1.ui.Button) ((com.codename1.ui.Container) cmp).getLeadComponent()).getCommand();
        } else {
            cmd = (ActionCommand) ((com.codename1.ui.Button) cmp).getCommand();
        }
        out.writeUTF(cmd.getCommandName());
        if (cmd.getIcon() != null) {
            out.writeUTF(res.findId(cmd.getIcon()));
        } else {
            out.writeUTF("");
        }
        if (cmd.getRolloverIcon() != null) {
            out.writeUTF(res.findId(cmd.getRolloverIcon()));
        } else {
            out.writeUTF("");
        }
        if (cmd.getPressedIcon() != null) {
            out.writeUTF(res.findId(cmd.getPressedIcon()));
        } else {
            out.writeUTF("");
        }
        if (cmd.getDisabledIcon() != null) {
            out.writeUTF(res.findId(cmd.getDisabledIcon()));
        } else {
            out.writeUTF("");
        }
        out.writeInt(cmd.getId());
        if (cmd.getAction() != null) {
            out.writeUTF(cmd.getAction());
            if (cmd.getAction().equals("$Execute")) {
                out.writeUTF(cmd.getArgument());
            }
        } else {
            out.writeUTF("");
        }
        out.writeBoolean(cmp.getComponentForm().getBackCommand() == cmd || cmd.isBackCommand());
    }
    if (isPropertyModified(cmp, PROPERTY_LABEL_FOR)) {
        if (cmp.getLabelForComponent() != null) {
            out.writeUTF(cmp.getName());
            out.writeInt(PROPERTY_LABEL_FOR);
            out.writeUTF(cmp.getLabelForComponent().getName());
        }
    }
    if (isPropertyModified(cmp, PROPERTY_LEAD_COMPONENT) && ((com.codename1.ui.Container) cmp).getLeadComponent() != null) {
        out.writeUTF(cmp.getName());
        out.writeInt(PROPERTY_LEAD_COMPONENT);
        out.writeUTF(((com.codename1.ui.Container) cmp).getLeadComponent().getName());
    }
    if (isPropertyModified(cmp, PROPERTY_NEXT_FOCUS_DOWN) && cmp.getNextFocusDown() != null) {
        out.writeUTF(cmp.getName());
        out.writeInt(PROPERTY_NEXT_FOCUS_DOWN);
        out.writeUTF(cmp.getNextFocusDown().getName());
    }
    if (isPropertyModified(cmp, PROPERTY_NEXT_FOCUS_UP) && cmp.getNextFocusUp() != null) {
        out.writeUTF(cmp.getName());
        out.writeInt(PROPERTY_NEXT_FOCUS_UP);
        out.writeUTF(cmp.getNextFocusUp().getName());
    }
    if (isPropertyModified(cmp, PROPERTY_NEXT_FOCUS_LEFT) && cmp.getNextFocusLeft() != null) {
        out.writeUTF(cmp.getName());
        out.writeInt(PROPERTY_NEXT_FOCUS_LEFT);
        out.writeUTF(cmp.getNextFocusLeft().getName());
    }
    if (isPropertyModified(cmp, PROPERTY_NEXT_FOCUS_RIGHT) && cmp.getNextFocusRight() != null) {
        out.writeUTF(cmp.getName());
        out.writeInt(PROPERTY_NEXT_FOCUS_RIGHT);
        out.writeUTF(cmp.getNextFocusRight().getName());
    }
    if (isActualContainer(cmp)) {
        com.codename1.ui.Container c = (com.codename1.ui.Container) cmp;
        for (int iter = 0; iter < c.getComponentCount(); iter++) {
            postCreateComponent(c.getComponentAt(iter), out, res);
        }
    }
}
Also used : EmbeddedContainer(com.codename1.ui.util.EmbeddedContainer) JButton(javax.swing.JButton) Point(java.awt.Point)

Example 3 with EditableResources

use of com.codename1.ui.util.EditableResources in project CodenameOne by codenameone.

the class EditableResources method saveXMLFile.

private void saveXMLFile(File xml, File resourcesDir) throws IOException {
    // disable override for the duration of the save so stuff from the override doesn't
    // get into the main resource file
    File overrideFileBackup = overrideFile;
    EditableResources overrideResourceBackup = overrideResource;
    overrideResource = null;
    overrideFile = null;
    try {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xml), "UTF-8"));
        String[] resourceNames = getResourceNames();
        Arrays.sort(resourceNames, String.CASE_INSENSITIVE_ORDER);
        bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
        bw.write("<resource majorVersion=\"" + MAJOR_VERSION + "\" minorVersion=\"" + MINOR_VERSION + "\" useXmlUI=\"" + xmlUI + "\">\n");
        for (int iter = 0; iter < resourceNames.length; iter++) {
            String xResourceName = xmlize(resourceNames[iter]);
            // write the magic number
            byte magic = getResourceType(resourceNames[iter]);
            switch(magic) {
                case MAGIC_TIMELINE:
                case MAGIC_ANIMATION_LEGACY:
                case MAGIC_IMAGE_LEGACY:
                case MAGIC_INDEXED_IMAGE_LEGACY:
                    magic = MAGIC_IMAGE;
                    break;
                case MAGIC_THEME_LEGACY:
                    magic = MAGIC_THEME;
                    break;
                case MAGIC_FONT_LEGACY:
                    magic = MAGIC_FONT;
                    break;
            }
            switch(magic) {
                case MAGIC_IMAGE:
                    Object o = getResourceObject(resourceNames[iter]);
                    if (!(o instanceof MultiImage)) {
                        o = null;
                    }
                    bw.write("    <image name=\"" + xResourceName + "\" ");
                    com.codename1.ui.Image image = getImage(resourceNames[iter]);
                    MultiImage mi = (MultiImage) o;
                    int rType = getImageType(image, mi);
                    switch(rType) {
                        // PNG file
                        case 0xf1:
                        // JPEG File
                        case 0xf2:
                            if (image instanceof EncodedImage) {
                                byte[] data = ((EncodedImage) image).getImageData();
                                writeToFile(data, new File(resourcesDir, normalizeFileName(resourceNames[iter])));
                            } else {
                                FileOutputStream fo = new FileOutputStream(new File(resourcesDir, normalizeFileName(resourceNames[iter])));
                                BufferedImage buffer = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
                                buffer.setRGB(0, 0, image.getWidth(), image.getHeight(), image.getRGB(), 0, image.getWidth());
                                ImageIO.write(buffer, "png", fo);
                                fo.close();
                            }
                            break;
                        // SVG
                        case 0xf5:
                        // multiimage with SVG
                        case 0xf7:
                            SVG s = (SVG) image.getSVGDocument();
                            writeToFile(s.getSvgData(), new File(resourcesDir, normalizeFileName(resourceNames[iter])));
                            if (s.getBaseURL() != null && s.getBaseURL().length() > 0) {
                                bw.write("baseUrl=\"" + s.getBaseURL() + "\" ");
                            }
                            bw.write("type=\"svg\" ");
                            break;
                        case 0xF6:
                            File multiImageDir = new File(resourcesDir, normalizeFileName(resourceNames[iter]));
                            multiImageDir.mkdirs();
                            for (int imageIter = 0; imageIter < mi.getDpi().length; imageIter++) {
                                File f = null;
                                switch(mi.getDpi()[imageIter]) {
                                    case Display.DENSITY_4K:
                                        f = new File(multiImageDir, "4k.png");
                                        break;
                                    case Display.DENSITY_2HD:
                                        f = new File(multiImageDir, "2hd.png");
                                        break;
                                    case Display.DENSITY_560:
                                        f = new File(multiImageDir, "560.png");
                                        break;
                                    case Display.DENSITY_HD:
                                        f = new File(multiImageDir, "hd.png");
                                        break;
                                    case Display.DENSITY_VERY_HIGH:
                                        f = new File(multiImageDir, "veryhigh.png");
                                        break;
                                    case Display.DENSITY_HIGH:
                                        f = new File(multiImageDir, "high.png");
                                        break;
                                    case Display.DENSITY_MEDIUM:
                                        f = new File(multiImageDir, "medium.png");
                                        break;
                                    case Display.DENSITY_LOW:
                                        f = new File(multiImageDir, "low.png");
                                        break;
                                    case Display.DENSITY_VERY_LOW:
                                        f = new File(multiImageDir, "verylow.png");
                                        break;
                                }
                                writeToFile(mi.getInternalImages()[imageIter].getImageData(), f);
                            }
                            bw.write("type=\"multi\" ");
                            break;
                        // Timeline
                        case MAGIC_TIMELINE:
                            File timeline = new File(resourcesDir, normalizeFileName(resourceNames[iter]));
                            DataOutputStream timelineOut = new DataOutputStream(new FileOutputStream(timeline));
                            writeTimeline(timelineOut, (Timeline) image);
                            timelineOut.close();
                            bw.write("type=\"timeline\" ");
                            break;
                        // Fail this is the wrong data type
                        default:
                            throw new IOException("Illegal type while creating image: " + Integer.toHexString(rType));
                    }
                    bw.write(" />\n");
                    continue;
                case MAGIC_THEME:
                    Hashtable<String, Object> theme = getTheme(resourceNames[iter]);
                    theme.remove("name");
                    bw.write("    <theme name=\"" + xResourceName + "\">\n");
                    ArrayList<String> setOfKeys = new ArrayList<String>(theme.keySet());
                    Collections.sort(setOfKeys);
                    for (String key : setOfKeys) {
                        if (key.startsWith("@")) {
                            if (key.endsWith("Image")) {
                                bw.write("        <val key=\"" + key + "\" value=\"" + findId(theme.get(key), true) + "\" />\n");
                            } else {
                                bw.write("        <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
                            }
                            continue;
                        }
                        // if this is a simple numeric value
                        if (key.endsWith("Color")) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
                            continue;
                        }
                        if (key.endsWith("align") || key.endsWith("textDecoration")) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + ((Number) theme.get(key)).shortValue() + "\" />\n");
                            continue;
                        }
                        // if this is a short numeric value
                        if (key.endsWith("transparency")) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
                            continue;
                        }
                        if (key.endsWith("opacity")) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
                            continue;
                        }
                        // if this is a padding or margin then we will have the 4 values as bytes
                        if (key.endsWith("padding") || key.endsWith("margin")) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
                            continue;
                        }
                        // padding and or margin type
                        if (key.endsWith("Unit")) {
                            byte[] b = (byte[]) theme.get(key);
                            bw.write("        <val key=\"" + key + "\" value=\"" + b[0] + "," + b[1] + "," + b[2] + "," + b[3] + "\" />\n");
                            continue;
                        }
                        if (key.endsWith("border")) {
                            Border border = (Border) theme.get(key);
                            if (border instanceof RoundBorder) {
                                RoundBorder rb = (RoundBorder) border;
                                bw.write("        <border key=\"" + key + "\" type=\"round\" " + "roundBorderColor=\"" + rb.getColor() + "\" " + "opacity=\"" + rb.getOpacity() + "\" " + "strokeColor=\"" + rb.getStrokeColor() + "\" " + "strokeOpacity=\"" + rb.getStrokeOpacity() + "\" " + "strokeThickness=\"" + rb.getStrokeThickness() + "\" " + "strokeMM=\"" + rb.isStrokeMM() + "\" " + "shadowSpread=\"" + rb.getShadowSpread() + "\" " + "shadowOpacity=\"" + rb.getShadowOpacity() + "\" " + "shadowX=\"" + rb.getShadowX() + "\" " + "shadowY=\"" + rb.getShadowY() + "\" " + "shadowBlur=\"" + rb.getShadowBlur() + "\" " + "shadowMM=\"" + rb.isShadowMM() + "\" " + "rectangle=\"" + rb.isRectangle() + "\" />\n");
                                continue;
                            }
                            if (border instanceof RoundRectBorder) {
                                RoundRectBorder rb = (RoundRectBorder) border;
                                bw.write("        <border key=\"" + key + "\" type=\"roundRect\" " + "strokeColor=\"" + rb.getStrokeColor() + "\" " + "strokeOpacity=\"" + rb.getStrokeOpacity() + "\" " + "strokeThickness=\"" + rb.getStrokeThickness() + "\" " + "strokeMM=\"" + rb.isStrokeMM() + "\" " + "shadowSpread=\"" + rb.getShadowSpread() + "\" " + "shadowOpacity=\"" + rb.getShadowOpacity() + "\" " + "shadowX=\"" + rb.getShadowX() + "\" " + "shadowY=\"" + rb.getShadowY() + "\" " + "shadowBlur=\"" + rb.getShadowBlur() + "\" " + "topOnlyMode=\"" + rb.isTopOnlyMode() + "\" " + "bottomOnlyMode=\"" + rb.isBottomOnlyMode() + "\" " + "cornerRadius=\"" + rb.getCornerRadius() + "\" " + "bezierCorners=\"" + rb.isBezierCorners() + "\" />\n");
                                continue;
                            }
                            int type = Accessor.getType(border);
                            switch(type) {
                                case BORDER_TYPE_EMPTY:
                                    bw.write("        <border key=\"" + key + "\" type=\"empty\" />\n");
                                    continue;
                                case BORDER_TYPE_LINE:
                                    // use theme colors?
                                    if (Accessor.isThemeColors(border)) {
                                        bw.write("        <border key=\"" + key + "\" type=\"line\" millimeters=\"" + Accessor.isMillimeters(border) + "\" thickness=\"" + Accessor.getThickness(border) + "\" />\n");
                                    } else {
                                        bw.write("        <border key=\"" + key + "\" type=\"line\" millimeters=\"" + Accessor.isMillimeters(border) + "\" thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" />\n");
                                    }
                                    continue;
                                case BORDER_TYPE_UNDERLINE:
                                    // use theme colors?
                                    if (Accessor.isThemeColors(border)) {
                                        bw.write("        <border key=\"" + key + "\" type=\"underline\" millimeters=\"" + Accessor.isMillimeters(border) + "\" thickness=\"" + Accessor.getThickness(border) + "\" />\n");
                                    } else {
                                        bw.write("        <border key=\"" + key + "\" type=\"underline\"  millimeters=\"" + Accessor.isMillimeters(border) + "\" thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" />\n");
                                    }
                                    continue;
                                case BORDER_TYPE_ROUNDED:
                                    if (Accessor.isThemeColors(border)) {
                                        bw.write("        <border key=\"" + key + "\" type=\"rounded\" " + "thickness=\"" + Accessor.getThickness(border) + "\" arcW=\"" + Accessor.getArcWidth(border) + "\" arcH=\"" + Accessor.getArcHeight(border) + "\" />\n");
                                    } else {
                                        bw.write("        <border key=\"" + key + "\" type=\"rounded\" " + "thickness=\"" + Accessor.getThickness(border) + "\" arcW=\"" + Accessor.getArcWidth(border) + "\" arcH=\"" + Accessor.getArcHeight(border) + "\" color=\"" + Accessor.getColorA(border) + "\" />\n");
                                    }
                                    continue;
                                case BORDER_TYPE_ETCHED_RAISED:
                                    // use theme colors?
                                    if (Accessor.isThemeColors(border)) {
                                        bw.write("        <border key=\"" + key + "\" type=\"etchedRaised\" " + "thickness=\"" + Accessor.getThickness(border) + "\" />\n");
                                    } else {
                                        bw.write("        <border key=\"" + key + "\" type=\"etchedRaised\" " + "thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" colorB=\"" + Accessor.getColorB(border) + "\" />\n");
                                    }
                                    continue;
                                case BORDER_TYPE_ETCHED_LOWERED:
                                    // use theme colors?
                                    if (Accessor.isThemeColors(border)) {
                                        bw.write("        <border key=\"" + key + "\" type=\"etchedLowered\" " + "thickness=\"" + Accessor.getThickness(border) + "\" />\n");
                                    } else {
                                        bw.write("        <border key=\"" + key + "\" type=\"etchedLowered\" " + "thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" colorB=\"" + Accessor.getColorB(border) + "\" />\n");
                                    }
                                    continue;
                                case BORDER_TYPE_BEVEL_LOWERED:
                                    // use theme colors?
                                    if (Accessor.isThemeColors(border)) {
                                        bw.write("        <border key=\"" + key + "\" type=\"bevelLowered\" " + "thickness=\"" + Accessor.getThickness(border) + "\" />\n");
                                    } else {
                                        bw.write("        <border key=\"" + key + "\" type=\"bevelLowered\" " + "thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" colorB=\"" + Accessor.getColorB(border) + "\" colorC=\"" + Accessor.getColorC(border) + "\" colorD=\"" + Accessor.getColorD(border) + "\" />\n");
                                    }
                                    continue;
                                case BORDER_TYPE_BEVEL_RAISED:
                                    if (Accessor.isThemeColors(border)) {
                                        bw.write("        <border key=\"" + key + "\" type=\"bevelRaised\" " + "thickness=\"" + Accessor.getThickness(border) + "\" />\n");
                                    } else {
                                        bw.write("        <border key=\"" + key + "\" type=\"bevelRaised\" " + "thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" colorB=\"" + Accessor.getColorB(border) + "\" colorC=\"" + Accessor.getColorC(border) + "\" colorD=\"" + Accessor.getColorD(border) + "\" />\n");
                                    }
                                    continue;
                                // case BORDER_TYPE_IMAGE_SCALED:
                                case BORDER_TYPE_IMAGE:
                                    {
                                        Image[] images = Accessor.getImages(border);
                                        int resourceCount = 0;
                                        for (int counter = 0; counter < images.length; counter++) {
                                            if (images[counter] != null && findId(images[counter], true) != null) {
                                                resourceCount++;
                                            }
                                        }
                                        if (resourceCount != 2 && resourceCount != 3 && resourceCount != 8 && resourceCount != 9) {
                                            System.out.println("Odd resource count for image border: " + resourceCount);
                                            resourceCount = 2;
                                        }
                                        switch(resourceCount) {
                                            case 2:
                                                bw.write("        <border key=\"" + key + "\" type=\"image\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[4], true) + "\" />\n");
                                                break;
                                            case 3:
                                                bw.write("        <border key=\"" + key + "\" type=\"image\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[4], true) + "\" " + "i3=\"" + findId(images[8], true) + "\" />\n");
                                                break;
                                            case 8:
                                                bw.write("        <border key=\"" + key + "\" type=\"image\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[1], true) + "\" " + "i3=\"" + findId(images[2], true) + "\" " + "i4=\"" + findId(images[3], true) + "\" " + "i5=\"" + findId(images[4], true) + "\" " + "i6=\"" + findId(images[5], true) + "\" " + "i7=\"" + findId(images[6], true) + "\" " + "i8=\"" + findId(images[7], true) + "\" />\n");
                                                break;
                                            case 9:
                                                bw.write("        <border key=\"" + key + "\" type=\"image\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[1], true) + "\" " + "i3=\"" + findId(images[2], true) + "\" " + "i4=\"" + findId(images[3], true) + "\" " + "i5=\"" + findId(images[4], true) + "\" " + "i6=\"" + findId(images[5], true) + "\" " + "i7=\"" + findId(images[6], true) + "\" " + "i8=\"" + findId(images[7], true) + "\" " + "i9=\"" + findId(images[8], true) + "\" />\n");
                                                break;
                                        }
                                        continue;
                                    }
                                case BORDER_TYPE_IMAGE_HORIZONTAL:
                                    {
                                        Image[] images = Accessor.getImages(border);
                                        bw.write("        <border key=\"" + key + "\" type=\"imageH\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[1], true) + "\" " + "i3=\"" + findId(images[2], true) + "\" />\n");
                                        continue;
                                    }
                                case BORDER_TYPE_IMAGE_VERTICAL:
                                    {
                                        Image[] images = Accessor.getImages(border);
                                        bw.write("        <border key=\"" + key + "\" type=\"imageV\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[1], true) + "\" " + "i3=\"" + findId(images[2], true) + "\" />\n");
                                        continue;
                                    }
                            }
                            continue;
                        }
                        // if this is a font
                        if (key.endsWith("font")) {
                            com.codename1.ui.Font f = (com.codename1.ui.Font) theme.get(key);
                            // is this a new font?
                            boolean newFont = f instanceof EditorFont;
                            if (newFont) {
                                bw.write("        <font key=\"" + key + "\" type=\"named\" " + "name=\"" + findId(f) + "\" />\n");
                            } else {
                                if (f instanceof EditorTTFFont && (((EditorTTFFont) f).getFontFile() != null || ((EditorTTFFont) f).getNativeFontName() != null)) {
                                    EditorTTFFont ed = (EditorTTFFont) f;
                                    String fname;
                                    String ffName;
                                    if (((EditorTTFFont) f).getNativeFontName() != null) {
                                        fname = ((EditorTTFFont) f).getNativeFontName();
                                        ffName = fname;
                                    } else {
                                        fname = ed.getFontFile().getName();
                                        ffName = ((java.awt.Font) ed.getNativeFont()).getPSName();
                                    }
                                    bw.write("        <font key=\"" + key + "\" type=\"ttf\" " + "face=\"" + f.getFace() + "\" " + "style=\"" + f.getStyle() + "\" " + "size=\"" + f.getSize() + "\" " + "name=\"" + fname + "\" " + "family=\"" + ffName + "\" " + "sizeSettings=\"" + ed.getSizeSetting() + "\" " + "actualSize=\"" + ed.getActualSize() + "\" />\n");
                                } else {
                                    bw.write("        <font key=\"" + key + "\" type=\"system\" " + "face=\"" + f.getFace() + "\" " + "style=\"" + f.getStyle() + "\" " + "size=\"" + f.getSize() + "\" />\n");
                                }
                            }
                            continue;
                        }
                        // if this is a background image
                        if (key.endsWith("bgImage")) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + findId(theme.get(key), true) + "\" />\n");
                            continue;
                        }
                        if (key.endsWith("scaledImage")) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
                            continue;
                        }
                        if (key.endsWith("derive")) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
                            continue;
                        }
                        // if this is a background gradient
                        if (key.endsWith("bgGradient")) {
                            Object[] gradient = (Object[]) theme.get(key);
                            bw.write("        <gradient key=\"" + key + "\" color1=\"" + gradient[0] + "\"" + " color2=\"" + gradient[1] + "\"" + " posX=\"" + gradient[2] + "\"" + " posY=\"" + gradient[3] + "\"" + " radius=\"" + gradient[4] + "\" />\n");
                            continue;
                        }
                        if (key.endsWith(Style.BACKGROUND_TYPE) || key.endsWith(Style.BACKGROUND_ALIGNMENT)) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
                            continue;
                        }
                        // thow an exception no idea what this is
                        throw new IOException("Error while trying to read theme property: " + key);
                    }
                    bw.write("    </theme>\n");
                    continue;
                case MAGIC_FONT:
                    File legacyFont = new File(resourcesDir, normalizeFileName(resourceNames[iter]));
                    DataOutputStream legacyFontOut = new DataOutputStream(new FileOutputStream(legacyFont));
                    saveFont(legacyFontOut, false, resourceNames[iter]);
                    legacyFontOut.close();
                    bw.write("    <legacyFont name=\"" + xResourceName + "\" />\n");
                    continue;
                case MAGIC_DATA:
                    {
                        File dataFile = new File(resourcesDir, normalizeFileName(resourceNames[iter]));
                        DataOutputStream dataFileOut = new DataOutputStream(new FileOutputStream(dataFile));
                        InputStream i = getData(resourceNames[iter]);
                        ByteArrayOutputStream outArray = new ByteArrayOutputStream();
                        int val = i.read();
                        while (val != -1) {
                            outArray.write(val);
                            val = i.read();
                        }
                        byte[] data = outArray.toByteArray();
                        dataFileOut.write(data);
                        dataFileOut.close();
                        bw.write("    <data name=\"" + xResourceName + "\" />\n");
                        continue;
                    }
                case MAGIC_UI:
                    {
                        File uiXML = new File(resourcesDir, resourceNames[iter] + ".ui");
                        UIBuilderOverride u = new UIBuilderOverride();
                        com.codename1.ui.Container cnt = u.createContainer(this, resourceNames[iter]);
                        FileOutputStream fos = new FileOutputStream(uiXML);
                        writeUIXml(cnt, fos);
                        fos.close();
                        File ui = new File(resourcesDir, resourceNames[iter]);
                        DataOutputStream uiOut = new DataOutputStream(new FileOutputStream(ui));
                        InputStream i = getUi(resourceNames[iter]);
                        ByteArrayOutputStream outArray = new ByteArrayOutputStream();
                        int val = i.read();
                        while (val != -1) {
                            outArray.write(val);
                            val = i.read();
                        }
                        byte[] data = outArray.toByteArray();
                        uiOut.write(data);
                        uiOut.close();
                        bw.write("    <ui name=\"" + xResourceName + "\" />\n");
                        continue;
                    }
                case MAGIC_L10N:
                    // we are getting the theme which allows us to acces the l10n data
                    bw.write("    <l10n name=\"" + xResourceName + "\">\n");
                    Hashtable<String, Object> l10n = getTheme(resourceNames[iter]);
                    for (String locale : l10n.keySet()) {
                        bw.write("        <lang name=\"" + locale + "\">\n");
                        Hashtable<String, String> current = (Hashtable<String, String>) l10n.get(locale);
                        for (String key : current.keySet()) {
                            String val = current.get(key);
                            bw.write("            <entry key=\"" + xmlize(key) + "\" value=\"" + xmlize(val) + "\" />\n");
                        }
                        bw.write("        </lang>\n");
                    }
                    bw.write("    </l10n>\n");
                    continue;
                default:
                    throw new IOException("Corrupt theme file unrecognized magic number: " + Integer.toHexString(magic & 0xff));
            }
        }
        bw.write("</resource>\n");
        bw.close();
    } finally {
        overrideFile = overrideFileBackup;
        overrideResource = overrideResourceBackup;
    }
}
Also used : SVG(com.codename1.impl.javase.SVG) DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) BufferedImage(java.awt.image.BufferedImage) LegacyFont(com.codename1.ui.util.xml.LegacyFont) EditorTTFFont(com.codename1.ui.EditorTTFFont) EditorFont(com.codename1.ui.EditorFont) BufferedWriter(java.io.BufferedWriter) EditorTTFFont(com.codename1.ui.EditorTTFFont) RoundRectBorder(com.codename1.ui.plaf.RoundRectBorder) Image(com.codename1.ui.Image) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Hashtable(java.util.Hashtable) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EncodedImage(com.codename1.ui.EncodedImage) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) AnimationObject(com.codename1.ui.animations.AnimationObject) EditorFont(com.codename1.ui.EditorFont) RoundBorder(com.codename1.ui.plaf.RoundBorder) File(java.io.File) RoundRectBorder(com.codename1.ui.plaf.RoundRectBorder) RoundBorder(com.codename1.ui.plaf.RoundBorder) Border(com.codename1.ui.plaf.Border)

Example 4 with EditableResources

use of com.codename1.ui.util.EditableResources in project CodenameOne by codenameone.

the class EditableResources method refreshThemeMultiImages.

public void refreshThemeMultiImages() {
    EditableResources ed = (EditableResources) JavaSEPortWithSVGSupport.getNativeTheme();
    if (ed != null && ed != this) {
        ed.refreshThemeMultiImages();
    }
    for (String themeName : getThemeResourceNames()) {
        Hashtable theme = getTheme(themeName);
        for (Object key : theme.keySet()) {
            Object currentValue = theme.get(key);
            if (currentValue instanceof EditorTTFFont) {
                ((EditorTTFFont) currentValue).refresh();
            }
            if (currentValue instanceof com.codename1.ui.Image) {
                String id = findId(currentValue);
                if (isMultiImage(id)) {
                    theme.put(key, ((MultiImage) getResourceObject(id)).getBest());
                }
            }
            if (currentValue instanceof com.codename1.ui.plaf.Border) {
                com.codename1.ui.Image[] images = Accessor.getImages((com.codename1.ui.plaf.Border) currentValue);
                if (images != null) {
                    for (int iter = 0; iter < images.length; iter++) {
                        com.codename1.ui.Image img = images[iter];
                        if (img != null) {
                            String id = findId(img);
                            if (id == null) {
                                JOptionPane.showMessageDialog(java.awt.Frame.getFrames()[0], "Missing image from border: " + key, "Error", JOptionPane.ERROR_MESSAGE);
                                continue;
                            }
                            if (isMultiImage(id)) {
                                images[iter] = ((MultiImage) getResourceObject(id)).getBest();
                            }
                        }
                    }
                }
            }
        }
    }
    com.codename1.ui.Form f = Display.getInstance().getCurrent();
    if (f != null) {
        f.revalidate();
    }
}
Also used : Hashtable(java.util.Hashtable) Image(com.codename1.ui.Image) BufferedImage(java.awt.image.BufferedImage) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) Form(com.codename1.ui.Form) EditorTTFFont(com.codename1.ui.EditorTTFFont) AnimationObject(com.codename1.ui.animations.AnimationObject) RoundRectBorder(com.codename1.ui.plaf.RoundRectBorder) RoundBorder(com.codename1.ui.plaf.RoundBorder) Border(com.codename1.ui.plaf.Border)

Example 5 with EditableResources

use of com.codename1.ui.util.EditableResources 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;
}
Also used : Container(com.codename1.ui.Container) ArrayList(java.util.ArrayList)

Aggregations

EditableResources (com.codename1.ui.util.EditableResources)12 IOException (java.io.IOException)12 Hashtable (java.util.Hashtable)11 ArrayList (java.util.ArrayList)10 EncodedImage (com.codename1.ui.EncodedImage)8 File (java.io.File)8 FileInputStream (java.io.FileInputStream)8 InputStream (java.io.InputStream)7 AnimationObject (com.codename1.ui.animations.AnimationObject)6 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)6 FileOutputStream (java.io.FileOutputStream)6 Point (java.awt.Point)5 BufferedImage (java.awt.image.BufferedImage)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Container (com.codename1.ui.Container)4 BorderLayout (com.codename1.ui.layouts.BorderLayout)4 TableLayout (com.codename1.ui.table.TableLayout)4 EmbeddedContainer (com.codename1.ui.util.EmbeddedContainer)4 DataInputStream (java.io.DataInputStream)4 DataOutputStream (java.io.DataOutputStream)4