Search in sources :

Example 11 with Row

use of com.codename1.db.Row in project CodenameOne by codenameone.

the class UserInterfaceEditor method persistComponent.

public static void persistComponent(com.codename1.ui.Container containerInstance, com.codename1.ui.Component cmp, DataOutputStream out, EditableResources res) throws IOException {
    if (cmp.getClientProperty("%base_form%") != null) {
        out.writeUTF((String) cmp.getClientProperty("%base_form%"));
        out.writeInt(PROPERTY_BASE_FORM);
    }
    out.writeUTF((String) cmp.getClientProperty(TYPE_KEY));
    out.writeInt(PROPERTY_NAME);
    if (cmp.getName() != null) {
        out.writeUTF(cmp.getName());
    } else {
        out.writeUTF("");
    }
    if (cmp.getClientProperty("cn1$Properties") != null) {
        String[] p = ((String) cmp.getClientProperty("cn1$Properties")).split(",");
        if (p.length > 0) {
            out.writeInt(PROPERTY_CLIENT_PROPERTIES);
            out.writeInt(p.length);
            for (String c : p) {
                out.writeUTF(c);
                out.writeUTF((String) cmp.getClientProperty(c));
            }
        }
    }
    if (cmp.getCloudBoundProperty() != null) {
        out.writeInt(PROPERTY_CLOUD_BOUND_PROPERTY);
        out.writeUTF(cmp.getCloudBoundProperty());
    }
    if (cmp.getCloudDestinationProperty() != null) {
        out.writeInt(PROPERTY_CLOUD_DESTINATION_PROPERTY);
        out.writeUTF(cmp.getCloudDestinationProperty());
    }
    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)) {
            out.writeInt(PROPERTY_SCROLLABLE_X);
            out.writeBoolean(CodenameOneAccessor.isScrollableX(cnt));
        }
        if (isPropertyModified(cnt, PROPERTY_SCROLLABLE_Y)) {
            out.writeInt(PROPERTY_SCROLLABLE_Y);
            out.writeBoolean(CodenameOneAccessor.isScrollableY(cnt));
        }
        if (cmp instanceof com.codename1.ui.Tabs) {
            com.codename1.ui.Tabs tab = (com.codename1.ui.Tabs) cmp;
            out.writeInt(PROPERTY_COMPONENTS);
            out.writeInt(tab.getTabCount());
            for (int iter = 0; iter < tab.getTabCount(); iter++) {
                out.writeUTF(tab.getTabTitle(iter));
                persistComponent(containerInstance, tab.getTabComponentAt(iter), out, res);
            }
            if (isPropertyModified(cmp, PROPERTY_TAB_PLACEMENT)) {
                out.writeInt(PROPERTY_TAB_PLACEMENT);
                out.writeInt(((com.codename1.ui.Tabs) cmp).getTabPlacement());
            }
            if (isPropertyModified(cmp, PROPERTY_TAB_TEXT_POSITION)) {
                out.writeInt(PROPERTY_TAB_TEXT_POSITION);
                out.writeInt(((com.codename1.ui.Tabs) cmp).getTabTextPosition());
            }
        } else {
            if (isPropertyModified(cmp, PROPERTY_LAYOUT)) {
                com.codename1.ui.layouts.Layout l = cnt.getLayout();
                out.writeInt(PROPERTY_LAYOUT);
                if (l instanceof com.codename1.ui.layouts.FlowLayout) {
                    out.writeShort(LAYOUT_FLOW);
                    com.codename1.ui.layouts.FlowLayout f = (com.codename1.ui.layouts.FlowLayout) l;
                    out.writeBoolean(f.isFillRows());
                    out.writeInt(f.getAlign());
                    out.writeInt(f.getValign());
                } else {
                    if (l instanceof com.codename1.ui.layouts.BorderLayout) {
                        out.writeShort(LAYOUT_BORDER);
                        com.codename1.ui.layouts.BorderLayout b = (com.codename1.ui.layouts.BorderLayout) l;
                        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);
                        out.writeBoolean(north != null);
                        if (north != null) {
                            out.writeUTF(north);
                        }
                        out.writeBoolean(east != null);
                        if (east != null) {
                            out.writeUTF(east);
                        }
                        out.writeBoolean(west != null);
                        if (west != null) {
                            out.writeUTF(west);
                        }
                        out.writeBoolean(south != null);
                        if (south != null) {
                            out.writeUTF(south);
                        }
                        out.writeBoolean(center != null);
                        if (center != null) {
                            out.writeUTF(center);
                        }
                        out.writeBoolean(b.isAbsoluteCenter());
                    } else {
                        if (l instanceof com.codename1.ui.layouts.GridLayout) {
                            out.writeShort(LAYOUT_GRID);
                            out.writeInt(((com.codename1.ui.layouts.GridLayout) l).getRows());
                            out.writeInt(((com.codename1.ui.layouts.GridLayout) l).getColumns());
                        } else {
                            if (l instanceof com.codename1.ui.layouts.BoxLayout) {
                                if (getInt("axis", l.getClass(), l) == com.codename1.ui.layouts.BoxLayout.X_AXIS) {
                                    out.writeShort(LAYOUT_BOX_X);
                                } else {
                                    out.writeShort(LAYOUT_BOX_Y);
                                }
                            } else {
                                if (l instanceof com.codename1.ui.table.TableLayout) {
                                    out.writeShort(LAYOUT_TABLE);
                                    out.writeInt(((com.codename1.ui.table.TableLayout) l).getRows());
                                    out.writeInt(((com.codename1.ui.table.TableLayout) l).getColumns());
                                } else {
                                    if (l instanceof com.codename1.ui.layouts.LayeredLayout) {
                                        out.writeShort(LAYOUT_LAYERED);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (cmp instanceof com.codename1.ui.Form) {
                com.codename1.ui.Form frm = (com.codename1.ui.Form) cmp;
                out.writeInt(PROPERTY_COMPONENTS);
                out.writeInt(frm.getContentPane().getComponentCount());
                for (int iter = 0; iter < frm.getContentPane().getComponentCount(); iter++) {
                    persistComponent(containerInstance, frm.getContentPane().getComponentAt(iter), out, res);
                }
                if (isPropertyModified(cmp, PROPERTY_NEXT_FORM) && frm.getClientProperty("%next_form%") != null) {
                    out.writeInt(PROPERTY_NEXT_FORM);
                    out.writeUTF((String) frm.getClientProperty("%next_form%"));
                }
                if (isPropertyModified(cmp, PROPERTY_TITLE)) {
                    out.writeInt(PROPERTY_TITLE);
                    out.writeUTF(frm.getTitle());
                }
                if (isPropertyModified(cmp, PROPERTY_CYCLIC_FOCUS)) {
                    out.writeInt(PROPERTY_CYCLIC_FOCUS);
                    out.writeBoolean(frm.isCyclicFocus());
                }
                if (isPropertyModified(cmp, PROPERTY_DIALOG_UIID) && cmp instanceof com.codename1.ui.Dialog) {
                    com.codename1.ui.Dialog dlg = (com.codename1.ui.Dialog) cmp;
                    out.writeInt(PROPERTY_DIALOG_UIID);
                    out.writeUTF(dlg.getDialogUIID());
                }
                if (isPropertyModified(cmp, PROPERTY_DISPOSE_WHEN_POINTER_OUT) && cmp instanceof com.codename1.ui.Dialog) {
                    com.codename1.ui.Dialog dlg = (com.codename1.ui.Dialog) cmp;
                    out.writeInt(PROPERTY_DISPOSE_WHEN_POINTER_OUT);
                    out.writeBoolean(dlg.isDisposeWhenPointerOutOfBounds());
                }
                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) {
                        out.writeInt(PROPERTY_DIALOG_POSITION);
                        out.writeUTF(dlg.getDialogPosition());
                    }
                }
                if (frm.getCommandCount() > 0 || frm.getBackCommand() != null) {
                    if (isPropertyModified(cmp, PROPERTY_COMMANDS) || isPropertyModified(cmp, PROPERTY_COMMANDS_LEGACY)) {
                        out.writeInt(PROPERTY_COMMANDS);
                        if (frm.getBackCommand() != null && !hasBackCommand(frm, frm.getBackCommand())) {
                            out.writeInt(frm.getCommandCount() + 1);
                            ActionCommand cmd = (ActionCommand) frm.getBackCommand();
                            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(frm.getBackCommand() == cmd);
                        } else {
                            out.writeInt(frm.getCommandCount());
                        }
                        for (int iter = frm.getCommandCount() - 1; iter >= 0; iter--) {
                            ActionCommand cmd = (ActionCommand) frm.getCommand(iter);
                            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(frm.getBackCommand() == cmd);
                        }
                    }
                }
            } else {
                if (!(cmp instanceof com.codename1.ui.list.ContainerList)) {
                    out.writeInt(PROPERTY_COMPONENTS);
                    out.writeInt(cnt.getComponentCount());
                    for (int iter = 0; iter < cnt.getComponentCount(); iter++) {
                        persistComponent(containerInstance, cnt.getComponentAt(iter), out, res);
                    }
                } 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) {
                        out.writeInt(PROPERTY_LIST_RENDERER);
                        com.codename1.ui.list.GenericListCellRenderer g = (com.codename1.ui.list.GenericListCellRenderer) lst.getRenderer();
                        if (g.getSelectedEven() == null) {
                            out.writeByte(2);
                            out.writeUTF(g.getSelected().getName());
                            out.writeUTF(g.getUnselected().getName());
                        } else {
                            out.writeByte(4);
                            out.writeUTF(g.getSelected().getName());
                            out.writeUTF(g.getUnselected().getName());
                            out.writeUTF(g.getSelectedEven().getName());
                            out.writeUTF(g.getUnselectedEven().getName());
                        }
                    }
                }
            }
        }
    } else {
        if (cmp instanceof com.codename1.ui.Label) {
            com.codename1.ui.Label lbl = (com.codename1.ui.Label) cmp;
            out.writeInt(PROPERTY_TEXT);
            out.writeUTF(lbl.getText());
            if (isPropertyModified(cmp, PROPERTY_ALIGNMENT)) {
                out.writeInt(PROPERTY_ALIGNMENT);
                out.writeInt(lbl.getAlignment());
            }
            if (isPropertyModified(cmp, PROPERTY_ICON)) {
                if (lbl.getIcon() != null) {
                    out.writeInt(PROPERTY_ICON);
                    out.writeUTF(res.findId(lbl.getIcon()));
                }
            }
            if (lbl instanceof com.codename1.ui.Button) {
                com.codename1.ui.Button button = (com.codename1.ui.Button) lbl;
                if (isPropertyModified(cmp, PROPERTY_ROLLOVER_ICON)) {
                    if (button.getRolloverIcon() != null) {
                        out.writeInt(PROPERTY_ROLLOVER_ICON);
                        out.writeUTF(res.findId(button.getRolloverIcon()));
                    }
                }
                if (isPropertyModified(cmp, PROPERTY_PRESSED_ICON)) {
                    if (button.getPressedIcon() != null) {
                        out.writeInt(PROPERTY_PRESSED_ICON);
                        out.writeUTF(res.findId(button.getPressedIcon()));
                    }
                }
                if (isPropertyModified(cmp, PROPERTY_DISABLED_ICON)) {
                    if (button.getDisabledIcon() != null) {
                        out.writeInt(PROPERTY_DISABLED_ICON);
                        out.writeUTF(res.findId(button.getDisabledIcon()));
                    }
                }
                if (isPropertyModified(cmp, PROPERTY_TOGGLE_BUTTON)) {
                    out.writeInt(PROPERTY_TOGGLE_BUTTON);
                    out.writeBoolean(((com.codename1.ui.Button) cmp).isToggle());
                }
            } else {
                if (lbl instanceof com.codename1.ui.Slider) {
                    com.codename1.ui.Slider sld = (com.codename1.ui.Slider) lbl;
                    if (isPropertyModified(cmp, PROPERTY_EDITABLE)) {
                        out.writeInt(PROPERTY_EDITABLE);
                        out.writeBoolean(sld.isEditable());
                    }
                    if (isPropertyModified(cmp, PROPERTY_INFINITE)) {
                        out.writeInt(PROPERTY_INFINITE);
                        out.writeBoolean(sld.isInfinite());
                    }
                    if (isPropertyModified(cmp, PROPERTY_SLIDER_THUMB) && sld.getThumbImage() != null) {
                        out.writeInt(PROPERTY_SLIDER_THUMB);
                        out.writeUTF(res.findId(sld.getThumbImage()));
                    }
                    if (isPropertyModified(cmp, PROPERTY_PROGRESS)) {
                        out.writeInt(PROPERTY_PROGRESS);
                        out.writeInt(sld.getProgress());
                    }
                    if (isPropertyModified(cmp, PROPERTY_VERTICAL)) {
                        out.writeInt(PROPERTY_VERTICAL);
                        out.writeBoolean(sld.isVertical());
                    }
                    if (isPropertyModified(cmp, PROPERTY_INCREMENTS)) {
                        out.writeInt(PROPERTY_INCREMENTS);
                        out.writeInt(sld.getIncrements());
                    }
                    if (isPropertyModified(cmp, PROPERTY_MAX_VALUE)) {
                        out.writeInt(PROPERTY_MAX_VALUE);
                        out.writeInt(sld.getMaxValue());
                    }
                    if (isPropertyModified(cmp, PROPERTY_MIN_VALUE)) {
                        out.writeInt(PROPERTY_MIN_VALUE);
                        out.writeInt(sld.getMinValue());
                    }
                    if (isPropertyModified(cmp, PROPERTY_RENDER_PERCENTAGE_ON_TOP)) {
                        out.writeInt(PROPERTY_RENDER_PERCENTAGE_ON_TOP);
                        out.writeBoolean(sld.isRenderPercentageOnTop());
                    }
                }
            }
            if (isPropertyModified(cmp, PROPERTY_RADIO_GROUP)) {
                out.writeInt(PROPERTY_RADIO_GROUP);
                out.writeUTF(((com.codename1.ui.RadioButton) cmp).getGroup());
            }
            if (isPropertyModified(cmp, PROPERTY_SELECTED)) {
                out.writeInt(PROPERTY_SELECTED);
                out.writeBoolean(((com.codename1.ui.Button) cmp).isSelected());
            }
            if (isPropertyModified(cmp, PROPERTY_GAP)) {
                out.writeInt(PROPERTY_GAP);
                out.writeInt(lbl.getGap());
            }
            if (isPropertyModified(cmp, PROPERTY_VERTICAL_ALIGNMENT)) {
                out.writeInt(PROPERTY_VERTICAL_ALIGNMENT);
                out.writeInt(lbl.getVerticalAlignment());
            }
            if (isPropertyModified(cmp, PROPERTY_TEXT_POSITION)) {
                out.writeInt(PROPERTY_TEXT_POSITION);
                out.writeInt(lbl.getTextPosition());
            }
        } else {
            if (cmp instanceof com.codename1.ui.TextArea) {
                com.codename1.ui.TextArea txt = (com.codename1.ui.TextArea) cmp;
                if (isPropertyModified(cmp, PROPERTY_VERTICAL_ALIGNMENT)) {
                    out.writeInt(PROPERTY_VERTICAL_ALIGNMENT);
                    out.writeInt(txt.getVerticalAlignment());
                }
                if (isPropertyModified(cmp, PROPERTY_TEXT)) {
                    out.writeInt(PROPERTY_TEXT);
                    out.writeUTF(txt.getText());
                }
                if (isPropertyModified(cmp, PROPERTY_TEXT_AREA_GROW)) {
                    out.writeInt(PROPERTY_TEXT_AREA_GROW);
                    out.writeBoolean(txt.isGrowByContent());
                }
                if (isPropertyModified(cmp, PROPERTY_TEXT_CONSTRAINT)) {
                    out.writeInt(PROPERTY_TEXT_CONSTRAINT);
                    out.writeInt(txt.getConstraint());
                }
                if (isPropertyModified(cmp, PROPERTY_TEXT_MAX_LENGTH)) {
                    out.writeInt(PROPERTY_TEXT_MAX_LENGTH);
                    out.writeInt(txt.getMaxSize());
                }
                if (isPropertyModified(cmp, PROPERTY_EDITABLE)) {
                    out.writeInt(PROPERTY_EDITABLE);
                    out.writeBoolean(txt.isEditable());
                }
                if (isPropertyModified(cmp, PROPERTY_ALIGNMENT)) {
                    out.writeInt(PROPERTY_ALIGNMENT);
                    out.writeInt(txt.getAlignment());
                }
                if (isPropertyModified(cmp, PROPERTY_HINT)) {
                    out.writeInt(PROPERTY_HINT);
                    out.writeUTF(txt.getHint());
                }
                if (isPropertyModified(cmp, PROPERTY_HINT_ICON) && txt.getHintIcon() != null) {
                    out.writeInt(PROPERTY_HINT_ICON);
                    out.writeUTF(res.findId(txt.getHintIcon()));
                }
                if (isPropertyModified(cmp, PROPERTY_COLUMNS)) {
                    out.writeInt(PROPERTY_COLUMNS);
                    out.writeInt(txt.getColumns());
                }
                if (isPropertyModified(cmp, PROPERTY_ROWS)) {
                    out.writeInt(PROPERTY_ROWS);
                    out.writeInt(txt.getRows());
                }
            } else {
                if (cmp instanceof com.codename1.ui.List) {
                    com.codename1.ui.List lst = (com.codename1.ui.List) cmp;
                    if (isPropertyModified(cmp, PROPERTY_ITEM_GAP)) {
                        out.writeInt(PROPERTY_ITEM_GAP);
                        out.writeInt(lst.getItemGap());
                    }
                    if (isPropertyModified(cmp, PROPERTY_LIST_FIXED)) {
                        out.writeInt(PROPERTY_LIST_FIXED);
                        out.writeInt(lst.getFixedSelection());
                    }
                    if (isPropertyModified(cmp, PROPERTY_LIST_ORIENTATION)) {
                        out.writeInt(PROPERTY_LIST_ORIENTATION);
                        out.writeInt(lst.getOrientation());
                    }
                    if (isPropertyModified(cmp, PROPERTY_HINT)) {
                        out.writeInt(PROPERTY_HINT);
                        out.writeUTF(lst.getHint());
                    }
                    if (isPropertyModified(cmp, PROPERTY_HINT_ICON) && lst.getHintIcon() != null) {
                        out.writeInt(PROPERTY_HINT_ICON);
                        out.writeUTF(res.findId(lst.getHintIcon()));
                    }
                    if (isPropertyModified(cmp, PROPERTY_LIST_RENDERER) && lst.getRenderer() instanceof com.codename1.ui.list.GenericListCellRenderer) {
                        out.writeInt(PROPERTY_LIST_RENDERER);
                        com.codename1.ui.list.GenericListCellRenderer g = (com.codename1.ui.list.GenericListCellRenderer) lst.getRenderer();
                        if (g.getSelectedEven() == null) {
                            out.writeByte(2);
                            out.writeUTF(g.getSelected().getName());
                            out.writeUTF(g.getUnselected().getName());
                        } else {
                            out.writeByte(4);
                            out.writeUTF(g.getSelected().getName());
                            out.writeUTF(g.getUnselected().getName());
                            out.writeUTF(g.getSelectedEven().getName());
                            out.writeUTF(g.getUnselectedEven().getName());
                        }
                    }
                    if (!(cmp instanceof com.codename1.components.RSSReader)) {
                        out.writeInt(PROPERTY_LIST_ITEMS);
                        out.writeInt(lst.getModel().getSize());
                        for (int iter = 0; iter < lst.getModel().getSize(); iter++) {
                            Object o = lst.getModel().getItemAt(iter);
                            if (o instanceof String) {
                                out.writeByte(1);
                                out.writeUTF((String) o);
                            } else {
                                out.writeByte(2);
                                Hashtable h = (Hashtable) o;
                                out.writeInt(h.size());
                                for (Object key : h.keySet()) {
                                    Object val = h.get(key);
                                    if (val instanceof com.codename1.ui.Image) {
                                        out.writeInt(2);
                                        out.writeUTF((String) key);
                                        out.writeUTF(res.findId(val));
                                    } else {
                                        out.writeInt(1);
                                        out.writeUTF((String) key);
                                        if (val instanceof ActionCommand) {
                                            out.writeUTF(((ActionCommand) val).getAction());
                                        } else {
                                            out.writeUTF((String) val);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (isPropertyModified(cmp, PROPERTY_LAYOUT_CONSTRAINT) || (cmp.getParent() != null && cmp.getParent().getLayout() instanceof com.codename1.ui.layouts.BorderLayout)) {
        if (cmp.getParent() != null && cmp != containerInstance && cmp.getClientProperty("%base_form%") == null) {
            com.codename1.ui.layouts.Layout l = cmp.getParent().getLayout();
            if (l instanceof com.codename1.ui.layouts.BorderLayout) {
                out.writeInt(PROPERTY_LAYOUT_CONSTRAINT);
                out.writeUTF((String) l.getComponentConstraint(cmp));
            } else {
                if (l instanceof com.codename1.ui.table.TableLayout) {
                    out.writeInt(PROPERTY_LAYOUT_CONSTRAINT);
                    com.codename1.ui.table.TableLayout.Constraint con = (com.codename1.ui.table.TableLayout.Constraint) l.getComponentConstraint(cmp);
                    out.writeInt(getInt("row", con.getClass(), con));
                    out.writeInt(getInt("column", con.getClass(), con));
                    out.writeInt(getInt("height", con.getClass(), con));
                    out.writeInt(getInt("width", con.getClass(), con));
                    out.writeInt(getInt("align", con.getClass(), con));
                    out.writeInt(getInt("spanHorizontal", con.getClass(), con));
                    out.writeInt(getInt("valign", con.getClass(), con));
                    out.writeInt(getInt("spanVertical", con.getClass(), con));
                }
            }
        }
    }
    if (isPropertyModified(cmp, PROPERTY_EMBED)) {
        out.writeInt(PROPERTY_EMBED);
        out.writeUTF(((EmbeddedContainer) cmp).getEmbed());
    }
    if (isPropertyModified(cmp, PROPERTY_UIID)) {
        out.writeInt(PROPERTY_UIID);
        out.writeUTF(cmp.getUIID());
    }
    if (isPropertyModified(cmp, PROPERTY_FOCUSABLE)) {
        out.writeInt(PROPERTY_FOCUSABLE);
        out.writeBoolean(cmp.isFocusable());
    }
    if (isPropertyModified(cmp, PROPERTY_ENABLED)) {
        out.writeInt(PROPERTY_ENABLED);
        out.writeBoolean(cmp.isEnabled());
    }
    if (isPropertyModified(cmp, PROPERTY_RTL)) {
        out.writeInt(PROPERTY_RTL);
        out.writeBoolean(cmp.isRTL());
    }
    if (isPropertyModified(cmp, PROPERTY_SCROLL_VISIBLE)) {
        out.writeInt(PROPERTY_SCROLL_VISIBLE);
        out.writeBoolean(cmp.isScrollVisible());
    }
    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)) {
        out.writeInt(PROPERTY_TENSILE_DRAG_ENABLED);
        out.writeBoolean(cmp.isTensileDragEnabled());
    }
    if (isPropertyModified(cmp, PROPERTY_TACTILE_TOUCH)) {
        out.writeInt(PROPERTY_TACTILE_TOUCH);
        out.writeBoolean(cmp.isTactileTouch());
    }
    if (isPropertyModified(cmp, PROPERTY_SNAP_TO_GRID)) {
        out.writeInt(PROPERTY_SNAP_TO_GRID);
        out.writeBoolean(cmp.isSnapToGrid());
    }
    if (isPropertyModified(cmp, PROPERTY_FLATTEN)) {
        out.writeInt(PROPERTY_FLATTEN);
        out.writeBoolean(cmp.isFlatten());
    }
    if (isPropertyModified(cmp, PROPERTY_CUSTOM)) {
        for (String propName : cmp.getPropertyNames()) {
            if (isCustomPropertyModified(cmp, propName) && !propName.startsWith("$")) {
                out.writeInt(PROPERTY_CUSTOM);
                out.writeUTF(propName);
                Class type = getPropertyCustomType(cmp, propName);
                Object value = cmp.getPropertyValue(propName);
                if (value == null) {
                    out.writeBoolean(true);
                    continue;
                }
                out.writeBoolean(false);
                if (type == String.class) {
                    out.writeUTF((String) value);
                    continue;
                }
                if (type == String[].class) {
                    String[] result = (String[]) value;
                    out.writeInt(result.length);
                    for (int i = 0; i < result.length; i++) {
                        out.writeUTF(result[i]);
                    }
                    continue;
                }
                if (type == String[][].class) {
                    String[][] result = (String[][]) value;
                    out.writeInt(result.length);
                    for (int i = 0; i < result.length; i++) {
                        out.writeInt(result[i].length);
                        for (int j = 0; j < result[i].length; j++) {
                            out.writeUTF(result[i][j]);
                        }
                    }
                    continue;
                }
                if (type == Integer.class) {
                    out.writeInt(((Number) value).intValue());
                    continue;
                }
                if (type == Long.class) {
                    out.writeLong(((Number) value).longValue());
                    continue;
                }
                if (type == Double.class) {
                    out.writeDouble(((Number) value).doubleValue());
                    continue;
                }
                if (type == Date.class) {
                    if (value == null) {
                        out.writeBoolean(false);
                        continue;
                    }
                    out.writeBoolean(true);
                    out.writeLong(((Date) value).getTime());
                    continue;
                }
                if (type == Float.class) {
                    out.writeFloat(((Number) value).floatValue());
                    continue;
                }
                if (type == Byte.class) {
                    out.writeByte(((Number) value).byteValue());
                    continue;
                }
                if (type == Boolean.class) {
                    out.writeBoolean(((Boolean) value).booleanValue());
                    continue;
                }
                if (type == com.codename1.ui.Image[].class) {
                    com.codename1.ui.Image[] result = (com.codename1.ui.Image[]) value;
                    out.writeInt(result.length);
                    for (int i = 0; i < result.length; i++) {
                        if (result[i] == null) {
                            out.writeUTF("");
                        } else {
                            String id = res.findId(result[i]);
                            if (id == null) {
                                out.writeUTF("");
                            } else {
                                out.writeUTF(id);
                            }
                        }
                    }
                    continue;
                }
                if (type == com.codename1.ui.Image.class) {
                    com.codename1.ui.Image result = (com.codename1.ui.Image) value;
                    if (result == null) {
                        out.writeUTF("");
                    } else {
                        String id = res.findId(result);
                        if (id == null) {
                            out.writeUTF("");
                        } else {
                            out.writeUTF(id);
                        }
                    }
                    continue;
                }
                if (type == com.codename1.ui.Container.class) {
                    out.writeUTF(((com.codename1.ui.Container) value).getName());
                    continue;
                }
                if (type == com.codename1.ui.list.CellRenderer.class) {
                    com.codename1.ui.list.GenericListCellRenderer g = (com.codename1.ui.list.GenericListCellRenderer) value;
                    if (g.getSelectedEven() == null) {
                        out.writeByte(2);
                        out.writeUTF(g.getSelected().getName());
                        out.writeUTF(g.getUnselected().getName());
                    } else {
                        out.writeByte(4);
                        out.writeUTF(g.getSelected().getName());
                        out.writeUTF(g.getUnselected().getName());
                        out.writeUTF(g.getSelectedEven().getName());
                        out.writeUTF(g.getUnselectedEven().getName());
                    }
                    continue;
                }
                if (type == Object[].class) {
                    Object[] arr = (Object[]) value;
                    out.writeInt(arr.length);
                    for (int iter = 0; iter < arr.length; iter++) {
                        Object o = arr[iter];
                        if (o instanceof String) {
                            out.writeByte(1);
                            out.writeUTF((String) o);
                        } else {
                            out.writeByte(2);
                            Hashtable h = (Hashtable) o;
                            out.writeInt(h.size());
                            for (Object key : h.keySet()) {
                                Object val = h.get(key);
                                if (val instanceof com.codename1.ui.Image) {
                                    out.writeInt(2);
                                    out.writeUTF((String) key);
                                    out.writeUTF(res.findId(val));
                                } else {
                                    out.writeInt(1);
                                    out.writeUTF((String) key);
                                    out.writeUTF((String) val);
                                }
                            }
                        }
                    }
                    continue;
                }
                // none of the above then its a char
                out.writeChar(((Character) value).charValue());
            }
        }
    }
    out.writeInt(-1);
}
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) BorderLayout(com.codename1.ui.layouts.BorderLayout) EventObject(java.util.EventObject) TableLayout(com.codename1.ui.table.TableLayout) Hashtable(java.util.Hashtable) Hashtable(java.util.Hashtable) Point(java.awt.Point)

Example 12 with Row

use of com.codename1.db.Row in project CodenameOne by codenameone.

the class UserInterfaceEditor method appendComponentXMLBody.

private static void appendComponentXMLBody(com.codename1.ui.Container containerInstance, com.codename1.ui.Component cmp, StringBuilder build, EditableResources res, String indent) {
    if (isPropertyModified(cmp, PROPERTY_LAYOUT_CONSTRAINT) || (cmp.getParent() != null && cmp.getParent().getLayout() instanceof com.codename1.ui.layouts.BorderLayout)) {
        if (cmp.getParent() != null && cmp != containerInstance && cmp.getClientProperty("%base_form%") == null) {
            com.codename1.ui.layouts.Layout l = cmp.getParent().getLayout();
            if (l instanceof com.codename1.ui.layouts.BorderLayout) {
                build.append(indent);
                build.append("<layoutConstraint value=\"");
                build.append((String) l.getComponentConstraint(cmp));
                build.append("\" />\n");
            } else {
                if (l instanceof com.codename1.ui.table.TableLayout) {
                    build.append(indent);
                    com.codename1.ui.table.TableLayout.Constraint con = (com.codename1.ui.table.TableLayout.Constraint) l.getComponentConstraint(cmp);
                    build.append("<layoutConstraint row=\"");
                    build.append(getInt("row", con.getClass(), con));
                    build.append("\" column=\"");
                    build.append(getInt("column", con.getClass(), con));
                    build.append("\" height=\"");
                    build.append(getInt("height", con.getClass(), con));
                    build.append("\" width=\"");
                    build.append(getInt("width", con.getClass(), con));
                    build.append("\" align=\"");
                    build.append(getInt("align", con.getClass(), con));
                    build.append("\" spanHorizontal=\"");
                    build.append(getInt("spanHorizontal", con.getClass(), con));
                    build.append("\" valign=\"");
                    build.append(getInt("valign", con.getClass(), con));
                    build.append("\" spanVertical=\"");
                    build.append(getInt("spanVertical", con.getClass(), con));
                    build.append("\" />\n");
                }
            }
        }
    }
    // don't persist children of subclasses like Table etc.
    if (cmp.getClass() == com.codename1.ui.Container.class || cmp instanceof com.codename1.ui.Form || cmp instanceof com.codename1.ui.ComponentGroup) {
        com.codename1.ui.Container cnt = (com.codename1.ui.Container) cmp;
        if (cnt instanceof com.codename1.ui.Form) {
            cnt = ((com.codename1.ui.Form) cnt).getContentPane();
        }
        for (int iter = 0; iter < cnt.getComponentCount(); iter++) {
            persistToXML(containerInstance, cnt.getComponentAt(iter), build, res, indent);
        }
    }
    if (cmp instanceof com.codename1.ui.List && !(cmp instanceof com.codename1.components.RSSReader)) {
        com.codename1.ui.List lst = (com.codename1.ui.List) cmp;
        for (int iter = 0; iter < lst.getModel().getSize(); iter++) {
            Object o = lst.getModel().getItemAt(iter);
            appendMapOrString(o, build, indent, res);
        }
    }
    if (isPropertyModified(cmp, PROPERTY_CUSTOM)) {
        for (String propName : cmp.getPropertyNames()) {
            if (isCustomPropertyModified(cmp, propName) && !propName.startsWith("$")) {
                build.append(indent);
                build.append("<custom name=\"");
                build.append(propName);
                Class type = getPropertyCustomType(cmp, propName);
                Object value = cmp.getPropertyValue(propName);
                if (value == null) {
                    build.append("\" />\n");
                    continue;
                }
                if (type.isArray()) {
                    // 2d array
                    if (type.getComponentType().isArray()) {
                        build.append("\" type=\"");
                        build.append(type.getComponentType().getComponentType().getName());
                        build.append("\" array=\"true\" dimensions=\"2");
                    } else {
                        build.append("\" type=\"");
                        build.append(type.getComponentType().getName());
                        build.append("\" array=\"true\" dimensions=\"1");
                    }
                } else {
                    build.append("\" type=\"");
                    build.append(type.getName());
                }
                build.append("\" ");
                if (type == String.class) {
                    build.append("value=\"");
                    build.append(xmlize((String) value));
                    build.append("\" />\n");
                    continue;
                }
                if (type == String[].class) {
                    build.append(">\n");
                    String[] result = (String[]) value;
                    for (int i = 0; i < result.length; i++) {
                        build.append(indent);
                        build.append("  ");
                        build.append("<str>");
                        build.append(xmlize(result[i]));
                        build.append("</str>\n");
                    }
                    build.append(indent);
                    build.append("</custom>\n");
                    continue;
                }
                if (type == String[][].class) {
                    String[][] result = (String[][]) value;
                    build.append(">\n");
                    for (int i = 0; i < result.length; i++) {
                        build.append(indent);
                        build.append("  ");
                        build.append("<arr>");
                        for (int j = 0; j < result[i].length; j++) {
                            build.append(indent);
                            build.append("    ");
                            build.append("<str>");
                            build.append(xmlize(result[i][j]));
                            build.append("</str>\n");
                        }
                        build.append(indent);
                        build.append("  ");
                        build.append("</arr>\n");
                    }
                    build.append(indent);
                    build.append("</custom>\n");
                    continue;
                }
                if (type == Integer.class) {
                    build.append("value=\"");
                    build.append(((Number) value).intValue());
                    build.append("\" />\n");
                    continue;
                }
                if (type == Long.class) {
                    build.append("value=\"");
                    build.append(((Number) value).longValue());
                    build.append("\" />\n");
                    continue;
                }
                if (type == Double.class) {
                    build.append("value=\"");
                    build.append(((Number) value).doubleValue());
                    build.append("\" />\n");
                    continue;
                }
                if (type == Date.class) {
                    build.append("value=\"");
                    build.append(((Date) value).getTime());
                    build.append("\" />\n");
                    continue;
                }
                if (type == Float.class) {
                    build.append("value=\"");
                    build.append(((Number) value).floatValue());
                    build.append("\" />\n");
                    continue;
                }
                if (type == Byte.class) {
                    build.append("value=\"");
                    build.append(((Number) value).byteValue());
                    build.append("\" />\n");
                    continue;
                }
                if (type == Boolean.class) {
                    build.append("value=\"");
                    build.append(((Boolean) value).booleanValue());
                    build.append("\" />\n");
                    continue;
                }
                if (type == com.codename1.ui.Image[].class) {
                    com.codename1.ui.Image[] result = (com.codename1.ui.Image[]) value;
                    build.append(">\n");
                    for (int i = 0; i < result.length; i++) {
                        build.append(indent);
                        build.append("  ");
                        if (result[i] == null) {
                            build.append("<str/>\n");
                        } else {
                            String id = res.findId(result[i]);
                            if (id == null) {
                                build.append("<str/>\n");
                            } else {
                                build.append("<str>");
                                build.append(xmlize(id));
                                build.append("</str>\n");
                            }
                        }
                    }
                    build.append(indent);
                    build.append("</custom>\n");
                    continue;
                }
                if (type == com.codename1.ui.Image.class) {
                    com.codename1.ui.Image result = (com.codename1.ui.Image) value;
                    String id = res.findId(result);
                    if (id != null) {
                        build.append("value=\"");
                        build.append(xmlize(id));
                    }
                    build.append("\" />\n");
                    continue;
                }
                if (type == com.codename1.ui.Container.class) {
                    build.append("value=\"");
                    build.append(xmlize(((com.codename1.ui.Container) value).getName()));
                    build.append("\" />\n");
                    continue;
                }
                if (type == com.codename1.ui.list.CellRenderer.class) {
                    com.codename1.ui.list.GenericListCellRenderer g = (com.codename1.ui.list.GenericListCellRenderer) value;
                    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");
                    continue;
                }
                if (type == Object[].class) {
                    build.append(">\n");
                    Object[] arr = (Object[]) value;
                    for (int iter = 0; iter < arr.length; iter++) {
                        Object o = arr[iter];
                        appendMapOrString(o, build, indent, res);
                    }
                    build.append(indent);
                    build.append("</custom>\n");
                    continue;
                }
                // none of the above then its a char
                build.append("value=\"");
                build.append(xmlize("" + ((Character) value).charValue()));
                build.append("\" />\n");
            }
        }
    }
}
Also used : EmbeddedContainer(com.codename1.ui.util.EmbeddedContainer) BorderLayout(com.codename1.ui.layouts.BorderLayout) ArrayList(java.util.ArrayList) List(java.util.List) JList(javax.swing.JList) TableLayout(com.codename1.ui.table.TableLayout) Hashtable(java.util.Hashtable) BorderLayout(com.codename1.ui.layouts.BorderLayout) Point(java.awt.Point) EventObject(java.util.EventObject)

Example 13 with Row

use of com.codename1.db.Row in project CodenameOne by codenameone.

the class L10nEditor method initTable.

private void initTable() {
    bundleTable.setModel(new AbstractTableModel() {

        public int getRowCount() {
            return keys.size();
        }

        public int getColumnCount() {
            return 1 + localeList.size();
        }

        public boolean isCellEditable(int row, int col) {
            boolean b = col != 0;
            if (b) {
                String s = (String) getValueAt(row, col);
                return s == null || !s.contains("\n");
            }
            return b;
        }

        public String getColumnName(int columnIndex) {
            if (columnIndex == 0) {
                return "Key";
            }
            return (String) localeList.get(columnIndex - 1);
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex == 0) {
                return keys.get(rowIndex);
            }
            Hashtable h = res.getL10N(localeName, (String) localeList.get(columnIndex - 1));
            return h.get(keys.get(rowIndex));
        }

        public void setValueAt(Object val, int rowIndex, int columnIndex) {
            res.setModified();
            if (columnIndex == 0) {
                if (!keys.contains(val)) {
                // ...
                }
                return;
            }
            // Hashtable h = (Hashtable)bundle.get(localeList.get(columnIndex - 1));
            // h.put(keys.get(rowIndex), val);
            String currentKey = (String) keys.get(rowIndex);
            res.setLocaleProperty(localeName, (String) localeList.get(columnIndex - 1), currentKey, val);
            if (currentKey.equals("@im")) {
                StringTokenizer tok = new StringTokenizer((String) val, "|");
                boolean modified = false;
                while (tok.hasMoreTokens()) {
                    String currentIm = tok.nextToken();
                    if ("ABC".equals(currentIm) || "123".equals(currentIm) || "Abc".equals(currentIm) || "abc".equals(currentIm)) {
                        continue;
                    }
                    String prop = "@im-" + currentIm;
                    if (!keys.contains(prop)) {
                        keys.add(prop);
                        for (Object locale : localeList) {
                            res.setLocaleProperty(localeName, (String) locale, prop, "");
                        }
                        modified = true;
                    }
                }
                if (modified) {
                    fireTableDataChanged();
                }
                return;
            }
            if (currentKey.equals("@vkb")) {
                boolean modified = false;
                StringTokenizer tok = new StringTokenizer((String) val, "|");
                while (tok.hasMoreTokens()) {
                    String currentIm = tok.nextToken();
                    if ("ABC".equals(currentIm) || "123".equals(currentIm) || ".,123".equals(currentIm) || ".,?".equals(currentIm)) {
                        continue;
                    }
                    String prop = "@vkb-" + currentIm;
                    if (!keys.contains(prop)) {
                        keys.add(prop);
                        for (Object locale : localeList) {
                            res.setLocaleProperty(localeName, (String) locale, prop, "");
                        }
                        modified = true;
                    }
                }
                if (modified) {
                    fireTableDataChanged();
                }
            }
        }
    });
    bundleTable.setDefaultRenderer(Object.class, new SwingRenderer() {

        private JCheckBox chk = new JCheckBox();

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (column > 0) {
                // constant value
                String key = (String) keys.get(row);
                if (key.startsWith("@")) {
                    if (key.equalsIgnoreCase("@rtl")) {
                        chk.setSelected(value != null && "true".equalsIgnoreCase(value.toString()));
                        updateComponentSelectedState(chk, isSelected, table, row, column, hasFocus);
                        return chk;
                    }
                    if (key.startsWith("@vkb") || key.startsWith("@im")) {
                        JButton b = new JButton("...");
                        updateComponentSelectedState(b, isSelected, table, row, column, hasFocus);
                        return b;
                    }
                }
            }
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }
    });
    bundleTable.setDefaultEditor(Object.class, new DefaultCellEditor(new JTextField()) {

        private Object currentValue;

        String editedKey;

        private DefaultCellEditor standardEditor = new DefaultCellEditor(new JTextField());

        private DefaultCellEditor buttonEditor = new DefaultCellEditor(new JTextField()) {

            private JButton button = new JButton("...");

            {
                button.setBorderPainted(false);
                button.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        if (editedKey.equals("@vkb") || editedKey.equals("@im")) {
                            currentValue = editInputModeOrder((String) currentValue, editedKey.equals("@vkb"));
                            fireEditingStoppedExt();
                            return;
                        }
                        /*if(editedKey.startsWith("@vkb")) {
                                VKBEditor v = new VKBEditor(button, editedKey.substring(5), (String)currentValue);
                                currentValue = v.getValue();
                                fireEditingStoppedExt();
                                return;
                            }*/
                        if (editedKey.startsWith("@im")) {
                            currentValue = editTextFieldInputMode((String) currentValue);
                            fireEditingStoppedExt();
                            return;
                        }
                    }
                });
            }

            public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
                editedKey = (String) keys.get(row);
                return button;
            }
        };

        private DefaultCellEditor checkBoxEditor = new DefaultCellEditor(new JCheckBox()) {

            public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
                return super.getTableCellEditorComponent(table, new Boolean("true".equalsIgnoreCase((String) value)), isSelected, row, column);
            }

            public Object getCellEditorValue() {
                Boolean b = (Boolean) super.getCellEditorValue();
                if (b.booleanValue()) {
                    return "true";
                }
                return "false";
            }
        };

        private TableCellEditor current = standardEditor;

        {
            buttonEditor.setClickCountToStart(1);
            checkBoxEditor.setClickCountToStart(1);
        }

        private void updateEditor(int row) {
            // constant value
            final String key = (String) keys.get(row);
            if (key.startsWith("@")) {
                if (key.equalsIgnoreCase("@rtl")) {
                    current = checkBoxEditor;
                    return;
                }
                if (key.startsWith("@vkb") || key.startsWith("@im")) {
                    current = buttonEditor;
                    return;
                }
            }
            current = standardEditor;
        }

        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            updateEditor(row);
            currentValue = value;
            return current.getTableCellEditorComponent(table, value, isSelected, row, column);
        }

        public void fireEditingStoppedExt() {
            fireEditingStopped();
        }

        public Object getCellEditorValue() {
            if (current == buttonEditor) {
                return currentValue;
            }
            return current.getCellEditorValue();
        }

        public boolean stopCellEditing() {
            return current.stopCellEditing();
        }

        public void cancelCellEditing() {
            current.cancelCellEditing();
        }

        public void addCellEditorListener(CellEditorListener l) {
            current.addCellEditorListener(l);
            super.addCellEditorListener(l);
        }

        public void removeCellEditorListener(CellEditorListener l) {
            current.removeCellEditorListener(l);
            super.removeCellEditorListener(l);
        }

        public boolean isCellEditable(EventObject anEvent) {
            return current.isCellEditable(anEvent);
        }

        public boolean shouldSelectCell(EventObject anEvent) {
            return current.shouldSelectCell(anEvent);
        }
    });
    locales.setModel(new DefaultComboBoxModel(localeList.toArray()));
    removeLocale.setEnabled(localeList.size() > 1);
}
Also used : Hashtable(java.util.Hashtable) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) CellEditorListener(javax.swing.event.CellEditorListener) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) JTextField(javax.swing.JTextField) SwingRenderer(com.codename1.ui.resource.util.SwingRenderer) EventObject(java.util.EventObject) DefaultCellEditor(javax.swing.DefaultCellEditor) JCheckBox(javax.swing.JCheckBox) StringTokenizer(java.util.StringTokenizer) ActionListener(java.awt.event.ActionListener) JTable(javax.swing.JTable) AbstractTableModel(javax.swing.table.AbstractTableModel) EventObject(java.util.EventObject) TableCellEditor(javax.swing.table.TableCellEditor) Component(java.awt.Component) UIBuilderOverride(com.codename1.ui.util.UIBuilderOverride)

