Search in sources :

Example 1 with ActionCommand

use of com.codename1.designer.ActionCommand 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 ActionCommand

use of com.codename1.designer.ActionCommand 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 ActionCommand

use of com.codename1.designer.ActionCommand in project CodenameOne by codenameone.

the class ResourceEditorApp method migrateGuiBuilder.

private static void migrateGuiBuilder(File projectDir, EditableResources res, String destPackageName) throws IOException {
    File propertiesFile = new File(projectDir, "codenameone_settings.properties");
    Properties props = new Properties();
    FileInputStream pIn = new FileInputStream(propertiesFile);
    props.load(pIn);
    pIn.close();
    if (props.getProperty("guiResource") == null) {
        System.out.println("Not a legacy GUI builder project!\nConversion failed!");
        System.exit(1);
        return;
    }
    UserInterfaceEditor.exportToNewGuiBuilderMode = true;
    String mainForm = props.getProperty("mainForm");
    File stateMachineBase = new File(projectDir, "src" + File.separatorChar + "generated" + File.separator + "StateMachineBase.java");
    StringBuilder stateMachineBaseSource = new StringBuilder("/**\n * This class was generated by the migration wizard, ultimately both it and the Statemachine can be removed.\n");
    stateMachineBaseSource.append(" * This class is no longer updated automatically\n");
    stateMachineBaseSource.append("*/\n");
    stateMachineBaseSource.append("package generated;\n");
    stateMachineBaseSource.append("\nimport com.codename1.ui.*;\n");
    stateMachineBaseSource.append("import com.codename1.ui.util.*;\n");
    stateMachineBaseSource.append("import com.codename1.ui.plaf.*;\n");
    stateMachineBaseSource.append("import java.util.Hashtable;\n");
    stateMachineBaseSource.append("import com.codename1.ui.events.*;\n\n");
    stateMachineBaseSource.append("public abstract class StateMachineBase extends UIBuilder {\n");
    stateMachineBaseSource.append("    private static final java.util.HashMap<String, Class> formNameToClassHashMap = new java.util.HashMap<String, Class>();");
    stateMachineBaseSource.append("    public static StateMachineBase instance;");
    stateMachineBaseSource.append("    protected void initVars() {}\n\n");
    stateMachineBaseSource.append("    protected void initVars(Resources res) {}\n\n");
    stateMachineBaseSource.append("    public StateMachineBase(Resources res, String resPath, boolean loadTheme) {\n    instance = this;\n");
    stateMachineBaseSource.append("        startApp(res, resPath, loadTheme);\n");
    stateMachineBaseSource.append("    }\n\n\n");
    stateMachineBaseSource.append("    public Container startApp(Resources res, String resPath, boolean loadTheme) {\n");
    stateMachineBaseSource.append("        initVars();\n");
    stateMachineBaseSource.append("        if(loadTheme) {\n");
    stateMachineBaseSource.append("            if(res == null) {\n");
    stateMachineBaseSource.append("                try {\n");
    stateMachineBaseSource.append("                    if(resPath.endsWith(\".res\")) {\n");
    stateMachineBaseSource.append("                        res = Resources.open(resPath);\n");
    stateMachineBaseSource.append("                        System.out.println(\"Warning: you should construct the state machine without the .res extension to allow theme overlays\");\n");
    stateMachineBaseSource.append("                    } else {\n");
    stateMachineBaseSource.append("                        res = Resources.openLayered(resPath);\n");
    stateMachineBaseSource.append("                    }\n");
    stateMachineBaseSource.append("                } catch(java.io.IOException err) { err.printStackTrace(); }\n");
    stateMachineBaseSource.append("            }\n");
    stateMachineBaseSource.append("            initTheme(res);\n");
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("        if(res != null) {\n");
    stateMachineBaseSource.append("            setResourceFilePath(resPath);\n");
    stateMachineBaseSource.append("            setResourceFile(res);\n");
    stateMachineBaseSource.append("            Resources.setGlobalResources(res);");
    stateMachineBaseSource.append("            initVars(res);\n");
    stateMachineBaseSource.append("            return showForm(getFirstFormName(), null);\n");
    stateMachineBaseSource.append("        } else {\n");
    stateMachineBaseSource.append("            Form f = (Form)createContainer(resPath, getFirstFormName());\n");
    stateMachineBaseSource.append("            Resources.setGlobalResources(fetchResourceFile());");
    stateMachineBaseSource.append("            initVars(fetchResourceFile());\n");
    stateMachineBaseSource.append("            beforeShow(f);\n");
    stateMachineBaseSource.append("            f.show();\n");
    stateMachineBaseSource.append("            postShow(f);\n");
    stateMachineBaseSource.append("            return f;\n");
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("    }\n\n\n");
    stateMachineBaseSource.append("    protected String getFirstFormName() {\n");
    stateMachineBaseSource.append("        return \"");
    stateMachineBaseSource.append(mainForm);
    stateMachineBaseSource.append("\";\n");
    stateMachineBaseSource.append("    }\n\n\n");
    stateMachineBaseSource.append("    protected void initTheme(Resources res) {\n");
    stateMachineBaseSource.append("            String[] themes = res.getThemeResourceNames();\n");
    stateMachineBaseSource.append("            Resources.setGlobalResources(res);\n");
    stateMachineBaseSource.append("            if(themes != null && themes.length > 0) {\n");
    stateMachineBaseSource.append("                UIManager.getInstance().setThemeProps(res.getTheme(themes[0]));\n");
    stateMachineBaseSource.append("            }\n");
    stateMachineBaseSource.append("    }\n\n\n");
    stateMachineBaseSource.append("    public StateMachineBase() {\n    instance = this;\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public StateMachineBase(String resPath) {\n");
    stateMachineBaseSource.append("        this(null, resPath, true);\n    instance = this;\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public StateMachineBase(Resources res) {\n");
    stateMachineBaseSource.append("        this(res, null, true);\n    instance = this;\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public StateMachineBase(String resPath, boolean loadTheme) {\n");
    stateMachineBaseSource.append("        this(null, resPath, loadTheme);\n    instance = this;\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public StateMachineBase(Resources res, boolean loadTheme) {\n");
    stateMachineBaseSource.append("        this(res, null, loadTheme);\n    instance = this;\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public Form showForm(String resourceName, Command sourceCommand) {\n");
    stateMachineBaseSource.append("        try {\n");
    stateMachineBaseSource.append("            Form f = (Form)formNameToClassHashMap.get(resourceName).newInstance();\n");
    stateMachineBaseSource.append("            Form current = Display.getInstance().getCurrent();\n");
    stateMachineBaseSource.append("            if(current != null && isBackCommandEnabled() && allowBackTo(resourceName)) {\n");
    stateMachineBaseSource.append("                f.putClientProperty(\"previousForm\", current);\n");
    stateMachineBaseSource.append("                setBackCommand(f, new Command(getBackCommandText(current.getTitle())) {\n");
    stateMachineBaseSource.append("                    public void actionPerformed(ActionEvent evt) {\n");
    stateMachineBaseSource.append("                          back(null);\n");
    stateMachineBaseSource.append("                    }\n");
    stateMachineBaseSource.append("                });\n");
    stateMachineBaseSource.append("            }\n");
    stateMachineBaseSource.append("            if(sourceCommand != null && current != null && current.getBackCommand() == sourceCommand) {\n");
    stateMachineBaseSource.append("                f.showBack();\n");
    stateMachineBaseSource.append("            } else {\n");
    stateMachineBaseSource.append("                f.show();\n");
    stateMachineBaseSource.append("            }\n");
    stateMachineBaseSource.append("            return f;\n");
    stateMachineBaseSource.append("        } catch(Exception err) {\n");
    stateMachineBaseSource.append("            err.printStackTrace();\n");
    stateMachineBaseSource.append("            throw new RuntimeException(\"Form not found: \" + resourceName);\n");
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    protected void beforeShow(Form f) {\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public final void beforeShow__(Form f) {\n        beforeShow(f);\n");
    stateMachineBaseSource.append("        if(Display.getInstance().getCurrent() != null) {\n");
    stateMachineBaseSource.append("            exitForm(Display.getInstance().getCurrent());\n");
    stateMachineBaseSource.append("            invokeFormExit__(Display.getInstance().getCurrent());\n");
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    protected void exitForm(Form f) {\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    protected void postShow(Form f) {\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public final void postShow__(Form f) {\n        postShow(f);\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    private Container getRootComponent__(Component rootComponent) {\n");
    stateMachineBaseSource.append("        if(rootComponent.getParent() != null) {\n");
    stateMachineBaseSource.append("            return getRoot__(rootComponent.getParent());\n");
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("        return (Container)rootComponent;\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    private Container getRoot__(Container rootComponent) {\n");
    stateMachineBaseSource.append("        Container p = rootComponent.getParent();\n");
    stateMachineBaseSource.append("        while(p != null) {\n");
    stateMachineBaseSource.append("            rootComponent = p;\n");
    stateMachineBaseSource.append("            p = rootComponent.getParent();\n");
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("        return rootComponent;\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public Component findByName(String componentName, Container rootComponent) {\n");
    stateMachineBaseSource.append("        Container root = getRoot__(rootComponent);\n");
    stateMachineBaseSource.append("        return findByName__(componentName, root);\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public Component findByName__(String componentName, Container root) {\n");
    stateMachineBaseSource.append("        int count = root.getComponentCount();\n");
    stateMachineBaseSource.append("        for(int iter = 0 ; iter < count ; iter++) {\n");
    stateMachineBaseSource.append("            Component c = root.getComponentAt(iter);\n");
    stateMachineBaseSource.append("            String n = c.getName();\n");
    stateMachineBaseSource.append("            if(n != null && n.equals(componentName)) {\n");
    stateMachineBaseSource.append("                return c;\n");
    stateMachineBaseSource.append("            }\n");
    stateMachineBaseSource.append("            if(c instanceof Container && ((Container)c).getLeadComponent() == null) {\n");
    stateMachineBaseSource.append("                c = findByName__(componentName, (Container)c);\n");
    stateMachineBaseSource.append("                if(c != null) {\n");
    stateMachineBaseSource.append("                    return c;\n");
    stateMachineBaseSource.append("                }\n");
    stateMachineBaseSource.append("            }\n");
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("        return null;\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    protected void handleComponentAction(Component c, ActionEvent event) {\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public void handleComponentAction__(Component c, ActionEvent event) {\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public void processCommand__(ActionEvent ev, Command cmd) {\n");
    stateMachineBaseSource.append("        processCommand(ev, cmd);\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public void back() {\n");
    stateMachineBaseSource.append("        back(null);\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("    public void back(Component sourceComponent) {\n");
    stateMachineBaseSource.append("        Form current = (Form)Display.getInstance().getCurrent().getClientProperty(\"previousForm\");\n");
    stateMachineBaseSource.append("        current.showBack();\n");
    stateMachineBaseSource.append("    }\n\n");
    StringBuilder formNameMapBuilder = new StringBuilder("static {");
    StringBuilder invokeFormExitBuilder = new StringBuilder("    private void invokeFormExit__(Form f) {\n");
    UserInterfaceEditor.componentNames = new HashMap<String, Class>();
    UserInterfaceEditor.commandList = new ArrayList<ActionCommand>();
    for (String uiName : res.getUIResourceNames()) {
        System.out.println("Processing: " + uiName);
        String fileName = convertToVarName(uiName);
        formNameMapBuilder.append("    formNameToClassHashMap.put(\"");
        formNameMapBuilder.append(uiName);
        formNameMapBuilder.append("\", ");
        formNameMapBuilder.append(destPackageName);
        formNameMapBuilder.append(".");
        formNameMapBuilder.append(fileName);
        formNameMapBuilder.append(".class);\n");
        String normalizedUiName = ResourceEditorView.normalizeFormName(uiName);
        if (RESEVERVED_WORDS.contains(fileName)) {
            fileName += "X";
        } else {
            try {
                if (Class.forName("java.lang." + fileName) != null) {
                    fileName += "X";
                }
            } catch (Throwable t) {
            // passed...
            }
        }
        File guiFile = new File(projectDir, "res" + File.separatorChar + "guibuilder" + File.separatorChar + destPackageName.replace('.', File.separatorChar) + File.separatorChar + fileName + ".gui");
        guiFile.getParentFile().mkdirs();
        File sourcePackageDir = new File(projectDir, "src" + File.separatorChar + destPackageName.replace('.', File.separatorChar));
        sourcePackageDir.mkdirs();
        File sourceFile = new File(sourcePackageDir, fileName + ".java");
        UIBuilderOverride u = new UIBuilderOverride();
        com.codename1.ui.Container cnt = u.createContainer(res, uiName);
        FileOutputStream fos = new FileOutputStream(guiFile);
        Writer w = new OutputStreamWriter(fos, "UTF-8");
        w.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
        StringBuilder bld = new StringBuilder();
        UserInterfaceEditor.actionEventNames = new ArrayList<String>();
        UserInterfaceEditor.listNames = new ArrayList<String>();
        UserInterfaceEditor.persistToXML(cnt, cnt, bld, res, "");
        w.write(bld.toString());
        w.flush();
        w.close();
        fos = new FileOutputStream(sourceFile);
        w = new OutputStreamWriter(fos, "UTF-8");
        w.write("package ");
        w.write(destPackageName);
        w.write(";\n");
        w.write("\n");
        w.write("/**\n");
        w.write(" * GUI builder created Form\n");
        w.write(" */\n");
        w.write("public class ");
        w.write(fileName);
        String prePostCode;
        w.write(" extends com.codename1.ui.");
        if (cnt instanceof com.codename1.ui.Form) {
            invokeFormExitBuilder.append("        if(f.getName().equals(\"");
            invokeFormExitBuilder.append(uiName);
            invokeFormExitBuilder.append("\")) {\n");
            invokeFormExitBuilder.append("            exit");
            invokeFormExitBuilder.append(normalizedUiName);
            invokeFormExitBuilder.append("(f);\n        }\n");
            stateMachineBaseSource.append("    protected void before");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("(Form f) {\n");
            stateMachineBaseSource.append("    }\n\n");
            stateMachineBaseSource.append("    public final void before");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("__(Form f) {\n        before");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("(f);\n    }\n\n");
            stateMachineBaseSource.append("    protected void post");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("(Form f) {\n");
            stateMachineBaseSource.append("    }\n\n");
            stateMachineBaseSource.append("    public final void post");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("__(Form f) {\n        post");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("(f);\n    }\n\n");
            stateMachineBaseSource.append("    protected void exit");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("(Form f) {\n");
            stateMachineBaseSource.append("    }\n\n");
        }
        if (cnt instanceof com.codename1.ui.Dialog) {
            w.write("Dialog");
            prePostCode = "\n    public void initComponent() {\n        generated.StateMachineBase.instance.beforeShow__(this);\n";
            prePostCode += "        generated.StateMachineBase.instance.before";
            prePostCode += normalizedUiName;
            prePostCode += "__(this);\n    }\n";
            prePostCode = "\n    public void onShow() {\n        generated.StateMachineBase.instance.postShow__(this);\n";
            prePostCode += "        generated.StateMachineBase.instance.post";
            prePostCode += normalizedUiName;
            prePostCode += "__(this);\n    }\n";
            prePostCode += "    protected void actionCommand(com.codename1.ui.Command cmd) {\n";
            prePostCode += "        generated.StateMachineBase.instance.processCommand__(new com.codename1.ui.events.ActionEvent(cmd), cmd);\n";
            prePostCode += "    }\n\n";
        } else {
            if (cnt instanceof com.codename1.ui.Form) {
                w.write("Form");
                prePostCode = "\n    public void show() {\n        generated.StateMachineBase.instance.beforeShow__(this);\n";
                prePostCode += "        generated.StateMachineBase.instance.before";
                prePostCode += normalizedUiName;
                prePostCode += "__(this);\n        super.show();\n        generated.StateMachineBase.instance.post";
                prePostCode += normalizedUiName;
                prePostCode += "__(this);\n    }\n";
                prePostCode += "    protected void actionCommand(com.codename1.ui.Command cmd) {\n";
                prePostCode += "        generated.StateMachineBase.instance.processCommand__(new com.codename1.ui.events.ActionEvent(cmd), cmd);\n";
                prePostCode += "    }\n\n";
            } else {
                w.write("Container");
                prePostCode = "";
            }
        }
        w.write(" {\n    public ");
        w.write(fileName);
        w.write("() {\n");
        w.write("        this(com.codename1.ui.util.Resources.getGlobalResources());\n");
        w.write("    }\n    \n    public ");
        w.write(fileName);
        w.write("(com.codename1.ui.util.Resources resourceObjectInstance) {\n");
        w.write("        initGuiBuilderComponents(resourceObjectInstance);\n");
        w.write("    }\n\n");
        w.write("//-- DON'T EDIT BELOW THIS LINE!!!\n\n    private void initGuiBuilderComponents(com.codename1.ui.util.Resources resourceObjectInstance) {}\n\n");
        w.write("//-- DON'T EDIT ABOVE THIS LINE!!!\n");
        for (String actionListenerNames : UserInterfaceEditor.actionEventNames) {
            w.write("\n    public void on");
            w.write(actionListenerNames);
            w.write("ActionEvent(com.codename1.ui.events.ActionEvent ev) {\n        ");
            w.write("generated.StateMachineBase.instance.handleComponentAction__((com.codename1.ui.Component)ev.getSource(), ev);\n        ");
            w.write("generated.StateMachineBase.instance.on");
            w.write(normalizedUiName);
            w.write("_");
            String normalizedActionListenerName = ResourceEditorView.normalizeFormName(actionListenerNames);
            w.write(normalizedActionListenerName);
            w.write("Action__((com.codename1.ui.Component)ev.getSource(), ev);\n    }\n\n");
            stateMachineBaseSource.append("    protected void on");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("_");
            stateMachineBaseSource.append(normalizedActionListenerName);
            stateMachineBaseSource.append("Action(Component cmp, ActionEvent ev) {\n    }\n\n");
            stateMachineBaseSource.append("    public void on");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("_");
            stateMachineBaseSource.append(normalizedActionListenerName);
            stateMachineBaseSource.append("Action__(Component cmp, ActionEvent ev) {\n        on");
            stateMachineBaseSource.append(normalizedUiName);
            stateMachineBaseSource.append("_");
            stateMachineBaseSource.append(normalizedActionListenerName);
            stateMachineBaseSource.append("Action(cmp, ev);\n    }\n\n");
        }
        w.write(prePostCode);
        w.write("}\n");
        w.flush();
        w.close();
    }
    formNameMapBuilder.append("}\n");
    invokeFormExitBuilder.append("}\n");
    stateMachineBaseSource.append(formNameMapBuilder);
    stateMachineBaseSource.append(invokeFormExitBuilder);
    ArrayList<String> uniqueNames = new ArrayList<String>();
    for (String cmpName : UserInterfaceEditor.componentNames.keySet()) {
        String nomName = ResourceEditorView.normalizeFormName(cmpName);
        if (uniqueNames.contains(nomName)) {
            continue;
        }
        uniqueNames.add(nomName);
        stateMachineBaseSource.append("    public ");
        stateMachineBaseSource.append(UserInterfaceEditor.componentNames.get(cmpName).getName());
        stateMachineBaseSource.append(" find");
        stateMachineBaseSource.append(nomName);
        stateMachineBaseSource.append("(Component root) {\n        return (");
        stateMachineBaseSource.append(UserInterfaceEditor.componentNames.get(cmpName).getName());
        stateMachineBaseSource.append(")findByName(\"");
        stateMachineBaseSource.append(cmpName);
        stateMachineBaseSource.append("\", getRootComponent__(root));\n    }\n\n");
        stateMachineBaseSource.append("    public ");
        stateMachineBaseSource.append(UserInterfaceEditor.componentNames.get(cmpName).getName());
        stateMachineBaseSource.append(" find");
        stateMachineBaseSource.append(nomName);
        stateMachineBaseSource.append("() {\n        return (");
        stateMachineBaseSource.append(UserInterfaceEditor.componentNames.get(cmpName).getName());
        stateMachineBaseSource.append(")findByName(\"");
        stateMachineBaseSource.append(cmpName);
        stateMachineBaseSource.append("\", Display.getInstance().getCurrent());\n    }\n\n");
    }
    ArrayList<Integer> commandIdsAdded = new ArrayList<Integer>();
    ArrayList<String> commandNamesAdded = new ArrayList<String>();
    for (ActionCommand cmd : UserInterfaceEditor.commandList) {
        String formName = (String) cmd.getClientProperty("FORMNAME");
        if (formName == null) {
            continue;
        }
        String normalizedCommandName = ResourceEditorView.normalizeFormName(formName) + ResourceEditorView.normalizeFormName(cmd.getCommandName());
        if (commandNamesAdded.contains(normalizedCommandName)) {
            continue;
        }
        if (commandIdsAdded.contains(cmd.getId())) {
            continue;
        }
        commandIdsAdded.add(cmd.getId());
        commandNamesAdded.add(normalizedCommandName);
        stateMachineBaseSource.append("    public static final int COMMAND_");
        stateMachineBaseSource.append(normalizedCommandName);
        stateMachineBaseSource.append(" = ");
        stateMachineBaseSource.append(cmd.getId());
        stateMachineBaseSource.append(";\n\n    protected boolean on");
        stateMachineBaseSource.append(normalizedCommandName);
        stateMachineBaseSource.append("() {\n        return false;\n    }\n\n");
    }
    stateMachineBaseSource.append("    protected void processCommand(ActionEvent ev, Command cmd) {\n");
    stateMachineBaseSource.append("        switch(cmd.getId()) {\n");
    commandIdsAdded.clear();
    commandNamesAdded.clear();
    for (ActionCommand cmd : UserInterfaceEditor.commandList) {
        String formName = (String) cmd.getClientProperty("FORMNAME");
        if (formName == null) {
            continue;
        }
        String normalizedCommandName = ResourceEditorView.normalizeFormName(formName) + ResourceEditorView.normalizeFormName(cmd.getCommandName());
        if (commandNamesAdded.contains(normalizedCommandName)) {
            continue;
        }
        if (commandIdsAdded.contains(cmd.getId())) {
            continue;
        }
        commandIdsAdded.add(cmd.getId());
        commandNamesAdded.add(normalizedCommandName);
        stateMachineBaseSource.append("\n        case COMMAND_");
        stateMachineBaseSource.append(normalizedCommandName);
        stateMachineBaseSource.append(":\n");
        if (cmd.getAction() != null && cmd.getAction().length() > 0) {
            if (!cmd.getAction().startsWith("$")) {
                stateMachineBaseSource.append("            showForm(\"");
                stateMachineBaseSource.append(cmd.getAction());
                stateMachineBaseSource.append("\", null);\n");
            }
        }
        stateMachineBaseSource.append("            if(on");
        stateMachineBaseSource.append(normalizedCommandName);
        stateMachineBaseSource.append("()) {\n");
        stateMachineBaseSource.append("                ev.consume();\n");
        stateMachineBaseSource.append("                return;\n");
        stateMachineBaseSource.append("            }\n");
        stateMachineBaseSource.append("            break;\n\n");
    }
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("        if(ev.getComponent() != null) {\n");
    stateMachineBaseSource.append("            handleComponentAction(ev.getComponent(), ev);\n");
    stateMachineBaseSource.append("        }\n");
    stateMachineBaseSource.append("    }\n\n");
    stateMachineBaseSource.append("\n}\n");
    FileOutputStream sbout = new FileOutputStream(stateMachineBase);
    sbout.write(stateMachineBaseSource.toString().getBytes("UTF-8"));
    sbout.close();
    props.remove("mainForm");
    props.remove("package");
    props.remove("guiResource");
    props.remove("baseClass");
    props.remove("userClass");
    FileOutputStream pOut = new FileOutputStream(propertiesFile);
    props.store(pOut, "Updated by GUI builder migration wizard");
    pOut.close();
    System.out.println("Conversion completed successfully!");
    System.exit(0);
}
Also used : UIBuilderOverride(com.codename1.ui.util.UIBuilderOverride) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Container(com.codename1.ui.Container) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Example 4 with ActionCommand

use of com.codename1.designer.ActionCommand in project CodenameOne by codenameone.

the class MenuBar method actionPerformed.

/**
 * Invoked when a softbutton is pressed
 */
public void actionPerformed(ActionEvent evt) {
    if (evt.isConsumed()) {
        return;
    }
    Object src = evt.getSource();
    if (commandList == null) {
        Button source = (Button) src;
        for (int iter = 0; iter < soft.length; iter++) {
            if (source == soft[iter]) {
                if (softCommand[iter] == menuCommand) {
                    showMenu();
                    return;
                }
                if (softCommand[iter] != null) {
                    ActionEvent e = new ActionEvent(softCommand[iter], ActionEvent.Type.Command);
                    softCommand[iter].actionPerformed(e);
                    if (!e.isConsumed()) {
                        parent.actionCommandImpl(softCommand[iter]);
                    }
                }
                return;
            }
        }
    } else {
        // the list for the menu sent the event
        if (src instanceof Button) {
            for (int iter = 0; iter < soft.length; iter++) {
                if (src == soft[iter]) {
                    Container parent = commandList.getParent();
                    while (parent != null) {
                        if (parent instanceof Dialog) {
                            ((Dialog) parent).actionCommand(softCommand[iter]);
                            return;
                        }
                        parent = parent.getParent();
                    }
                }
            }
        }
        Command c = getComponentSelectedCommand(commandList);
        if (!c.isEnabled()) {
            return;
        }
        Container p = commandList.getParent();
        while (p != null) {
            if (p instanceof Dialog) {
                ((Dialog) p).actionCommand(c);
                return;
            }
            p = p.getParent();
        }
    }
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent)

Example 5 with ActionCommand

use of com.codename1.designer.ActionCommand in project CodenameOne by codenameone.

the class Progress method actionCommand.

/**
 * {@inheritDoc}
 */
protected void actionCommand(Command cmd) {
    if (Display.getInstance().isTouchScreenDevice() || getSoftButtonCount() < 2) {
        for (int iter = 0; iter < getComponentCount(); iter++) {
            Component c = getComponentAt(iter);
            if (c instanceof Button) {
                c.setEnabled(false);
            }
        }
    } else {
        removeAllCommands();
    }
    // cancel was pressed
    request.kill();
    dispose();
}
Also used : Button(com.codename1.ui.Button) Component(com.codename1.ui.Component)

Aggregations

EmbeddedContainer (com.codename1.ui.util.EmbeddedContainer)3 Point (java.awt.Point)3 ArrayList (java.util.ArrayList)3 JButton (javax.swing.JButton)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)2 TableLayout (com.codename1.ui.table.TableLayout)2 Hashtable (java.util.Hashtable)2 List (java.util.List)2 JLabel (javax.swing.JLabel)2 JList (javax.swing.JList)2 Button (com.codename1.ui.Button)1 Component (com.codename1.ui.Component)1 Container (com.codename1.ui.Container)1 ActionEvent (com.codename1.ui.events.ActionEvent)1 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1