Search in sources :

Example 1 with CheckBox

use of com.codename1.ui.CheckBox in project CodenameOne by codenameone.

the class GenericListCellRenderer method setComponentValue.

/**
 * Initializes the given component with the given value
 *
 * @param cmp one of the components that is or is a part of the renderer
 * @param value the value to install into the component
 */
private void setComponentValue(Component cmp, Object value, Component parent, Component rootRenderer) {
    // process so renderer selected/unselected styles are applied
    if (cmp.getName().toLowerCase().endsWith("fixed")) {
        return;
    }
    if (cmp instanceof Label) {
        if (value instanceof Image) {
            Image i = (Image) value;
            if (i.isAnimation()) {
                if (pendingAnimations == null) {
                    pendingAnimations = new ArrayList<Image>();
                }
                if (!pendingAnimations.contains(i)) {
                    pendingAnimations.add(i);
                    if (parentList == null) {
                        parentList = parent;
                    }
                    if (parentList != null) {
                        Form f = parentList.getComponentForm();
                        if (f != null) {
                            f.registerAnimated(mon);
                            waitingForRegisterAnimation = false;
                        } else {
                            waitingForRegisterAnimation = true;
                        }
                    }
                } else {
                    if (waitingForRegisterAnimation) {
                        if (parentList != null) {
                            Form f = parentList.getComponentForm();
                            if (f != null) {
                                f.registerAnimated(mon);
                                waitingForRegisterAnimation = false;
                            }
                        }
                    }
                }
            }
            Image oldImage = ((Label) cmp).getIcon();
            ((Label) cmp).setIcon(i);
            ((Label) cmp).setText("");
            if (oldImage == null || oldImage.getWidth() != i.getWidth() || oldImage.getHeight() != i.getHeight()) {
                ((Container) rootRenderer).revalidate();
            }
            return;
        } else {
            ((Label) cmp).setIcon(null);
        }
        if (cmp instanceof CheckBox) {
            ((CheckBox) cmp).setSelected(isSelectedValue(value));
            return;
        }
        if (cmp instanceof RadioButton) {
            ((RadioButton) cmp).setSelected(isSelectedValue(value));
            return;
        }
        if (cmp instanceof Slider) {
            ((Slider) cmp).setProgress(((Integer) value).intValue());
            return;
        }
        Label l = (Label) cmp;
        if (value == null) {
            l.setText("");
        } else {
            if (value instanceof Label) {
                l.setText(((Label) value).getText());
                l.setIcon(((Label) value).getIcon());
            } else {
                l.setText(value.toString());
            }
        }
        if (firstCharacterRTL) {
            String t = l.getText();
            if (t.length() > 0) {
                l.setRTL(Display.getInstance().isRTL(t.charAt(0)));
            }
        }
        return;
    }
    if (cmp instanceof TextArea) {
        if (value == null) {
            ((TextArea) cmp).setText("");
        } else {
            ((TextArea) cmp).setText(value.toString());
        }
    }
}
Also used : Container(com.codename1.ui.Container) Slider(com.codename1.ui.Slider) Form(com.codename1.ui.Form) TextArea(com.codename1.ui.TextArea) CheckBox(com.codename1.ui.CheckBox) Label(com.codename1.ui.Label) RadioButton(com.codename1.ui.RadioButton) Image(com.codename1.ui.Image) EncodedImage(com.codename1.ui.EncodedImage) URLImage(com.codename1.ui.URLImage)

Example 2 with CheckBox

use of com.codename1.ui.CheckBox in project CodenameOne by codenameone.

the class DefaultLookAndFeel method drawCheckBox.

/**
 * {@inheritDoc}
 */
public void drawCheckBox(Graphics g, Button cb) {
    if (chkBoxImages != null) {
        Image x;
        if (chkBoxImagesFocus != null && chkBoxImagesFocus[0] != null && cb.hasFocus() && Display.getInstance().shouldRenderSelection(cb)) {
            if (cb.isEnabled()) {
                x = chkBoxImagesFocus[cb.isSelected() ? 1 : 0];
            } else {
                x = chkBoxImagesFocus[cb.isSelected() ? 3 : 2];
            }
        } else {
            if (cb.isEnabled()) {
                x = chkBoxImages[cb.isSelected() ? 1 : 0];
            } else {
                x = chkBoxImages[cb.isSelected() ? 3 : 2];
            }
        }
        drawComponent(g, cb, cb.getIconFromState(), x, 0);
    } else {
        Style style = cb.getStyle();
        // checkbox square needs to be the height and width of the font height even
        // when no text is in the check box this is a good indication of phone DPI
        int height = cb.getStyle().getFont().getHeight();
        drawComponent(g, cb, cb.getIconFromState(), null, height);
        int gradientColor;
        g.setColor(style.getFgColor());
        gradientColor = style.getBgColor();
        int width = height;
        int rectWidth = scaleCoordinate(12f, 16, width);
        int tX = cb.getX();
        if (cb.isRTL()) {
            tX = tX + cb.getWidth() - style.getPaddingLeft(cb.isRTL()) - rectWidth;
        } else {
            tX += style.getPaddingLeft(cb.isRTL());
        }
        int tY = cb.getY() + style.getPaddingTop() + (cb.getHeight() - style.getPaddingTop() - style.getPaddingBottom()) / 2 - height / 2;
        g.translate(tX, tY);
        int x = scaleCoordinate(1.04f, 16, width);
        int y = scaleCoordinate(4.0f, 16, height);
        int rectHeight = scaleCoordinate(12f, 16, height);
        // brighten or darken the color slightly
        int destColor = findDestColor(gradientColor);
        g.fillLinearGradient(gradientColor, destColor, x + 1, y + 1, rectWidth - 2, rectHeight - 1, false);
        g.drawRoundRect(x, y, rectWidth, rectHeight, 5, 5);
        if (cb.isSelected()) {
            int color = g.getColor();
            g.setColor(0x111111);
            g.translate(0, 1);
            fillCheckbox(g, width, height);
            g.setColor(color);
            g.translate(0, -1);
            fillCheckbox(g, width, height);
        }
        g.translate(-tX, -tY);
    }
}
Also used : Image(com.codename1.ui.Image) FontImage(com.codename1.ui.FontImage)

