001: package snow.sortabletable;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.awt.font.*;
006: import java.awt.geom.*;
007: import javax.swing.*;
008: import javax.swing.table.*;
009: import javax.swing.plaf.*;
010: import javax.swing.plaf.metal.*;
011:
012: public class SortDirectionSelfDrawingIcon implements Icon {
013:
014: // types :
015: public static final int Ascending = 1;
016: public static final int Descending = 2;
017: public static final int Left = 3; // used for "expand" buttons
018: public static final int Right = 4;
019:
020: // Member attributes :
021: private Color shapeBorderColor = null;
022:
023: private int height = 0;
024: private int width = 0;
025:
026: GradientShapeEntity shape;
027:
028: private final int fs;
029:
030: private int strokeThickness = 1;
031:
032: /**
033: * Creates a filetreeicon with the specified type.
034: * The available types are defined static in this class.
035: */
036: public SortDirectionSelfDrawingIcon(final int iconStyle) {
037: Font basisFont = UIManager.getFont("Tree.font");
038: fs = basisFont.getSize();
039: this .strokeThickness = 1 + fs / 16;
040:
041: final Color bg = UIManager.getColor("TextField.background");
042: final Color fg = UIManager.getColor("TextField.foreground");
043:
044: // border color [UI dependant] :
045: int r = (bg.getRed() + 4 * fg.getRed()) / 5;
046: int g = (bg.getGreen() + 4 * fg.getGreen()) / 5;
047: int b = (bg.getBlue() + 4 * fg.getBlue()) / 5;
048: this .shapeBorderColor = new Color(r, g, b);
049:
050: // Icon size :
051:
052: this .height = (7 * fs / 6);
053: this .width = this .height / 2;
054:
055: // The shape entities :
056: int verticalOffset = this .height / 4;
057: int h2 = this .height * 3 / 4;
058: Color shapeInsideColor = new Color(130, 242, 110);
059: Color shapeInsideSelectedColor = new Color(190, 250, 170);
060: shape = new GradientShapeEntity(this .shapeBorderColor,
061: this .shapeBorderColor, shapeInsideColor,
062: shapeInsideSelectedColor, 1f * this .width, 0f,
063: this .strokeThickness);
064: switch (iconStyle) {
065: case Ascending:
066: shape.moveTo(0, verticalOffset);
067: shape.lineTo(this .width / 2, h2);
068: shape.lineTo(this .width, verticalOffset);
069: shape.closePath();
070: break;
071:
072: case Descending:
073: shape.moveTo(0, h2);
074: shape.lineTo(this .width / 2, verticalOffset);
075: shape.lineTo(this .width, h2);
076: shape.closePath();
077: break;
078:
079: case Left:
080: shape.moveTo(width, height);
081: shape.lineTo(width / 4, height / 2);
082: shape.lineTo(width, 0);
083: shape.closePath();
084: break;
085:
086: case Right:
087: shape.moveTo(0, height);
088: shape.lineTo(width * 3 / 4, height / 2);
089: shape.lineTo(0, 0);
090: shape.closePath();
091: break;
092:
093: default:
094: }
095: }
096:
097: /**
098: * Draws the icon at the specified location. Icon implementations
099: * may use the Component argument to get properties useful for
100: * painting, e.g. the foreground or background color.
101: */
102: public void paintIcon(Component c, Graphics g, int x, int y) {
103: final Graphics2D graphics2D = (Graphics2D) g;
104: // Backup:
105: final Color saveColor = graphics2D.getColor();
106: final AffineTransform backupTransform = graphics2D
107: .getTransform();
108: g.translate(x, y);
109: final Stroke backupStroke = graphics2D.getStroke();
110: final Paint savePaint = graphics2D.getPaint();
111:
112: final Object backupAntiAliasRendering = graphics2D
113: .getRenderingHint(RenderingHints.KEY_ANTIALIASING);
114: final Object backupQualityRendering = graphics2D
115: .getRenderingHint(RenderingHints.KEY_RENDERING);
116:
117: // Reconfigure / draw:
118:
119: graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
120: RenderingHints.VALUE_ANTIALIAS_ON);
121: graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING,
122: RenderingHints.VALUE_RENDER_QUALITY);
123:
124: shape.paint(graphics2D);
125:
126: // Restore :
127: graphics2D.setTransform(backupTransform);
128: graphics2D.setColor(saveColor);
129: graphics2D.setStroke(backupStroke);
130:
131: graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
132: backupAntiAliasRendering);
133: graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING,
134: backupQualityRendering);
135:
136: graphics2D.setPaint(savePaint);
137: }
138:
139: public int getIconWidth() {
140: return this .width + 1 + fs / 10; // always add a small offset
141: }
142:
143: public int getIconHeight() {
144: return this .height + 1 + fs / 10; // always add a small offset
145: }
146:
147: }
|