Search in sources :

Example 6 with ComponentSelector

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

the class ComponentSelector method fadeOut.

/**
 * Fades out components in this set.
 * @param duration Duration of animation.
 * @param callback Callback to run when animation completes.
 * @return Self for chaining.
 */
public ComponentSelector fadeOut(int duration, final SuccessCallback<ComponentSelector> callback) {
    final String placeholderProperty = "com.codename1.ui.ComponentSelector#fadeOutPlaceholder";
    AnimationManager mgr = null;
    ArrayList<ComponentAnimation> animations = new ArrayList<ComponentAnimation>();
    final ArrayList<Component> animatingComponents = new ArrayList<Component>();
    for (Component c : this) {
        Container parent = c.getParent();
        if (parent != null) {
            AnimationManager cmgr = c.getAnimationManager();
            if (cmgr != null) {
                mgr = cmgr;
                Container placeholder = new Container();
                // placeholder.setShowEvenIfBlank(true);
                c.putClientProperty(placeholderProperty, placeholder);
                Component.setSameHeight(placeholder, c);
                Component.setSameWidth(placeholder, c);
                $(placeholder).setMargin(c.getStyle().getMarginTop(), c.getStyle().getMarginRight(false), c.getStyle().getMarginBottom(), c.getStyle().getMarginLeft(false)).setPadding(c.getStyle().getPaddingTop(), c.getStyle().getPaddingRight(false), c.getStyle().getPaddingBottom(), c.getStyle().getPaddingLeft(false));
                ComponentAnimation a = parent.createReplaceTransition(c, placeholder, CommonTransitions.createFade(duration));
                animations.add(a);
                animatingComponents.add(c);
            }
        // centerBackground.add(BorderLayout.CENTER, boxy);
        }
    }
    if (mgr != null) {
        mgr.addAnimation(ComponentAnimation.compoundAnimation(animations.toArray(new ComponentAnimation[animations.size()])), new Runnable() {

            public void run() {
                for (final Component c : animatingComponents) {
                    // c.setHidden(true);
                    c.setVisible(false);
                    final Container placeholder = (Container) c.getClientProperty(placeholderProperty);
                    c.putClientProperty(placeholderProperty, null);
                    if (placeholder != null) {
                        Container parent = placeholder.getParent();
                        /*
                            if (parent == null) {
                                System.out.println("Deferring replace back");
                                $(new Runnable() {
                                    public void run() {
                                        Container parent = placeholder.getParent();
                                        if (parent != null) {
                                            System.out.println("Found parent after deferral");
                                            parent.replace(placeholder, c, CommonTransitions.createEmpty());
                                        }
                                    }
                                });
                            } else {
                            */
                        if (parent != null) {
                            parent.replace(placeholder, c, CommonTransitions.createEmpty());
                        }
                    // }
                    }
                }
                if (callback != null) {
                    callback.onSucess(ComponentSelector.this);
                }
            }
        });
    }
    return this;
}
Also used : ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) ArrayList(java.util.ArrayList)

Example 7 with ComponentSelector

use of com.codename1.ui.ComponentSelector 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());
    LinkedHashSet<Component> matches = new LinkedHashSet<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 : LinkedHashSet(java.util.LinkedHashSet) SpanLabel(com.codename1.components.SpanLabel)

Example 8 with ComponentSelector

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

the class ComponentSelector method fadeOutAndWait.

/**
 * Hide the matched elements by fading them to transparent. Blocks thread until animation is complete.
 * @param duration Duration of animation.
 * @return Self for chaining.
 */
