| 1 |
package com.jidesoft.plaf.basic; |
| 2 |
|
| 3 |
import com.jidesoft.swing.JideLabel; |
| 4 |
import com.jidesoft.swing.JideSwingUtilities; |
| 5 |
|
| 6 |
import javax.swing.*; |
| 7 |
import javax.swing.plaf.ComponentUI; |
| 8 |
import javax.swing.plaf.LabelUI; |
| 9 |
import javax.swing.plaf.basic.BasicHTML; |
| 10 |
import javax.swing.plaf.basic.BasicLabelUI; |
| 11 |
import javax.swing.text.View; |
| 12 |
import java.awt.*; |
| 13 |
import java.awt.geom.AffineTransform; |
| 14 |
import java.beans.PropertyChangeEvent; |
| 15 |
|
| 16 |
public class BasicJideLabelUI extends BasicLabelUI { |
| 17 |
private static final LabelUI INSTANCE = new BasicJideLabelUI(); |
| 18 |
|
| 19 |
public static ComponentUI createUI(JComponent c) { |
| 20 |
return INSTANCE; |
| 21 |
} |
| 22 |
|
| 23 |
@Override |
| 24 |
protected void installDefaults(JLabel c) { |
| 25 |
super.installDefaults(c); |
| 26 |
LookAndFeel.installColorsAndFont(c, "JideLabel.background", "JideLabel.foreground", "JideLabel.font"); |
| 27 |
} |
| 28 |
|
| 29 |
@Override |
| 30 |
public Dimension getPreferredSize(JComponent c) { |
| 31 |
Dimension d = super.getPreferredSize(c); |
| 32 |
if (JideSwingUtilities.getOrientationOf(c) == SwingConstants.HORIZONTAL) { |
| 33 |
return d; |
| 34 |
} |
| 35 |
else { |
| 36 |
//noinspection SuspiciousNameCombination |
| 37 |
return new Dimension(d.height, d.width); // swap width and height |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
private static Rectangle paintIconR = new Rectangle(); |
| 42 |
private static Rectangle paintTextR = new Rectangle(); |
| 43 |
private static Rectangle paintViewR = new Rectangle(); |
| 44 |
private static Insets paintViewInsets = new Insets(0, 0, 0, 0); |
| 45 |
|
| 46 |
@Override |
| 47 |
public void paint(Graphics g, JComponent c) { |
| 48 |
if (JideSwingUtilities.getOrientationOf(c) == SwingConstants.VERTICAL) { |
| 49 |
boolean clockwise = true; |
| 50 |
if (c instanceof JideLabel) { |
| 51 |
clockwise = ((JideLabel) c).isClockwise(); |
| 52 |
} |
| 53 |
paintVertically(g, c, clockwise); |
| 54 |
} |
| 55 |
else { |
| 56 |
super.paint(g, c); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public void paintVertically(Graphics g, JComponent c, boolean clockwise) { |
| 61 |
|
| 62 |
JLabel label = (JLabel) c; |
| 63 |
String text = label.getText(); |
| 64 |
Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon(); |
| 65 |
|
| 66 |
if ((icon == null) && (text == null)) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
FontMetrics fm = c.getFontMetrics(c.getFont()); |
| 71 |
paintViewInsets = c.getInsets(paintViewInsets); |
| 72 |
|
| 73 |
paintViewR.x = paintViewInsets.left; |
| 74 |
paintViewR.y = paintViewInsets.top; |
| 75 |
|
| 76 |
// Use inverted height & width |
| 77 |
paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right); |
| 78 |
paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom); |
| 79 |
|
| 80 |
paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0; |
| 81 |
paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0; |
| 82 |
|
| 83 |
String clippedText = layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR); |
| 84 |
|
| 85 |
Graphics2D g2 = (Graphics2D) g; |
| 86 |
AffineTransform tr = g2.getTransform(); |
| 87 |
if (clockwise) { |
| 88 |
g2.rotate(Math.PI / 2); |
| 89 |
g2.translate(0, -c.getWidth()); |
| 90 |
} |
| 91 |
else { |
| 92 |
g2.rotate(-Math.PI / 2); |
| 93 |
g2.translate(-c.getHeight(), 0); |
| 94 |
} |
| 95 |
|
| 96 |
if (icon != null) { |
| 97 |
icon.paintIcon(c, g, paintIconR.x, paintIconR.y); |
| 98 |
} |
| 99 |
|
| 100 |
if (text != null) { |
| 101 |
View v = (View) c.getClientProperty(BasicHTML.propertyKey); |
| 102 |
if (v != null) { |
| 103 |
v.paint(g, paintTextR); |
| 104 |
} |
| 105 |
else { |
| 106 |
int textX = paintTextR.x; |
| 107 |
int textY = paintTextR.y + fm.getAscent(); |
| 108 |
|
| 109 |
if (label.isEnabled()) { |
| 110 |
paintEnabledText(label, g, clippedText, textX, textY); |
| 111 |
} |
| 112 |
else { |
| 113 |
paintDisabledText(label, g, clippedText, textX, textY); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
g2.setTransform(tr); |
| 120 |
} |
| 121 |
|
| 122 |
public void propertyChange(PropertyChangeEvent e) { |
| 123 |
super.propertyChange(e); |
| 124 |
if (JideLabel.PROPERTY_ORIENTATION == e.getPropertyName()) { |
| 125 |
if (e.getSource() instanceof JLabel) { |
| 126 |
JLabel label = (JLabel) e.getSource(); |
| 127 |
label.revalidate(); |
| 128 |
} |
| 129 |
} |
| 130 |
else if (JideLabel.PROPERTY_CLOCKWISE.equals(e.getPropertyName())) { |
| 131 |
if (e.getSource() instanceof JLabel) { |
| 132 |
JLabel label = (JLabel) e.getSource(); |
| 133 |
label.repaint(); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} |