Search in sources :

Example 6 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());
    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 7 with ComponentSelector.$

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

the class ComponentSelector method fadeIn.

/**
 * Fade in this set of components.  Prior to calling this, the component visibility should
 * be set to "false".
 * @param duration The duration of the fade in.
 * @param callback Callback to run when animation completes.
 * @return
 */
public ComponentSelector fadeIn(final int duration, final SuccessCallback<ComponentSelector> callback) {
    final String placeholderProperty = "com.codename1.ui.ComponentSelector#fadeOutPlaceholder";
    AnimationManager mgr = null;
    ArrayList<ComponentAnimation> animations1 = new ArrayList<ComponentAnimation>();
    final ArrayList<ComponentAnimation> animations2 = 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.getStyle().setBgColor(0xff0000);
                // placeholder.getStyle().setBgTransparency(255);
                // 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));
                // System.out.println("Placeholder height "+c.getHeight());
                // parent.replace(c, placeholder, false);
                // c.setHidden(false);
                ComponentAnimation a = parent.createReplaceTransition(c, placeholder, CommonTransitions.createEmpty());
                animations1.add(a);
                animatingComponents.add(c);
            }
        // centerBackground.add(BorderLayout.CENTER, boxy);
        }
    }
    if (mgr != null) {
        mgr.addAnimation(ComponentAnimation.compoundAnimation(animations1.toArray(new ComponentAnimation[animations1.size()])), new Runnable() {

            public void run() {
                AnimationManager mgr = null;
                for (final Component c : animatingComponents) {
                    Container placeholder = (Container) c.getClientProperty(placeholderProperty);
                    if (placeholder != null) {
                        // System.out.println("Placeholder height after replace "+(c.getHeight() + c.getStyle().getMarginBottom() + c.getStyle().getMarginTop()));
                        // System.out.println("Placeholder not null");
                        c.putClientProperty(placeholderProperty, null);
                        AnimationManager cmgr = placeholder.getAnimationManager();
                        if (cmgr != null) {
                            // System.out.println("Animation manager not null");
                            mgr = cmgr;
                            c.setVisible(true);
                            Container parent = placeholder.getParent();
                            if (parent != null) {
                                // System.out.println("Parent not null");
                                ComponentAnimation a = parent.createReplaceTransition(placeholder, c, CommonTransitions.createFade(duration));
                                animations2.add(a);
                            }
                        }
                    }
                }
                if (mgr != null) {
                    final AnimationManager fmgr = mgr;
                    $(new Runnable() {

                        public void run() {
                            fmgr.addAnimation(ComponentAnimation.compoundAnimation(animations2.toArray(new ComponentAnimation[animations2.size()])), new Runnable() {

                                public void run() {
                                    if (callback != null) {
                                        callback.onSucess(ComponentSelector.this);
                                    }
                                }
                            });
                        }
                    });
                }
            }
        });
    }
    return this;
}
Also used : ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) ArrayList(java.util.ArrayList)

Example 8 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)

Example 9 with ComponentSelector.$

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

the class ComponentSelector method fadeInAndWait.

/**
 * Fades in this component and blocks until animation is complete.
 * @param duration The duration of the animation.
 * @return Self for chaining.
 */
public ComponentSelector fadeInAndWait(final int duration) {
    final String placeholderProperty = "com.codename1.ui.ComponentSelector#fadeOutPlaceholder";
    AnimationManager mgr = null;
    ArrayList<ComponentAnimation> animations1 = new ArrayList<ComponentAnimation>();
    final ArrayList<ComponentAnimation> animations2 = 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.getStyle().setBgColor(0xff0000);
                // placeholder.getStyle().setBgTransparency(255);
                // 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));
                // System.out.println("Placeholder height "+c.getHeight());
                // parent.replace(c, placeholder, false);
                // c.setHidden(false);
                ComponentAnimation a = parent.createReplaceTransition(c, placeholder, CommonTransitions.createEmpty());
                animations1.add(a);
                animatingComponents.add(c);
            }
        // centerBackground.add(BorderLayout.CENTER, boxy);
        }
    }
    if (mgr != null) {
        mgr.addAnimationAndBlock(ComponentAnimation.compoundAnimation(animations1.toArray(new ComponentAnimation[animations1.size()])));
        for (final Component c : animatingComponents) {
            Container placeholder = (Container) c.getClientProperty(placeholderProperty);
            if (placeholder != null) {
                // System.out.println("Placeholder height after replace "+(c.getHeight() + c.getStyle().getMarginBottom() + c.getStyle().getMarginTop()));
                // System.out.println("Placeholder not null");
                c.putClientProperty(placeholderProperty, null);
                AnimationManager cmgr = placeholder.getAnimationManager();
                if (cmgr != null) {
                    // System.out.println("Animation manager not null");
                    mgr = cmgr;
                    c.setVisible(true);
                    Container parent = placeholder.getParent();
                    if (parent != null) {
                        // System.out.println("Parent not null");
                        ComponentAnimation a = parent.createReplaceTransition(placeholder, c, CommonTransitions.createFade(duration));
                        animations2.add(a);
                    }
                }
            }
        }
        if (mgr != null) {
            mgr.addAnimationAndBlock(ComponentAnimation.compoundAnimation(animations2.toArray(new ComponentAnimation[animations2.size()])));
        }
    }
    return this;
}
Also used : ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) ArrayList(java.util.ArrayList)

Example 10 with ComponentSelector.$

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

the class ComponentSelector method setIcon.

/**
 * Sets the icon of all elements in this set to a material icon.  This will use
 * the foreground color of the label.
 * @param materialIcon The material icon charcode.
 * @return Self for chaining.
 */
public ComponentSelector setIcon(char materialIcon) {
    for (Component c : this) {
        if (c instanceof Label) {
            Label l = (Label) c;
            Style style = new Style();
            Style cStyle = c.getUnselectedStyle();
            style.setBgTransparency(0);
            style.setFgColor(cStyle.getFgColor());
            l.setIcon(FontImage.createMaterial(materialIcon, style, 3));
            if (c instanceof Button) {
                Button b = (Button) c;
                style = new Style();
                cStyle = c.getPressedStyle();
                style.setBgTransparency(0);
                style.setFgColor(cStyle.getFgColor());
                b.setPressedIcon(FontImage.createMaterial(materialIcon, style, 3));
            }
        }
    }
    return this;
}
Also used : SpanButton(com.codename1.components.SpanButton) SpanLabel(com.codename1.components.SpanLabel) Style(com.codename1.ui.plaf.Style)

Aggregations

Style (com.codename1.ui.plaf.Style)9 SpanLabel (com.codename1.components.SpanLabel)5 ComponentAnimation (com.codename1.ui.animations.ComponentAnimation)5 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)3 SpanButton (com.codename1.components.SpanButton)2