Search in sources :

Example 1 with CustomBevelBorder

use of com.servoy.j2db.util.gui.CustomBevelBorder in project servoy-client by Servoy.

the class FixedStyleSheet method getBorder.

public Border getBorder(AttributeSet a) {
    Border b = null;
    // Initial values.
    Object borderStyle = null;
    Object borderColor = null;
    float top = getLength(null);
    float right = getLength(null);
    float bottom = getLength(null);
    float left = getLength(null);
    // Try to use the unexpanded "border" attribute, if any.
    Object unexpandedBorder = a.getAttribute(CSS.Attribute.BORDER);
    if (unexpandedBorder != null) {
        StringTokenizer st = new StringTokenizer(unexpandedBorder.toString());
        // general case.
        while (st.hasMoreTokens()) {
            String tok = st.nextToken();
            // If we got digits in the token, then it refers to border size.
            if (// $NON-NLS-1$
            tok.matches("^[0-9]+.*")) {
                top = right = bottom = left = getLength(tok);
            } else // If it is a border style keyword, use it accordingly.
            if (Arrays.asList(IStyleSheet.BORDER_STYLES).contains(tok)) {
                borderStyle = tok;
            } else // If "transparent" then transparent.
            if (tok.equals(IStyleSheet.COLOR_TRANSPARENT)) {
                borderColor = IStyleSheet.COLOR_TRANSPARENT;
            } else // Otherwise assume it is a color.
            {
                Color c = PersistHelper.createColor(tok);
                if (c != null)
                    borderColor = tok;
            }
        }
    }
    // extracted from the unexpanded "border" attribute.
    if (a.isDefined(CSS.Attribute.BORDER_STYLE))
        borderStyle = a.getAttribute(CSS.Attribute.BORDER_STYLE);
    if (a.isDefined(CSS.Attribute.BORDER_COLOR))
        borderColor = a.getAttribute(CSS.Attribute.BORDER_COLOR);
    if (a.isDefined(CSS.Attribute.BORDER_TOP_WIDTH))
        top = getLength(a.getAttribute(CSS.Attribute.BORDER_TOP_WIDTH));
    if (a.isDefined(CSS.Attribute.BORDER_RIGHT_WIDTH))
        right = getLength(a.getAttribute(CSS.Attribute.BORDER_RIGHT_WIDTH));
    if (a.isDefined(CSS.Attribute.BORDER_BOTTOM_WIDTH))
        bottom = getLength(a.getAttribute(CSS.Attribute.BORDER_BOTTOM_WIDTH));
    if (a.isDefined(CSS.Attribute.BORDER_LEFT_WIDTH))
        left = getLength(a.getAttribute(CSS.Attribute.BORDER_LEFT_WIDTH));
    if (borderStyle == null) {
        // TODO this should be fixed when we compile against java 7.
        // java 7 expands the complete border:
        Enumeration<?> attributes = a.getAttributeNames();
        while (attributes.hasMoreElements()) {
            Object element = attributes.nextElement();
            if (element != null && element.toString().startsWith("border-top-style")) {
                borderStyle = a.getAttribute(element);
                break;
            }
        }
    }
    if (borderStyle == null && (borderColor != null || top >= 0 || right >= 0 || bottom >= 0 || left >= 0)) {
        borderStyle = IStyleSheet.BORDER_STYLE_SOLID;
    }
    if (borderStyle != null) {
        Color[] colors = getBorderColor(borderColor, a);
        String bstyle = borderStyle.toString();
        if (a.isDefined(CSSName.BORDER_TOP_LEFT_RADIUS.toString())) {
            top = makeSizeSave(top);
            right = makeSizeSave(right);
            bottom = makeSizeSave(bottom);
            left = makeSizeSave(left);
            colors = expandColors(colors);
            b = new RoundedBorder(top, left, bottom, right, colors[0], colors[3], colors[2], colors[1]);
            float[] radius = new float[8];
            parseCornerBorderRadius(a, radius, 0, CSSName.BORDER_TOP_LEFT_RADIUS.toString());
            parseCornerBorderRadius(a, radius, 1, CSSName.BORDER_TOP_RIGHT_RADIUS.toString());
            parseCornerBorderRadius(a, radius, 2, CSSName.BORDER_BOTTOM_RIGHT_RADIUS.toString());
            parseCornerBorderRadius(a, radius, 3, CSSName.BORDER_BOTTOM_LEFT_RADIUS.toString());
            ((RoundedBorder) b).setRoundingRadius(radius);
            ((RoundedBorder) b).setBorderStyles(new String[] { (String) a.getAttribute(CSSName.BORDER_TOP_STYLE.toString()), (String) a.getAttribute(CSSName.BORDER_LEFT_STYLE.toString()), (String) a.getAttribute(CSSName.BORDER_BOTTOM_STYLE.toString()), (String) a.getAttribute(CSSName.BORDER_RIGHT_STYLE.toString()) });
        } else if (bstyle.equals(IStyleSheet.BORDER_STYLE_INSET) || bstyle.equals(IStyleSheet.BORDER_STYLE_OUTSET)) {
            int style = BevelBorder.LOWERED;
            if (bstyle.equals(IStyleSheet.BORDER_STYLE_OUTSET))
                style = BevelBorder.RAISED;
            Insets customBorderInsets = null;
            if (top != -1.0 || right != -1.0 || bottom != -1.0 || left != -1.0) {
                top = makeSizeSave(top);
                right = makeSizeSave(right);
                bottom = makeSizeSave(bottom);
                left = makeSizeSave(left);
                customBorderInsets = new Insets((int) top, (int) left, (int) bottom, (int) right);
            }
            if (colors != null && colors.length > 0) {
                if (colors.length == 1 || (colors.length == 4 && Utils.equalObjects(colors[0], colors[1]) && Utils.equalObjects(colors[0], colors[2]) && Utils.equalObjects(colors[0], colors[3]))) {
                    // this tries to do the same thing as the web does..
                    b = new CustomBevelBorder(style, colors[0], customBorderInsets);
                } else if (colors.length == 2) {
                    b = new CustomBevelBorder(style, colors[0], colors[1], customBorderInsets);
                } else if (colors.length > 3) {
                    b = new CustomBevelBorder(style, colors[0], colors[1], colors[2], colors[3], customBorderInsets);
                }
            } else {
                b = new CustomBevelBorder(style, customBorderInsets);
            }
        } else if (bstyle.equals(IStyleSheet.BORDER_STYLE_NONE)) {
            b = BorderFactory.createEmptyBorder();
        } else if (bstyle.equals(IStyleSheet.BORDER_STYLE_DOUBLE)) {
            top = makeSizeSave(top);
            right = makeSizeSave(right);
            bottom = makeSizeSave(bottom);
            left = makeSizeSave(left);
            b = BorderFactory.createEmptyBorder((int) top, (int) left, (int) bottom, (int) right);
        } else if (bstyle.equals(IStyleSheet.BORDER_STYLE_GROOVE) || bstyle.equals(IStyleSheet.BORDER_STYLE_RIDGE)) {
            int style = EtchedBorder.LOWERED;
            if (bstyle.equals(IStyleSheet.BORDER_STYLE_RIDGE))
                style = EtchedBorder.RAISED;
            Insets customBorderInsets = null;
            if (top != -1.0 || right != -1.0 || bottom != -1.0 || left != -1.0) {
                top = makeSizeSave(top);
                right = makeSizeSave(right);
                bottom = makeSizeSave(bottom);
                left = makeSizeSave(left);
                customBorderInsets = new Insets((int) top, (int) left, (int) bottom, (int) right);
            }
            if (colors != null && colors.length > 0) {
                if (colors.length == 1 || (colors.length == 4 && Utils.equalObjects(colors[0], colors[1]) && Utils.equalObjects(colors[0], colors[2]) && Utils.equalObjects(colors[0], colors[3]))) {
                    // this tries to do the same thing as the web does..
                    b = new CustomEtchedBorder(style, colors[0], customBorderInsets);
                } else if (colors.length >= 2) {
                    b = new CustomEtchedBorder(style, colors[0], colors[1], customBorderInsets);
                }
            } else {
                b = new CustomEtchedBorder(style, customBorderInsets);
            }
        } else if (bstyle.equals(IStyleSheet.BORDER_STYLE_SOLID) || bstyle.equals(IStyleSheet.BORDER_STYLE_DOTTED) || bstyle.equals(IStyleSheet.BORDER_STYLE_DASHED)) {
            // int bw = (int) getLength(CSS.Attribute.BORDER_WIDTH, a);
            // int colorCount = (colors == null ? 0 : colors.length);
            colors = expandColors(colors);
            // Object obj = a.getAttribute(CSS.Attribute.BORDER_WIDTH);
            if (bstyle.equals(IStyleSheet.BORDER_STYLE_SOLID)) {
                if (borderColor != null && IStyleSheet.COLOR_TRANSPARENT.equals(borderColor.toString())) {
                    top = makeSizeSave(top);
                    right = makeSizeSave(right);
                    bottom = makeSizeSave(bottom);
                    left = makeSizeSave(left);
                    b = BorderFactory.createEmptyBorder((int) top, (int) left, (int) bottom, (int) right);
                } else if (colors != null) {
                    if (// is undefined, make 1 px
                    top == -1f && right == -1f && bottom == -1f && left == -1f) {
                        b = BorderFactory.createLineBorder(colors[0]);
                    } else if (// is contradiction solid but width 0 make empty border
                    top == 0f && right == 0f && bottom == 0f && left == 0f) {
                        b = BorderFactory.createEmptyBorder();
                    } else {
                        top = makeSizeSave(top);
                        right = makeSizeSave(right);
                        bottom = makeSizeSave(bottom);
                        left = makeSizeSave(left);
                        b = new SpecialMatteBorder(top, left, bottom, right, colors[0], colors[3], colors[2], colors[1]);
                    }
                }
            } else if (bstyle.equals(IStyleSheet.BORDER_STYLE_DASHED)) {
                if (top <= 0 && left <= 0 && bottom <= 0 && right <= 0) {
                    top = left = bottom = right = 1;
                }
                b = new SpecialMatteBorder(top, left, bottom, right, colors[0], colors[3], colors[2], colors[1]);
                ((SpecialMatteBorder) b).setDashPattern(new float[] { 3, 3 });
            } else if (bstyle.equals(IStyleSheet.BORDER_STYLE_DOTTED)) {
                if (top <= 0 && left <= 0 && bottom <= 0 && right <= 0) {
                    top = left = bottom = right = 1;
                }
                b = new SpecialMatteBorder(top, left, bottom, right, colors[0], colors[3], colors[2], colors[1]);
                ((SpecialMatteBorder) b).setDashPattern(new float[] { 1, 1 });
            }
        }
    }
    return b;
}
Also used : Insets(java.awt.Insets) SpecialMatteBorder(com.servoy.j2db.util.gui.SpecialMatteBorder) Color(java.awt.Color) StringTokenizer(java.util.StringTokenizer) CustomBevelBorder(com.servoy.j2db.util.gui.CustomBevelBorder) CustomEtchedBorder(com.servoy.j2db.util.gui.CustomEtchedBorder) RoundedBorder(com.servoy.j2db.util.gui.RoundedBorder) BevelBorder(javax.swing.border.BevelBorder) SpecialMatteBorder(com.servoy.j2db.util.gui.SpecialMatteBorder) Border(javax.swing.border.Border) CustomEtchedBorder(com.servoy.j2db.util.gui.CustomEtchedBorder) CustomBevelBorder(com.servoy.j2db.util.gui.CustomBevelBorder) RoundedBorder(com.servoy.j2db.util.gui.RoundedBorder) EtchedBorder(javax.swing.border.EtchedBorder)

Aggregations

CustomBevelBorder (com.servoy.j2db.util.gui.CustomBevelBorder)1 CustomEtchedBorder (com.servoy.j2db.util.gui.CustomEtchedBorder)1 RoundedBorder (com.servoy.j2db.util.gui.RoundedBorder)1 SpecialMatteBorder (com.servoy.j2db.util.gui.SpecialMatteBorder)1 Color (java.awt.Color)1 Insets (java.awt.Insets)1 StringTokenizer (java.util.StringTokenizer)1 BevelBorder (javax.swing.border.BevelBorder)1 Border (javax.swing.border.Border)1 EtchedBorder (javax.swing.border.EtchedBorder)1