Search in sources :

Example 1 with Ruler

use of org.apache.pivot.wtk.Ruler in project pivot by apache.

the class RulerSkin method getPreferredHeight.

@Override
public int getPreferredHeight(int width) {
    Ruler ruler = (Ruler) getComponent();
    Orientation orientation = ruler.getOrientation();
    // Give a little extra height if showing numbers
    return (orientation == Orientation.HORIZONTAL) ? ((showMajorNumbers || showMinorNumbers) ? ((int) Math.ceil(charHeight) + MAJOR_SIZE + 5) : MAJOR_SIZE * 2) : 0;
}
Also used : Ruler(org.apache.pivot.wtk.Ruler) Orientation(org.apache.pivot.wtk.Orientation)

Example 2 with Ruler

use of org.apache.pivot.wtk.Ruler in project pivot by apache.

the class RulerSkin method paint.

@Override
public void paint(Graphics2D graphics) {
    int width = getWidth();
    int height = getHeight();
    int top = markerInsets.top;
    int left = markerInsets.left;
    int bottom = height - markerInsets.bottom;
    int right = width - markerInsets.right;
    Ruler ruler = (Ruler) getComponent();
    graphics.setColor(backgroundColor);
    graphics.fillRect(0, 0, width, height);
    graphics.setColor(color);
    GraphicsUtilities.drawBorders(graphics, borders, 0, 0, height - 1, width - 1);
    height -= markerInsets.getHeight();
    width -= markerInsets.getWidth();
    FontRenderContext fontRenderContext = showMajorNumbers || showMinorNumbers ? GraphicsUtilities.prepareForText(graphics, font, color) : null;
    Orientation orientation = ruler.getOrientation();
    switch(orientation) {
        case HORIZONTAL:
            {
                int start = flip ? bottom - 1 : top;
                int end2 = flip ? (start - (MAJOR_SIZE - 1)) : (MAJOR_SIZE - 1);
                int end3 = flip ? (start - (MINOR_SIZE - 1)) : (MINOR_SIZE - 1);
                int end4 = flip ? (start - (REGULAR_SIZE - 1)) : (REGULAR_SIZE - 1);
                for (int i = 0, n = right / markerSpacing + 1; i < n; i++) {
                    int x = i * markerSpacing + left;
                    if (majorDivision != 0 && i % majorDivision == 0) {
                        graphics.drawLine(x, start, x, end2);
                        // Don't show any numbers at 0 -- make a style for this?
                        if (showMajorNumbers && i > 0) {
                            showNumber(graphics, fontRenderContext, i, x, end2);
                        }
                    } else if (minorDivision != 0 && i % minorDivision == 0) {
                        graphics.drawLine(x, start, x, end3);
                        if (showMinorNumbers && i > 0) {
                            // Show the minor numbers at the same y point as the major
                            showNumber(graphics, fontRenderContext, i, x, end2);
                        }
                    } else {
                        graphics.drawLine(x, start, x, end4);
                    }
                }
                break;
            }
        case VERTICAL:
            {
                int start = flip ? right - 1 : left;
                int end2 = flip ? (start - (MAJOR_SIZE - 1)) : (MAJOR_SIZE - 1);
                int end3 = flip ? (start - (MINOR_SIZE - 1)) : (MINOR_SIZE - 1);
                int end4 = flip ? (start - (REGULAR_SIZE - 1)) : (REGULAR_SIZE - 1);
                for (int i = 0, n = bottom / markerSpacing + 1; i < n; i++) {
                    int y = i * markerSpacing + top;
                    if (majorDivision != 0 && i % majorDivision == 0) {
                        graphics.drawLine(start, y, end2, y);
                        // Don't show any numbers at 0 -- make a style for this?
                        if (showMajorNumbers && i > 0) {
                            showNumber(graphics, fontRenderContext, i, end2, y);
                        }
                    } else if (minorDivision != 0 && i % minorDivision == 0) {
                        graphics.drawLine(start, y, end3, y);
                        if (showMinorNumbers && i > 0) {
                            showNumber(graphics, fontRenderContext, i, end3, y);
                        }
                    } else {
                        graphics.drawLine(start, y, end4, y);
                    }
                }
                break;
            }
        default:
            {
                break;
            }
    }
}
Also used : FontRenderContext(java.awt.font.FontRenderContext) Ruler(org.apache.pivot.wtk.Ruler) Orientation(org.apache.pivot.wtk.Orientation)

