You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2017/12/19 19:32:50 UTC
svn commit: r1818707 - in /pivot/trunk/wtk/src/org/apache/pivot/wtk/skin:
NumberRulerSkin.java RulerSkin.java
Author: rwhitcomb
Date: Tue Dec 19 19:32:50 2017
New Revision: 1818707
URL: http://svn.apache.org/viewvc?rev=1818707&view=rev
Log:
PIVOT-1019, PIVOT-1017: Tweak the positioning of horizontal ruler text,
and clean up some code.
Copy a bunch of the code and concepts from RulerSkin to NumberRulerSkin for
drawing the horizontal ruler and text.
Modified:
pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/NumberRulerSkin.java
pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/RulerSkin.java
Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/NumberRulerSkin.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/NumberRulerSkin.java?rev=1818707&r1=1818706&r2=1818707&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/NumberRulerSkin.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/NumberRulerSkin.java Tue Dec 19 19:32:50 2017
@@ -28,9 +28,11 @@ import java.text.StringCharacterIterator
import java.util.Arrays;
import org.apache.pivot.collections.Dictionary;
+import org.apache.pivot.collections.Sequence;
import org.apache.pivot.util.Utils;
import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.GraphicsUtilities;
+import org.apache.pivot.wtk.Insets;
import org.apache.pivot.wtk.NumberRuler;
import org.apache.pivot.wtk.NumberRulerListener;
import org.apache.pivot.wtk.Orientation;
@@ -38,19 +40,42 @@ import org.apache.pivot.wtk.Platform;
import org.apache.pivot.wtk.Theme;
public class NumberRulerSkin extends ComponentSkin implements NumberRulerListener {
+ private static final int MAJOR_SIZE = 10;
+ private static final int MINOR_SIZE = 8;
+ private static final int REGULAR_SIZE = 5;
+
private Font font;
private Color color;
private Color backgroundColor;
private int padding = 2;
+ private int markerSpacing;
+ private Insets markerInsets;
+ private int majorDivision;
+ private int minorDivision;
+ private boolean showMajorNumbers;
+ private boolean showMinorNumbers;
+ private float charHeight, descent;
+ private int lineHeight;
@Override
public void install(Component component) {
super.install(component);
Theme theme = Theme.getTheme();
- font = theme.getFont();
- color = theme.getColor(0);
- backgroundColor = theme.getColor(19);
+ setFont(theme.getFont());
+
+ setColor(0);
+ setBackgroundColor(19);
+
+ markerSpacing = 5;
+ markerInsets = 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);
@@ -66,7 +91,10 @@ public class NumberRulerSkin extends Com
NumberRuler ruler = (NumberRuler) getComponent();
Orientation orientation = ruler.getOrientation();
- return (orientation == Orientation.HORIZONTAL) ? 20 : 0;
+ // 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;
}
@Override
@@ -89,10 +117,32 @@ public class NumberRulerSkin extends Com
return 0;
}
+ 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;
+
+ // 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);
+ fy = (float)(y - 2);
+ graphics.drawGlyphVector(glyphVector, fx, fy);
+ }
+
@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();
@@ -103,26 +153,43 @@ public class NumberRulerSkin extends Com
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 / 5 + 1; i < n; i++) {
- int x = i * 5;
+ for (int i = 0, n = width / markerSpacing + 1; i < n; i++) {
+ int x = i * markerSpacing + markerInsets.left;
- if (i % 4 == 0) {
- graphics.drawLine(x, 0, x, height / 2);
+ 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, 0, x, height / 4);
+ graphics.drawLine(x, start, x, end4);
}
}
- // TODO: put in the numbers every so often
break;
}
@@ -132,12 +199,6 @@ public class NumberRulerSkin extends Com
Rectangle clippedLineRect = lineRect.intersection(clipRect);
graphics.drawLine(clippedLineRect.x, clippedLineRect.y, clippedLineRect.x, clippedLineRect.y + clippedLineRect.height);
- FontRenderContext fontRenderContext = Platform.getFontRenderContext();
- LineMetrics lm = font.getLineMetrics("", fontRenderContext);
- float descent = lm.getDescent();
- int lineHeight = (int)Math.ceil(lm.getHeight());
- graphics.setFont(font);
-
// 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.
@@ -175,6 +236,113 @@ public class NumberRulerSkin extends Com
invalidateComponent();
}
+ /**
+ * @return The insets for the markers (only applicable for horizontal
+ * orientation).
+ */
+ public Insets getMarkerInsets() {
+ return markerInsets;
+ }
+
+ public final void setMarkerInsets(Insets insets) {
+ Utils.checkNull(insets, "markerInsets");
+
+ this.markerInsets = insets;
+ repaintComponent();
+ }
+
+ public final void setMarkerInsets(Dictionary<String, ?> insets) {
+ setMarkerInsets(new Insets(insets));
+ }
+
+ public final void setMarkerInsets(Sequence<?> insets) {
+ setMarkerInsets(new Insets(insets));
+ }
+
+ public final void setMarkerInsets(int insets) {
+ setMarkerInsets(new Insets(insets));
+ }
+
+ public final void setMarkerInsets(Number insets) {
+ setMarkerInsets(new Insets(insets));
+ }
+
+ public final void setMarkerInsets(String insets) {
+ setMarkerInsets(Insets.decode(insets));
+ }
+
+ /**
+ * @return The number of pixels interval at which to draw markers.
+ */
+ public int getMarkerSpacing() {
+ return markerSpacing;
+ }
+
+ /**
+ * Set the number of pixels interval at which to draw the markers
+ * (for horizontal orientation only).
+ *
+ * @param spacing The number of pixels between markers (must be >= 1).
+ */
+ public final void setMarkerSpacing(int spacing) {
+ Utils.checkPositive(spacing, "markerSpacing");
+
+ this.markerSpacing = spacing;
+ invalidateComponent();
+ }
+
+ public final void setMarkerSpacing(Number spacing) {
+ Utils.checkNull(spacing, "markerSpacing");
+
+ setMarkerSpacing(spacing.intValue());
+ }
+
+ /**
+ * @return Whether to display numbers at each major division
+ * (only applicable for horizontal orientation).
+ */
+ public boolean getShowMajorNumbers() {
+ return showMajorNumbers;
+ }
+
+ /**
+ * 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();
+ }
+ }
+
+ /**
+ * @return Whether to display numbers at each minor division
+ * (only for horizontal orientation).
+ */
+ public boolean getShowMinorNumbers() {
+ return showMinorNumbers;
+ }
+
+ /**
+ * 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();
+ }
+ }
+
public Font getFont() {
return font;
}
@@ -188,6 +356,14 @@ public class NumberRulerSkin extends Com
Utils.checkNull(font, "font");
this.font = font;
+
+ // Make some size calculations for the drawing code
+ FontRenderContext fontRenderContext = Platform.getFontRenderContext();
+ LineMetrics lm = this.font.getLineMetrics("0", fontRenderContext);
+ this.charHeight = lm.getAscent();
+ this.descent = lm.getDescent();
+ this.lineHeight = (int)Math.ceil(lm.getHeight());
+
invalidateComponent();
}
@@ -240,6 +416,11 @@ public class NumberRulerSkin extends Com
setColor(GraphicsUtilities.decodeColor(color, "color"));
}
+ public final void setColor(int color) {
+ Theme theme = currentTheme();
+ setColor(theme.getColor(color));
+ }
+
/**
* Returns the background color of the ruler.
*
@@ -270,4 +451,9 @@ public class NumberRulerSkin extends Com
setBackgroundColor(GraphicsUtilities.decodeColor(backgroundColor, "backgroundColor"));
}
+ public final void setBackgroundColor(int backgroundColor) {
+ Theme theme = currentTheme();
+ setBackgroundColor(theme.getColor(backgroundColor));
+ }
+
}
Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/RulerSkin.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/RulerSkin.java?rev=1818707&r1=1818706&r2=1818707&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/RulerSkin.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/RulerSkin.java Tue Dec 19 19:32:50 2017
@@ -130,7 +130,7 @@ public class RulerSkin extends Component
textBounds = glyphVector.getLogicalBounds();
width = (float) textBounds.getWidth();
height = (float) textBounds.getHeight();
- fx = (float)(x + 1) - (width / 2.0f);
+ fx = (float)x - (width / 2.0f);
if (flip) {
fy = (float)(y - 2);
} else {
@@ -189,16 +189,14 @@ public class RulerSkin extends Component
Orientation orientation = ruler.getOrientation();
switch (orientation) {
case HORIZONTAL: {
- int mid = MINOR_SIZE - 1;
int start = flip ? bottom - 1 : top;
- int end2 = flip ? (bottom - 1 - (MAJOR_SIZE - 1)) : (MAJOR_SIZE - 1);
- int end3 = flip ? (bottom - 1 - mid) : mid;
- int end4 = flip ? (bottom - 1 - (REGULAR_SIZE - 1)) : (REGULAR_SIZE - 1);
+ 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 = width / markerSpacing + 1; i < n; i++) {
+ 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?
@@ -208,7 +206,8 @@ public class RulerSkin extends Component
} else if (minorDivision != 0 && i % minorDivision == 0) {
graphics.drawLine(x, start, x, end3);
if (showMinorNumbers && i > 0) {
- showNumber(graphics, fontRenderContext, i, x, end3);
+ // 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);
@@ -219,13 +218,12 @@ public class RulerSkin extends Component
}
case VERTICAL: {
- int mid = MINOR_SIZE - 1;
int start = flip ? right - 1 : left;
- int end2 = flip ? (right - 1 - (MAJOR_SIZE - 1)) : (MAJOR_SIZE - 1);
- int end3 = flip ? (right - 1 - mid) : mid;
- int end4 = flip ? (right - 1 - (REGULAR_SIZE - 1)) : (REGULAR_SIZE - 1);
+ 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 = height / markerSpacing + 1; i < n; i++) {
+ for (int i = 0, n = bottom / markerSpacing + 1; i < n; i++) {
int y = i * markerSpacing + top;
if (majorDivision != 0 && i % majorDivision == 0) {