Search in sources :

Example 16 with Label

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

the class CSSEngine method evalContentExpression.

/**
 * Evaluates a CSS content property expression and returns the matching label component
 *
 * @param htmlC The HTMLComponent
 * @param exp The expression to evaluate
 * @param element The element this content property
 * @param selector The CSS selector that includes the content property (mainly for error messages)
 * @return A label representing the evaluated expression or null if not found
 */
private Label evalContentExpression(HTMLComponent htmlC, String exp, HTMLElement element, CSSElement selector) {
    if (exp.length() != 0) {
        if (exp.startsWith("counter(")) {
            exp = exp.substring(8);
            int index = exp.indexOf(")");
            if (index != -1) {
                return new Label("" + htmlC.getCounterValue(exp.substring(0, index)));
            }
        } else if (exp.startsWith("attr(")) {
            exp = exp.substring(5);
            int index = exp.indexOf(")");
            if (index != -1) {
                String attr = exp.substring(0, index);
                String attrValue = element.getAttribute(attr);
                return new Label(attrValue == null ? "" : attrValue);
            }
        } else if (exp.equals("open-quote")) {
            return getQuote(true);
        } else if (exp.equals("close-quote")) {
            return getQuote(false);
        } else if (exp.startsWith("url(")) {
            String url = getCSSUrl(exp);
            Label imgLabel = new Label();
            if (htmlC.showImages) {
                if (htmlC.getDocumentInfo() != null) {
                    htmlC.getThreadQueue().add(imgLabel, htmlC.convertURL(url));
                } else {
                    if (DocumentInfo.isAbsoluteURL(url)) {
                        htmlC.getThreadQueue().add(imgLabel, url);
                    } else {
                        if (htmlC.getHTMLCallback() != null) {
                            htmlC.getHTMLCallback().parsingError(HTMLCallback.ERROR_NO_BASE_URL, selector.getTagName(), selector.getAttributeName(new Integer(CSSElement.CSS_CONTENT)), url, "Ignoring image file referred in a CSS file/segment (" + url + "), since page was set by setBody/setHTML/setDOM so there's no way to access relative URLs");
                        }
                    }
                }
            }
            return imgLabel;
        }
    }
    return null;
}
Also used : Label(com.codename1.ui.Label)

Example 17 with Label

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

the class CSSEngine method handleContentProperty.

/**
 * Handles a CSS content property
 *
 * @param element The element this content property
 * @param selector The CSS selector that includes the content property
 * @param htmlC The HTMLComponent
 */
