< prev index next >

src/java.desktop/share/classes/com/sun/java/swing/plaf/motif/MotifGraphicsUtils.java

Print this page




  96         drawStringInRect(null, g, aString, x, y, width, height, justification);
  97     }
  98 
  99     static void drawStringInRect(JComponent c, Graphics g, String aString,
 100                                  int x, int y, int width, int height,
 101                                  int justification) {
 102         FontMetrics  fontMetrics;
 103         int          drawWidth, startX, startY, delta;
 104 
 105         if (g.getFont() == null) {
 106 //            throw new InconsistencyException("No font set");
 107             return;
 108         }
 109         fontMetrics = SwingUtilities2.getFontMetrics(c, g);
 110         if (fontMetrics == null) {
 111 //            throw new InconsistencyException("No metrics for Font " + font());
 112             return;
 113         }
 114 
 115         if (justification == CENTER) {
 116             drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);

 117             if (drawWidth > width) {
 118                 drawWidth = width;
 119             }
 120             startX = x + (width - drawWidth) / 2;
 121         } else if (justification == RIGHT) {
 122             drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);

 123             if (drawWidth > width) {
 124                 drawWidth = width;
 125             }
 126             startX = x + width - drawWidth;
 127         } else {
 128             startX = x;
 129         }
 130 
 131         delta = (height - fontMetrics.getAscent() - fontMetrics.getDescent()) / 2;
 132         if (delta < 0) {
 133             delta = 0;
 134         }
 135 
 136         startY = y + height - delta - fontMetrics.getDescent();
 137 
 138         SwingUtilities2.drawString(c, g, aString, startX, startY);

 139     }
 140 
 141   /**
 142    * This method is not being used to paint menu item since
 143    * 6.0 This code left for compatibility only. Do not use or
 144    * override it, this will not cause any visible effect.
 145    */
 146   public static void paintMenuItem(Graphics g, JComponent c,
 147                                    Icon checkIcon, Icon arrowIcon,
 148                                    Color background, Color foreground,
 149                                    int defaultTextIconGap)
 150     {
 151 
 152         JMenuItem b = (JMenuItem) c;
 153         ButtonModel model = b.getModel();
 154 
 155         Dimension size = b.getSize();
 156         Insets i = c.getInsets();
 157 
 158         Rectangle viewRect = new Rectangle(size);


 237             }
 238 
 239             if (icon!=null) {
 240                 icon.paintIcon(c, g, iconRect.x, iconRect.y);
 241             }
 242         }
 243 
 244         // Draw the Text
 245         if(text != null && !text.equals("")) {
 246             // Once BasicHTML becomes public, use BasicHTML.propertyKey
 247             // instead of the hardcoded string below!
 248             View v = (View) c.getClientProperty("html");
 249             if (v != null) {
 250                 v.paint(g, textRect);
 251             } else {
 252                 int mnemIndex = b.getDisplayedMnemonicIndex();
 253 
 254                 if(!model.isEnabled()) {
 255                     // *** paint the text disabled
 256                     g.setColor(b.getBackground().brighter());
 257                     SwingUtilities2.drawStringUnderlineCharAt(b, g,text,
 258                         mnemIndex,
 259                         textRect.x, textRect.y + fmAccel.getAscent());
 260                     g.setColor(b.getBackground().darker());
 261                     SwingUtilities2.drawStringUnderlineCharAt(b, g,text,

 262                         mnemIndex,
 263                         textRect.x - 1, textRect.y + fmAccel.getAscent() - 1);
 264 
 265                 } else {
 266                     // *** paint the text normally
 267                     if (model.isArmed()|| (c instanceof JMenu && model.isSelected())) {
 268                         g.setColor(foreground);
 269                     } else {
 270                         g.setColor(b.getForeground());
 271                     }
 272                     SwingUtilities2.drawStringUnderlineCharAt(b, g,text,

 273                                                   mnemIndex,
 274                                                   textRect.x,
 275                                                   textRect.y + fm.getAscent());
 276                 }
 277             }
 278         }
 279 
 280         // Draw the Accelerator Text
 281         if(acceleratorText != null && !acceleratorText.equals("")) {
 282 
 283             //Get the maxAccWidth from the parent to calculate the offset.
 284             int accOffset = 0;
 285             Container parent = b.getParent();
 286             if (parent != null && parent instanceof JComponent) {
 287                 JComponent p = (JComponent) parent;
 288                 Integer maxValueInt = (Integer) p.getClientProperty(MotifGraphicsUtils.MAX_ACC_WIDTH);
 289                 int maxValue = maxValueInt != null ?
 290                     maxValueInt.intValue() : acceleratorRect.width;
 291 
 292                 //Calculate the offset, with which the accelerator texts will be drawn with.
 293                 accOffset = maxValue - acceleratorRect.width;
 294             }
 295 
 296             g.setFont( UIManager.getFont("MenuItem.acceleratorFont") );
 297             if(!model.isEnabled()) {
 298                 // *** paint the acceleratorText disabled
 299                 g.setColor(b.getBackground().brighter());
 300                 SwingUtilities2.drawString(c, g,acceleratorText,
 301                                               acceleratorRect.x - accOffset, acceleratorRect.y + fm.getAscent());
 302                 g.setColor(b.getBackground().darker());
 303                 SwingUtilities2.drawString(c, g,acceleratorText,
 304                                               acceleratorRect.x - accOffset - 1, acceleratorRect.y + fm.getAscent() - 1);
 305             } else {
 306                 // *** paint the acceleratorText normally
 307                 if (model.isArmed()|| (c instanceof JMenu && model.isSelected()))
 308                     {
 309                         g.setColor(foreground);
 310                     } else {
 311                         g.setColor(b.getForeground());
 312                     }
 313                 SwingUtilities2.drawString(c, g,acceleratorText,
 314                                               acceleratorRect.x - accOffset,
 315                                               acceleratorRect.y + fmAccel.getAscent());
 316             }
 317         }
 318 
 319         // Paint the Arrow
 320         if (arrowIcon != null) {
 321             if(model.isArmed() || (c instanceof JMenu && model.isSelected()))
 322                 g.setColor(foreground);
 323             if( !(b.getParent() instanceof JMenuBar) )
 324                 arrowIcon.paintIcon(c, g, arrowRect.x, arrowRect.y);
 325         }
 326 
 327         g.setColor(holdc);
 328         g.setFont(holdf);
 329     }
 330 
 331 
 332     /**
 333      * Compute and return the location of the icons origin, the


 366                                            icon,
 367                                            verticalAlignment,
 368                                            horizontalAlignment,
 369                                            verticalTextPosition,
 370                                            horizontalTextPosition,
 371                                            viewR,
 372                                            iconR,
 373                                            textR,
 374                                            textIconGap);
 375 
 376         /* Initialize the acceelratorText bounds rectangle textR.  If a null
 377          * or and empty String was specified we substitute "" here
 378          * and use 0,0,0,0 for acceleratorTextR.
 379          */
 380         if( (acceleratorText == null) || acceleratorText.equals("") ) {
 381             acceleratorR.width = acceleratorR.height = 0;
 382             acceleratorText = "";
 383         }
 384         else {
 385             acceleratorR.width
 386                 = SwingUtilities2.stringWidth(c, fmAccel, acceleratorText);

 387             acceleratorR.height = fmAccel.getHeight();
 388         }
 389 
 390         /* Initialize the checkIcon bounds rectangle checkIconR.
 391          */
 392 
 393         if (checkIcon != null) {
 394             checkIconR.width = checkIcon.getIconWidth();
 395             checkIconR.height = checkIcon.getIconHeight();
 396         }
 397         else {
 398             checkIconR.width = checkIconR.height = 0;
 399         }
 400 
 401         /* Initialize the arrowIcon bounds rectangle arrowIconR.
 402          */
 403 
 404         if (arrowIcon != null) {
 405             arrowIconR.width = arrowIcon.getIconWidth();
 406             arrowIconR.height = arrowIcon.getIconHeight();




  96         drawStringInRect(null, g, aString, x, y, width, height, justification);
  97     }
  98 
  99     static void drawStringInRect(JComponent c, Graphics g, String aString,
 100                                  int x, int y, int width, int height,
 101                                  int justification) {
 102         FontMetrics  fontMetrics;
 103         int          drawWidth, startX, startY, delta;
 104 
 105         if (g.getFont() == null) {
 106 //            throw new InconsistencyException("No font set");
 107             return;
 108         }
 109         fontMetrics = SwingUtilities2.getFontMetrics(c, g);
 110         if (fontMetrics == null) {
 111 //            throw new InconsistencyException("No metrics for Font " + font());
 112             return;
 113         }
 114 
 115         if (justification == CENTER) {
 116             drawWidth = SwingUtilities2.getTextUIDrawing(c)
 117                     .getStringWidth(c, fontMetrics, aString);
 118             if (drawWidth > width) {
 119                 drawWidth = width;
 120             }
 121             startX = x + (width - drawWidth) / 2;
 122         } else if (justification == RIGHT) {
 123             drawWidth = SwingUtilities2.getTextUIDrawing(c)
 124                     .getStringWidth(c, fontMetrics, aString);
 125             if (drawWidth > width) {
 126                 drawWidth = width;
 127             }
 128             startX = x + width - drawWidth;
 129         } else {
 130             startX = x;
 131         }
 132 
 133         delta = (height - fontMetrics.getAscent() - fontMetrics.getDescent()) / 2;
 134         if (delta < 0) {
 135             delta = 0;
 136         }
 137 
 138         startY = y + height - delta - fontMetrics.getDescent();
 139 
 140         SwingUtilities2.getTextUIDrawing(c)
 141                 .drawString(c, g, aString, startX, startY);
 142     }
 143 
 144   /**
 145    * This method is not being used to paint menu item since
 146    * 6.0 This code left for compatibility only. Do not use or
 147    * override it, this will not cause any visible effect.
 148    */
 149   public static void paintMenuItem(Graphics g, JComponent c,
 150                                    Icon checkIcon, Icon arrowIcon,
 151                                    Color background, Color foreground,
 152                                    int defaultTextIconGap)
 153     {
 154 
 155         JMenuItem b = (JMenuItem) c;
 156         ButtonModel model = b.getModel();
 157 
 158         Dimension size = b.getSize();
 159         Insets i = c.getInsets();
 160 
 161         Rectangle viewRect = new Rectangle(size);


 240             }
 241 
 242             if (icon!=null) {
 243                 icon.paintIcon(c, g, iconRect.x, iconRect.y);
 244             }
 245         }
 246 
 247         // Draw the Text
 248         if(text != null && !text.equals("")) {
 249             // Once BasicHTML becomes public, use BasicHTML.propertyKey
 250             // instead of the hardcoded string below!
 251             View v = (View) c.getClientProperty("html");
 252             if (v != null) {
 253                 v.paint(g, textRect);
 254             } else {
 255                 int mnemIndex = b.getDisplayedMnemonicIndex();
 256 
 257                 if(!model.isEnabled()) {
 258                     // *** paint the text disabled
 259                     g.setColor(b.getBackground().brighter());
 260                     SwingUtilities2.getTextUIDrawing(c).drawStringUnderlineCharAt(b, g,text,
 261                         mnemIndex,
 262                         textRect.x, textRect.y + fmAccel.getAscent());
 263                     g.setColor(b.getBackground().darker());
 264                     SwingUtilities2.getTextUIDrawing(c).
 265                             drawStringUnderlineCharAt(b, g,text,
 266                         mnemIndex,
 267                         textRect.x - 1, textRect.y + fmAccel.getAscent() - 1);
 268 
 269                 } else {
 270                     // *** paint the text normally
 271                     if (model.isArmed()|| (c instanceof JMenu && model.isSelected())) {
 272                         g.setColor(foreground);
 273                     } else {
 274                         g.setColor(b.getForeground());
 275                     }
 276                     SwingUtilities2.getTextUIDrawing(c)
 277                             .drawStringUnderlineCharAt(b, g,text,
 278                                                   mnemIndex,
 279                                                   textRect.x,
 280                                                   textRect.y + fm.getAscent());
 281                 }
 282             }
 283         }
 284 
 285         // Draw the Accelerator Text
 286         if(acceleratorText != null && !acceleratorText.equals("")) {
 287 
 288             //Get the maxAccWidth from the parent to calculate the offset.
 289             int accOffset = 0;
 290             Container parent = b.getParent();
 291             if (parent != null && parent instanceof JComponent) {
 292                 JComponent p = (JComponent) parent;
 293                 Integer maxValueInt = (Integer) p.getClientProperty(MotifGraphicsUtils.MAX_ACC_WIDTH);
 294                 int maxValue = maxValueInt != null ?
 295                     maxValueInt.intValue() : acceleratorRect.width;
 296 
 297                 //Calculate the offset, with which the accelerator texts will be drawn with.
 298                 accOffset = maxValue - acceleratorRect.width;
 299             }
 300 
 301             g.setFont( UIManager.getFont("MenuItem.acceleratorFont") );
 302             if(!model.isEnabled()) {
 303                 // *** paint the acceleratorText disabled
 304                 g.setColor(b.getBackground().brighter());
 305                 SwingUtilities2.getTextUIDrawing(c).drawString(c, g,acceleratorText,
 306                                               acceleratorRect.x - accOffset, acceleratorRect.y + fm.getAscent());
 307                 g.setColor(b.getBackground().darker());
 308                 SwingUtilities2.getTextUIDrawing(c).drawString(c, g,acceleratorText,
 309                                               acceleratorRect.x - accOffset - 1, acceleratorRect.y + fm.getAscent() - 1);
 310             } else {
 311                 // *** paint the acceleratorText normally
 312                 if (model.isArmed()|| (c instanceof JMenu && model.isSelected()))
 313                     {
 314                         g.setColor(foreground);
 315                     } else {
 316                         g.setColor(b.getForeground());
 317                     }
 318                 SwingUtilities2.getTextUIDrawing(c).drawString(c, g,acceleratorText,
 319                                               acceleratorRect.x - accOffset,
 320                                               acceleratorRect.y + fmAccel.getAscent());
 321             }
 322         }
 323 
 324         // Paint the Arrow
 325         if (arrowIcon != null) {
 326             if(model.isArmed() || (c instanceof JMenu && model.isSelected()))
 327                 g.setColor(foreground);
 328             if( !(b.getParent() instanceof JMenuBar) )
 329                 arrowIcon.paintIcon(c, g, arrowRect.x, arrowRect.y);
 330         }
 331 
 332         g.setColor(holdc);
 333         g.setFont(holdf);
 334     }
 335 
 336 
 337     /**
 338      * Compute and return the location of the icons origin, the


 371                                            icon,
 372                                            verticalAlignment,
 373                                            horizontalAlignment,
 374                                            verticalTextPosition,
 375                                            horizontalTextPosition,
 376                                            viewR,
 377                                            iconR,
 378                                            textR,
 379                                            textIconGap);
 380 
 381         /* Initialize the acceelratorText bounds rectangle textR.  If a null
 382          * or and empty String was specified we substitute "" here
 383          * and use 0,0,0,0 for acceleratorTextR.
 384          */
 385         if( (acceleratorText == null) || acceleratorText.equals("") ) {
 386             acceleratorR.width = acceleratorR.height = 0;
 387             acceleratorText = "";
 388         }
 389         else {
 390             acceleratorR.width
 391                 = SwingUtilities2.getTextUIDrawing(c)
 392                         .getStringWidth(c, fmAccel, acceleratorText);
 393             acceleratorR.height = fmAccel.getHeight();
 394         }
 395 
 396         /* Initialize the checkIcon bounds rectangle checkIconR.
 397          */
 398 
 399         if (checkIcon != null) {
 400             checkIconR.width = checkIcon.getIconWidth();
 401             checkIconR.height = checkIcon.getIconHeight();
 402         }
 403         else {
 404             checkIconR.width = checkIconR.height = 0;
 405         }
 406 
 407         /* Initialize the arrowIcon bounds rectangle arrowIconR.
 408          */
 409 
 410         if (arrowIcon != null) {
 411             arrowIconR.width = arrowIcon.getIconWidth();
 412             arrowIconR.height = arrowIcon.getIconHeight();


< prev index next >