Example 14 with Row

use of com.codename1.db.Row in project CodenameOne by codenameone.

the class JavaSEPort method createSkinsMenu.

private JMenu createSkinsMenu(final JFrame frm, final JMenu menu) throws MalformedURLException {
    JMenu m;
    if (menu == null) {
        m = new JMenu("Skins");
        m.setDoubleBuffered(true);
    } else {
        m = menu;
        m.removeAll();
    }
    final JMenu skinMenu = m;
    Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
    String skinNames = pref.get("skins", DEFAULT_SKINS);
    if (skinNames != null) {
        if (skinNames.length() < DEFAULT_SKINS.length()) {
            skinNames = DEFAULT_SKINS;
        }
        ButtonGroup skinGroup = new ButtonGroup();
        StringTokenizer tkn = new StringTokenizer(skinNames, ";");
        while (tkn.hasMoreTokens()) {
            final String current = tkn.nextToken();
            String name = current;
            if (current.contains(":")) {
                try {
                    URL u = new URL(current);
                    File f = new File(u.getFile());
                    if (!f.exists()) {
                        continue;
                    }
                    name = f.getName();
                } catch (Exception e) {
                    continue;
                }
            } else {
                // remove the old builtin skins from the menu
                if (current.startsWith("/") && !current.equals("/iphone3gs.skin")) {
                    continue;
                }
            }
            String d = System.getProperty("dskin");
            JRadioButtonMenuItem i = new JRadioButtonMenuItem(name, name.equals(pref.get("skin", d)));
            i.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent ae) {
                    if (netMonitor != null) {
                        netMonitor.dispose();
                        netMonitor = null;
                    }
                    if (perfMonitor != null) {
                        perfMonitor.dispose();
                        perfMonitor = null;
                    }
                    Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
                    pref.putBoolean("desktopSkin", false);
                    String mainClass = System.getProperty("MainClass");
                    if (mainClass != null) {
                        pref.put("skin", current);
                        deinitializeSync();
                        frm.dispose();
                        System.setProperty("reload.simulator", "true");
                    } else {
                        loadSkinFile(current, frm);
                        refreshSkin(frm);
                    }
                }
            });
            skinGroup.add(i);
            skinMenu.add(i);
        }
    }
    JMenuItem dSkin = new JMenuItem("Desktop.skin");
    dSkin.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent ae) {
            if (netMonitor != null) {
                netMonitor.dispose();
                netMonitor = null;
            }
            if (perfMonitor != null) {
                perfMonitor.dispose();
                perfMonitor = null;
            }
            Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
            pref.putBoolean("desktopSkin", true);
            String mainClass = System.getProperty("MainClass");
            if (mainClass != null) {
                deinitializeSync();
                frm.dispose();
                System.setProperty("reload.simulator", "true");
            }
        }
    });
    skinMenu.addSeparator();
    skinMenu.add(dSkin);
    skinMenu.addSeparator();
    JMenuItem more = new JMenuItem("More...");
    skinMenu.add(more);
    more.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final JDialog pleaseWait = new JDialog(frm, false);
            pleaseWait.setLocationRelativeTo(frm);
            pleaseWait.setTitle("Message");
            pleaseWait.setLayout(new BorderLayout());
            pleaseWait.add(new JLabel("  Please Wait...  "), BorderLayout.CENTER);
            pleaseWait.pack();
            pleaseWait.setVisible(true);
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    final JDialog d = new JDialog(frm, true);
                    d.setLocationRelativeTo(frm);
                    d.setTitle("Skins");
                    d.setLayout(new BorderLayout());
                    String userDir = System.getProperty("user.home");
                    final File skinDir = new File(userDir + "/.codenameone/");
                    if (!skinDir.exists()) {
                        skinDir.mkdir();
                    }
                    Vector data = new Vector();
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    final Document[] doc = new Document[1];
                    try {
                        DocumentBuilder db = dbf.newDocumentBuilder();
                        Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
                        InputStream is = openSkinsURL();
                        doc[0] = db.parse(is);
                        NodeList skins = doc[0].getElementsByTagName("Skin");
                        for (int i = 0; i < skins.getLength(); i++) {
                            Node skin = skins.item(i);
                            NamedNodeMap attr = skin.getAttributes();
                            String url = attr.getNamedItem("url").getNodeValue();
                            int ver = 0;
                            Node n = attr.getNamedItem("version");
                            if (n != null) {
                                ver = Integer.parseInt(n.getNodeValue());
                            }
                            boolean exists = new File(skinDir.getAbsolutePath() + url).exists();
                            if (!(exists) || Integer.parseInt(pref.get(url, "0")) < ver) {
                                Vector row = new Vector();
                                row.add(new Boolean(false));
                                row.add(new ImageIcon(new URL(defaultCodenameOneComProtocol + "://www.codenameone.com/OTA" + attr.getNamedItem("icon").getNodeValue())));
                                row.add(attr.getNamedItem("name").getNodeValue());
                                if (exists) {
                                    row.add("Update");
                                } else {
                                    row.add("New");
                                }
                                data.add(row);
                            }
                        }
                    } catch (Exception ex) {
                        Logger.getLogger(JavaSEPort.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    if (data.size() == 0) {
                        pleaseWait.setVisible(false);
                        JOptionPane.showMessageDialog(frm, "No New Skins to Install");
                        return;
                    }
                    Vector cols = new Vector();
                    cols.add("Install");
                    cols.add("Icon");
                    cols.add("Name");
                    cols.add("");
                    final DefaultTableModel tableModel = new DefaultTableModel(data, cols) {

                        @Override
                        public boolean isCellEditable(int row, int column) {
                            return column == 0;
                        }
                    };
                    JTable skinsTable = new JTable(tableModel) {

                        @Override
                        public Class<?> getColumnClass(int column) {
                            if (column == 0) {
                                return Boolean.class;
                            }
                            if (column == 1) {
                                return Icon.class;
                            }
                            return super.getColumnClass(column);
                        }
                    };
                    skinsTable.setRowHeight(112);
                    skinsTable.getTableHeader().setReorderingAllowed(false);
                    d.add(new JScrollPane(skinsTable), BorderLayout.CENTER);
                    JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
                    JButton download = new JButton("Download");
                    download.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            final Vector toDowload = new Vector();
                            NodeList skins = doc[0].getElementsByTagName("Skin");
                            for (int i = 0; i < tableModel.getRowCount(); i++) {
                                if (((Boolean) tableModel.getValueAt(i, 0)).booleanValue()) {
                                    Node skin;
                                    for (int j = 0; j < skins.getLength(); j++) {
                                        skin = skins.item(j);
                                        NamedNodeMap attr = skin.getAttributes();
                                        if (attr.getNamedItem("name").getNodeValue().equals(tableModel.getValueAt(i, 2))) {
                                            String url = attr.getNamedItem("url").getNodeValue();
                                            String[] data = new String[2];
                                            data[0] = defaultCodenameOneComProtocol + "://www.codenameone.com/OTA" + url;
                                            data[1] = attr.getNamedItem("version").getNodeValue();
                                            toDowload.add(data);
                                            break;
                                        }
                                    }
                                }
                            }
                            if (toDowload.size() > 0) {
                                final JDialog downloadMessage = new JDialog(d, true);
                                downloadMessage.setTitle("Downloading");
                                downloadMessage.setLayout(new FlowLayout());
                                downloadMessage.setLocationRelativeTo(d);
                                final JLabel details = new JLabel("<br><br>Details");
                                downloadMessage.add(details);
                                final JLabel progress = new JLabel("Progress<br><br>");
                                downloadMessage.add(progress);
                                new Thread() {

                                    @Override
                                    public void run() {
                                        for (Iterator it = toDowload.iterator(); it.hasNext(); ) {
                                            String[] data = (String[]) it.next();
                                            String url = data[0];
                                            details.setText(url.substring(url.lastIndexOf("/")));
                                            details.repaint();
                                            progress.setText("");
                                            progress.repaint();
                                            try {
                                                File skin = downloadSkin(skinDir, url, data[1], progress);
                                                if (skin.exists()) {
                                                    addSkinName(skin.toURI().toString());
                                                }
                                            } catch (Exception e) {
                                            }
                                        }
                                        downloadMessage.setVisible(false);
                                        d.setVisible(false);
                                        try {
                                            createSkinsMenu(frm, skinMenu);
                                        } catch (MalformedURLException ex) {
                                            Logger.getLogger(JavaSEPort.class.getName()).log(Level.SEVERE, null, ex);
                                        }
                                    }
                                }.start();
                                downloadMessage.pack();
                                downloadMessage.setSize(200, 70);
                                downloadMessage.setVisible(true);
                            } else {
                                JOptionPane.showMessageDialog(d, "Choose a Skin to Download");
                            }
                        }
                    });
                    p.add(download);
                    d.add(p, BorderLayout.SOUTH);
                    d.pack();
                    pleaseWait.dispose();
                    d.setVisible(true);
                }
            });
        }
    });
    skinMenu.addSeparator();
    JMenuItem addSkin = new JMenuItem("Add New...");
    skinMenu.add(addSkin);
    addSkin.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent ae) {
            FileDialog picker = new FileDialog(frm, "Add Skin");
            picker.setMode(FileDialog.LOAD);
            picker.setFilenameFilter(new FilenameFilter() {

                public boolean accept(File file, String string) {
                    return string.endsWith(".skin");
                }
            });
            picker.setModal(true);
            picker.setVisible(true);
            String file = picker.getFile();
            if (file != null) {
                if (netMonitor != null) {
                    netMonitor.dispose();
                    netMonitor = null;
                }
                if (perfMonitor != null) {
                    perfMonitor.dispose();
                    perfMonitor = null;
                }
                String mainClass = System.getProperty("MainClass");
                if (mainClass != null) {
                    Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
                    pref.put("skin", picker.getDirectory() + File.separator + file);
                    deinitializeSync();
                    frm.dispose();
                    System.setProperty("reload.simulator", "true");
                } else {
                    loadSkinFile(picker.getDirectory() + File.separator + file, frm);
                    refreshSkin(frm);
                }
            }
        }
    });
    skinMenu.addSeparator();
    JMenuItem reset = new JMenuItem("Reset Skins");
    skinMenu.add(reset);
    reset.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent ae) {
            if (JOptionPane.showConfirmDialog(frm, "Are you sure you want to reset skins to default?", "Clean Storage", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
                Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
                pref.put("skins", DEFAULT_SKINS);
                String userDir = System.getProperty("user.home");
                final File skinDir = new File(userDir + "/.codenameone/");
                if (skinDir.exists()) {
                    File[] childs = skinDir.listFiles();
                    for (int i = 0; i < childs.length; i++) {
                        File child = childs[i];
                        if (child.getName().endsWith(".skin")) {
                            child.delete();
                        }
                    }
                }
                if (netMonitor != null) {
                    netMonitor.dispose();
                    netMonitor = null;
                }
                if (perfMonitor != null) {
                    perfMonitor.dispose();
                    perfMonitor = null;
                }
                String mainClass = System.getProperty("MainClass");
                if (mainClass != null) {
                    pref.put("skin", "/iphone3gs.skin");
                    deinitializeSync();
                    frm.dispose();
                    System.setProperty("reload.simulator", "true");
                } else {
                    loadSkinFile("/iphone3gs.skin", frm);
                    refreshSkin(frm);
                }
            }
        }
    });
    return skinMenu;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ActionEvent(java.awt.event.ActionEvent) Node(org.w3c.dom.Node) DefaultTableModel(javax.swing.table.DefaultTableModel) Document(org.w3c.dom.Document) FilenameFilter(java.io.FilenameFilter) PathIterator(java.awt.geom.PathIterator) Preferences(java.util.prefs.Preferences) Vector(java.util.Vector) NamedNodeMap(org.w3c.dom.NamedNodeMap) BufferedInputStream(com.codename1.io.BufferedInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipInputStream(java.util.zip.ZipInputStream) NodeList(org.w3c.dom.NodeList) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) EOFException(java.io.EOFException) FontFormatException(java.awt.FontFormatException) Point(java.awt.Point) StringTokenizer(java.util.StringTokenizer) ActionListener(java.awt.event.ActionListener) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileDialog(java.awt.FileDialog)