private void handleContentProperty(HTMLElement element, CSSElement selector, HTMLComponent htmlC) {
    boolean after = ((selector.getSelectorPseudoClass() & CSSElement.PC_AFTER) != 0);
    String content = selector.getAttributeById(CSSElement.CSS_CONTENT);
    if (content != null) {
        // if there's no content, we don't add anything
        Component cmp = after ? (Component) element.getUi().lastElement() : (Component) element.getUi().firstElement();
        Component styleCmp = cmp;
        Container parent = null;
        int pos = 0;
        if (cmp instanceof Container) {
            parent = ((Container) cmp);
            while ((parent.getComponentCount() > 0) && (parent.getComponentAt(after ? parent.getComponentCount() - 1 : 0) instanceof Container)) {
                // find the actual content
                parent = (Container) parent.getComponentAt(after ? parent.getComponentCount() - 1 : 0);
            }
            if (parent.getComponentCount() > 0) {
                pos = after ? parent.getComponentCount() - 1 : 0;
                styleCmp = parent.getComponentAt(pos);
            }
        } else {
            parent = cmp.getParent();
            pos = cmp.getParent().getComponentIndex(cmp);
        }
        if (after) {
            pos++;
        }
        int initPos = pos;
        String str = "";
        // to make sure the last expression is evaluated, note that this will not print an extra space in any case, since it is out of the quotes if any
        content = content + " ";
        boolean segment = false;
        for (int i = 0; i < content.length(); i++) {
            char c = content.charAt(i);
            Label lbl = null;
            if (c == '"') {
                segment = !segment;
                if ((!segment) && (str.length() > 0)) {
                    lbl = new Label(str);
                    str = "";
                }
            } else if (CSSParser.isWhiteSpace(c)) {
                if (segment) {
                    str += c;
                    lbl = new Label(str);
                } else if (str.length() > 0) {
                    lbl = evalContentExpression(htmlC, str, element, selector);
                    if (lbl == null) {
                        // if we didn't find a match we search for the following expressions which are used to remove added content
                        int removeQuoteType = -1;
                        boolean removeAll = false;
                        if ((str.equals("none")) || (str.equals("normal"))) {
                            // normal/none means remove all content
                            removeAll = true;
                        } else if (str.equals("no-open-quote")) {
                            // 0 is the quote type for open quote, 1 for closed one
                            removeQuoteType = 0;
                        } else if (str.equals("no-close-quote")) {
                            removeQuoteType = 1;
                        }
                        if ((removeAll) || (removeQuoteType != -1)) {
                            Vector v = element.getUi();
                            if (v != null) {
                                Vector toRemove = new Vector();
                                for (Enumeration e = v.elements(); e.hasMoreElements(); ) {
                                    Component ui = (Component) e.nextElement();
                                    String conStr = (String) ui.getClientProperty(CLIENT_PROPERTY_CSS_CONTENT);
                                    if ((conStr != null) && (((after) && (conStr.equals("a"))) || ((!after) && (conStr.equals("b"))))) {
                                        boolean remove = true;
                                        if (removeQuoteType != -1) {
                                            Object obj = ui.getClientProperty(HTMLComponent.CLIENT_PROPERTY_QUOTE);
                                            if (obj != null) {
                                                int quoteType = ((Integer) obj).intValue();
                                                remove = (quoteType == removeQuoteType);
                                            } else {
                                                remove = false;
                                            }
                                        }
                                        if (remove) {
                                            parent.removeComponent(ui);
                                            toRemove.addElement(ui);
                                        }
                                    }
                                }
                                for (Enumeration e = toRemove.elements(); e.hasMoreElements(); ) {
                                    v.removeElement(e.nextElement());
                                }
                            }
                            // stop processing after removal clauses such as none/normal
                            return;
                        }
                    }
                }
                str = "";
            } else {
                str += c;
            }
            if (lbl != null) {
                if (after) {
                    element.addAssociatedComponent(lbl);
                } else {
                    element.addAssociatedComponentAt(pos - initPos, lbl);
                }
                lbl.setUnselectedStyle(new Style(styleCmp.getUnselectedStyle()));
                lbl.putClientProperty(CLIENT_PROPERTY_CSS_CONTENT, after ? "a" : "b");
                if (parent.getComponentCount() == 0) {
                    parent.addComponent(lbl);
                } else {
                    parent.addComponent(pos, lbl);
                }
                pos++;
                applyStyleToUIElement(lbl, selector, element, htmlC);
            }
        }
    }
}
Also used : Container(com.codename1.ui.Container) Enumeration(java.util.Enumeration) Label(com.codename1.ui.Label) Style(com.codename1.ui.plaf.Style) Component(com.codename1.ui.Component) Vector(java.util.Vector)

Example 18 with Label

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

the class Dialog method show.

/**
 * Shows a modal dialog with the given component as its "body" placed in the
 * center.
 *
 * @param title title for the dialog
 * @param body component placed in the center of the dialog
 * @param defaultCommand command to be assigned as the default command or null
 * @param cmds commands that are added to the form any click on any command
 * will dispose the form
 * @param type the type of the alert one of TYPE_WARNING, TYPE_INFO,
 * TYPE_ERROR, TYPE_CONFIRMATION or TYPE_ALARM
 * @param icon the icon for the dialog, can be null
 * @param timeout a timeout after which null would be returned if timeout is 0 inifinite time is used
 * @param transition the transition installed when the dialog enters/leaves
 * @return the command pressed by the user
 */
