Search in sources :

Example 41 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class RoundRectBorder method createShape.

private GeneralPath createShape(int shapeW, int shapeH) {
    GeneralPath gp = new GeneralPath();
    float radius = Display.getInstance().convertToPixels(cornerRadius);
    float x = 0;
    float y = 0;
    float widthF = shapeW;
    float heightF = shapeH;
    if (getTrackComponent() != null || trackComponentSide >= 0) {
        int ah = CN.convertToPixels(arrowSize);
        switch(arrowDirection) {
            case CN.TOP:
                y = ah;
            // intentional fall through to the next statement...
            case CN.BOTTOM:
                heightF -= ah;
                break;
            case CN.LEFT:
                x = ah;
            // intentional fall through to the next statement...
            case CN.RIGHT:
                widthF -= ah;
                break;
        }
    }
    if (this.stroke != null && strokeOpacity > 0 && strokeThickness > 0) {
        int strokePx = (int) strokeThickness;
        if (strokeMM) {
            strokePx = Display.getInstance().convertToPixels(strokeThickness);
        }
        widthF -= strokePx;
        heightF -= strokePx;
        x += strokePx / 2;
        y += strokePx / 2;
        if (strokePx % 2 == 1) {
            x += 0.5f;
            y += 0.5f;
        }
    }
    if (topLeft) {
        gp.moveTo(x + radius, y);
    } else {
        gp.moveTo(x, y);
    }
    if ((trackComponentSide >= 0 || getTrackComponent() != null) && arrowDirection == CN.TOP) {
        int actualArrowPosition = (int) Math.min(x + widthF, Math.max(arrowPosition, x + radius));
        gp.lineTo(actualArrowPosition, y);
        int ah = CN.convertToPixels(arrowSize);
        gp.lineTo(actualArrowPosition + ah / 2 - 4, 4);
        gp.quadTo(actualArrowPosition + ah / 2, 4, actualArrowPosition + ah / 2 + 4, 4);
        gp.lineTo(actualArrowPosition + ah, y);
        gp.lineTo(x + widthF - radius, y);
        gp.quadTo(x + widthF, y, x + widthF, y + radius);
    } else {
        if (topRight) {
            gp.lineTo(x + widthF - radius, y);
            gp.quadTo(x + widthF, y, x + widthF, y + radius);
        } else {
            gp.lineTo(x + widthF, y);
        }
    }
    if (bottomRight) {
        gp.lineTo(x + widthF, y + heightF - radius);
        gp.quadTo(x + widthF, y + heightF, x + widthF - radius, y + heightF);
    } else {
        gp.lineTo(x + widthF, y + heightF);
    }
    if ((trackComponentSide >= 0 || getTrackComponent() != null) && arrowDirection == CN.BOTTOM) {
        int actualArrowPosition = (int) Math.min(x + widthF, Math.max(arrowPosition, x + radius));
        gp.lineTo(actualArrowPosition, y + heightF);
        int ah = CN.convertToPixels(arrowSize);
        gp.lineTo(actualArrowPosition + ah / 2 - 4, y + heightF + ah - 4);
        gp.quadTo(actualArrowPosition + ah / 2, y + heightF + ah - 4, actualArrowPosition + ah / 2 + 4, y + heightF + ah - 4);
        gp.lineTo(actualArrowPosition + ah, y + heightF);
        gp.lineTo(x + radius, y + heightF);
        gp.quadTo(x, y + heightF, x, y + heightF - radius);
    } else {
        if (bottomLeft) {
            gp.lineTo(x + radius, y + heightF);
            gp.quadTo(x, y + heightF, x, y + heightF - radius);
        } else {
            gp.lineTo(x, y + heightF);
        }
    }
    if (topLeft) {
        gp.lineTo(x, y + radius);
        gp.quadTo(x, y, x + radius, y);
    } else {
        gp.lineTo(x, y);
    }
    gp.closePath();
    if ((trackComponentSide >= 0 || getTrackComponent() != null) && arrowDirection == CN.LEFT) {
        int ah = CN.convertToPixels(arrowSize);
        int actualArrowPosition = (int) Math.max(y, Math.min(arrowPosition, y + heightF - radius - 4));
        gp.moveTo(0, actualArrowPosition);
        gp.lineTo(x, actualArrowPosition - ah / 2);
        gp.lineTo(x, actualArrowPosition + ah / 2);
        gp.lineTo(0, actualArrowPosition);
        gp.closePath();
    }
    if ((trackComponentSide >= 0 || getTrackComponent() != null) && arrowDirection == CN.RIGHT) {
        int ah = CN.convertToPixels(arrowSize);
        int actualArrowPosition = (int) Math.max(y, Math.min(arrowPosition, y + heightF - radius - 4));
        gp.moveTo(x + widthF + ah, actualArrowPosition);
        gp.lineTo(x + widthF, actualArrowPosition + ah / 2);
        gp.lineTo(x + widthF, actualArrowPosition - ah / 2);
        gp.lineTo(x + widthF + ah, actualArrowPosition);
        gp.closePath();
    }
    return gp;
}
Also used : GeneralPath(com.codename1.ui.geom.GeneralPath)

