1 32 33 package it.businesslogic.ireport.gui.docking; 34 35 import java.awt.*; 36 import javax.swing.*; 37 import java.beans.*; 38 39 50 public class VTextIcon implements Icon, PropertyChangeListener { 51 String fLabel; 52 String [] fCharStrings; int[] fCharWidths; int[] fPosition; int fWidth, fHeight, fCharHeight, fDescent; int fRotation; 57 Component fComponent; 58 59 static final int POSITION_NORMAL = 0; 60 static final int POSITION_TOP_RIGHT = 1; 61 static final int POSITION_FAR_TOP_RIGHT = 2; 62 63 public static final int ROTATE_DEFAULT = 0x00; 64 public static final int ROTATE_NONE = 0x01; 65 public static final int ROTATE_LEFT = 0x02; 66 public static final int ROTATE_RIGHT = 0x04; 67 68 74 public VTextIcon(Component component, String label) { 75 this(component, label, ROTATE_DEFAULT); 76 } 77 78 84 public VTextIcon(Component component, String label, int rotateHint) { 85 fComponent = component; 86 fLabel = label; 87 fRotation = verifyRotation(label, rotateHint); 88 calcDimensions(); 89 fComponent.addPropertyChangeListener(this); 90 } 91 92 97 public void setLabel(String label) { 98 fLabel = label; 99 fRotation = verifyRotation(label, fRotation); recalcDimensions(); 101 } 102 103 107 public void propertyChange(PropertyChangeEvent e) { 108 String prop = e.getPropertyName(); 109 if("font".equals(prop)) { 110 recalcDimensions(); 111 } 112 } 113 114 118 void recalcDimensions() { 119 int wOld = getIconWidth(); 120 int hOld = getIconHeight(); 121 calcDimensions(); 122 if (wOld != getIconWidth() || hOld != getIconHeight()) 123 fComponent.invalidate(); 124 } 125 126 void calcDimensions() { 127 FontMetrics fm = fComponent.getFontMetrics(fComponent.getFont()); 128 fCharHeight = fm.getAscent() + fm.getDescent(); 129 fDescent = fm.getDescent(); 130 if (fRotation == ROTATE_NONE) { 131 int len = fLabel.length(); 132 char data[] = new char[len]; 133 fLabel.getChars(0, len, data, 0); 134 fWidth = 0; 136 fCharStrings = new String [len]; 138 fCharWidths = new int[len]; 139 fPosition = new int[len]; 140 char ch; 141 for (int i = 0; i < len; i++) { 142 ch = data[i]; 143 fCharWidths[i] = fm.charWidth(ch); 144 if (fCharWidths[i] > fWidth) 145 fWidth = fCharWidths[i]; 146 fCharStrings[i] = new String (data, i, 1); 147 if (sDrawsInTopRight.indexOf(ch) >= 0) fPosition[i] = POSITION_TOP_RIGHT; 150 else if (sDrawsInFarTopRight.indexOf(ch) >= 0) 151 fPosition[i] = POSITION_FAR_TOP_RIGHT; 152 else 153 fPosition[i] = POSITION_NORMAL; 154 } 155 fHeight = fCharHeight * len + fDescent; 157 } 158 else { 159 fWidth = fCharHeight; 161 fHeight = fm.stringWidth(fLabel) + 2*kBufferSpace; 163 } 164 } 165 166 171 public void paintIcon(Component c, Graphics g, int x, int y) { 172 g.setColor(c.getForeground()); 174 g.setFont(c.getFont()); 175 Graphics2D g2 = (Graphics2D)g; 177 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 178 RenderingHints.VALUE_ANTIALIAS_ON); 179 g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, 180 RenderingHints.VALUE_STROKE_DEFAULT); 181 g2.setRenderingHint(RenderingHints.KEY_DITHERING, 182 RenderingHints.VALUE_DITHER_ENABLE); 183 184 if (fRotation == ROTATE_NONE) { 185 int yPos = y + fCharHeight; 186 for (int i = 0; i < fCharStrings.length; i++) { 187 int tweak; 191 switch (fPosition[i]) { 192 case POSITION_NORMAL: 193 g.drawString(fCharStrings[i], x+((fWidth-fCharWidths[i])/2), yPos); 195 break; 196 case POSITION_TOP_RIGHT: 197 tweak = fCharHeight/3; g.drawString(fCharStrings[i], x+(tweak/2), yPos-tweak); 199 break; 200 case POSITION_FAR_TOP_RIGHT: 201 tweak = fCharHeight - fCharHeight/3; 202 g.drawString(fCharStrings[i], x+(tweak/2), yPos-tweak); 203 break; 204 } 205 yPos += fCharHeight; 206 } 207 } 208 else if (fRotation == ROTATE_LEFT) { 209 g.translate(x+fWidth,y+fHeight); 210 ((Graphics2D)g).rotate(-NINETY_DEGREES); 211 g.drawString(fLabel, kBufferSpace, -fDescent); 212 ((Graphics2D)g).rotate(NINETY_DEGREES); 213 g.translate(-(x+fWidth),-(y+fHeight)); 214 } 215 else if (fRotation == ROTATE_RIGHT) { 216 g.translate(x,y); 217 ((Graphics2D)g).rotate(NINETY_DEGREES); 218 g.drawString(fLabel, kBufferSpace, -fDescent); 219 ((Graphics2D)g).rotate(-NINETY_DEGREES); 220 g.translate(-x,-y); 221 } 222 223 } 224 225 230 public int getIconWidth() { 231 return fWidth; 232 } 233 234 239 public int getIconHeight() { 240 return fHeight; 241 } 242 243 289 public static int verifyRotation(String label, int rotateHint) { 290 boolean hasCJK = false; 291 boolean hasMustRotate = false; 293 int len = label.length(); 294 char data[] = new char[len]; 295 char ch; 296 label.getChars(0, len, data, 0); 297 for (int i = 0; i < len; i++) { 298 ch = data[i]; 299 if ((ch >= '\u4E00' && ch <= '\u9FFF') || 300 (ch >= '\u3400' && ch <= '\u4DFF') || 301 (ch >= '\uF900' && ch <= '\uFAFF') || 302 (ch >= '\u3040' && ch <= '\u309F') || 303 (ch >= '\u30A0' && ch <= '\u30FF') ) 304 hasCJK = true; 305 if ((ch >= '\u0590' && ch <= '\u05FF') || (ch >= '\u0600' && ch <= '\u06FF') || (ch >= '\u0700' && ch <= '\u074F') ) hasMustRotate = true; 309 } 310 if (hasCJK) 312 return DEFAULT_CJK; 313 314 int legal = hasMustRotate ? LEGAL_MUST_ROTATE : LEGAL_ROMAN; 315 if ((rotateHint & legal) > 0) 316 return rotateHint; 317 318 return hasMustRotate ? DEFAULT_MUST_ROTATE : DEFAULT_ROMAN; 320 } 321 322 static final String sDrawsInTopRight = 325 "\u3041\u3043\u3045\u3047\u3049\u3063\u3083\u3085\u3087\u308E" + "\u30A1\u30A3\u30A5\u30A7\u30A9\u30C3\u30E3\u30E5\u30E7\u30EE\u30F5\u30F6"; static final String sDrawsInFarTopRight = "\u3001\u3002"; 329 static final int DEFAULT_CJK = ROTATE_NONE; 330 static final int LEGAL_ROMAN = ROTATE_NONE | ROTATE_LEFT | ROTATE_RIGHT; 331 static final int DEFAULT_ROMAN = ROTATE_RIGHT; 332 static final int LEGAL_MUST_ROTATE = ROTATE_LEFT | ROTATE_RIGHT; 333 static final int DEFAULT_MUST_ROTATE = ROTATE_LEFT; 334 335 static final double NINETY_DEGREES = Math.toRadians(90.0); 336 static final int kBufferSpace = 5; 337 } 338 | Popular Tags |