public static Command show(String title, Component body, Command defaultCommand, Command[] cmds, int type, Image icon, long timeout, Transition transition) {
    Dialog dialog = new Dialog(title);
    dialog.dialogType = type;
    dialog.setTransitionInAnimator(transition);
    dialog.setTransitionOutAnimator(transition);
    dialog.lastCommandPressed = null;
    dialog.setLayout(new BorderLayout());
    if (cmds != null) {
        if (commandsAsButtons) {
            dialog.placeButtonCommands(cmds);
        } else {
            for (int iter = 0; iter < cmds.length; iter++) {
                dialog.addCommand(cmds[iter]);
            }
        }
        // maps the first command to back
        if (cmds.length == 1 || cmds.length == 2) {
            dialog.setBackCommand(cmds[0]);
        }
    }
    if (defaultCommand != null) {
        dialog.setDefaultCommand(defaultCommand);
    }
    dialog.addComponent(BorderLayout.CENTER, body);
    if (icon != null) {
        dialog.addComponent(BorderLayout.EAST, new Label(icon));
    }
    if (timeout != 0) {
        dialog.setTimeout(timeout);
    }
    if (body.isScrollable() || disableStaticDialogScrolling) {
        dialog.setScrollable(false);
    }
    dialog.show();
    return dialog.lastCommandPressed;
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout)

Example 19 with Label

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

the class Dialog method showPopupDialog.

/**
 * A popup dialog is shown with the context of a component and  its selection, it is disposed seamlessly if the back button is pressed
 * or if the user touches outside its bounds. It can optionally provide an arrow in the theme to point at the context component. The popup
 * dialog has the PopupDialog style by default.
 *
 * @param rect the screen rectangle to which the popup should point
 * @return the command that might have been triggered by the user within the dialog if commands are placed in the dialog
 */
