Search in sources :

Example 36 with Label

use of com.codename1.rad.models.Property.Label in project CodenameOne by codenameone.

the class WebPushTest method createPushForm.

void createPushForm() {
    Form f = new Form("Test Push", new BorderLayout());
    TextField serverUrl = new TextField("https://push.codenameone.com/push/push");
    String nexus5XEmulatorId = "cn1-fcm-c_t22hK-weg:APA91bGNa2hP9SHLBhUmLmzY1ANyjCH2ydEQz0JUFlMPFUHDAUfTQysC1r9K6I7jdk5LN6mEP5k5IWvyQX4dvubbHeY1KWQRKb-aYuXaHHHg2U3XGH1xXXa7xfJbsCf7nB0vu1aWhOg_4M4Z-znC2X5a0tpYf59QmA";
    // APA91bHWiuoPMA4OgIr3ZyVgSjVmnh4H0BQ4jhB3hblIAZfmMs-SfRN1tb4662MudPEULjIkl8P_oTrQ14sKgowz4Q45n6iaPl1GwXb_9HbtlAQDAlnX60Eo4SamzZJkB_6kcnsEMKt_
    String iphoneId = "cn1-ios-c76b23de81f6389f37d2621f000b7f01c52e5c1d6f1f947b199a7c2d8844f713";
    TextArea targetId = new TextArea(Preferences.get("targetId", ""));
    targetId.addActionListener(e -> Preferences.set("targetId", targetId.getText()));
    TextField pushType = new TextField(Preferences.get("pushType", "99"));
    pushType.addActionListener(e -> Preferences.set("pushType", pushType.getText()));
    TextField payload = new TextField(Preferences.get("payload", "<push type=\"0\" body=\"Hello\" category=\"fo\"/>"));
    payload.addActionListener(e -> Preferences.set("payload", payload.getText()));
    TextField gcmServerKey = new TextField(Preferences.get("gcmServerKey", ""));
    gcmServerKey.setHint("FCM_SERVER_API_KEY");
    gcmServerKey.addActionListener(e -> Preferences.set("gcmServerKey", gcmServerKey.getText()));
    TextField tokenField = new TextField(Preferences.get("token", ""));
    tokenField.setHint("Enter your CN1 Token");
    tokenField.addActionListener(e -> {
        Preferences.set("token", tokenField.getText());
    });
    targetId.setRows(4);
    targetId.getAllStyles().setFgColor(0x0);
    targetId.getAllStyles().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
    Button send = new Button("Send");
    send.addActionListener(e -> {
        /*
            boolean result = new Push("E729F5CE-DCE8-4D63-B517-85112994EC66", payload.getText(), targetId.getText())
                    .gcmAuth(gcmServerKey.getText())
                    .apnsAuth(IOS_DEV_CERT_URL, IOS_DEV_CERT_PASS, false)
                    .pushType(Integer.parseInt(pushType.getText()))
                    .wnsAuth("xxxxx", "xxxxxxxx")
                    .send()
                    //.sendAsync()
                    ;
            
            System.out.println("Push sent.  Result: "+result);
            */
        ConnectionRequest req = new ConnectionRequest();
        req.setUrl(serverUrl.getText());
        req.addArgument("token", tokenField.getText());
        req.addArgument("device", targetId.getText());
        req.addArgument("body", payload.getText());
        req.addArgument("type", pushType.getText());
        req.addArgument("auth", gcmServerKey.getText());
        req.addArgument("cert", IOS_DEV_CERT_URL);
        req.addArgument("certPassword", IOS_DEV_CERT_PASS);
        req.addArgument("sid", "ms-app://xxxxxxxxx");
        req.addArgument("client_secret", "xxxxxxxxx");
        req.setHttpMethod("POST");
        req.setPost(true);
        NetworkManager.getInstance().addToQueueAndWait(req);
        System.out.println(req.getResponseCode());
        try {
            System.out.println(new String(req.getResponseData(), "UTF-8"));
        } catch (Throwable t) {
            Log.e(t);
        }
    // new Push("E729F5CE-DCE8-4D63-B517-85112994EC66", payload.getText(), targetId.getText())
    // .wnsAuth("ms-app://s-1-15-2-2674027049-292503787-1918612089-438606370-903203898-836476968-4131729547", "2S37cRtqCR3vQVqhfrFBA2w6PAsWwZ/m")
    // .send();
    });
    Container center = BoxLayout.encloseY(new Label("Server URL"), serverUrl, new Label("Target ID"), targetId, new Label("Push Type"), pushType, new Label("Payload"), payload, new Label("GCM Server Key"), gcmServerKey, new Label("Codename One Token"), tokenField, send);
    center.setScrollableY(true);
    f.add(BorderLayout.CENTER, center);
    f.show();
}
Also used : Container(com.codename1.ui.Container) ConnectionRequest(com.codename1.io.ConnectionRequest) BorderLayout(com.codename1.ui.layouts.BorderLayout) Form(com.codename1.ui.Form) TextArea(com.codename1.ui.TextArea) Button(com.codename1.ui.Button) Label(com.codename1.ui.Label) TextField(com.codename1.ui.TextField)

