Search in sources :

Example 11 with Element

use of com.codename1.xml.Element in project CodenameOne by codenameone.

the class CSSEngine method setNowrapText.

/**
 * Replaces a wrapped text with an unwrapped version.
 * This in fact removes all the labels that contains a single word each, and replaces them with one label that contains the whole text.
 * This way the label is not wrapped.
 *
 * @param label The first label of this text element (can be derived from ui but already fetched before the call)
 * @param ui The vector consisting all components (labels) that contain the text's words
 * @param newText The new text that should replace current components (unwrapped text without extra white spaces)
 * @param element The text element
 */
private void setNowrapText(Label label, Vector ui, String newText, HTMLElement element) {
    label.setText(newText);
    for (int i = 1; i < ui.size(); i++) {
        Component cmp = (Component) ui.elementAt(i);
        cmp.getParent().removeComponent(cmp);
    }
    if (label instanceof HTMLLink) {
        // Reset all associated link fragments so we don't have unneeded references
        ((HTMLLink) label).childLinks = new Vector();
    }
    element.setAssociatedComponents(label);
    label.getParent().revalidate();
}
Also used : Component(com.codename1.ui.Component) Vector(java.util.Vector)

Example 12 with Element

use of com.codename1.xml.Element 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 13 with Element

use of com.codename1.xml.Element in project CodenameOne by codenameone.

the class HTMLEventsListener method registerComponent.

/**
 * Registeres the specified component/element duo to listen to all available events
 *
 * @param cmp The actual component
 * @param element The element representing the component
 */
void registerComponent(final Component cmp, final HTMLElement element) {
    comps.put(cmp, element);
    cmp.addFocusListener(this);
    if (cmp instanceof Button) {
        // catches Button, CheckBox, RadioButton
        ((Button) cmp).addActionListener(this);
    } else if (cmp instanceof List) {
        // catches ComboBox
        final List list = (List) cmp;
        list.addActionListener(this);
        SelectionListener sl = new // We create a listener and not listen ourself since the listener's method does not pass the event origin, so we need to make one listener per component
        SelectionListener() {

            public void selectionChanged(int oldSelected, int newSelected) {
                if (htmlC.getHTMLCallback() != null) {
                    htmlC.getHTMLCallback().selectionChanged(oldSelected, newSelected, htmlC, list, element);
                }
            }
        };
        list.addSelectionListener(sl);
        listeners.put(cmp, sl);
    } else if (cmp instanceof TextArea) {
        ((TextArea) cmp).addActionListener(this);
        if (cmp instanceof TextField) {
            final TextField tf = (TextField) cmp;
            DataChangedListener dcl = new // We create a listener and not listen ourself since the listener's method does not pass the event origin, so we need to make one listener per component
            DataChangedListener() {

                public void dataChanged(int type, int index) {
                    element.setAttributeById(HTMLElement.ATTR_VALUE, tf.getText());
                    if (htmlC.getHTMLCallback() != null) {
                        htmlC.getHTMLCallback().dataChanged(type, index, htmlC, tf, element);
                    }
                }
            };
            tf.addDataChangedListener(dcl);
            listeners.put(cmp, dcl);
        }
    }
}
Also used : RadioButton(com.codename1.ui.RadioButton) Button(com.codename1.ui.Button) TextArea(com.codename1.ui.TextArea) TextField(com.codename1.ui.TextField) DataChangedListener(com.codename1.ui.events.DataChangedListener) List(com.codename1.ui.List) SelectionListener(com.codename1.ui.events.SelectionListener)

Example 14 with Element

use of com.codename1.xml.Element in project CodenameOne by codenameone.

the class GridBagLayoutInfo method updateParentInfo.

private void updateParentInfo(Container parent) {
    int count = parent.getComponentCount();
    for (int iter = 0; iter < count; iter++) {
        Component element = parent.getComponentAt(iter);
        GridBagConstraints gbc = comptable.get(element);
        updateParentInfo(parent, gbc);
    }
}
Also used : Component(com.codename1.ui.Component)

Example 15 with Element

use of com.codename1.xml.Element in project CodenameOne by codenameone.

the class CodenameOneImplementation method repaint.

/**
 * Invoked to add an element to the paintQueue
 *
 * @param cmp component or animation to push into the paint queue
 */
public void repaint(Animation cmp) {
    synchronized (displayLock) {
        for (int iter = 0; iter < paintQueueFill; iter++) {
            Animation ani = paintQueue[iter];
            if (ani == cmp) {
                return;
            }
            // no need to paint a Component if one of its parent is already in the queue
            if (ani instanceof Container && cmp instanceof Component) {
                Component parent = ((Component) cmp).getParent();
                while (parent != null) {
                    if (parent == ani) {
                        return;
                    }
                    parent = parent.getParent();
                }
            }
        }
        // overcrowding the queue don't try to grow the array!
        if (paintQueueFill >= paintQueue.length) {
            System.out.println("Warning paint queue size exceeded, please watch the amount of repaint calls");
            return;
        }
        paintQueue[paintQueueFill] = cmp;
        paintQueueFill++;
        displayLock.notify();
    }
}
Also used : Animation(com.codename1.ui.animations.Animation)

Aggregations

Container (com.codename1.ui.Container)9 Label (com.codename1.ui.Label)9 Vector (java.util.Vector)9 Component (com.codename1.ui.Component)8 Button (com.codename1.ui.Button)5 Style (com.codename1.ui.plaf.Style)5 RadioButton (com.codename1.ui.RadioButton)4 TextArea (com.codename1.ui.TextArea)4 BorderLayout (com.codename1.ui.layouts.BorderLayout)4 BoxLayout (com.codename1.ui.layouts.BoxLayout)4 Border (com.codename1.ui.plaf.Border)4 Dimension (com.codename1.ui.geom.Dimension)3 EditableResources (com.codename1.ui.util.EditableResources)3 Hashtable (java.util.Hashtable)3 TextField (com.codename1.ui.TextField)2 Rectangle (com.codename1.ui.geom.Rectangle)2 CharArrayReader (com.codename1.io.CharArrayReader)1 NetworkEvent (com.codename1.io.NetworkEvent)1 CheckBox (com.codename1.ui.CheckBox)1 Command (com.codename1.ui.Command)1