public Command showPopupDialog(Rectangle rect) {
    if (getDialogUIID().equals("Dialog")) {
        setDialogUIID("PopupDialog");
        if (getTitleComponent().getUIID().equals("DialogTitle")) {
            getTitleComponent().setUIID("PopupDialogTitle");
        }
        getContentPane().setUIID("PopupContentPane");
    }
    disposeOnRotation = true;
    disposeWhenPointerOutOfBounds = true;
    Command backCommand = null;
    if (getBackCommand() == null) {
        backCommand = new Command("Back");
        setBackCommand(backCommand);
    }
    Component contentPane = super.getContentPane();
    Label title = super.getTitleComponent();
    int menuHeight = calcMenuHeight();
    UIManager manager = getUIManager();
    // preferred size logic of the dialog won't work with large title borders
    if (dialogTitle != null && manager.isThemeConstant("hideEmptyTitleBool", false)) {
        boolean b = getTitle().length() > 0;
        getTitleArea().setVisible(b);
        getTitleComponent().setVisible(b);
        if (!b && manager.isThemeConstant("shrinkPopupTitleBool", true)) {
            getTitleComponent().setPreferredSize(new Dimension(0, 0));
            getTitleComponent().getStyle().setBorder(null);
            getTitleArea().setPreferredSize(new Dimension(0, 0));
            if (getContentPane().getClientProperty("$ENLARGED_POP") == null) {
                getContentPane().putClientProperty("$ENLARGED_POP", Boolean.TRUE);
                int cpPaddingTop = getContentPane().getStyle().getPaddingTop();
                int titlePT = getTitleComponent().getStyle().getPaddingTop();
                byte[] pu = getContentPane().getStyle().getPaddingUnit();
                if (pu == null) {
                    pu = new byte[4];
                }
                pu[0] = Style.UNIT_TYPE_PIXELS;
                getContentPane().getStyle().setPaddingUnit(pu);
                int pop = Display.getInstance().convertToPixels(manager.getThemeConstant("popupNoTitleAddPaddingInt", 1), false);
                getContentPane().getStyle().setPadding(TOP, pop + cpPaddingTop + titlePT);
            }
        }
    }
    // allows a text area to recalculate its preferred size if embedded within a dialog
    revalidate();
    Style contentPaneStyle = getDialogStyle();
    boolean restoreArrow = false;
    if (manager.isThemeConstant(getDialogUIID() + "ArrowBool", false)) {
        Image t = manager.getThemeImageConstant(getDialogUIID() + "ArrowTopImage");
        Image b = manager.getThemeImageConstant(getDialogUIID() + "ArrowBottomImage");
        Image l = manager.getThemeImageConstant(getDialogUIID() + "ArrowLeftImage");
        Image r = manager.getThemeImageConstant(getDialogUIID() + "ArrowRightImage");
        Border border = contentPaneStyle.getBorder();
        if (border != null) {
            border.setImageBorderSpecialTile(t, b, l, r, rect);
            restoreArrow = true;
        }
    } else {
        Border border = contentPaneStyle.getBorder();
        if (border != null) {
            border.setTrackComponent(rect);
        }
    }
    int prefHeight = contentPane.getPreferredH();
    int prefWidth = contentPane.getPreferredW();
    if (contentPaneStyle.getBorder() != null) {
        prefWidth = Math.max(contentPaneStyle.getBorder().getMinimumWidth(), prefWidth);
        prefHeight = Math.max(contentPaneStyle.getBorder().getMinimumHeight(), prefHeight);
    }
    prefWidth += getUIManager().getLookAndFeel().getVerticalScrollWidth();
    int availableHeight = Display.getInstance().getDisplayHeight() - menuHeight - title.getPreferredH();
    int availableWidth = Display.getInstance().getDisplayWidth();
    int width = Math.min(availableWidth, prefWidth);
    int x = 0;
    int y = 0;
    Command result;
    boolean showPortrait;
    if (popupDirectionBiasPortrait != null) {
        showPortrait = popupDirectionBiasPortrait.booleanValue();
    } else {
        showPortrait = Display.getInstance().isPortrait();
    }
    // if we don't have enough space then disregard device orientation
    if (showPortrait) {
        if (availableHeight < (availableWidth - rect.getWidth()) / 2) {
            showPortrait = false;
        }
    } else {
        if (availableHeight / 2 > availableWidth - rect.getWidth()) {
            showPortrait = true;
        }
    }
    if (showPortrait) {
        if (width < availableWidth) {
            int idealX = rect.getX() - width / 2 + rect.getSize().getWidth() / 2;
            // if the ideal position is less than 0 just use 0
            if (idealX > 0) {
                // if the idealX is too far to the right just align to the right
                if (idealX + width > availableWidth) {
                    x = availableWidth - width;
                } else {
                    x = idealX;
                }
            }
        }
        if (rect.getY() < availableHeight / 2) {
            // popup downwards
            if (getDialogUIID().equals("PopupDialog") && isUIIDByPopupPosition) {
                getContentPane().setUIID("PopupContentPaneDownwards");
            }
            y = rect.getY() + rect.getSize().getHeight();
            int height = Math.min(prefHeight, availableHeight - y);
            result = show(y, availableHeight - height - y, x, availableWidth - width - x, true, true);
        } else {
            // popup upwards
            if (getDialogUIID().equals("PopupDialog") && isUIIDByPopupPosition) {
                getContentPane().setUIID("PopupContentPaneUpwards");
            }
            int height = Math.min(prefHeight, availableHeight - (availableHeight - rect.getY()));
            y = rect.getY() - height;
            result = show(y, availableHeight - height - y, x, availableWidth - width - x, true, true);
        }
    } else {
        int height = Math.min(prefHeight, availableHeight);
        if (height < availableHeight) {
            int idealY = rect.getY() - height / 2 + rect.getSize().getHeight() / 2;
            // if the ideal position is less than 0 just use 0
            if (idealY > 0) {
                // if the idealY is too far up just align to the top
                if (idealY + height > availableHeight) {
                    y = availableHeight - height;
                } else {
                    y = idealY;
                }
            }
        }
        if (prefWidth > rect.getX()) {
            // popup right
            if (getDialogUIID().equals("PopupDialog") && isUIIDByPopupPosition) {
                getContentPane().setUIID("PopupContentPaneRight");
            }
            x = rect.getX() + rect.getSize().getWidth();
            if (x + prefWidth > availableWidth) {
                x = availableWidth - prefWidth;
            }
            width = Math.min(prefWidth, availableWidth - x);
            result = show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x), true, true);
        } else {
            // popup left
            if (getDialogUIID().equals("PopupDialog") && isUIIDByPopupPosition) {
                getContentPane().setUIID("PopupContentPaneLeft");
            }
            width = Math.min(prefWidth, availableWidth - (availableWidth - rect.getX()));
            x = rect.getX() - width;
            result = show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x), true, true);
        }
    }
    if (restoreArrow) {
        contentPaneStyle.getBorder().clearImageBorderSpecialTile();
    }
    if (result == backCommand) {
        return null;
    }
    return result;
}
Also used : UIManager(com.codename1.ui.plaf.UIManager) Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension) Border(com.codename1.ui.plaf.Border)