Example 37 with Label

use of com.codename1.rad.models.Property.Label in project CodenameOne by codenameone.

the class WebSocketsSample method showLogin.

void showLogin() {
    Form f = new Form("Login");
    f.addComponent(new Label("Name: "));
    final TextField nameField = new TextField();
    f.addComponent(nameField);
    f.addComponent(new Button(new Command("Login") {

        @Override
        public void actionPerformed(ActionEvent evt) {
            System.out.println("Hello");
            if (sock.getReadyState() == WebSocketState.OPEN) {
                System.out.println("Open");
                sock.send(nameField.getText());
                showChat();
            } else {
                System.out.println("Closed");
                Dialog.show("Dialog", "The socket is not open: " + sock.getReadyState(), "OK", null);
                sock.reconnect();
            }
        }
    }));
    f.show();
}
Also used : Form(com.codename1.ui.Form) Button(com.codename1.ui.Button) Command(com.codename1.ui.Command) ActionEvent(com.codename1.ui.events.ActionEvent) SpanLabel(com.codename1.components.SpanLabel) Label(com.codename1.ui.Label) TextField(com.codename1.ui.TextField)

Example 38 with Label

use of com.codename1.rad.models.Property.Label in project CodenameOne by codenameone.

the class WebSocketsSample method start.

public void start() {
    if (false) {
        testTimeouts();
        return;
    }
    System.out.println("About to start socket");
    sock = new WebSocket(SERVER_URL) {

        @Override
        protected void onOpen() {
            System.out.println("In onOpen");
            System.out.println("Ready state: " + sock.getReadyState());
        }

        @Override
        protected void onClose(int statusCode, String reason) {
            System.out.println("Closing: " + sock.getReadyState());
            Display.getInstance().callSerially(new Runnable() {

                public void run() {
                    showLogin();
                }
            });
        }

        @Override
        protected void onMessage(final String message) {
            System.out.println("Received message " + message);
            System.out.println("Ready state: " + sock.getReadyState());
            Display.getInstance().callSerially(new Runnable() {

                public void run() {
                    if (chatContainer == null) {
                        return;
                    }
                    SpanLabel label = new SpanLabel();
                    label.setText(message);
                    chatContainer.addComponent(label);
                    chatContainer.animateHierarchy(100);
                }
            });
        }

        @Override
        protected void onError(Exception ex) {
            if (sock == null) {
                System.out.println("Error while socket is null: " + ex.getMessage());
            } else {
                System.out.println("Ready state: " + sock.getReadyState());
                System.out.println("in onError " + ex.getMessage());
            // Log.e(ex);
            }
        }

        @Override
        protected void onMessage(byte[] message) {
            System.out.println("Received bytes " + message.length);
            System.out.println(Arrays.toString(message));
        }
    }.autoReconnect(10000);
    System.out.println("Sending connect");
    System.out.println("Ready State: " + sock.getReadyState());
    sock.connect();
    try {
        sock.send("Test Message");
    } catch (Throwable t) {
        t.printStackTrace();
    }
    showLogin();
}
Also used : SpanLabel(com.codename1.components.SpanLabel) IOException(java.io.IOException) WebSocket(com.codename1.io.websocket.WebSocket)

