Search in sources :

Example 1 with Meter

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

the class TerraMeterSkin method getPreferredHeight.

@Override
public int getPreferredHeight(int width) {
    Meter meter = (Meter) getComponent();
    int preferredHeight;
    if (meter.getOrientation() == Orientation.HORIZONTAL) {
        preferredHeight = getPreferredWidth(width);
    } else {
        String text = meter.getText();
        if (text != null && text.length() > 0) {
            FontRenderContext fontRenderContext = Platform.getFontRenderContext();
            LineMetrics lm = font.getLineMetrics("", fontRenderContext);
            preferredHeight = (int) Math.ceil(lm.getHeight()) + 2;
        } else {
            preferredHeight = 0;
        }
        // If the meter has no content, its preferred height is hard-coded by the
        // class and is not affected by the width constraint
        preferredHeight = Math.max(preferredHeight, DEFAULT_HEIGHT);
    }
    return preferredHeight;
}
Also used : Meter(org.apache.pivot.wtk.Meter) FontRenderContext(java.awt.font.FontRenderContext) LineMetrics(java.awt.font.LineMetrics) GradientPaint(java.awt.GradientPaint)

Example 2 with Meter

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

the class TerraMeterSkin method getBaseline.

@Override
public int getBaseline(int width, int height) {
    int baseline = -1;
    Meter meter = (Meter) getComponent();
    if (meter.getOrientation() == Orientation.HORIZONTAL) {
        String text = meter.getText();
        if (text != null && text.length() > 0) {
            FontRenderContext fontRenderContext = Platform.getFontRenderContext();
            LineMetrics lm = font.getLineMetrics("", fontRenderContext);
            float ascent = lm.getAscent();
            float textHeight = lm.getHeight();
            baseline = Math.round((height - textHeight) / 2 + ascent);
        }
    }
    return baseline;
}
Also used : Meter(org.apache.pivot.wtk.Meter) FontRenderContext(java.awt.font.FontRenderContext) LineMetrics(java.awt.font.LineMetrics) GradientPaint(java.awt.GradientPaint)

Example 3 with Meter

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

the class MeterTest method testListeners.

@Test
public void testListeners() {
    Meter meter = new Meter();
    meter.getMeterListeners().add(this);
    // Test all the listeners getting fired as they should
    meter.setText("abc");
    meter.setText(null);
    meter.setText(null);
    meter.setText("123");
    meter.setText("123");
    meter.setOrientation(Orientation.HORIZONTAL);
    meter.setOrientation(Orientation.VERTICAL);
    meter.setOrientation(Orientation.HORIZONTAL);
    meter.setPercentage(0.25);
    meter.setPercentage(0.25);
    meter.setPercentage(0.5);
    meter.setPercentage(0.75);
    meter.setPercentage(1.0);
    // Now check for proper listener event counts
    assertEquals(percentChangeCount, 4);
    assertEquals(textChangeCount, 3);
    assertEquals(orientationChangeCount, 2);
}
Also used : Meter(org.apache.pivot.wtk.Meter) Test(org.junit.Test)

Example 4 with Meter

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

the class TerraMeterSkin method getPreferredWidth.

@Override
public int getPreferredWidth(int height) {
    Meter meter = (Meter) getComponent();
    int preferredWidth;
    if (meter.getOrientation() == Orientation.HORIZONTAL) {
        String text = meter.getText();
        if (text != null && text.length() > 0) {
            FontRenderContext fontRenderContext = Platform.getFontRenderContext();
            Rectangle2D stringBounds = font.getStringBounds(text, fontRenderContext);
            preferredWidth = (int) Math.ceil(stringBounds.getWidth()) + 2;
        } else {
            preferredWidth = 0;
        }
        // If the meter has no content, its preferred width is hard-coded by the
        // class and is not affected by the height constraint
        preferredWidth = Math.max(preferredWidth, DEFAULT_WIDTH);
    } else {
        preferredWidth = getPreferredHeight(-1);
    }
    return preferredWidth;
}
Also used : Meter(org.apache.pivot.wtk.Meter) Rectangle2D(java.awt.geom.Rectangle2D) FontRenderContext(java.awt.font.FontRenderContext) GradientPaint(java.awt.GradientPaint)

Example 5 with Meter

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

the class TerraMeterSkin method getPreferredSize.

@Override
public Dimensions getPreferredSize() {
    Meter meter = (Meter) getComponent();
    String text = meter.getText();
    int preferredWidth = 0;
    int preferredHeight = 0;
    if (text != null && text.length() > 0) {
        FontRenderContext fontRenderContext = Platform.getFontRenderContext();
        Rectangle2D stringBounds = font.getStringBounds(text, fontRenderContext);
        preferredWidth = (int) Math.ceil(stringBounds.getWidth()) + 2;
        LineMetrics lm = font.getLineMetrics("", fontRenderContext);
        preferredHeight = (int) Math.ceil(lm.getHeight()) + 2;
    }
    // If meter has no content, its preferred size is hard coded by the class
    preferredWidth = Math.max(preferredWidth, DEFAULT_WIDTH);
    preferredHeight = Math.max(preferredHeight, DEFAULT_HEIGHT);
    Dimensions preferredSize;
    if (meter.getOrientation() == Orientation.HORIZONTAL) {
        preferredSize = new Dimensions(preferredWidth, preferredHeight);
    } else {
        preferredSize = new Dimensions(preferredHeight, preferredWidth);
    }
    return preferredSize;
}
Also used : Meter(org.apache.pivot.wtk.Meter) Rectangle2D(java.awt.geom.Rectangle2D) Dimensions(org.apache.pivot.wtk.Dimensions) FontRenderContext(java.awt.font.FontRenderContext) LineMetrics(java.awt.font.LineMetrics) GradientPaint(java.awt.GradientPaint)

Aggregations

Meter (org.apache.pivot.wtk.Meter)7 GradientPaint (java.awt.GradientPaint)5 FontRenderContext (java.awt.font.FontRenderContext)4 LineMetrics (java.awt.font.LineMetrics)3 Rectangle2D (java.awt.geom.Rectangle2D)2 Dimensions (org.apache.pivot.wtk.Dimensions)1 Test (org.junit.Test)1