1 7 package com.sun.java.swing.plaf.motif; 8 9 import com.sun.java.swing.SwingUtilities2; 10 11 import javax.swing.*; 12 import java.awt.Color ; 13 import java.awt.Dimension ; 14 import java.awt.Graphics ; 15 import java.awt.Font ; 16 import java.awt.FontMetrics ; 17 import java.awt.Rectangle ; 18 import java.awt.Component ; 19 import java.awt.Insets ; 20 import java.awt.event.KeyEvent ; 21 import java.awt.Container ; 22 23 import javax.swing.plaf.basic.*; 24 import javax.swing.text.View ; 25 26 31 32 public class MotifGraphicsUtils implements SwingConstants 33 { 34 35 private static final String MAX_ACC_WIDTH = "maxAccWidth"; 36 37 40 static void drawPoint(Graphics g, int x, int y) { 41 g.drawLine(x, y, x, y); 42 } 43 44 48 public static void drawGroove(Graphics g, int x, int y, int w, int h, 49 Color shadow, Color highlight) 50 { 51 Color oldColor = g.getColor(); g.translate(x, y); 53 54 g.setColor(shadow); 55 g.drawRect(0, 0, w-2, h-2); 56 57 g.setColor(highlight); 58 g.drawLine(1, h-3, 1, 1); 59 g.drawLine(1, 1, w-3, 1); 60 61 g.drawLine(0, h-1, w-1, h-1); 62 g.drawLine(w-1, h-1, w-1, 0); 63 64 g.translate(-x, -y); 65 g.setColor(oldColor); 66 } 67 68 77 public static void drawStringInRect(Graphics g, String aString, int x, int y, 78 int width, int height, int justification) { 79 drawStringInRect(null, g, aString, x, y, width, height, justification); 80 } 81 82 static void drawStringInRect(JComponent c, Graphics g, String aString, 83 int x, int y, int width, int height, 84 int justification) { 85 FontMetrics fontMetrics; 86 int drawWidth, startX, startY, delta; 87 88 if (g.getFont() == null) { 89 return; 91 } 92 fontMetrics = SwingUtilities2.getFontMetrics(c, g); 93 if (fontMetrics == null) { 94 return; 96 } 97 98 if (justification == CENTER) { 99 drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString); 100 if (drawWidth > width) { 101 drawWidth = width; 102 } 103 startX = x + (width - drawWidth) / 2; 104 } else if (justification == RIGHT) { 105 drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString); 106 if (drawWidth > width) { 107 drawWidth = width; 108 } 109 startX = x + width - drawWidth; 110 } else { 111 startX = x; 112 } 113 114 delta = (height - fontMetrics.getAscent() - fontMetrics.getDescent()) / 2; 115 if (delta < 0) { 116 delta = 0; 117 } 118 119 startY = y + height - delta - fontMetrics.getDescent(); 120 121 SwingUtilities2.drawString(c, g, aString, startX, startY); 122 } 123 124 125 public static void paintMenuItem(Graphics g, JComponent c, 126 Icon checkIcon, Icon arrowIcon, 127 Color background, Color foreground, 128 int defaultTextIconGap) 129 { 130 131 JMenuItem b = (JMenuItem) c; 132 ButtonModel model = b.getModel(); 133 134 Dimension size = b.getSize(); 135 Insets i = c.getInsets(); 136 137 Rectangle viewRect = new Rectangle (size); 138 139 viewRect.x += i.left; 140 viewRect.y += i.top; 141 viewRect.width -= (i.right + viewRect.x); 142 viewRect.height -= (i.bottom + viewRect.y); 143 144 Rectangle iconRect = new Rectangle (); 145 Rectangle textRect = new Rectangle (); 146 Rectangle acceleratorRect = new Rectangle (); 147 Rectangle checkRect = new Rectangle (); 148 Rectangle arrowRect = new Rectangle (); 149 150 Font holdf = g.getFont(); 151 Font f = c.getFont(); 152 g.setFont(f); 153 FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f); 154 FontMetrics fmAccel = SwingUtilities2.getFontMetrics( 155 c, g, UIManager.getFont("MenuItem.acceleratorFont")); 156 157 if (c.isOpaque()) { 158 if (model.isArmed()|| (c instanceof JMenu && model.isSelected())) { 159 g.setColor(background); 160 } else { 161 g.setColor(c.getBackground()); 162 } 163 g.fillRect(0,0, size.width, size.height); 164 } 165 166 KeyStroke accelerator = b.getAccelerator(); 168 String acceleratorText = ""; 169 if (accelerator != null) { 170 int modifiers = accelerator.getModifiers(); 171 if (modifiers > 0) { 172 acceleratorText = KeyEvent.getKeyModifiersText(modifiers); 173 acceleratorText += "+"; 174 } 175 acceleratorText += KeyEvent.getKeyText(accelerator.getKeyCode()); 176 } 177 178 String text = layoutMenuItem(c, fm, b.getText(), fmAccel, 180 acceleratorText, b.getIcon(), 181 checkIcon, arrowIcon, 182 b.getVerticalAlignment(), 183 b.getHorizontalAlignment(), 184 b.getVerticalTextPosition(), 185 b.getHorizontalTextPosition(), 186 viewRect, iconRect, 187 textRect, acceleratorRect, 188 checkRect, arrowRect, 189 b.getText() == null 190 ? 0 : defaultTextIconGap, 191 defaultTextIconGap 192 ); 193 194 Color holdc = g.getColor(); 196 if (checkIcon != null) { 197 if(model.isArmed() || (c instanceof JMenu && model.isSelected())) 198 g.setColor(foreground); 199 checkIcon.paintIcon(c, g, checkRect.x, checkRect.y); 200 g.setColor(holdc); 201 } 202 203 if(b.getIcon() != null) { 205 Icon icon; 206 if(!model.isEnabled()) { 207 icon = (Icon) b.getDisabledIcon(); 208 } else if(model.isPressed() && model.isArmed()) { 209 icon = (Icon) b.getPressedIcon(); 210 if(icon == null) { 211 icon = (Icon) b.getIcon(); 213 } 214 } else { 215 icon = (Icon) b.getIcon(); 216 } 217 218 if (icon!=null) { 219 icon.paintIcon(c, g, iconRect.x, iconRect.y); 220 } 221 } 222 223 if(text != null && !text.equals("")) { 225 View v = (View ) c.getClientProperty("html"); 228 if (v != null) { 229 v.paint(g, textRect); 230 } else { 231 int mnemIndex = b.getDisplayedMnemonicIndex(); 232 233 if(!model.isEnabled()) { 234 g.setColor(b.getBackground().brighter()); 236 SwingUtilities2.drawStringUnderlineCharAt(b, g,text, 237 mnemIndex, 238 textRect.x, textRect.y + fmAccel.getAscent()); 239 g.setColor(b.getBackground().darker()); 240 SwingUtilities2.drawStringUnderlineCharAt(b, g,text, 241 mnemIndex, 242 textRect.x - 1, textRect.y + fmAccel.getAscent() - 1); 243 244 } else { 245 if (model.isArmed()|| (c instanceof JMenu && model.isSelected())) { 247 g.setColor(foreground); 248 } else { 249 g.setColor(b.getForeground()); 250 } 251 SwingUtilities2.drawStringUnderlineCharAt(b, g,text, 252 mnemIndex, 253 textRect.x, 254 textRect.y + fm.getAscent()); 255 } 256 } 257 } 258 259 if(acceleratorText != null && !acceleratorText.equals("")) { 261 262 int accOffset = 0; 264 Container parent = b.getParent(); 265 if (parent != null && parent instanceof JComponent) { 266 JComponent p = (JComponent) parent; 267 Integer maxValueInt = (Integer ) p.getClientProperty(MotifGraphicsUtils.MAX_ACC_WIDTH); 268 int maxValue = maxValueInt != null ? 269 maxValueInt.intValue() : acceleratorRect.width; 270 271 accOffset = maxValue - acceleratorRect.width; 273 } 274 275 g.setFont( UIManager.getFont("MenuItem.acceleratorFont") ); 276 if(!model.isEnabled()) { 277 g.setColor(b.getBackground().brighter()); 279 SwingUtilities2.drawString(c, g,acceleratorText, 280 acceleratorRect.x - accOffset, acceleratorRect.y + fm.getAscent()); 281 g.setColor(b.getBackground().darker()); 282 SwingUtilities2.drawString(c, g,acceleratorText, 283 acceleratorRect.x - accOffset - 1, acceleratorRect.y + fm.getAscent() - 1); 284 } else { 285 if (model.isArmed()|| (c instanceof JMenu && model.isSelected())) 287 { 288 g.setColor(foreground); 289 } else { 290 g.setColor(b.getForeground()); 291 } 292 SwingUtilities2.drawString(c, g,acceleratorText, 293 acceleratorRect.x - accOffset, 294 acceleratorRect.y + fmAccel.getAscent()); 295 } 296 } 297 298 if (arrowIcon != null) { 300 if(model.isArmed() || (c instanceof JMenu && model.isSelected())) 301 g.setColor(foreground); 302 if( !(b.getParent() instanceof JMenuBar) ) 303 arrowIcon.paintIcon(c, g, arrowRect.x, arrowRect.y); 304 } 305 306 g.setColor(holdc); 307 g.setFont(holdf); 308 } 309 310 311 317 318 private static String layoutMenuItem( 319 JComponent c, 320 FontMetrics fm, 321 String text, 322 FontMetrics fmAccel, 323 String acceleratorText, 324 Icon icon, 325 Icon checkIcon, 326 Icon arrowIcon, 327 int verticalAlignment, 328 int horizontalAlignment, 329 int verticalTextPosition, 330 int horizontalTextPosition, 331 Rectangle viewR, 332 Rectangle iconR, 333 Rectangle textR, 334 Rectangle acceleratorR, 335 Rectangle checkIconR, 336 Rectangle arrowIconR, 337 int textIconGap, 338 int menuItemGap 339 ) 340 { 341 342 SwingUtilities.layoutCompoundLabel(c, 343 fm, 344 text, 345 icon, 346 verticalAlignment, 347 horizontalAlignment, 348 verticalTextPosition, 349 horizontalTextPosition, 350 viewR, 351 iconR, 352 textR, 353 textIconGap); 354 355 359 if( (acceleratorText == null) || acceleratorText.equals("") ) { 360 acceleratorR.width = acceleratorR.height = 0; 361 acceleratorText = ""; 362 } 363 else { 364 acceleratorR.width 365 = SwingUtilities2.stringWidth(c, fmAccel, acceleratorText); 366 acceleratorR.height = fmAccel.getHeight(); 367 } 368 369 371 372 if (checkIcon != null) { 373 checkIconR.width = checkIcon.getIconWidth(); 374 checkIconR.height = checkIcon.getIconHeight(); 375 } 376 else { 377 checkIconR.width = checkIconR.height = 0; 378 } 379 380 382 383 if (arrowIcon != null) { 384 arrowIconR.width = arrowIcon.getIconWidth(); 385 arrowIconR.height = arrowIcon.getIconHeight(); 386 } 387 else { 388 arrowIconR.width = arrowIconR.height = 0; 389 } 390 391 392 Rectangle labelR = iconR.union(textR); 393 if( MotifGraphicsUtils.isLeftToRight(c) ) { 394 textR.x += checkIconR.width + menuItemGap; 395 iconR.x += checkIconR.width + menuItemGap; 396 397 acceleratorR.x = viewR.x + viewR.width - arrowIconR.width 399 - menuItemGap - acceleratorR.width; 400 401 checkIconR.x = viewR.x; 403 arrowIconR.x = viewR.x + viewR.width - menuItemGap 404 - arrowIconR.width; 405 } else { 406 textR.x -= (checkIconR.width + menuItemGap); 407 iconR.x -= (checkIconR.width + menuItemGap); 408 409 acceleratorR.x = viewR.x + arrowIconR.width + menuItemGap; 411 412 checkIconR.x = viewR.x + viewR.width - checkIconR.width; 414 arrowIconR.x = viewR.x + menuItemGap; 415 } 416 417 acceleratorR.y = labelR.y + (labelR.height/2) - (acceleratorR.height/2); 420 arrowIconR.y = labelR.y + (labelR.height/2) - (arrowIconR.height/2); 421 checkIconR.y = labelR.y + (labelR.height/2) - (checkIconR.height/2); 422 423 427 return text; 428 } 429 430 private static void drawMenuBezel(Graphics g, Color background, 431 int x, int y, 432 int width, int height) 433 { 434 g.setColor(background); 436 g.fillRect(x,y,width,height); 437 438 g.setColor(background.brighter().brighter()); 439 g.drawLine(x+1, y+height-1, x+width-1, y+height-1); 440 g.drawLine(x+width-1, y+height-2, x+width-1, y+1); 441 442 g.setColor(background.darker().darker()); 443 g.drawLine(x, y, x+width-2, y); 444 g.drawLine(x, y+1, x, y+height-2); 445 446 } 447 448 452 static boolean isLeftToRight( Component c ) { 453 return c.getComponentOrientation().isLeftToRight(); 454 } 455 } 456 | Popular Tags |