1 17 18 package org.sape.carbon.services.swing.ui; 19 20 import java.awt.Dimension ; 21 import java.awt.FontMetrics ; 22 import java.awt.Graphics ; 23 import java.awt.Graphics2D ; 24 import java.awt.Insets ; 25 import java.awt.Rectangle ; 26 import java.awt.geom.AffineTransform ; 27 28 import javax.swing.Icon ; 29 import javax.swing.JComponent ; 30 import javax.swing.JLabel ; 31 import javax.swing.plaf.basic.BasicLabelUI ; 32 33 42 public class VerticalLabelUI extends BasicLabelUI { 43 static { 44 labelUI = new VerticalLabelUI(false); 45 } 46 47 protected boolean clockwise; 48 49 50 public VerticalLabelUI(boolean clockwise) { 51 super(); 52 this.clockwise = clockwise; 53 } 54 55 56 public Dimension getPreferredSize(JComponent c) { 57 Dimension dim = super.getPreferredSize(c); 58 return new Dimension ( dim.height, dim.width ); 59 } 60 61 private static Rectangle paintIconR = new Rectangle (); 62 private static Rectangle paintTextR = new Rectangle (); 63 private static Rectangle paintViewR = new Rectangle (); 64 private static Insets paintViewInsets = new Insets (0, 0, 0, 0); 65 66 public void paint(Graphics g, JComponent c) { 67 68 JLabel label = (JLabel )c; 69 String text = label.getText(); 70 Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon(); 71 72 if ((icon == null) && (text == null)) { 73 return; 74 } 75 76 FontMetrics fm = g.getFontMetrics(); 77 paintViewInsets = c.getInsets(paintViewInsets); 78 79 paintViewR.x = paintViewInsets.left; 80 paintViewR.y = paintViewInsets.top; 81 82 paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right); 84 paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom); 85 86 paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0; 87 paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0; 88 89 String clippedText = 90 layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR); 91 92 Graphics2D g2 = (Graphics2D ) g; 93 AffineTransform tr = g2.getTransform(); 94 if (clockwise) { 95 g2.rotate( Math.PI / 2 ); 96 g2.translate( 0, - c.getWidth() ); 97 } else { 98 g2.rotate( - Math.PI / 2 ); 99 g2.translate( - c.getHeight(), 0 ); 100 } 101 102 if (icon != null) { 103 icon.paintIcon(c, g, paintIconR.x, paintIconR.y); 104 } 105 106 if (text != null) { 107 int textX = paintTextR.x; 108 int textY = paintTextR.y + fm.getAscent(); 109 110 if (label.isEnabled()) { 111 paintEnabledText(label, g, clippedText, textX, textY); 112 } else { 113 paintDisabledText(label, g, clippedText, textX, textY); 114 } 115 } 116 117 g2.setTransform( tr ); 118 } 119 } | Popular Tags |