Example 42 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class BorderLayout method addLayoutComponent.

/**
 * {@inheritDoc}
 */
public void addLayoutComponent(Object name, Component comp, Container c) {
    // helper check for a common mistake...
    if (name == null) {
        throw new IllegalArgumentException("Cannot add component to BorderLayout Container without constraint parameter");
    }
    // allows us to work with Component constraints too which makes some code simpler
    if (name instanceof Integer) {
        switch(((Integer) name).intValue()) {
            case Component.TOP:
                name = NORTH;
                break;
            case Component.BOTTOM:
                name = SOUTH;
                break;
            case Component.LEFT:
                name = WEST;
                break;
            case Component.RIGHT:
                name = EAST;
                break;
            case Component.CENTER:
                name = CENTER;
                break;
            default:
                throw new IllegalArgumentException("BorderLayout Container expects one of the constraints BorderLayout.NORTH/SOUTH/EAST/WEST/CENTER");
        }
    }
    Component previous = null;
    /* Assign the component to one of the known regions of the layout.
         */
    if (CENTER.equals(name)) {
        previous = portraitCenter;
        portraitCenter = comp;
    } else if (NORTH.equals(name)) {
        previous = portraitNorth;
        portraitNorth = comp;
    } else if (SOUTH.equals(name)) {
        previous = portraitSouth;
        portraitSouth = comp;
    } else if (EAST.equals(name)) {
        previous = portraitEast;
        portraitEast = comp;
    } else if (WEST.equals(name)) {
        previous = portraitWest;
        portraitWest = comp;
    } else if (OVERLAY.equals(name)) {
        previous = overlay;
        overlay = comp;
    } else {
        throw new IllegalArgumentException("cannot add to layout: unknown constraint: " + name);
    }
    if (previous != null && previous != comp) {
        c.removeComponent(previous);
    }
}
Also used : Component(com.codename1.ui.Component)

Example 43 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class BorderLayout method layoutContainer.

/**
 * {@inheritDoc}
 */