Example 15 with Row

use of com.codename1.db.Row in project CodenameOne by codenameone.

the class MenuBar method createCommandComponent.

/**
 * Creates the component containing the commands within the given vector
 * used for showing the menu dialog, this method calls the createCommandList
 * method by default however it allows more elaborate menu creation.
 *
 * @param commands list of command objects
 * @return Component that will result in the parent menu dialog recieving a command event
 */
protected Component createCommandComponent(Vector commands) {
    UIManager manager = parent.getUIManager();
    // Create a touch based menu interface
    if (manager.getLookAndFeel().isTouchMenus()) {
        Container menu = new Container();
        menu.setScrollableY(true);
        for (int iter = 0; iter < commands.size(); iter++) {
            Command c = (Command) commands.elementAt(iter);
            menu.addComponent(createTouchCommandButton(c));
        }
        if (!manager.isThemeConstant("touchCommandFlowBool", false)) {
            int cols = calculateTouchCommandGridColumns(menu);
            if (cols > getCommandCount()) {
                cols = getCommandCount();
            }
            int rows = Math.max(1, getCommandCount() / cols + (getCommandCount() % cols != 0 ? 1 : 0));
            if (rows > 1) {
                // try to prevent too many columns concentraiting within a single row
                int remainingColumns = (rows * cols) % getCommandCount();
                int newCols = cols;
                int newRows = rows;
                while (remainingColumns != 0 && remainingColumns > 1 && newCols >= 2) {
                    newCols--;
                    newRows = Math.max(1, getCommandCount() / newCols + (getCommandCount() % newCols != 0 ? 1 : 0));
                    if (newRows != rows) {
                        break;
                    }
                    remainingColumns = (newRows * newCols) % getCommandCount();
                }
                if (newRows == rows) {
                    cols = newCols;
                    rows = newRows;
                }
            }
            GridLayout g = new GridLayout(rows, cols);
            g.setFillLastRow(manager.isThemeConstant("touchCommandFillBool", true));
            menu.setLayout(g);
        } else {
            ((FlowLayout) menu.getLayout()).setFillRows(true);
        }
        menu.setPreferredW(Display.getInstance().getDisplayWidth());
        return menu;
    }
    return createCommandList(commands);
}
Also used : GridLayout(com.codename1.ui.layouts.GridLayout) FlowLayout(com.codename1.ui.layouts.FlowLayout) UIManager(com.codename1.ui.plaf.UIManager)

Aggregations

Component (com.codename1.ui.Component)8 Style (com.codename1.ui.plaf.Style)5 ArrayList (java.util.ArrayList)4 Point (java.awt.Point)3 EventObject (java.util.EventObject)3 Hashtable (java.util.Hashtable)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)2 TableLayout (com.codename1.ui.table.TableLayout)2 EmbeddedContainer (com.codename1.ui.util.EmbeddedContainer)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 List (java.util.List)2 StringTokenizer (java.util.StringTokenizer)2 JButton (javax.swing.JButton)2 JList (javax.swing.JList)2 Cursor (com.codename1.db.Cursor)1 Row (com.codename1.db.Row)1 BufferedInputStream (com.codename1.io.BufferedInputStream)1 Button (com.codename1.ui.Button)1 Container (com.codename1.ui.Container)1