public ComponentSelector fadeOutAndWait(int duration) {
    final String placeholderProperty = "com.codename1.ui.ComponentSelector#fadeOutPlaceholder";
    AnimationManager mgr = null;
    ArrayList<ComponentAnimation> animations = new ArrayList<ComponentAnimation>();
    final ArrayList<Component> animatingComponents = new ArrayList<Component>();
    for (Component c : this) {
        Container parent = c.getParent();
        if (parent != null) {
            AnimationManager cmgr = c.getAnimationManager();
            if (cmgr != null) {
                mgr = cmgr;
                Container placeholder = new Container();
                // placeholder.setShowEvenIfBlank(true);
                c.putClientProperty(placeholderProperty, placeholder);
                Component.setSameHeight(placeholder, c);
                Component.setSameWidth(placeholder, c);
                $(placeholder).setMargin(c.getStyle().getMarginTop(), c.getStyle().getMarginRight(false), c.getStyle().getMarginBottom(), c.getStyle().getMarginLeft(false)).setPadding(c.getStyle().getPaddingTop(), c.getStyle().getPaddingRight(false), c.getStyle().getPaddingBottom(), c.getStyle().getPaddingLeft(false));
                ComponentAnimation a = parent.createReplaceTransition(c, placeholder, CommonTransitions.createFade(duration));
                animations.add(a);
                animatingComponents.add(c);
            }
        // centerBackground.add(BorderLayout.CENTER, boxy);
        }
    }
    if (mgr != null) {
        mgr.addAnimationAndBlock(ComponentAnimation.compoundAnimation(animations.toArray(new ComponentAnimation[animations.size()])));
        for (final Component c : animatingComponents) {
            c.setVisible(false);
            final Container placeholder = (Container) c.getClientProperty(placeholderProperty);
            c.putClientProperty(placeholderProperty, null);
            if (placeholder != null) {
                Container parent = placeholder.getParent();
                if (parent != null) {
                    parent.replace(placeholder, c, CommonTransitions.createEmpty());
                }
            }
        }
    }
    return this;
}
Also used : ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) ArrayList(java.util.ArrayList)

Example 9 with ComponentSelector

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

the class ComponentSelector method setMarginPercent.

/**
 * Sets margin on all components in found set as a percentage of their respective parents' dimensions.
 * @param top Top margin as percentage of parent height.
 * @param right Right margin as percentage of parent width.
 * @param bottom Bottom margin as percentage of parent height.
 * @param left Left margin as percentage of parent width.
 * @return
 */
public ComponentSelector setMarginPercent(double top, double right, double bottom, double left) {
    for (Component c : this) {
        Component parent = c.getParent();
        if (parent != null) {
            Style s = getStyle(c);
            s.setMarginUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS);
            s.setMargin(percentHeight(top, parent), percentHeight(bottom, parent), percentWidth(left, parent), percentWidth(right, parent));
        }
    }
    return this;
}
Also used : Style(com.codename1.ui.plaf.Style)

Example 10 with ComponentSelector

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

the class ComponentSelector method setFontSizePercent.

/**
 * Sets the fonts size of all components in the found set as a percentage of the font
 * size of the components' respective parents.
 * @param sizePercentage Font size as a percentage of parent font size.
 * @return
 */
public ComponentSelector setFontSizePercent(double sizePercentage) {
    for (Component c : this) {
        Component parent = c.getParent();
        if (parent != null) {
            float size = (float) (getEffectiveFontSize(parent) * sizePercentage / 100.0);
            Style style = getStyle(c);
            Font curr = style.getFont();
            if (curr == null || !curr.isTTFNativeFont()) {
                curr = c.getStyle().getFont();
            }
            if (curr == null || !curr.isTTFNativeFont()) {
                parent = c.getParent();
                while (parent != null && (curr == null || !curr.isTTFNativeFont())) {
                    curr = parent.getStyle().getFont();
                    parent = parent.getParent();
                }
            }
            if (curr == null || !curr.isTTFNativeFont()) {
                curr = Font.create("native:MainRegular");
            }
            if (curr != null && curr.isTTFNativeFont()) {
                curr = curr.derive(size, 0);
                style.setFont(curr);
            }
        }
    }
    return this;
}
Also used : Style(com.codename1.ui.plaf.Style)

Aggregations

Style (com.codename1.ui.plaf.Style)10 SpanLabel (com.codename1.components.SpanLabel)6 ComponentAnimation (com.codename1.ui.animations.ComponentAnimation)5 ArrayList (java.util.ArrayList)5 LinkedHashSet (java.util.LinkedHashSet)4 SpanButton (com.codename1.components.SpanButton)2 BorderLayout (com.codename1.ui.layouts.BorderLayout)2 CollapsibleHeaderContainer (ca.weblite.shared.components.CollapsibleHeaderContainer)1 Button (com.codename1.ui.Button)1 Component (com.codename1.ui.Component)1 ComponentSelector (com.codename1.ui.ComponentSelector)1 Container (com.codename1.ui.Container)1 Form (com.codename1.ui.Form)1 Label (com.codename1.ui.Label)1