Example 39 with Label

use of com.codename1.rad.models.Property.Label 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 40 with Label

use of com.codename1.rad.models.Property.Label in project CodenameOne by codenameone.

the class LayoutTests method flowLayoutTests.

public boolean flowLayoutTests() throws Exception {
    System.out.println("FlowLayout Tests");
    boolean rtl = UIManager.getInstance().getLookAndFeel().isRTL();
    try {
        UIManager.getInstance().getLookAndFeel().setRTL(false);
        FlowLayout fl = new FlowLayout();
        Container cnt = new Container(fl);
        cnt.setRTL(false);
        Label lbl = new Label("Test");
        $(cnt, lbl).selectAllStyles().setPadding(0).setMargin(0);
        lbl.setRTL(false);
        cnt.add(lbl);
        int cntWidth = 500;
        int cntHeight = 500;
        int labelPreferredWidth = lbl.getPreferredW();
        int labelPreferredHeight = lbl.getPreferredH();
        cnt.setWidth(cntWidth);
        cnt.setHeight(cntHeight);
        // Start with NO margin, NO padding, default FlowLayout, with single child label also with no padding or margin
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "Label should be laid out with its preferred height");
        assertEqual(0, lbl.getX(), "Label should have x=0 in default FlowLayout");
        assertEqual(0, lbl.getY(), "Label should have y=0 in default FlowLayout");
        // Now try with Change to RTL.  FlowLayout should respect RTL so that left padding/margin/align is interpreted
        // as the opposite.
        cnt.setRTL(true);
        lbl.setRTL(true);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "Label should be laid out with its preferred width in RTL");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "Label should be laid out with its preferred height in RTL");
        assertEqual(cntWidth - lbl.getWidth(), lbl.getX(), "Label should be aligned right default FlowLayout in RTL");
        assertEqual(0, lbl.getY(), "Label should have y=0 in default FlowLayout in RTL");
        // Now add left Margin to the label.  This should be applied to right side in RTL
        lbl.getStyle().setMarginLeft(10);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "LM10: Label should be laid out with its preferred width in RTL ");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "LM10: Label should be laid out with its preferred height in RTL");
        assertEqual(cntWidth - lbl.getWidth() - 10, lbl.getX(), "LM10: Label should be aligned right default FlowLayout in RTL");
        assertEqual(0, lbl.getY(), "LM10: Label should have y=0 in default FlowLayout in RTL");
        // Now change to LTR
        cnt.setRTL(false);
        lbl.setRTL(false);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "LM10: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "LM10: Label should be laid out with its preferred height");
        assertEqual(10, lbl.getX(), "LM10: Label should be aligned left with 10 offset default FlowLayout");
        assertEqual(0, lbl.getY(), "LM10: Label should have y=0 in default FlowLayout");
        // Now add left padding to the container.  In RTL this should be applied on right
        cnt.getStyle().setPaddingLeft(12);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "CP12,LM10: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "CP12,LM10: Label should be laid out with its preferred height");
        assertEqual(22, lbl.getX(), "CP12,LM10: Label should be aligned left with 22 offset default FlowLayout");
        assertEqual(0, lbl.getY(), "CP12,LM10: Label should have y=0 in default FlowLayout");
        // Now change back to RTL
        cnt.setRTL(true);
        lbl.setRTL(true);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "CP12,LM10: Label should be laid out with its preferred width in RTL");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "CP12,LM10: Label should be laid out with its preferred height in RTL");
        assertEqual(cntWidth - lbl.getWidth() - 22, lbl.getX(), "CP12,LM10: Label should be aligned right with 22 offset default FlowLayout in RTL");
        assertEqual(0, lbl.getY(), "CP12,LM10: Label should have y=0 in default FlowLayout in RTL");
        // Now change padding to right.  in RTL this is applied to left.  This shouldn't affect the layout at all
        cnt.getStyle().setPaddingLeft(0);
        cnt.getStyle().setPaddingRight(12);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "CP12,LM10: Label should be laid out with its preferred width in RTL");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "CP12,LM10: Label should be laid out with its preferred height in RTL");
        assertEqual(cntWidth - lbl.getWidth() - 10, lbl.getX(), "CP12,LM10: Label should be aligned left with 22 offset default FlowLayout in RTL");
        assertEqual(0, lbl.getY(), "CP12,LM10: Label should have y=0 in default FlowLayout in RTL");
        // Now in LTR
        cnt.setRTL(false);
        lbl.setRTL(false);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "CP12,LM10: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "CP12,LM10: Label should be laid out with its preferred height");
        assertEqual(10, lbl.getX(), "CP12,LM10: Label should be aligned left with 22 offset default FlowLayout");
        assertEqual(0, lbl.getY(), "CP12,LM10: Label should have y=0 in default FlowLayout");
        // Now add some top padding
        cnt.getStyle().setPaddingTop(5);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "TP5,CP12,LM10: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "TP5,CP12,LM10: Label should be laid out with its preferred height");
        assertEqual(10, lbl.getX(), "TP5,CP12,LM10: Label should be aligned left with 22 offset default FlowLayout");
        assertEqual(5, lbl.getY(), "TP5,CP12,LM10: Label should have y=5 in default FlowLayout");
        // And in RTL
        cnt.setRTL(true);
        lbl.setRTL(true);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "TP5,CP12,LM10: Label should be laid out with its preferred width in RTL");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "TP5,CP12,LM10: Label should be laid out with its preferred height in RTL");
        assertEqual(cntWidth - 10 - lbl.getWidth(), lbl.getX(), "TP5,CP12,LM10: Label should be aligned right with 10 offset default FlowLayout in RTL");
        assertEqual(5, lbl.getY(), "TP5,CP12,LM10: Label should have y=5 in default FlowLayout in RTL");
        // Now let's test center alignment with a single child.
        // All margins and padding back to ZERO and LTR
        $(cnt, lbl).selectAllStyles().setPadding(0).setMargin(0).setRTL(false);
        fl.setAlign(CENTER);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "LTR,Center: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "LTR Center: Label should be laid out with its preferred height");
        assertEqual((cntWidth - lbl.getWidth()) / 2, lbl.getX(), "LTR Center: Label should be aligned  center");
        assertEqual(0, lbl.getY(), "LTR Center: Label should have y=0 in default FlowLayout");
        // Now let's add some margin to the child, and padding to the parent
        // In this case, it should align center in the *inner* bounds of the container (i.e. inside the padding box).
        // But because the child has a left margin of 10, it should actually be 10 to the right of absolute center of this box.
        lbl.getStyle().setMarginLeft(10);
        cnt.getStyle().setPaddingLeft(5);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "LTR,P5,M10, Center: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "LTR,P5,M10, Center: Label should be laid out with its preferred height");
        assertEqual((cnt.getInnerWidth() - lbl.getOuterWidth()) / 2 + 15, lbl.getX(), "LTR,P5,M10, Center: Label should be aligned center");
        assertEqual(0, lbl.getY(), "LTR,P5,M10, Center: Label should have y=0 in default FlowLayout");
        // Now check RTL
        $(cnt, lbl).setRTL(true);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "RTL,P5,M10, Center: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "RTL,P5,M10, Center: Label should be laid out with its preferred height");
        // Expected value:  We subtract LTR expected value from the container width - which should take us to the right edge
        // of the label - so we additionally subtract the label width to take us to the left edge.
        assertEqual(cntWidth - ((cnt.getInnerWidth() - lbl.getOuterWidth()) / 2 + 15) - lbl.getWidth(), lbl.getX(), "RTL,P5,M10, Center: Label should be aligned center");
        assertEqual(0, lbl.getY(), "RTL,P5,M10, Center: Label should have y=0 in default FlowLayout");
        // Now let's test right alignment with a single child.
        fl.setAlign(RIGHT);
        $(cnt, lbl).selectAllStyles().setPadding(0).setMargin(0).setRTL(false);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "LTR,Right: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "LTR,Right: Label should be laid out with its preferred height");
        assertEqual(cntWidth - lbl.getWidth(), lbl.getX(), "LTR,Right: Label should be aligned right");
        assertEqual(0, lbl.getY(), "LTR,Right: Label should have y=0 in default FlowLayout");
        // Now in RTL
        $(cnt, lbl).setRTL(true);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "RTL,Right: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "RTL,Right: Label should be laid out with its preferred height");
        assertEqual(0, lbl.getX(), "RTL,Right: Label should be aligned right");
        assertEqual(0, lbl.getY(), "RTL,Right: Label should have y=0 in default FlowLayout");
        // Now add some padding to the left.  This should have no effect on this test because we are aligned right.
        cnt.getStyle().setPaddingLeft(10);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(0, lbl.getX(), "RTL,P10,Right: Label should be aligned right");
        $(lbl, cnt).setRTL(false);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(cntWidth - lbl.getWidth(), lbl.getX(), "LTR,Right: Label should be aligned right");
        // Now let's test vertical alignment
        $(cnt, lbl).selectAllStyles().setPadding(0).setMargin(0);
        fl.setValign(BOTTOM);
        $(cnt, lbl).setRTL(false);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(cntHeight - lbl.getHeight(), lbl.getY(), "Should be aligned bottom");
        // RTL should have no effect on the vertical alignment
        $(cnt, lbl).setRTL(true);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(cntHeight - lbl.getHeight(), lbl.getY(), "Should be aligned bottom");
        // Add some padding to the bottom
        cnt.getStyle().setPaddingBottom(10);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(cntHeight - lbl.getHeight() - 10, lbl.getY(), "Should be aligned bottom");
        // Add bottom margin to label.  This should effectively move it up.
        lbl.getStyle().setMarginBottom(12);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(cntHeight - lbl.getHeight() - 22, lbl.getY(), "Should be aligned bottom");
        // Vertical align center.
        fl.setValign(CENTER);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual((cnt.getInnerHeight() - lbl.getOuterHeight()) / 2, lbl.getY(), "Should be valigned middle");
        // Now valign by row.  With only a single component, this should cause the children to be rendered aligned TOP
        fl.setValignByRow(true);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(0, lbl.getY(), "Should be valigned top, when valignbyrow is true");
        // Check valign=BOTTOM now.  Should be same as center when aligning by row, since it is its own row.
        fl.setValign(BOTTOM);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(0, lbl.getY(), "Should be valigned top, when valignbyrow is true");
        // Baseline should be same as others since there is only a single child.
        fl.setValign(BASELINE);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(0, lbl.getY(), "Should be valigned top, when valignbyrow is true");
        // Now let's try laying out a 2nd child.
        Component spacer1 = createEmptyComponent(100, 100);
        $(spacer1, lbl, cnt).setPadding(0).setMargin(0).setRTL(false);
        cnt.add(spacer1);
        fl.setValign(TOP);
        fl.setAlign(LEFT);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(0, lbl.getY(), "Should be valigned top, when valignbyrow is true");
        assertEqual(0, spacer1.getY(), "Spacer should be aligned to top");
        assertEqual(0, lbl.getX(), "Label should be aligned to left edge");
        assertEqual(lbl.getWidth(), spacer1.getX(), "Spacer should be aligned to right edge of label");
        assertEqual(lbl.getPreferredW(), lbl.getWidth(), "Label should be rendered to its preferred width");
        assertEqual(lbl.getPreferredH(), lbl.getHeight(), "Lable should be rendered to its preferred height");
        assertEqual(100, spacer1.getWidth(), "Spacer should be its preferred width");
        assertEqual(100, spacer1.getHeight(), "Spacer should be its preferred height");
        // Let's try aligning bottom by row with the two of them.
        fl.setValign(BOTTOM);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        int rowH = Math.max(100, lbl.getOuterHeight());
        assertEqual(rowH - lbl.getHeight(), lbl.getY(), "Label should be aligned with the bottom of its row.");
        assertEqual(rowH - 100, spacer1.getY(), "Spacer shoudl be aligned with the bottom of its row");
        // Now let's valign center
        fl.setValign(CENTER);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual((rowH - lbl.getHeight()) / 2, lbl.getY(), "Label should be aligned with the bottom of its row.");
        assertEqual((rowH - 100) / 2, spacer1.getY(), "Spacer shoudl be aligned with the bottom of its row");
        // Now same thing with RTL
        $(cnt, lbl, spacer1).setRTL(true);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual((rowH - lbl.getHeight()) / 2, lbl.getY(), "Label should be aligned with the bottom of its row.");
        assertEqual((rowH - 100) / 2, spacer1.getY(), "Spacer shoudl be aligned with the bottom of its row");
        // Now align top left but still RTL.  Make sure everything is Kosher
        fl.setValign(TOP);
        fl.setAlign(LEFT);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(0, lbl.getY(), "Should be valigned top, when valignbyrow is true");
        assertEqual(0, spacer1.getY(), "Spacer should be aligned to top");
        assertEqual(cntWidth - lbl.getWidth(), lbl.getX(), "Label should be aligned to left edge");
        assertEqual(cntWidth - lbl.getWidth() - spacer1.getWidth(), spacer1.getX(), "Spacer should be aligned to right edge of label");
        assertEqual(lbl.getPreferredW(), lbl.getWidth(), "Label should be rendered to its preferred width");
        assertEqual(lbl.getPreferredH(), lbl.getHeight(), "Lable should be rendered to its preferred height");
        assertEqual(100, spacer1.getWidth(), "Spacer should be its preferred width");
        assertEqual(100, spacer1.getHeight(), "Spacer should be its preferred height");
    } finally {
        UIManager.getInstance().getLookAndFeel().setRTL(rtl);
    }
    return true;
}
Also used : FlowLayout(com.codename1.ui.layouts.FlowLayout)

Aggregations

Label (com.codename1.ui.Label)129 Form (com.codename1.ui.Form)95 Button (com.codename1.ui.Button)50 BorderLayout (com.codename1.ui.layouts.BorderLayout)50 Container (com.codename1.ui.Container)49 Component (com.codename1.ui.Component)27 SpanLabel (com.codename1.components.SpanLabel)26 Style (com.codename1.ui.plaf.Style)24 BoxLayout (com.codename1.ui.layouts.BoxLayout)19 TextArea (com.codename1.ui.TextArea)18 ActionEvent (com.codename1.ui.events.ActionEvent)18 IOException (java.io.IOException)18 Image (com.codename1.ui.Image)17 Dimension (com.codename1.ui.geom.Dimension)16 TextField (com.codename1.ui.TextField)15 ActionListener (com.codename1.ui.events.ActionListener)15 LayeredLayout (com.codename1.ui.layouts.LayeredLayout)15 Toolbar (com.codename1.ui.Toolbar)13 FlowLayout (com.codename1.ui.layouts.FlowLayout)12 EncodedImage (com.codename1.ui.EncodedImage)11