Example 3 with CheckBox

use of com.codename1.ui.CheckBox in project CodenameOne by codenameone.

the class InstantUI method createEditUI.

/**
 * Creates editing UI for the given business object
 * @param bo the business object
 * @param autoCommit true if the bindings used should be auto-committed
 * @return a UI container that can be used to edit the business object
 */
public Container createEditUI(PropertyBusinessObject bo, boolean autoCommit) {
    Container cnt;
    if (Display.getInstance().isTablet()) {
        TableLayout tl = new TableLayout(1, 2);
        tl.setGrowHorizontally(true);
        cnt = new Container(tl);
    } else {
        cnt = new Container(BoxLayout.y());
    }
    UiBinding uib = new UiBinding();
    ArrayList<UiBinding.Binding> allBindings = new ArrayList<UiBinding.Binding>();
    for (PropertyBase b : bo.getPropertyIndex()) {
        if (isExcludedProperty(b)) {
            continue;
        }
        Class cls = (Class) b.getClientProperty("cn1$cmpCls");
        if (cls != null) {
            try {
                Component cmp = (Component) cls.newInstance();
                cmp.setName(b.getName());
                cnt.add(b.getLabel()).add(cmp);
                allBindings.add(uib.bind(b, cmp));
            } catch (Exception err) {
                Log.e(err);
                throw new RuntimeException("Custom property instant UI failed for " + b.getName() + " " + err);
            }
            continue;
        }
        String[] multiLabels = (String[]) b.getClientProperty("cn1$multiChceLbl");
        if (multiLabels != null) {
            // multi choice component
            final Object[] multiValues = (Object[]) b.getClientProperty("cn1$multiChceVal");
            if (multiLabels.length < 5) {
                // toggle buttons
                ButtonGroup bg = new ButtonGroup();
                RadioButton[] rbs = new RadioButton[multiLabels.length];
                cnt.add(b.getLabel());
                Container radioBox = new Container(new GridLayout(multiLabels.length));
                for (int iter = 0; iter < multiLabels.length; iter++) {
                    rbs[iter] = RadioButton.createToggle(multiLabels[iter], bg);
                    radioBox.add(rbs[iter]);
                }
                cnt.add(radioBox);
                allBindings.add(uib.bindGroup(b, multiValues, rbs));
            } else {
                Picker stringPicker = new Picker();
                stringPicker.setStrings(multiLabels);
                Map<Object, Object> m1 = new HashMap<Object, Object>();
                Map<Object, Object> m2 = new HashMap<Object, Object>();
                for (int iter = 0; iter < multiLabels.length; iter++) {
                    m1.put(multiLabels[iter], multiValues[iter]);
                    m2.put(multiValues[iter], multiLabels[iter]);
                }
                cnt.add(b.getLabel()).add(stringPicker);
                allBindings.add(uib.bind(b, stringPicker, new UiBinding.PickerAdapter<Object>(new UiBinding.MappingConverter(m1), new UiBinding.MappingConverter(m2))));
            }
            continue;
        }
        Class t = b.getGenericType();
        if (t != null) {
            if (t == Boolean.class) {
                CheckBox cb = new CheckBox();
                uib.bind(b, cb);
                cnt.add(b.getLabel()).add(cb);
                continue;
            }
            if (t == Date.class) {
                Picker dp = new Picker();
                dp.setType(Display.PICKER_TYPE_DATE);
                uib.bind(b, dp);
                cnt.add(b.getLabel()).add(dp);
                continue;
            }
        }
        TextField tf = new TextField();
        tf.setConstraint(getTextFieldConstraint(b));
        uib.bind(b, tf);
        cnt.add(b.getLabel()).add(tf);
    }
    cnt.putClientProperty("cn1$iui-binding", uib.createGroupBinding(allBindings));
    return cnt;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Container(com.codename1.ui.Container) GridLayout(com.codename1.ui.layouts.GridLayout) Picker(com.codename1.ui.spinner.Picker) TextField(com.codename1.ui.TextField) Component(com.codename1.ui.Component) TableLayout(com.codename1.ui.table.TableLayout) RadioButton(com.codename1.ui.RadioButton) ButtonGroup(com.codename1.ui.ButtonGroup) CheckBox(com.codename1.ui.CheckBox)

Example 4 with CheckBox

use of com.codename1.ui.CheckBox in project CodenameOne by codenameone.

the class UIBuilderOverride method createInstance.

/**
 * Create a component instance from XML
 */
private Component createInstance(final ComponentEntry root, final EditableResources res, Container rootCnt, final Container parentContainer, final ArrayList<Runnable> postCreateTasks) {
    try {
        final Component c = createComponentType(root.getType());
        if (rootCnt == null) {
            rootCnt = (Container) c;
        }
        final Container rootContainer = rootCnt;
        if (root.getBaseForm() != null) {
            c.putClientProperty("%base_form%", root.getBaseForm());
        }
        c.putClientProperty(TYPE_KEY, root.getType());
        c.setName(root.getName());
        String clientProps = root.getClientProperties();
        if (clientProps != null && clientProps.length() > 0) {
            String[] props = clientProps.split(",");
            StringBuilder b = new StringBuilder();
            for (String p : props) {
                String[] keyVal = p.split("=");
                c.putClientProperty(keyVal[0], keyVal[1]);
                if (b.length() > 0) {
                    b.append(",");
                }
                b.append(keyVal[0]);
            }
            c.putClientProperty("cn1$Properties", b.toString());
        }
        rootContainer.putClientProperty("%" + root.getName() + "%", c);
        // layout must be first since we might need to rely on it later on with things such as constraints
        if (root.getLayout() != null) {
            modifyingProperty(c, PROPERTY_LAYOUT);
            Layout l;
            if (root.getLayout().equals("BorderLayout")) {
                l = new BorderLayout();
                if (root.isBorderLayoutAbsoluteCenter() != null) {
                    ((BorderLayout) l).setAbsoluteCenter(root.isBorderLayoutAbsoluteCenter().booleanValue());
                }
                if (root.getBorderLayoutSwapCenter() != null) {
                    ((BorderLayout) l).defineLandscapeSwap(BorderLayout.CENTER, root.getBorderLayoutSwapCenter());
                }
                if (root.getBorderLayoutSwapNorth() != null) {
                    ((BorderLayout) l).defineLandscapeSwap(BorderLayout.NORTH, root.getBorderLayoutSwapNorth());
                }
                if (root.getBorderLayoutSwapSouth() != null) {
                    ((BorderLayout) l).defineLandscapeSwap(BorderLayout.SOUTH, root.getBorderLayoutSwapSouth());
                }
                if (root.getBorderLayoutSwapEast() != null) {
                    ((BorderLayout) l).defineLandscapeSwap(BorderLayout.EAST, root.getBorderLayoutSwapEast());
                }
                if (root.getBorderLayoutSwapWest() != null) {
                    ((BorderLayout) l).defineLandscapeSwap(BorderLayout.WEST, root.getBorderLayoutSwapWest());
                }
            } else {
                if (root.getLayout().equals("FlowLayout")) {
                    l = new FlowLayout();
                    ((FlowLayout) l).setFillRows(root.isFlowLayoutFillRows());
                    ((FlowLayout) l).setAlign(root.getFlowLayoutAlign());
                    ((FlowLayout) l).setValign(root.getFlowLayoutValign());
                } else {
                    if (root.getLayout().equals("GridLayout")) {
                        l = new GridLayout(root.getGridLayoutRows().intValue(), root.getGridLayoutColumns().intValue());
                    } else {
                        if (root.getLayout().equals("BoxLayout")) {
                            if (root.getBoxLayoutAxis().equals("X")) {
                                l = new BoxLayout(BoxLayout.X_AXIS);
                            } else {
                                l = new BoxLayout(BoxLayout.Y_AXIS);
                            }
                        } else {
                            if (root.getLayout().equals("TableLayout")) {
                                l = new TableLayout(root.getTableLayoutRows(), root.getTableLayoutColumns());
                            } else {
                                l = new LayeredLayout();
                            }
                        }
                    }
                }
            }
            ((Container) c).setLayout(l);
        }
        if (parentContainer != null && root.getLayoutConstraint() != null) {
            modifyingProperty(c, PROPERTY_LAYOUT_CONSTRAINT);
            if (parentContainer.getLayout() instanceof BorderLayout) {
                c.putClientProperty("layoutConstraint", root.getLayoutConstraint().getValue());
            } else {
                TableLayout tl = (TableLayout) parentContainer.getLayout();
                TableLayout.Constraint con = tl.createConstraint(root.getLayoutConstraint().getRow(), root.getLayoutConstraint().getColumn());
                con.setHeightPercentage(root.getLayoutConstraint().getHeight());
                con.setWidthPercentage(root.getLayoutConstraint().getWidth());
                con.setHorizontalAlign(root.getLayoutConstraint().getAlign());
                con.setHorizontalSpan(root.getLayoutConstraint().getSpanHorizontal());
                con.setVerticalAlign(root.getLayoutConstraint().getValign());
                con.setVerticalSpan(root.getLayoutConstraint().getSpanVertical());
                c.putClientProperty("layoutConstraint", con);
            }
        }
        if (root.getEmbed() != null && root.getEmbed().length() > 0) {
            modifyingProperty(c, PROPERTY_EMBED);
            rootContainer.putClientProperty(EMBEDDED_FORM_FLAG, "");
            ((EmbeddedContainer) c).setEmbed(root.getEmbed());
            Container embed = createContainer(res, root.getEmbed(), (EmbeddedContainer) c);
            if (embed != null) {
                if (embed instanceof Form) {
                    embed = formToContainer((Form) embed);
                }
                ((EmbeddedContainer) c).addComponent(BorderLayout.CENTER, embed);
                // this isn't exactly the "right thing" but its the best we can do to make all
                // use cases work
                beforeShowContainer(embed);
                postShowContainer(embed);
            }
        }
        if (root.isToggle() != null) {
            modifyingProperty(c, PROPERTY_TOGGLE_BUTTON);
            ((Button) c).setToggle(root.isToggle().booleanValue());
        }
        if (root.getGroup() != null) {
            modifyingProperty(c, PROPERTY_RADIO_GROUP);
            ((RadioButton) c).setGroup(root.getGroup());
        }
        if (root.isSelected() != null) {
            modifyingProperty(c, PROPERTY_SELECTED);
            if (c instanceof RadioButton) {
                ((RadioButton) c).setSelected(root.isSelected().booleanValue());
            } else {
                ((CheckBox) c).setSelected(root.isSelected().booleanValue());
            }
        }
        if (root.isScrollableX() != null) {
            modifyingProperty(c, PROPERTY_SCROLLABLE_X);
            ((Container) c).setScrollableX(root.isScrollableX().booleanValue());
        }
        if (root.isScrollableY() != null) {
            modifyingProperty(c, PROPERTY_SCROLLABLE_Y);
            ((Container) c).setScrollableY(root.isScrollableY().booleanValue());
        }
        if (root.isTensileDragEnabled() != null) {
            modifyingProperty(c, PROPERTY_TENSILE_DRAG_ENABLED);
            c.setTensileDragEnabled(root.isTensileDragEnabled().booleanValue());
        }
        if (root.isTactileTouch() != null) {
            modifyingProperty(c, PROPERTY_TACTILE_TOUCH);
            c.setTactileTouch(root.isTactileTouch().booleanValue());
        }
        if (root.isSnapToGrid() != null) {
            modifyingProperty(c, PROPERTY_SNAP_TO_GRID);
            c.setSnapToGrid(root.isSnapToGrid().booleanValue());
        }
        if (root.isFlatten() != null) {
            modifyingProperty(c, PROPERTY_FLATTEN);
            c.setFlatten(root.isFlatten().booleanValue());
        }
        if (root.getText() != null) {
            modifyingProperty(c, PROPERTY_TEXT);
            if (c instanceof Label) {
                ((Label) c).setText(root.getText());
            } else {
                ((TextArea) c).setText(root.getText());
            }
        }
        if (root.getMaxSize() != null) {
            modifyingProperty(c, PROPERTY_TEXT_MAX_LENGTH);
            ((TextArea) c).setMaxSize(root.getMaxSize().intValue());
        }
        if (root.getConstraint() != null) {
            modifyingProperty(c, PROPERTY_TEXT_CONSTRAINT);
            ((TextArea) c).setConstraint(root.getConstraint().intValue());
        }
        if (root.getAlignment() != null) {
            modifyingProperty(c, PROPERTY_ALIGNMENT);
            if (c instanceof Label) {
                ((Label) c).setAlignment(root.getAlignment().intValue());
            } else {
                ((TextArea) c).setAlignment(root.getAlignment().intValue());
            }
        }
        if (root.isGrowByContent() != null) {
            modifyingProperty(c, PROPERTY_TEXT_AREA_GROW);
            ((TextArea) c).setGrowByContent(root.isGrowByContent().booleanValue());
        }
        if (root.getTabPlacement() != null) {
            modifyingProperty(c, PROPERTY_TAB_PLACEMENT);
            ((Tabs) c).setTabPlacement(root.getTabPlacement().intValue());
        }
        if (root.getTabTextPosition() != null) {
            modifyingProperty(c, PROPERTY_TAB_TEXT_POSITION);
            ((Tabs) c).setTabTextPosition(root.getTabTextPosition().intValue());
        }
        if (root.getUiid() != null) {
            modifyingProperty(c, PROPERTY_UIID);
            c.setUIID(root.getUiid());
        }
        if (root.getDialogUIID() != null) {
            modifyingProperty(c, PROPERTY_DIALOG_UIID);
            ((Dialog) c).setDialogUIID(root.getDialogUIID());
        }
        if (root.isDisposeWhenPointerOutOfBounds() != null) {
            modifyingProperty(c, PROPERTY_DISPOSE_WHEN_POINTER_OUT);
            ((Dialog) c).setDisposeWhenPointerOutOfBounds(root.isDisposeWhenPointerOutOfBounds());
        }
        if (root.getCloudBoundProperty() != null) {
            modifyingProperty(c, PROPERTY_CLOUD_BOUND_PROPERTY);
            c.setCloudBoundProperty(root.getCloudBoundProperty());
        }
        if (root.getCloudDestinationProperty() != null) {
            modifyingProperty(c, PROPERTY_CLOUD_DESTINATION_PROPERTY);
            c.setCloudDestinationProperty(root.getCloudDestinationProperty());
        }
        if (root.getDialogPosition() != null && root.getDialogPosition().length() > 0) {
            modifyingProperty(c, PROPERTY_DIALOG_POSITION);
            ((Dialog) c).setDialogPosition(root.getDialogPosition());
        }
        if (root.isFocusable() != null) {
            modifyingProperty(c, PROPERTY_FOCUSABLE);
            c.setFocusable(root.isFocusable().booleanValue());
        }
        if (root.isEnabled() != null) {
            modifyingProperty(c, PROPERTY_ENABLED);
            c.setEnabled(root.isEnabled().booleanValue());
        }
        if (root.isScrollVisible() != null) {
            modifyingProperty(c, PROPERTY_SCROLL_VISIBLE);
            c.setScrollVisible(root.isScrollVisible().booleanValue());
        }
        if (root.getIcon() != null) {
            modifyingProperty(c, PROPERTY_ICON);
            ((Label) c).setIcon(res.getImage(root.getIcon()));
        }
        if (root.getRolloverIcon() != null) {
            modifyingProperty(c, PROPERTY_ROLLOVER_ICON);
            ((Button) c).setRolloverIcon(res.getImage(root.getRolloverIcon()));
        }
        if (root.getPressedIcon() != null) {
            modifyingProperty(c, PROPERTY_PRESSED_ICON);
            ((Button) c).setPressedIcon(res.getImage(root.getPressedIcon()));
        }
        if (root.getDisabledIcon() != null) {
            modifyingProperty(c, PROPERTY_DISABLED_ICON);
            ((Button) c).setDisabledIcon(res.getImage(root.getDisabledIcon()));
        }
        if (root.getGap() != null) {
            modifyingProperty(c, PROPERTY_GAP);
            ((Label) c).setGap(root.getGap().intValue());
        }
        if (root.getVerticalAlignment() != null) {
            modifyingProperty(c, PROPERTY_VERTICAL_ALIGNMENT);
            if (c instanceof Label) {
                ((Label) c).setVerticalAlignment(root.getVerticalAlignment().intValue());
            } else {
                ((TextArea) c).setVerticalAlignment(root.getVerticalAlignment().intValue());
            }
        }
        if (root.getTextPosition() != null) {
            modifyingProperty(c, PROPERTY_TEXT_POSITION);
            ((Label) c).setTextPosition(root.getTextPosition().intValue());
        }
        if (root.getTitle() != null) {
            modifyingProperty(c, PROPERTY_TITLE);
            ((Form) c).setTitle(root.getTitle());
        }
        // components should be added when we've set everything else up
        if (root.getComponent() != null) {
            modifyingProperty(c, PROPERTY_COMPONENTS);
            if (c instanceof Tabs) {
                for (ComponentEntry ent : root.getComponent()) {
                    Component newCmp = createInstance(ent, res, rootContainer, (Container) c, postCreateTasks);
                    ((Tabs) c).addTab(ent.getTabTitle(), newCmp);
                }
            } else {
                for (ComponentEntry ent : root.getComponent()) {
                    Component newCmp = createInstance(ent, res, rootContainer, (Container) c, postCreateTasks);
                    Object cons = newCmp.getClientProperty("layoutConstraint");
                    if (cons != null) {
                        modifyingProperty(c, PROPERTY_LAYOUT_CONSTRAINT);
                        ((Container) c).addComponent(cons, newCmp);
                    } else {
                        ((Container) c).addComponent(newCmp);
                    }
                }
            }
        }
        if (root.getColumns() != null) {
            modifyingProperty(c, PROPERTY_COLUMNS);
            ((TextArea) c).setColumns(root.getColumns().intValue());
        }
        if (root.getRows() != null) {
            modifyingProperty(c, PROPERTY_ROWS);
            ((TextArea) c).setRows(root.getRows().intValue());
        }
        if (root.getHint() != null) {
            modifyingProperty(c, PROPERTY_HINT);
            if (c instanceof List) {
                ((List) c).setHint(root.getHint());
            } else {
                ((TextArea) c).setHint(root.getHint());
            }
        }
        if (root.getHintIcon() != null) {
            modifyingProperty(c, PROPERTY_HINT_ICON);
            if (c instanceof List) {
                ((List) c).setHintIcon(res.getImage(root.getHint()));
            } else {
                ((TextArea) c).setHintIcon(res.getImage(root.getHint()));
            }
        }
        if (root.getItemGap() != null) {
            modifyingProperty(c, PROPERTY_ITEM_GAP);
            ((List) c).setItemGap(root.getItemGap().intValue());
        }
        if (root.getFixedSelection() != null) {
            modifyingProperty(c, PROPERTY_LIST_FIXED);
            ((List) c).setFixedSelection(root.getFixedSelection().intValue());
        }
        if (root.getOrientation() != null) {
            modifyingProperty(c, PROPERTY_LIST_ORIENTATION);
            ((List) c).setOrientation(root.getOrientation().intValue());
        }
        if (c instanceof com.codename1.ui.List && !(c instanceof com.codename1.components.RSSReader)) {
            modifyingProperty(c, PROPERTY_LIST_ITEMS);
            if (root.getStringItem() != null && root.getStringItem().length > 0) {
                String[] arr = new String[root.getStringItem().length];
                for (int iter = 0; iter < arr.length; iter++) {
                    arr[iter] = root.getStringItem()[iter].getValue();
                }
                ((List) c).setModel(new DefaultListModel<String>(arr));
            } else {
                if (root.getMapItems() != null && root.getMapItems().length > 0) {
                    Hashtable[] arr = new Hashtable[root.getMapItems().length];
                    for (int iter = 0; iter < arr.length; iter++) {
                        arr[iter] = new Hashtable();
                        if (root.getMapItems()[iter].getActionItem() != null) {
                            for (Val v : root.getMapItems()[iter].getActionItem()) {
                                Command cmd = createCommandImpl((String) v.getValue(), null, -1, v.getValue(), false, "");
                                cmd.putClientProperty(COMMAND_ACTION, (String) v.getValue());
                                arr[iter].put(v.getKey(), cmd);
                            }
                        }
                        if (root.getMapItems()[iter].getStringItem() != null) {
                            for (Val v : root.getMapItems()[iter].getActionItem()) {
                                arr[iter].put(v.getKey(), v.getValue());
                            }
                        }
                        if (root.getMapItems()[iter].getImageItem() != null) {
                            for (Val v : root.getMapItems()[iter].getActionItem()) {
                                arr[iter].put(v.getKey(), res.getImage(v.getValue()));
                            }
                        }
                    }
                    ((List) c).setModel(new DefaultListModel<java.util.Map>(arr));
                }
            }
        }
        if (root.getSelectedRenderer() != null) {
            modifyingProperty(c, PROPERTY_LIST_RENDERER);
            GenericListCellRenderer g;
            if (root.getSelectedRendererEven() == null) {
                Component selected = createContainer(res, root.getSelectedRenderer());
                Component unselected = createContainer(res, root.getUnselectedRenderer());
                g = new GenericListCellRenderer(selected, unselected);
                g.setFisheye(!root.getSelectedRenderer().equals(root.getUnselectedRenderer()));
            } else {
                Component selected = createContainer(res, root.getSelectedRenderer());
                Component unselected = createContainer(res, root.getUnselectedRenderer());
                Component even = createContainer(res, root.getSelectedRendererEven());
                Component evenU = createContainer(res, root.getUnselectedRendererEven());
                g = new GenericListCellRenderer(selected, unselected, even, evenU);
                g.setFisheye(!root.getSelectedRenderer().equals(root.getUnselectedRenderer()));
            }
            if (c instanceof ContainerList) {
                ((ContainerList) c).setRenderer(g);
            } else {
                ((List) c).setRenderer(g);
            }
        }
        if (root.getNextForm() != null && root.getNextForm().length() > 0) {
            modifyingProperty(c, PROPERTY_NEXT_FORM);
            setNextForm(c, root.getNextForm(), res, rootContainer);
        }
        if (root.getCommand() != null) {
            modifyingProperty(c, PROPERTY_COMMANDS);
            for (CommandEntry cmd : root.getCommand()) {
                Command currentCommand = createCommandImpl(cmd.getName(), res.getImage(cmd.getIcon()), cmd.getId(), cmd.getAction(), cmd.isBackCommand(), cmd.getArgument());
                if (cmd.getRolloverIcon() != null && cmd.getRolloverIcon().length() > 0) {
                    currentCommand.setRolloverIcon(res.getImage(cmd.getRolloverIcon()));
                }
                if (cmd.getPressedIcon() != null && cmd.getPressedIcon().length() > 0) {
                    currentCommand.setPressedIcon(res.getImage(cmd.getPressedIcon()));
                }
                if (cmd.getDisabledIcon() != null && cmd.getDisabledIcon().length() > 0) {
                    currentCommand.setDisabledIcon(res.getImage(cmd.getDisabledIcon()));
                }
                if (cmd.isBackCommand()) {
                    ((Form) c).setBackCommand(currentCommand);
                }
                ((Form) c).addCommand(currentCommand);
                currentCommand.putClientProperty(COMMAND_ARGUMENTS, cmd.getArgument());
                currentCommand.putClientProperty(COMMAND_ACTION, cmd.getAction());
            }
        }
        if (root.isCyclicFocus() != null) {
            modifyingProperty(c, PROPERTY_CYCLIC_FOCUS);
            ((Form) c).setCyclicFocus(root.isCyclicFocus().booleanValue());
        }
        if (root.isRtl() != null) {
            modifyingProperty(c, PROPERTY_RTL);
            c.setRTL(root.isRtl().booleanValue());
        }
        if (root.getThumbImage() != null) {
            modifyingProperty(c, PROPERTY_SLIDER_THUMB);
            ((Slider) c).setThumbImage(res.getImage(root.getThumbImage()));
        }
        if (root.isInfinite() != null) {
            modifyingProperty(c, PROPERTY_INFINITE);
            ((Slider) c).setInfinite(root.isInfinite().booleanValue());
        }
        if (root.getProgress() != null) {
            modifyingProperty(c, PROPERTY_PROGRESS);
            ((Slider) c).setProgress(root.getProgress().intValue());
        }
        if (root.isVertical() != null) {
            modifyingProperty(c, PROPERTY_VERTICAL);
            ((Slider) c).setVertical(root.isVertical().booleanValue());
        }
        if (root.isEditable() != null) {
            modifyingProperty(c, PROPERTY_EDITABLE);
            if (c instanceof TextArea) {
                ((TextArea) c).setEditable(root.isEditable().booleanValue());
            } else {
                ((Slider) c).setEditable(root.isEditable().booleanValue());
            }
        }
        if (root.getIncrements() != null) {
            modifyingProperty(c, PROPERTY_INCREMENTS);
            ((Slider) c).setIncrements(root.getIncrements().intValue());
        }
        if (root.isRenderPercentageOnTop() != null) {
            modifyingProperty(c, PROPERTY_RENDER_PERCENTAGE_ON_TOP);
            ((Slider) c).setRenderPercentageOnTop(root.isRenderPercentageOnTop().booleanValue());
        }
        if (root.getMaxValue() != null) {
            modifyingProperty(c, PROPERTY_MAX_VALUE);
            ((Slider) c).setMaxValue(root.getMaxValue().intValue());
        }
        if (root.getMinValue() != null) {
            modifyingProperty(c, PROPERTY_MIN_VALUE);
            ((Slider) c).setMinValue(root.getMinValue().intValue());
        }
        if (root.getCommandName() != null) {
            modifyingProperty(c, PROPERTY_COMMAND);
            postCreateTasks.add(new Runnable() {

                public void run() {
                    Command cmd = createCommandImpl(root.getCommandName(), res.getImage(root.getCommandIcon()), root.getCommandId().intValue(), root.getCommandAction(), root.isCommandBack().booleanValue(), root.getCommandArgument());
                    if (c instanceof Container) {
                        Button b = (Button) ((Container) c).getLeadComponent();
                        b.setCommand(cmd);
                        return;
                    }
                    ((Button) c).setCommand(cmd);
                }
            });
        }
        if (root.getLabelFor() != null) {
            modifyingProperty(c, PROPERTY_LABEL_FOR);
            postCreateTasks.add(new Runnable() {

                public void run() {
                    ((Label) c).setLabelForComponent((Label) findByName(root.getLabelFor(), rootContainer));
                }
            });
        }
        if (root.getLeadComponent() != null) {
            modifyingProperty(c, PROPERTY_LEAD_COMPONENT);
            postCreateTasks.add(new Runnable() {

                public void run() {
                    ((Container) c).setLeadComponent(findByName(root.getLeadComponent(), rootContainer));
                }
            });
        }
        if (root.getNextFocusUp() != null) {
            modifyingProperty(c, PROPERTY_NEXT_FOCUS_UP);
            postCreateTasks.add(new Runnable() {

                public void run() {
                    c.setNextFocusUp(findByName(root.getNextFocusUp(), rootContainer));
                }
            });
        }
        if (root.getNextFocusDown() != null) {
            modifyingProperty(c, PROPERTY_NEXT_FOCUS_DOWN);
            postCreateTasks.add(new Runnable() {

                public void run() {
                    c.setNextFocusDown(findByName(root.getNextFocusDown(), rootContainer));
                }
            });
        }
        if (root.getNextFocusLeft() != null) {
            modifyingProperty(c, PROPERTY_NEXT_FOCUS_LEFT);
            postCreateTasks.add(new Runnable() {

                public void run() {
                    c.setNextFocusLeft(findByName(root.getNextFocusLeft(), rootContainer));
                }
            });
        }
        if (root.getNextFocusRight() != null) {
            modifyingProperty(c, PROPERTY_NEXT_FOCUS_RIGHT);
            postCreateTasks.add(new Runnable() {

                public void run() {
                    c.setNextFocusRight(findByName(root.getNextFocusRight(), rootContainer));
                }
            });
        }
        // custom settings are always last after all other properties
        if (root.getCustom() != null && root.getCustom().length > 0) {
            modifyingProperty(c, PROPERTY_CUSTOM);
            for (Custom cust : root.getCustom()) {
                modifyingCustomProperty(c, cust.getName());
                Object value = null;
                Class customType = UserInterfaceEditor.getPropertyCustomType(c, cust.getName());
                if (customType.isArray()) {
                    if (customType == String[].class) {
                        if (cust.getStr() != null) {
                            String[] arr = new String[cust.getStr().length];
                            for (int iter = 0; iter < arr.length; iter++) {
                                arr[iter] = cust.getStr()[iter].getValue();
                            }
                            c.setPropertyValue(cust.getName(), arr);
                        } else {
                            c.setPropertyValue(cust.getName(), null);
                        }
                        continue;
                    }
                    if (customType == String[][].class) {
                        if (cust.getArr() != null) {
                            String[][] arr = new String[cust.getArr().length][];
                            for (int iter = 0; iter < arr.length; iter++) {
                                if (cust.getArr()[iter] != null && cust.getArr()[iter].getValue() != null) {
                                    arr[iter] = new String[cust.getArr()[iter].getValue().length];
                                    for (int inter = 0; inter < arr[iter].length; inter++) {
                                        arr[iter][inter] = cust.getArr()[iter].getValue()[inter].getValue();
                                    }
                                }
                            }
                            c.setPropertyValue(cust.getName(), arr);
                        } else {
                            c.setPropertyValue(cust.getName(), null);
                        }
                        continue;
                    }
                    if (customType == com.codename1.ui.Image[].class) {
                        if (cust.getStr() != null) {
                            com.codename1.ui.Image[] arr = new com.codename1.ui.Image[cust.getStr().length];
                            for (int iter = 0; iter < arr.length; iter++) {
                                arr[iter] = res.getImage(cust.getStr()[iter].getValue());
                            }
                            c.setPropertyValue(cust.getName(), arr);
                        } else {
                            c.setPropertyValue(cust.getName(), null);
                        }
                        continue;
                    }
                    if (customType == Object[].class) {
                        if (cust.getStringItem() != null) {
                            String[] arr = new String[cust.getStringItem().length];
                            for (int iter = 0; iter < arr.length; iter++) {
                                arr[iter] = cust.getStringItem()[iter].getValue();
                            }
                            c.setPropertyValue(cust.getName(), arr);
                            continue;
                        } else {
                            if (cust.getMapItems() != null) {
                                Hashtable[] arr = new Hashtable[cust.getMapItems().length];
                                for (int iter = 0; iter < arr.length; iter++) {
                                    arr[iter] = new Hashtable();
                                    if (cust.getMapItems()[iter].getActionItem() != null) {
                                        for (Val v : cust.getMapItems()[iter].getActionItem()) {
                                            Command cmd = createCommandImpl(v.getValue(), null, -1, v.getValue(), false, "");
                                            cmd.putClientProperty(COMMAND_ACTION, v.getValue());
                                            value = cmd;
                                            arr[iter].put(v.getKey(), cmd);
                                        }
                                    }
                                    if (cust.getMapItems()[iter].getStringItem() != null) {
                                        for (Val v : cust.getMapItems()[iter].getActionItem()) {
                                            arr[iter].put(v.getKey(), v.getValue());
                                        }
                                    }
                                    if (cust.getMapItems()[iter].getImageItem() != null) {
                                        for (Val v : cust.getMapItems()[iter].getActionItem()) {
                                            arr[iter].put(v.getKey(), res.getImage(v.getValue()));
                                        }
                                    }
                                }
                                c.setPropertyValue(cust.getName(), arr);
                                continue;
                            }
                        }
                        c.setPropertyValue(cust.getName(), null);
                        continue;
                    }
                }
                if (customType == String.class) {
                    c.setPropertyValue(cust.getName(), cust.getValue());
                    continue;
                }
                if (customType == Integer.class) {
                    if (cust.getValue() != null) {
                        c.setPropertyValue(cust.getName(), Integer.valueOf(cust.getValue()));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == Long.class) {
                    if (cust.getValue() != null) {
                        c.setPropertyValue(cust.getName(), Long.valueOf(cust.getValue()));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == Double.class) {
                    if (cust.getValue() != null) {
                        c.setPropertyValue(cust.getName(), Double.valueOf(cust.getValue()));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == Date.class) {
                    if (cust.getValue() != null) {
                        c.setPropertyValue(cust.getName(), new Date(Long.parseLong(cust.getValue())));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == Float.class) {
                    if (cust.getValue() != null) {
                        c.setPropertyValue(cust.getName(), Float.valueOf(cust.getValue()));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == Byte.class) {
                    if (cust.getValue() != null) {
                        c.setPropertyValue(cust.getName(), Byte.valueOf(cust.getValue()));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == Character.class) {
                    if (cust.getValue() != null && ((String) cust.getValue()).length() > 0) {
                        c.setPropertyValue(cust.getName(), new Character(((String) cust.getValue()).charAt(0)));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == Boolean.class) {
                    if (cust.getValue() != null) {
                        c.setPropertyValue(cust.getName(), Boolean.valueOf(cust.getValue()));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == com.codename1.ui.Image.class) {
                    if (cust.getValue() != null) {
                        c.setPropertyValue(cust.getName(), res.getImage(cust.getValue()));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == com.codename1.ui.Container.class) {
                    // resource might have been removed we need to fail gracefully
                    String[] uiNames = res.getUIResourceNames();
                    for (int iter = 0; iter < uiNames.length; iter++) {
                        if (uiNames[iter].equals(cust.getName())) {
                            c.setPropertyValue(cust.getName(), createContainer(res, cust.getName()));
                            continue;
                        }
                    }
                    c.setPropertyValue(cust.getName(), null);
                    continue;
                }
                if (customType == com.codename1.ui.list.CellRenderer.class) {
                    if (cust.getUnselectedRenderer() != null) {
                        GenericListCellRenderer g;
                        if (cust.getSelectedRendererEven() == null) {
                            Component selected = createContainer(res, cust.getSelectedRenderer());
                            Component unselected = createContainer(res, cust.getUnselectedRenderer());
                            g = new GenericListCellRenderer(selected, unselected);
                            g.setFisheye(!cust.getSelectedRenderer().equals(cust.getUnselectedRenderer()));
                        } else {
                            Component selected = createContainer(res, cust.getSelectedRenderer());
                            Component unselected = createContainer(res, cust.getUnselectedRenderer());
                            Component even = createContainer(res, cust.getSelectedRendererEven());
                            Component evenU = createContainer(res, cust.getUnselectedRendererEven());
                            g = new GenericListCellRenderer(selected, unselected, even, evenU);
                            g.setFisheye(!cust.getSelectedRenderer().equals(cust.getUnselectedRenderer()));
                        }
                        c.setPropertyValue(cust.getName(), g);
                        continue;
                    }
                    c.setPropertyValue(cust.getName(), null);
                    continue;
                }
            }
        }
        return c;
    } catch (Throwable t) {
        t.printStackTrace();
        JOptionPane.showMessageDialog(java.awt.Frame.getFrames()[0], "Error creating component: " + root.getName() + "\n" + t.toString() + "\ntrying to recover...", "Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }
}
Also used : Val(com.codename1.ui.util.xml.Val) TextArea(com.codename1.ui.TextArea) Label(com.codename1.ui.Label) Container(com.codename1.ui.Container) BorderLayout(com.codename1.ui.layouts.BorderLayout) Button(com.codename1.ui.Button) RadioButton(com.codename1.ui.RadioButton) Dialog(com.codename1.ui.Dialog) ComponentEntry(com.codename1.ui.util.xml.comps.ComponentEntry) ArrayList(java.util.ArrayList) ContainerList(com.codename1.ui.list.ContainerList) List(com.codename1.ui.List) LayeredLayout(com.codename1.ui.layouts.LayeredLayout) List(com.codename1.ui.List) RadioButton(com.codename1.ui.RadioButton) CheckBox(com.codename1.ui.CheckBox) Tabs(com.codename1.ui.Tabs) FlowLayout(com.codename1.ui.layouts.FlowLayout) Slider(com.codename1.ui.Slider) Form(com.codename1.ui.Form) BoxLayout(com.codename1.ui.layouts.BoxLayout) ContainerList(com.codename1.ui.list.ContainerList) GridLayout(com.codename1.ui.layouts.GridLayout) Component(com.codename1.ui.Component) TableLayout(com.codename1.ui.table.TableLayout) GenericListCellRenderer(com.codename1.ui.list.GenericListCellRenderer) Hashtable(java.util.Hashtable) Custom(com.codename1.ui.util.xml.comps.Custom) Date(java.util.Date) CommandEntry(com.codename1.ui.util.xml.comps.CommandEntry) BoxLayout(com.codename1.ui.layouts.BoxLayout) LayeredLayout(com.codename1.ui.layouts.LayeredLayout) GridLayout(com.codename1.ui.layouts.GridLayout) Layout(com.codename1.ui.layouts.Layout) FlowLayout(com.codename1.ui.layouts.FlowLayout) BorderLayout(com.codename1.ui.layouts.BorderLayout) TableLayout(com.codename1.ui.table.TableLayout) ActionCommand(com.codename1.designer.ActionCommand) Command(com.codename1.ui.Command)

Example 5 with CheckBox

use of com.codename1.ui.CheckBox in project CodenameOne by codenameone.

the class DefaultCrashReporter method exception.

/**
 * {@inheritDoc}
 */
public void exception(Throwable t) {
    Preferences.set("$CN1_pendingCrash", true);
    if (promptUser) {
        Dialog error = new Dialog("Error");
        error.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        TextArea txt = new TextArea(errorText);
        txt.setEditable(false);
        txt.setUIID("DialogBody");
        error.addComponent(txt);
        CheckBox cb = new CheckBox(checkboxText);
        cb.setUIID("DialogBody");
        error.addComponent(cb);
        Container grid = new Container(new GridLayout(1, 2));
        error.addComponent(grid);
        Command ok = new Command(sendButtonText);
        Command dont = new Command(dontSendButtonText);
        Button send = new Button(ok);
        Button dontSend = new Button(dont);
        grid.addComponent(send);
        grid.addComponent(dontSend);
        Command result = error.showPacked(BorderLayout.CENTER, true);
        if (result == dont) {
            if (cb.isSelected()) {
                Preferences.set("$CN1_crashBlocked", true);
            }
            Preferences.set("$CN1_pendingCrash", false);
            return;
        } else {
            if (cb.isSelected()) {
                Preferences.set("$CN1_prompt", false);
            }
        }
    }
    Log.sendLog();
    Preferences.set("$CN1_pendingCrash", false);
}
Also used : Container(com.codename1.ui.Container) GridLayout(com.codename1.ui.layouts.GridLayout) TextArea(com.codename1.ui.TextArea) Command(com.codename1.ui.Command) Button(com.codename1.ui.Button) Dialog(com.codename1.ui.Dialog) CheckBox(com.codename1.ui.CheckBox) BoxLayout(com.codename1.ui.layouts.BoxLayout)

Aggregations

RadioButton (com.codename1.ui.RadioButton)10 Button (com.codename1.ui.Button)9 CheckBox (com.codename1.ui.CheckBox)9 Container (com.codename1.ui.Container)7 TextArea (com.codename1.ui.TextArea)7 TextField (com.codename1.ui.TextField)6 Component (com.codename1.ui.Component)5 Image (com.codename1.ui.Image)5 BoxLayout (com.codename1.ui.layouts.BoxLayout)5 Form (com.codename1.ui.Form)4 Label (com.codename1.ui.Label)4 List (com.codename1.ui.List)4 Slider (com.codename1.ui.Slider)4 GridLayout (com.codename1.ui.layouts.GridLayout)4 Hashtable (java.util.Hashtable)4 Command (com.codename1.ui.Command)3 Dialog (com.codename1.ui.Dialog)3 FontImage (com.codename1.ui.FontImage)3 ActionListener (com.codename1.ui.events.ActionListener)3 Dimension (com.codename1.ui.geom.Dimension)3