Search in sources :

Example 91 with Component

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

the class ComponentSelector method closest.

/**
 * Creates a new set of components consistng of all "closest" ancestors of components
 * in this set which match the provided selector.
 * @param selector The selector to use to match the nearest ancestor.
 * @return New set with ancestors of components in current set.
 */
public ComponentSelector closest(String selector) {
    ComponentSelector matcher = new ComponentSelector(selector, new Label());
    HashSet<Component> matches = new HashSet<Component>();
    for (Component c : this) {
        Component parent = c.getParent();
        while (parent != null) {
            if (matcher.match(parent)) {
                matches.add(parent);
                break;
            }
            parent = parent.getParent();
        }
    }
    return matcher.addAll(matches, true);
}
Also used : SpanLabel(com.codename1.components.SpanLabel) HashSet(java.util.HashSet)

Example 92 with Component

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

the class Container method wrapInLayeredPane.

/**
 * An atomic operation that wraps the current component in a Container with
 * a layered layout.  This prevents us from having to initialize and deinitialize
 * all of the components in a sub-tree because we want to re-root it.  In particular
 * Form.getLayeredPane() re-roots the entire content pane the first time it is
 * called on a form.  If the form contains native peers there is a flicker which
 * is quite annoying.  Providing a way to do this atomically results in a better
 * user experience.
 * @return The Container that is the new parent of this component.
 */
Container wrapInLayeredPane() {
    final Container oldParent = getParent();
    final Container newParent = new Container(new LayeredLayout());
    final Layout parentLayout = oldParent != null && oldParent.layout != null ? oldParent.layout : null;
    final Object constraint = parentLayout != null ? parentLayout.getComponentConstraint(this) : null;
    newParent.setParent(oldParent);
    newParent.components.add(this);
    final Runnable r = new Runnable() {

        public void run() {
            if (parentLayout != null) {
                parentLayout.removeLayoutComponent(Container.this);
                parentLayout.addLayoutComponent(constraint, newParent, oldParent);
            }
            newParent.initComponentImpl();
            if (oldParent != null) {
                int cmpIndex = -1;
                for (int i = 0; i < oldParent.getComponentCount(); i++) {
                    Component c = oldParent.getComponentAt(i);
                    if (c.equals(Container.this)) {
                        cmpIndex = i;
                        break;
                    }
                }
                // int cmpIndex = oldParent.getComponentIndex(Container.this);  <---  WTF... this always returns -1!!
                if (cmpIndex == -1) {
                    throw new RuntimeException("WTF we have parent but no index!!!!");
                }
                oldParent.components.set(cmpIndex, newParent);
            }
            Container.this.setParent(newParent);
            newParent.revalidate();
        }
    };
    AnimationManager a = getAnimationManager();
    if (a != null && a.isAnimating()) {
        a.addAnimation(new ComponentAnimation() {

            @Override
            public boolean isInProgress() {
                return false;
            }

            @Override
            protected void updateState() {
                r.run();
            }
        });
        return newParent;
    } else {
        r.run();
        return newParent;
    }
}
Also used : ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) Layout(com.codename1.ui.layouts.Layout) BoxLayout(com.codename1.ui.layouts.BoxLayout) FlowLayout(com.codename1.ui.layouts.FlowLayout) LayeredLayout(com.codename1.ui.layouts.LayeredLayout) BorderLayout(com.codename1.ui.layouts.BorderLayout) LayeredLayout(com.codename1.ui.layouts.LayeredLayout)

Example 93 with Component

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

the class Container method scrollComponentToVisible.

/**
 * Makes sure the component is visible in the scroll if this container is
 * scrollable
 *
 * @param c the component that will be scrolling for visibility
 */