public void layoutContainer(Container target) {
    Style s = target.getStyle();
    int top = s.getPaddingTop();
    int bottom = target.getLayoutHeight() - target.getBottomGap() - s.getPaddingBottom();
    int left = s.getPaddingLeft(target.isRTL());
    int right = target.getLayoutWidth() - target.getSideGap() - s.getPaddingRight(target.isRTL());
    int targetWidth = target.getWidth();
    int targetHeight = target.getHeight();
    boolean rtl = target.isRTL();
    if (rtl) {
        left += target.getSideGap();
    }
    Component east = getEast();
    Component west = getWest();
    Component south = getSouth();
    Component north = getNorth();
    Component center = getCenter();
    if (north != null) {
        Component c = north;
        positionTopBottom(target, c, right, left, targetHeight);
        c.setY(top + c.getStyle().getMarginTop());
        top += (c.getHeight() + c.getStyle().getMarginTop() + c.getStyle().getMarginBottom());
    }
    if (south != null) {
        Component c = south;
        positionTopBottom(target, c, right, left, targetHeight);
        c.setY(bottom - c.getHeight() - c.getStyle().getMarginBottom());
        bottom -= (c.getHeight() + c.getStyle().getMarginTop() + c.getStyle().getMarginBottom());
    }
    Component realEast = east;
    Component realWest = west;
    if (rtl) {
        realEast = west;
        realWest = east;
    }
    if (realEast != null) {
        Component c = realEast;
        positionLeftRight(realEast, targetWidth, bottom, top);
        c.setX(right - c.getWidth() - c.getStyle().getMarginRight(rtl));
        right -= (c.getWidth() + c.getStyle().getHorizontalMargins());
    }
    if (realWest != null) {
        Component c = realWest;
        positionLeftRight(realWest, targetWidth, bottom, top);
        c.setX(left + c.getStyle().getMarginLeft(rtl));
        left += (c.getWidth() + c.getStyle().getMarginLeftNoRTL() + c.getStyle().getMarginRightNoRTL());
    }
    if (center != null) {
        Component c = center;
        int w = right - left - c.getStyle().getMarginLeftNoRTL() - c.getStyle().getMarginRightNoRTL();
        int h = bottom - top - c.getStyle().getMarginTop() - c.getStyle().getMarginBottom();
        int x = left + c.getStyle().getMarginLeft(rtl);
        int y = top + c.getStyle().getMarginTop();
        switch(centerBehavior) {
            case CENTER_BEHAVIOR_CENTER_ABSOLUTE:
                {
                    Dimension d = c.getPreferredSize();
                    int passNum = 0;
                    while (passNum < 2) {
                        if (d.getWidth() < w) {
                            int newX = (s.getPaddingLeft(rtl) - s.getPaddingRight(rtl)) + targetWidth / 2 - d.getWidth() / 2;
                            if (newX > x) {
                                x = newX;
                            }
                            w = d.getWidth();
                        }
                        if (passNum == 0) {
                            // Some components, like SpanLabel and SpanButton have preferred heights
                            // that depend on their width.  So we need to set the width,
                            // and then recalc preferred size
                            c.setWidth(w);
                            d = c.getPreferredSize();
                        }
                        passNum++;
                    }
                    int append = 0;
                    int th = targetHeight;
                    if (north != null) {
                        append = north.getHeight();
                        th -= append;
                    }
                    if (south != null) {
                        th -= south.getHeight();
                    }
                    if (d.getHeight() < h) {
                        int newY = (s.getPaddingTop() + th) / 2 - d.getHeight() / 2 + append;
                        if (newY > y) {
                            y = newY;
                        }
                        h = d.getHeight();
                    }
                    break;
                }
            case CENTER_BEHAVIOR_CENTER:
                {
                    Dimension d = c.getPreferredSize();
                    int passNum = 0;
                    while (passNum < 2) {
                        if (d.getWidth() < w) {
                            x += w / 2 - d.getWidth() / 2;
                            w = d.getWidth();
                        }
                        if (passNum == 0) {
                            // Some components, like SpanLabel and SpanButton have preferred heights
                            // that depend on their width.  So we need to set the width,
                            // and then recalc preferred size
                            c.setWidth(w);
                            d = c.getPreferredSize();
                        }
                        passNum++;
                    }
                    if (d.getHeight() < h) {
                        y += h / 2 - d.getHeight() / 2;
                        h = d.getHeight();
                    }
                    break;
                }
            case CENTER_BEHAVIOR_TOTAL_BELOW:
                {
                    w = targetWidth;
                    h = targetHeight;
                    x = s.getPaddingLeft(rtl);
                    y = s.getPaddingTop();
                    ;
                }
        }
        c.setWidth(w);
        c.setHeight(h);
        c.setX(x);
        c.setY(y);
    }
    if (overlay != null) {
        Component c = overlay;
        c.setWidth(targetWidth);
        c.setHeight(targetHeight);
        c.setX(0);
        c.setY(0);
    }
}
Also used : Style(com.codename1.ui.plaf.Style) Component(com.codename1.ui.Component)

Example 44 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class SwitchScrollWheelingIssue method start.

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    Form f = new Form();
    f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    f.setScrollable(true);
    for (int x = 1; x < 100; x++) {
        Container cnt = new Container(new GridLayout(2));
        cnt.addAll(new Label("Line" + x), new Switch());
        f.add(cnt);
    }
    f.show();
}
Also used : Container(com.codename1.ui.Container) GridLayout(com.codename1.ui.layouts.GridLayout) Switch(com.codename1.components.Switch) Form(com.codename1.ui.Form) BoxLayout(com.codename1.ui.layouts.BoxLayout) Label(com.codename1.ui.Label)

Example 45 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class SwitchTest2644 method start.

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    Form hi = new Form("Hi World", BoxLayout.y());
    Switch sw = new Switch();
    hi.add(new Label("Hi World"));
    hi.add(sw);
    hi.show();
}
Also used : Switch(com.codename1.components.Switch) Form(com.codename1.ui.Form) Label(com.codename1.ui.Label)

Aggregations

Component (com.codename1.ui.Component)19 Font (com.codename1.ui.Font)18 Container (com.codename1.ui.Container)15 Form (com.codename1.ui.Form)14 Style (com.codename1.ui.plaf.Style)12 Button (com.codename1.ui.Button)11 Image (com.codename1.ui.Image)11 TextArea (com.codename1.ui.TextArea)11 ArrayList (java.util.ArrayList)11 File (java.io.File)10 IOException (java.io.IOException)10 Hashtable (java.util.Hashtable)10 BorderLayout (com.codename1.ui.layouts.BorderLayout)9 Label (com.codename1.ui.Label)8 FileInputStream (java.io.FileInputStream)8 ActionListener (com.codename1.ui.events.ActionListener)7 TextField (com.codename1.ui.TextField)6 ActionEvent (com.codename1.ui.events.ActionEvent)6 BoxLayout (com.codename1.ui.layouts.BoxLayout)6 EncodedImage (com.codename1.ui.EncodedImage)5