Example 20 with Label

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

the class MorphTransition method initTransition.

/**
 * {@inheritDoc}
 */
public final void initTransition() {
    animationMotion = Motion.createEaseInOutMotion(0, 255, duration);
    animationMotion.start();
    Container s = (Container) getSource();
    Container d = (Container) getDestination();
    Iterator<String> keyIterator = fromTo.keySet().iterator();
    int size = fromTo.size();
    fromToComponents = new CC[size];
    Form destForm = d.getComponentForm();
    destForm.forceRevalidate();
    Form sourceForm = s.getComponentForm();
    for (int iter = 0; iter < size; iter++) {
        String k = keyIterator.next();
        String v = fromTo.get(k);
        Component sourceCmp = findByName(s, k);
        Component destCmp = findByName(d, v);
        if (sourceCmp == null || destCmp == null) {
            continue;
        }
        CC cc = new CC(sourceCmp, destCmp, sourceForm, destForm);
        fromToComponents[iter] = cc;
        cc.placeholderDest = new Label();
        cc.placeholderDest.setVisible(false);
        Container destParent = cc.dest.getParent();
        cc.placeholderDest.setX(cc.dest.getX());
        cc.placeholderDest.setY(cc.dest.getY() - destForm.getContentPane().getY());
        cc.placeholderDest.setWidth(cc.dest.getWidth());
        cc.placeholderDest.setHeight(cc.dest.getHeight());
        cc.placeholderDest.setPreferredSize(new Dimension(cc.dest.getWidth(), cc.dest.getHeight()));
        destParent.replace(cc.dest, cc.placeholderDest, null);
        destForm.getLayeredPane().addComponent(cc.dest);
        cc.placeholderSrc = new Label();
        cc.placeholderSrc.setVisible(false);
        cc.placeholderSrc.setX(cc.source.getX());
        cc.placeholderSrc.setY(cc.source.getY() - sourceForm.getContentPane().getY());
        cc.placeholderSrc.setWidth(cc.source.getWidth());
        cc.placeholderSrc.setHeight(cc.source.getHeight());
        cc.placeholderSrc.setPreferredSize(new Dimension(cc.source.getWidth(), cc.source.getHeight()));
        cc.originalContainer = cc.source.getParent();
        cc.originalConstraint = cc.originalContainer.getLayout().getComponentConstraint(cc.source);
        cc.originalOffset = cc.originalContainer.getComponentIndex(cc.source);
        cc.originalContainer.replace(cc.source, cc.placeholderSrc, null);
        cc.originalContainer.getComponentForm().getLayeredPane().addComponent(cc.source);
    }
}
Also used : Container(com.codename1.ui.Container) Form(com.codename1.ui.Form) Label(com.codename1.ui.Label) Dimension(com.codename1.ui.geom.Dimension) Component(com.codename1.ui.Component)

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