001: package snow.utils.gui;
002:
003: import java.awt.*;
004: import javax.swing.border.*;
005: import javax.swing.UIManager;
006:
007: /**
008: * Like MatteBorder, but with two lines. ( dark and bright usually )
009: * The effect from the two lines is like an etched border, but one
010: * can specify :
011: * on_top,on_left,on_bottom and on_right define if it should be painted at these locations.
012: */
013: public class CustomEtchedBorder extends EmptyBorder {
014: protected Color lightColor;
015: protected Color darkColor;
016:
017: private boolean on_top;
018: private boolean on_left;
019: private boolean on_bottom;
020: private boolean on_right;
021:
022: boolean doUpdateColors = true; // on UI changes
023:
024: /**
025: * Version with passed constant colors.
026: */
027: public CustomEtchedBorder(boolean on_top, boolean on_left,
028: boolean on_bottom, boolean on_right,
029: final Color lightColor, final Color darkColor) {
030: super (on_top ? 2 : 0, on_left ? 2 : 0, on_bottom ? 2 : 0,
031: on_right ? 2 : 0);
032: this .lightColor = lightColor;
033: this .darkColor = darkColor;
034: this .doUpdateColors = false; // dont change these colors on UI changes
035: this .on_top = on_top;
036: this .on_left = on_left;
037: this .on_bottom = on_bottom;
038: this .on_right = on_right;
039: }
040:
041: /**
042: * Version with automatically calculated colors, which then follow UI changes.
043: */
044: public CustomEtchedBorder(boolean on_top, boolean on_left,
045: boolean on_bottom, boolean on_right) {
046: super (on_top ? 2 : 0, on_left ? 2 : 0, on_bottom ? 2 : 0,
047: on_right ? 2 : 0);
048: this .lightColor = UIManager.getColor("Panel.background")
049: .brighter();
050: this .darkColor = UIManager.getColor("Panel.background")
051: .darker();
052: this .doUpdateColors = true; // follow on UI changes
053: this .on_top = on_top;
054: this .on_left = on_left;
055: this .on_bottom = on_bottom;
056: this .on_right = on_right;
057: }
058:
059: /**
060: * Paints the matte border.
061: */
062: @Override
063: public void paintBorder(Component c, Graphics g, int x, int y,
064: int width, int height) {
065: if (this .doUpdateColors) {
066: this .lightColor = UIManager.getColor("Panel.background")
067: .brighter();
068: this .darkColor = UIManager.getColor("Panel.background")
069: .darker();
070: }
071:
072: Insets insets = getBorderInsets(c);
073: Color oldColor = g.getColor();
074: g.translate(x, y);
075:
076: g.setColor(this .darkColor);
077:
078: if (this .on_top)
079: g.drawLine(0, 0, width - 1, 0);
080: if (this .on_left)
081: g.drawLine(0, 0, 0, height - 1);
082: if (this .on_bottom)
083: g.drawLine(1, height - 2, width - 2, height - 2);
084: if (this .on_right)
085: g.drawLine(width - 2, 1, width - 2, height - 2);
086:
087: g.setColor(this .lightColor);
088:
089: if (this .on_bottom)
090: g.drawLine(1, height - 1, width - 1, height - 1);
091: if (this .on_right)
092: g.drawLine(width - 1, 1, width - 1, height - 1);
093: if (this .on_top)
094: g.drawLine(1, 1, width - 2, 1);
095: if (this .on_left)
096: g.drawLine(1, 1, 1, height - 2);
097:
098: g.translate(-x, -y);
099: g.setColor(oldColor);
100: }
101:
102: /**
103: * Returns the insets of the border.
104: * @param c the component for which this border insets value applies
105: */
106: @Override
107: public Insets getBorderInsets(Component c) {
108: return getBorderInsets();
109: }
110:
111: /**
112: * Reinitialize the insets parameter with this Border's current Insets.
113: * @param c the component for which this border insets value applies
114: * @param insets the object to be reinitialized
115: */
116: @Override
117: public Insets getBorderInsets(Component c, Insets insets) {
118: return computeInsets(insets);
119: }
120:
121: /**
122: * Returns the insets of the border.
123: */
124: @Override
125: public Insets getBorderInsets() {
126: return computeInsets(new Insets(0, 0, 0, 0));
127: }
128:
129: protected Insets computeInsets(Insets insets) {
130: insets.left = left;
131: insets.top = top;
132: insets.right = right;
133: insets.bottom = bottom;
134: return insets;
135: }
136:
137: /**
138: * @return whether or not the border is opaque.
139: */
140: @Override
141: public boolean isBorderOpaque() {
142: return true;
143: }
144:
145: }
|