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