Example 3 with Ruler

use of org.apache.pivot.wtk.Ruler in project pivot by apache.

the class RulerSkin method getPreferredWidth.

@Override
public int getPreferredWidth(int height) {
    Ruler ruler = (Ruler) getComponent();
    Orientation orientation = ruler.getOrientation();
    // Give a little extra width if showing numbers
    return (orientation == Orientation.VERTICAL) ? ((showMajorNumbers || showMinorNumbers) ? ((int) Math.ceil(charWidth) + MAJOR_SIZE + 5) : MAJOR_SIZE * 2) : 0;
}
Also used : Ruler(org.apache.pivot.wtk.Ruler) Orientation(org.apache.pivot.wtk.Orientation)

Example 4 with Ruler

use of org.apache.pivot.wtk.Ruler in project pivot by apache.

the class RulerSkin method install.

@Override
public void install(Component component) {
    super.install(component);
    Ruler ruler = (Ruler) component;
    ruler.getRulerListeners().add(this);
}
Also used : Ruler(org.apache.pivot.wtk.Ruler)

Example 5 with Ruler

use of org.apache.pivot.wtk.Ruler in project pivot by apache.

the class RulerSkin method showNumber.

private void showNumber(Graphics2D graphics, FontRenderContext fontRenderContext, int number, int x, int y) {
    String num = Integer.toString(number);
    StringCharacterIterator line;
    GlyphVector glyphVector;
    Rectangle2D textBounds;
    float width, height;
    float fx, fy;
    Ruler ruler = (Ruler) getComponent();
    Orientation orientation = ruler.getOrientation();
    switch(orientation) {
        case HORIZONTAL:
            // Draw the whole number just off the tip of the line given by (x,y)
            line = new StringCharacterIterator(num);
            glyphVector = font.createGlyphVector(fontRenderContext, line);
            textBounds = glyphVector.getLogicalBounds();
            width = (float) textBounds.getWidth();
            height = (float) textBounds.getHeight();
            fx = (float) x - (width / 2.0f);
            if (flip) {
                fy = (float) (y - 2);
            } else {
                fy = (float) (y - 1) + height;
            }
            graphics.drawGlyphVector(glyphVector, fx, fy);
            break;
        case VERTICAL:
            // Draw the number one digit at a time, vertically just off the tip of the line
            if (flip) {
                fx = (float) (x - 1) - charWidth;
            } else {
                fx = (float) (x + 3);
            }
            int numDigits = num.length();
            float heightAdjust = (numDigits % 2 == 1) ? charHeight / 2.0f : 0.0f;
            for (int i = 0; i < numDigits; i++) {
                line = new StringCharacterIterator(num.substring(i, i + 1));
                glyphVector = font.createGlyphVector(fontRenderContext, line);
                int midDigit = (numDigits + 1) / 2;
                if (i <= midDigit) {
                    fy = (float) y + heightAdjust - descent - (float) (midDigit - i - 1) * charHeight;
                } else {
                    fy = (float) y + heightAdjust - descent + (float) (i - midDigit - 1) * charHeight;
                }
                graphics.drawGlyphVector(glyphVector, fx, fy);
            }
            break;
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) GlyphVector(java.awt.font.GlyphVector) Rectangle2D(java.awt.geom.Rectangle2D) Ruler(org.apache.pivot.wtk.Ruler) Orientation(org.apache.pivot.wtk.Orientation)

Aggregations

Ruler (org.apache.pivot.wtk.Ruler)5 Orientation (org.apache.pivot.wtk.Orientation)4 FontRenderContext (java.awt.font.FontRenderContext)1 GlyphVector (java.awt.font.GlyphVector)1 Rectangle2D (java.awt.geom.Rectangle2D)1 StringCharacterIterator (java.text.StringCharacterIterator)1