public void scrollComponentToVisible(Component c) {
    if (isScrollable()) {
        if (c != null) {
            Rectangle r = c.getVisibleBounds();
            if (c.getParent() != null) {
                // special case for the first component to allow the user to scroll all the
                // way to the top
                Form f = getComponentForm();
                if (f != null && f.getInvisibleAreaUnderVKB() == 0 && f.findFirstFocusable() == c) {
                    // support this use case only if the component doesn't explicitly declare visible bounds
                    if (r == c.getBounds() && !Display.getInstance().isTouchScreenDevice()) {
                        scrollRectToVisible(new Rectangle(0, 0, c.getX() + Math.min(c.getWidth(), getWidth()), c.getY() + Math.min(c.getHeight(), getHeight())), this);
                        return;
                    }
                }
            }
            boolean moveToVisible = true;
            boolean large = c.getVisibleBounds().getSize().getHeight() > getHeight() || c.getVisibleBounds().getSize().getWidth() > getWidth();
            if (large) {
                int x = getScrollX();
                int y = getScrollY();
                int w = getWidth();
                int h = getHeight();
                boolean visible = contains(c) && Rectangle.intersects(c.getAbsoluteX(), c.getAbsoluteY(), c.getWidth(), c.getHeight(), getAbsoluteX() + x, getAbsoluteY() + y, w, h);
                // if this is a big component no need to scroll to the begining if it's
                // partially visible
                moveToVisible = !visible;
            }
            if (moveToVisible) {
                scrollRectToVisible(r.getX(), r.getY(), Math.min(r.getSize().getWidth(), getWidth()), Math.min(r.getSize().getHeight(), getHeight()), c);
            }
        }
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle)

Example 94 with Component

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

the class Container method paint.

/**
 * {@inheritDoc}
 */
public void paint(Graphics g) {
    if (enableLayoutOnPaint) {
        layoutContainer();
    }
    g.translate(getX(), getY());
    int size = components.size();
    int startIter = 0;
    if (size >= 30) {
        int clipX1 = g.getClipX();
        int clipX2 = g.getClipX() + g.getClipWidth();
        int clipY1 = g.getClipY();
        int clipY2 = g.getClipY() + g.getClipHeight();
        startIter = calculateFirstPaintableOffset(clipX1, clipY1, clipX2, clipY2);
        if (startIter < 0) {
            // There was no efficient way to calculate the offset
            startIter = 0;
        } else if (startIter < size) {
            // There was an efficient way to calculate the offset so we
            // will continue this approach
            size = calculateLastPaintableOffset(startIter, clipX1, clipY1, clipX2, clipY2) + 1;
        }
    }
    CodenameOneImplementation impl = Display.impl;
    if (dontRecurseContainer) {
        for (int iter = startIter; iter < size; iter++) {
            Component cmp = components.get(iter);
            if (cmp.getClass() == Container.class) {
                paintContainerChildrenForAnimation((Container) cmp, g);
            } else {
                cmp.paintInternal(impl.getComponentScreenGraphics(this, g), false);
            }
        }
    } else {
        for (int iter = startIter; iter < size; iter++) {
            Component cmp = components.get(iter);
            cmp.paintInternal(impl.getComponentScreenGraphics(this, g), false);
        }
    }
    int tx = g.getTranslateX();
    int ty = g.getTranslateY();
    g.translate(-tx, -ty);
    if (sidemenuBarTranslation > 0) {
        g.translate(sidemenuBarTranslation, 0);
        paintGlass(g);
        paintTensile(g);
        g.translate(-sidemenuBarTranslation, 0);
    } else {
        paintGlass(g);
        paintTensile(g);
    }
    g.translate(tx, ty);
    g.translate(-getX(), -getY());
}
Also used : CodenameOneImplementation(com.codename1.impl.CodenameOneImplementation)

Example 95 with Component

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

the class Container method isObscuredByChildren.

/**
 * Invoked internally to indicate if child components are hiding this container
 * thus removing the need to invoke its own paint methods
 * @return true if child components are obscuring this component
 */
boolean isObscuredByChildren() {
    if (!blockOverdraw) {
        return false;
    }
    if (!getLayout().obscuresPotential(this)) {
        return false;
    }
    Style s = getStyle();
    if (s.getPaddingTop() != 0 || s.getPaddingLeftNoRTL() != 0 || s.getPaddingRightNoRTL() != 0 || s.getPaddingBottom() != 0) {
        return false;
    }
    int size = components.size();
    for (int iter = 0; iter < size; iter++) {
        Component cmp = components.get(iter);
        s = cmp.getStyle();
        if (cmp.getWidth() == 0 || cmp.getHeight() == 0) {
            continue;
        }
        // need to think of a better way, this means we invoke the same logic recurisvely again and again by a factor of depth. Not good...
        if (cmp instanceof Container) {
            if (!((Container) cmp).getLayout().obscuresPotential(this)) {
                return false;
            }
            if (s.getOpacity() != 0xff || s.getMarginTop() != 0 || s.getMarginLeftNoRTL() != 0 || s.getMarginRightNoRTL() != 0 || s.getMarginBottom() != 0) {
                return false;
            }
            if ((s.getBgTransparency() & 0xff) != 0xff && !((Container) cmp).isObscuredByChildren()) {
                return false;
            }
        } else {
            if ((s.getBgTransparency() & 0xff) != 0xff || s.getOpacity() != 0xff || s.getMarginTop() != 0 || s.getMarginLeftNoRTL() != 0 || s.getMarginRightNoRTL() != 0 || s.getMarginBottom() != 0) {
                return false;
            }
        }
    }
    return true;
}
Also used : Style(com.codename1.ui.plaf.Style)

Aggregations

Component (com.codename1.ui.Component)152 Style (com.codename1.ui.plaf.Style)61 Container (com.codename1.ui.Container)55 Form (com.codename1.ui.Form)41 BorderLayout (com.codename1.ui.layouts.BorderLayout)40 TextArea (com.codename1.ui.TextArea)31 ActionEvent (com.codename1.ui.events.ActionEvent)28 Dimension (com.codename1.ui.geom.Dimension)28 Label (com.codename1.ui.Label)25 Point (com.codename1.ui.geom.Point)22 ArrayList (java.util.ArrayList)22 Rectangle (com.codename1.ui.geom.Rectangle)21 Vector (java.util.Vector)18 Button (com.codename1.ui.Button)16 Dialog (com.codename1.ui.Dialog)16 Hashtable (java.util.Hashtable)16 Image (com.codename1.ui.Image)15 TextField (com.codename1.ui.TextField)15 ActionListener (com.codename1.ui.events.ActionListener)13 BoxLayout (com.codename1.ui.layouts.BoxLayout)13