1 7 package javax.swing.plaf.synth; 8 9 import com.sun.java.swing.SwingUtilities2; 10 import java.awt.*; 11 import javax.swing.*; 12 import javax.swing.plaf.basic.BasicHTML ; 13 import javax.swing.text.*; 14 import sun.swing.plaf.synth.*; 15 16 23 public class SynthGraphicsUtils { 24 private Rectangle paintIconR = new Rectangle(); 27 private Rectangle paintTextR = new Rectangle(); 28 private Rectangle paintViewR = new Rectangle(); 29 private Insets paintInsets = new Insets(0, 0, 0, 0); 30 31 private Rectangle iconR = new Rectangle(); 34 private Rectangle textR = new Rectangle(); 35 private Rectangle viewR = new Rectangle(); 36 private Insets viewSizingInsets = new Insets(0, 0, 0, 0); 37 38 41 public SynthGraphicsUtils() { 42 } 43 44 56 public void drawLine(SynthContext context, Object paintKey, 57 Graphics g, int x1, int y1, int x2, int y2) { 58 g.drawLine(x1, y1, x2, y2); 59 } 60 61 78 public String layoutText(SynthContext ss, FontMetrics fm, 79 String text, Icon icon, int hAlign, 80 int vAlign, int hTextPosition, 81 int vTextPosition, Rectangle viewR, 82 Rectangle iconR, Rectangle textR, int iconTextGap) { 83 if (icon instanceof SynthIcon) { 84 SynthIconWrapper wrapper = SynthIconWrapper.get((SynthIcon)icon, 85 ss); 86 String formattedText = SwingUtilities.layoutCompoundLabel( 87 ss.getComponent(), fm, text, wrapper, vAlign, hAlign, 88 vTextPosition, hTextPosition, viewR, iconR, textR, 89 iconTextGap); 90 SynthIconWrapper.release(wrapper); 91 return formattedText; 92 } 93 return SwingUtilities.layoutCompoundLabel( 94 ss.getComponent(), fm, text, icon, vAlign, hAlign, 95 vTextPosition, hTextPosition, viewR, iconR, textR, 96 iconTextGap); 97 } 98 99 107 public int computeStringWidth(SynthContext ss, Font font, 108 FontMetrics metrics, String text) { 109 return SwingUtilities2.stringWidth(ss.getComponent(), metrics, 110 text); 111 } 112 113 128 public Dimension getMinimumSize(SynthContext ss, Font font, String text, 129 Icon icon, int hAlign, int vAlign, int hTextPosition, 130 int vTextPosition, int iconTextGap, int mnemonicIndex) { 131 JComponent c = ss.getComponent(); 132 Dimension size = getPreferredSize(ss, font, text, icon, hAlign, 133 vAlign, hTextPosition, vTextPosition, 134 iconTextGap, mnemonicIndex); 135 View v = (View) c.getClientProperty(BasicHTML.propertyKey); 136 137 if (v != null) { 138 size.width -= v.getPreferredSpan(View.X_AXIS) - 139 v.getMinimumSpan(View.X_AXIS); 140 } 141 return size; 142 } 143 144 159 public Dimension getMaximumSize(SynthContext ss, Font font, String text, 160 Icon icon, int hAlign, int vAlign, int hTextPosition, 161 int vTextPosition, int iconTextGap, int mnemonicIndex) { 162 JComponent c = ss.getComponent(); 163 Dimension size = getPreferredSize(ss, font, text, icon, hAlign, 164 vAlign, hTextPosition, vTextPosition, 165 iconTextGap, mnemonicIndex); 166 View v = (View) c.getClientProperty(BasicHTML.propertyKey); 167 168 if (v != null) { 169 size.width += v.getMaximumSpan(View.X_AXIS) - 170 v.getPreferredSpan(View.X_AXIS); 171 } 172 return size; 173 } 174 175 183 public int getMaximumCharHeight(SynthContext context) { 184 FontMetrics fm = context.getComponent().getFontMetrics( 185 context.getStyle().getFont(context)); 186 return (fm.getAscent() + fm.getDescent()); 187 } 188 189 204 public Dimension getPreferredSize(SynthContext ss, Font font, String text, 205 Icon icon, int hAlign, int vAlign, int hTextPosition, 206 int vTextPosition, int iconTextGap, int mnemonicIndex) { 207 JComponent c = ss.getComponent(); 208 Insets insets = c.getInsets(viewSizingInsets); 209 int dx = insets.left + insets.right; 210 int dy = insets.top + insets.bottom; 211 212 if (icon == null && (text == null || font == null)) { 213 return new Dimension(dx, dy); 214 } 215 else if ((text == null) || ((icon != null) && (font == null))) { 216 return new Dimension(SynthIcon.getIconWidth(icon, ss) + dx, 217 SynthIcon.getIconHeight(icon, ss) + dy); 218 } 219 else { 220 FontMetrics fm = c.getFontMetrics(font); 221 222 iconR.x = iconR.y = iconR.width = iconR.height = 0; 223 textR.x = textR.y = textR.width = textR.height = 0; 224 viewR.x = dx; 225 viewR.y = dy; 226 viewR.width = viewR.height = Short.MAX_VALUE; 227 228 layoutText(ss, fm, text, icon, hAlign, vAlign, 229 hTextPosition, vTextPosition, viewR, iconR, textR, 230 iconTextGap); 231 int x1 = Math.min(iconR.x, textR.x); 232 int x2 = Math.max(iconR.x + iconR.width, textR.x + textR.width); 233 int y1 = Math.min(iconR.y, textR.y); 234 int y2 = Math.max(iconR.y + iconR.height, textR.y + textR.height); 235 Dimension rv = new Dimension(x2 - x1, y2 - y1); 236 237 rv.width += dx; 238 rv.height += dy; 239 return rv; 240 } 241 } 242 243 254 public void paintText(SynthContext ss, Graphics g, String text, 255 Rectangle bounds, int mnemonicIndex) { 256 paintText(ss, g, text, bounds.x, bounds.y, mnemonicIndex); 257 } 258 259 271 public void paintText(SynthContext ss, Graphics g, String text, 272 int x, int y, int mnemonicIndex) { 273 if (text != null) { 274 JComponent c = ss.getComponent(); 275 SynthStyle style = ss.getStyle(); 276 FontMetrics fm = SwingUtilities2.getFontMetrics(c, g); 277 278 y += fm.getAscent(); 279 SwingUtilities2.drawString(c, g, text, x, y); 280 if (mnemonicIndex >= 0 && mnemonicIndex < text.length()) { 281 int underlineX = x + SwingUtilities2.stringWidth( 282 c, fm, text.substring(0, mnemonicIndex)); 283 int underlineY = y; 284 int underlineWidth = fm.charWidth(text.charAt(mnemonicIndex)); 285 int underlineHeight = 1; 286 287 g.fillRect(underlineX, underlineY + fm.getDescent() - 1, 288 underlineWidth, underlineHeight); 289 } 290 } 291 } 292 293 310 public void paintText(SynthContext ss, Graphics g, String text, 311 Icon icon, int hAlign, int vAlign, int hTextPosition, 312 int vTextPosition, int iconTextGap, int mnemonicIndex, 313 int textOffset) { 314 if ((icon == null) && (text == null)) { 315 return; 316 } 317 JComponent c = ss.getComponent(); 318 FontMetrics fm = SwingUtilities2.getFontMetrics(c, g); 319 Insets insets = SynthLookAndFeel.getPaintingInsets(ss, paintInsets); 320 321 paintViewR.x = insets.left; 322 paintViewR.y = insets.top; 323 paintViewR.width = c.getWidth() - (insets.left + insets.right); 324 paintViewR.height = c.getHeight() - (insets.top + insets.bottom); 325 326 paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0; 327 paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0; 328 329 String clippedText = 330 layoutText(ss, fm, text, icon, hAlign, vAlign, 331 hTextPosition, vTextPosition, paintViewR, paintIconR, 332 paintTextR, iconTextGap); 333 334 if (icon != null) { 335 Color color = g.getColor(); 336 paintIconR.x += textOffset; 337 paintIconR.y += textOffset; 338 SynthIcon.paintIcon(icon, ss, g, paintIconR.x, paintIconR.y, 339 paintIconR.width, paintIconR.height); 340 g.setColor(color); 341 } 342 343 if (text != null) { 344 View v = (View) c.getClientProperty(BasicHTML.propertyKey); 345 346 if (v != null) { 347 v.paint(g, paintTextR); 348 } else { 349 paintTextR.x += textOffset; 350 paintTextR.y += textOffset; 351 352 paintText(ss, g, clippedText, paintTextR, mnemonicIndex); 353 } 354 } 355 } 356 357 358 362 private static class SynthIconWrapper implements Icon { 363 private static final java.util.List CACHE = new java.util.ArrayList (1); 364 365 private SynthIcon synthIcon; 366 private SynthContext context; 367 368 static SynthIconWrapper get(SynthIcon icon, SynthContext context) { 369 synchronized(CACHE) { 370 int size = CACHE.size(); 371 if (size > 0) { 372 SynthIconWrapper wrapper = (SynthIconWrapper)CACHE.remove( 373 size - 1); 374 wrapper.reset(icon, context); 375 return wrapper; 376 } 377 } 378 return new SynthIconWrapper(icon, context); 379 } 380 381 static void release(SynthIconWrapper wrapper) { 382 wrapper.reset(null, null); 383 synchronized(CACHE) { 384 CACHE.add(wrapper); 385 } 386 } 387 388 SynthIconWrapper(SynthIcon icon, SynthContext context) { 389 reset(icon, context); 390 } 391 392 void reset(SynthIcon icon, SynthContext context) { 393 synthIcon = icon; 394 this.context = context; 395 } 396 397 public void paintIcon(Component c, Graphics g, int x, int y) { 398 } 400 401 public int getIconWidth() { 402 return synthIcon.getIconWidth(context); 403 } 404 405 public int getIconHeight() { 406 return synthIcon.getIconHeight(context); 407 } 408 } 409 } 410 | Popular Tags |