Search in sources :

Example 1 with NumberRuler

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

the class NumberRulerSkin method setShowMajorNumbers.

/**
 * Sets the flag to say whether to show numbers at each major division
 * (only for horizontal orientation).
 *
 * @param showMajorNumbers Whether numbers should be shown for major divisions.
 */
public final void setShowMajorNumbers(boolean showMajorNumbers) {
    this.showMajorNumbers = showMajorNumbers;
    NumberRuler ruler = (NumberRuler) getComponent();
    if (ruler.getOrientation() == Orientation.HORIZONTAL) {
        invalidateComponent();
    }
}
Also used : NumberRuler(org.apache.pivot.wtk.NumberRuler)

Example 2 with NumberRuler

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

the class NumberRulerSkin method setShowMinorNumbers.

/**
 * Sets the flag to say whether to show numbers at each minor division
 * (for horizontal orientation only).
 *
 * @param showMinorNumbers Whether numbers should be shown for minor divisions.
 */
public final void setShowMinorNumbers(boolean showMinorNumbers) {
    this.showMinorNumbers = showMinorNumbers;
    NumberRuler ruler = (NumberRuler) getComponent();
    if (ruler.getOrientation() == Orientation.HORIZONTAL) {
        invalidateComponent();
    }
}
Also used : NumberRuler(org.apache.pivot.wtk.NumberRuler)

Example 3 with NumberRuler

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

the class NumberRulerSkin method install.

@Override
public void install(Component component) {
    super.install(component);
    Theme theme = Theme.getTheme();
    setFont(theme.getFont());
    setColor(0);
    setBackgroundColor(19);
    markerSpacing = 5;
    markerInsets = new Insets(0);
    rowPadding = new Insets(0);
    // Note: these aren't settable
    majorDivision = 10;
    minorDivision = 5;
    // But these are
    showMajorNumbers = true;
    showMinorNumbers = false;
    NumberRuler ruler = (NumberRuler) component;
    ruler.getRulerListeners().add(this);
}
Also used : NumberRuler(org.apache.pivot.wtk.NumberRuler) Insets(org.apache.pivot.wtk.Insets) Theme(org.apache.pivot.wtk.Theme)

Example 4 with NumberRuler

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

the class NumberRulerSkin method paint.

@Override
public void paint(Graphics2D graphics) {
    int width = getWidth();
    int height = getHeight();
    int bottom = height - markerInsets.bottom;
    Rectangle clipRect = graphics.getClipBounds();
    NumberRuler ruler = (NumberRuler) getComponent();
    int textSize = ruler.getTextSize();
    graphics.setColor(backgroundColor);
    graphics.fillRect(0, 0, width, height);
    graphics.setColor(color);
    FontRenderContext fontRenderContext = Platform.getFontRenderContext();
    graphics.setFont(font);
    Orientation orientation = ruler.getOrientation();
    Rectangle fullRect = new Rectangle(width, height);
    Rectangle clippedRect = fullRect.intersection(clipRect);
    switch(orientation) {
        case HORIZONTAL:
            {
                int start = bottom - 1;
                int end2 = start - (MAJOR_SIZE - 1);
                int end3 = start - (MINOR_SIZE - 1);
                int end4 = start - (REGULAR_SIZE - 1);
                Rectangle lineRect = new Rectangle(0, height - 1, width - 1, 0);
                Rectangle clippedLineRect = lineRect.intersection(clipRect);
                graphics.drawLine(clippedLineRect.x, clippedLineRect.y, clippedLineRect.x + clippedLineRect.width, clippedLineRect.y);
                for (int i = 0, n = width / markerSpacing + 1; i < n; i++) {
                    int x = i * markerSpacing + markerInsets.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:
            {
                Rectangle lineRect = new Rectangle(width - 1, 0, 0, height - 1);
                Rectangle clippedLineRect = lineRect.intersection(clipRect);
                graphics.drawLine(clippedLineRect.x, clippedLineRect.y, clippedLineRect.x, clippedLineRect.y + clippedLineRect.height);
                // Optimize drawing by only starting just above the current clip bounds
                // down to the bottom (plus one) of the end of the clip bounds.
                // This is a 100x speed improvement for 500,000 lines.
                int linesAbove = clipRect.y / lineHeight;
                int linesBelow = (height - (clipRect.y + clipRect.height)) / lineHeight;
                int totalLines = height / lineHeight + 1;
                for (int num = 1 + linesAbove, n = totalLines - (linesBelow - 1); num < n; num++) {
                    String numberString = Integer.toString(num);
                    StringCharacterIterator line = new StringCharacterIterator(numberString);
                    int lineY = (num - 1) * lineHeight + markerInsets.top;
                    Graphics2D lineGraphics = (Graphics2D) graphics.create(0, lineY, width, lineHeight);
                    float y = (float) (lineHeight - rowPadding.bottom) - descent;
                    GlyphVector glyphVector = font.createGlyphVector(fontRenderContext, line);
                    Rectangle2D textBounds = glyphVector.getLogicalBounds();
                    float lineWidth = (float) textBounds.getWidth();
                    float x = (float) width - (lineWidth + (float) padding);
                    lineGraphics.drawGlyphVector(glyphVector, x, y);
                }
                break;
            }
        default:
            {
                break;
            }
    }
}
Also used : NumberRuler(org.apache.pivot.wtk.NumberRuler) StringCharacterIterator(java.text.StringCharacterIterator) GlyphVector(java.awt.font.GlyphVector) Rectangle(java.awt.Rectangle) Rectangle2D(java.awt.geom.Rectangle2D) FontRenderContext(java.awt.font.FontRenderContext) Orientation(org.apache.pivot.wtk.Orientation) Graphics2D(java.awt.Graphics2D)

Example 5 with NumberRuler

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

the class NumberRulerSkin method getPreferredHeight.

@Override
public int getPreferredHeight(int width) {
    NumberRuler ruler = (NumberRuler) 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 : NumberRuler(org.apache.pivot.wtk.NumberRuler) Orientation(org.apache.pivot.wtk.Orientation)

Aggregations

NumberRuler (org.apache.pivot.wtk.NumberRuler)6 Orientation (org.apache.pivot.wtk.Orientation)3 FontRenderContext (java.awt.font.FontRenderContext)2 Rectangle2D (java.awt.geom.Rectangle2D)2 Graphics2D (java.awt.Graphics2D)1 Rectangle (java.awt.Rectangle)1 GlyphVector (java.awt.font.GlyphVector)1 StringCharacterIterator (java.text.StringCharacterIterator)1 Insets (org.apache.pivot.wtk.Insets)1 Theme (org.apache.pivot.wtk.Theme)1