Save This Page
Home » openjdk-7 » javax » swing » plaf » metal » [javadoc | source]
    1   /*
    2    * Copyright 1998-2006 Sun Microsystems, Inc.  All Rights Reserved.
    3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    4    *
    5    * This code is free software; you can redistribute it and/or modify it
    6    * under the terms of the GNU General Public License version 2 only, as
    7    * published by the Free Software Foundation.  Sun designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Sun in the LICENSE file that accompanied this code.
   10    *
   11    * This code is distributed in the hope that it will be useful, but WITHOUT
   12    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13    * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   14    * version 2 for more details (a copy is included in the LICENSE file that
   15    * accompanied this code).
   16    *
   17    * You should have received a copy of the GNU General Public License version
   18    * 2 along with this work; if not, write to the Free Software Foundation,
   19    * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   20    *
   21    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   22    * CA 95054 USA or visit www.sun.com if you need additional information or
   23    * have any questions.
   24    */
   25   
   26   package javax.swing.plaf.metal;
   27   
   28   import javax.swing;
   29   import javax.swing.border;
   30   import javax.swing.plaf;
   31   import javax.swing.plaf.basic.BasicBorders;
   32   import javax.swing.text.JTextComponent;
   33   
   34   import java.awt.Component;
   35   import java.awt.Insets;
   36   import java.awt.Dimension;
   37   import java.awt.Rectangle;
   38   import java.awt.Color;
   39   import java.awt.Dialog;
   40   import java.awt.Frame;
   41   import java.awt.Graphics;
   42   import java.awt.Window;
   43   import java.io.Serializable;
   44   
   45   import sun.swing.StringUIClientPropertyKey;
   46   
   47   
   48   /**
   49    * Factory object that can vend Borders appropriate for the metal L & F.
   50    * @author Steve Wilson
   51    */
   52   
   53   public class MetalBorders {
   54   
   55       /**
   56        * Client property indicating the button shouldn't provide a rollover
   57        * indicator. Only used with the Ocean theme.
   58        */
   59       static Object NO_BUTTON_ROLLOVER =
   60           new StringUIClientPropertyKey("NoButtonRollover");
   61   
   62   
   63       public static class Flush3DBorder extends AbstractBorder implements UIResource{
   64           public void paintBorder(Component c, Graphics g, int x, int y,
   65                             int w, int h) {
   66               if (c.isEnabled()) {
   67                   MetalUtils.drawFlush3DBorder(g, x, y, w, h);
   68               } else {
   69                   MetalUtils.drawDisabledBorder(g, x, y, w, h);
   70               }
   71           }
   72   
   73           public Insets getBorderInsets(Component c, Insets newInsets) {
   74               newInsets.set(2, 2, 2, 2);
   75               return newInsets;
   76           }
   77       }
   78   
   79       public static class ButtonBorder extends AbstractBorder implements UIResource {
   80   
   81           protected static Insets borderInsets = new Insets( 3, 3, 3, 3 );
   82   
   83           public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
   84               if (MetalLookAndFeel.usingOcean()) {
   85                   paintOceanBorder(c, g, x, y, w, h);
   86                   return;
   87               }
   88               AbstractButton button = (AbstractButton)c;
   89               ButtonModel model = button.getModel();
   90   
   91               if ( model.isEnabled() ) {
   92                   boolean isPressed = model.isPressed() && model.isArmed();
   93                   boolean isDefault = (button instanceof JButton && ((JButton)button).isDefaultButton());
   94   
   95                   if (isPressed && isDefault) {
   96                       MetalUtils.drawDefaultButtonPressedBorder(g, x, y, w, h);
   97                   } else if (isPressed) {
   98                       MetalUtils.drawPressed3DBorder( g, x, y, w, h );
   99                   } else if (isDefault) {
  100                       MetalUtils.drawDefaultButtonBorder( g, x, y, w, h, false);
  101                   } else {
  102                       MetalUtils.drawButtonBorder( g, x, y, w, h, false);
  103                   }
  104               } else { // disabled state
  105                   MetalUtils.drawDisabledBorder( g, x, y, w-1, h-1 );
  106               }
  107           }
  108   
  109           private void paintOceanBorder(Component c, Graphics g, int x, int y,
  110                                         int w, int h) {
  111               AbstractButton button = (AbstractButton)c;
  112               ButtonModel model = ((AbstractButton)c).getModel();
  113   
  114               g.translate(x, y);
  115               if (MetalUtils.isToolBarButton(button)) {
  116                   if (model.isEnabled()) {
  117                       if (model.isPressed()) {
  118                           g.setColor(MetalLookAndFeel.getWhite());
  119                           g.fillRect(1, h - 1, w - 1, 1);
  120                           g.fillRect(w - 1, 1, 1, h - 1);
  121                           g.setColor(MetalLookAndFeel.getControlDarkShadow());
  122                           g.drawRect(0, 0, w - 2, h - 2);
  123                           g.fillRect(1, 1, w - 3, 1);
  124                       }
  125                       else if (model.isSelected() || model.isRollover()) {
  126                           g.setColor(MetalLookAndFeel.getWhite());
  127                           g.fillRect(1, h - 1, w - 1, 1);
  128                           g.fillRect(w - 1, 1, 1, h - 1);
  129                           g.setColor(MetalLookAndFeel.getControlDarkShadow());
  130                           g.drawRect(0, 0, w - 2, h - 2);
  131                       }
  132                       else {
  133                           g.setColor(MetalLookAndFeel.getWhite());
  134                           g.drawRect(1, 1, w - 2, h - 2);
  135                           g.setColor(UIManager.getColor(
  136                                   "Button.toolBarBorderBackground"));
  137                           g.drawRect(0, 0, w - 2, h - 2);
  138                       }
  139                   }
  140                   else {
  141                      g.setColor(UIManager.getColor(
  142                              "Button.disabledToolBarBorderBackground"));
  143                      g.drawRect(0, 0, w - 2, h - 2);
  144                   }
  145               }
  146               else if (model.isEnabled()) {
  147                   boolean pressed = model.isPressed();
  148                   boolean armed = model.isArmed();
  149   
  150                   if ((c instanceof JButton) && ((JButton)c).isDefaultButton()) {
  151                       g.setColor(MetalLookAndFeel.getControlDarkShadow());
  152                       g.drawRect(0, 0, w - 1, h - 1);
  153                       g.drawRect(1, 1, w - 3, h - 3);
  154                   }
  155                   else if (pressed) {
  156                       g.setColor(MetalLookAndFeel.getControlDarkShadow());
  157                       g.fillRect(0, 0, w, 2);
  158                       g.fillRect(0, 2, 2, h - 2);
  159                       g.fillRect(w - 1, 1, 1, h - 1);
  160                       g.fillRect(1, h - 1, w - 2, 1);
  161                   }
  162                   else if (model.isRollover() && button.getClientProperty(
  163                                  NO_BUTTON_ROLLOVER) == null) {
  164                       g.setColor(MetalLookAndFeel.getPrimaryControl());
  165                       g.drawRect(0, 0, w - 1, h - 1);
  166                       g.drawRect(2, 2, w - 5, h - 5);
  167                       g.setColor(MetalLookAndFeel.getControlDarkShadow());
  168                       g.drawRect(1, 1, w - 3, h - 3);
  169                   }
  170                   else {
  171                       g.setColor(MetalLookAndFeel.getControlDarkShadow());
  172                       g.drawRect(0, 0, w - 1, h - 1);
  173                   }
  174               }
  175               else {
  176                   g.setColor(MetalLookAndFeel.getInactiveControlTextColor());
  177                   g.drawRect(0, 0, w - 1, h - 1);
  178                   if ((c instanceof JButton) && ((JButton)c).isDefaultButton()) {
  179                       g.drawRect(1, 1, w - 3, h - 3);
  180                   }
  181               }
  182           }
  183   
  184           public Insets getBorderInsets(Component c, Insets newInsets) {
  185               newInsets.set(3, 3, 3, 3);
  186               return newInsets;
  187           }
  188       }
  189   
  190       public static class InternalFrameBorder extends AbstractBorder implements UIResource {
  191           private static final int corner = 14;
  192   
  193           public void paintBorder(Component c, Graphics g, int x, int y,
  194                             int w, int h) {
  195   
  196               Color background;
  197               Color highlight;
  198               Color shadow;
  199   
  200               if (c instanceof JInternalFrame && ((JInternalFrame)c).isSelected()) {
  201                   background = MetalLookAndFeel.getPrimaryControlDarkShadow();
  202                   highlight = MetalLookAndFeel.getPrimaryControlShadow();
  203                   shadow = MetalLookAndFeel.getPrimaryControlInfo();
  204               } else {
  205                   background = MetalLookAndFeel.getControlDarkShadow();
  206                   highlight = MetalLookAndFeel.getControlShadow();
  207                   shadow = MetalLookAndFeel.getControlInfo();
  208               }
  209   
  210                 g.setColor(background);
  211                 // Draw outermost lines
  212                 g.drawLine( 1, 0, w-2, 0);
  213                 g.drawLine( 0, 1, 0, h-2);
  214                 g.drawLine( w-1, 1, w-1, h-2);
  215                 g.drawLine( 1, h-1, w-2, h-1);
  216   
  217                 // Draw the bulk of the border
  218                 for (int i = 1; i < 5; i++) {
  219                     g.drawRect(x+i,y+i,w-(i*2)-1, h-(i*2)-1);
  220                 }
  221   
  222                 if (c instanceof JInternalFrame &&
  223                                  ((JInternalFrame)c).isResizable()) {
  224                     g.setColor(highlight);
  225                     // Draw the Long highlight lines
  226                     g.drawLine( corner+1, 3, w-corner, 3);
  227                     g.drawLine( 3, corner+1, 3, h-corner);
  228                     g.drawLine( w-2, corner+1, w-2, h-corner);
  229                     g.drawLine( corner+1, h-2, w-corner, h-2);
  230   
  231                     g.setColor(shadow);
  232                     // Draw the Long shadow lines
  233                     g.drawLine( corner, 2, w-corner-1, 2);
  234                     g.drawLine( 2, corner, 2, h-corner-1);
  235                     g.drawLine( w-3, corner, w-3, h-corner-1);
  236                     g.drawLine( corner, h-3, w-corner-1, h-3);
  237                 }
  238   
  239             }
  240   
  241             public Insets getBorderInsets(Component c, Insets newInsets) {
  242                 newInsets.set(5, 5, 5, 5);
  243                 return newInsets;
  244             }
  245       }
  246   
  247       /**
  248        * Border for a Frame.
  249        * @since 1.4
  250        */
  251       static class FrameBorder extends AbstractBorder implements UIResource {
  252           private static final int corner = 14;
  253   
  254           public void paintBorder(Component c, Graphics g, int x, int y,
  255               int w, int h) {
  256   
  257               Color background;
  258               Color highlight;
  259               Color shadow;
  260   
  261               Window window = SwingUtilities.getWindowAncestor(c);
  262               if (window != null && window.isActive()) {
  263                   background = MetalLookAndFeel.getPrimaryControlDarkShadow();
  264                   highlight = MetalLookAndFeel.getPrimaryControlShadow();
  265                   shadow = MetalLookAndFeel.getPrimaryControlInfo();
  266               } else {
  267                   background = MetalLookAndFeel.getControlDarkShadow();
  268                   highlight = MetalLookAndFeel.getControlShadow();
  269                   shadow = MetalLookAndFeel.getControlInfo();
  270               }
  271   
  272               g.setColor(background);
  273               // Draw outermost lines
  274               g.drawLine( x+1, y+0, x+w-2, y+0);
  275               g.drawLine( x+0, y+1, x+0, y +h-2);
  276               g.drawLine( x+w-1, y+1, x+w-1, y+h-2);
  277               g.drawLine( x+1, y+h-1, x+w-2, y+h-1);
  278   
  279               // Draw the bulk of the border
  280               for (int i = 1; i < 5; i++) {
  281                   g.drawRect(x+i,y+i,w-(i*2)-1, h-(i*2)-1);
  282               }
  283   
  284               if ((window instanceof Frame) && ((Frame) window).isResizable()) {
  285                   g.setColor(highlight);
  286                   // Draw the Long highlight lines
  287                   g.drawLine( corner+1, 3, w-corner, 3);
  288                   g.drawLine( 3, corner+1, 3, h-corner);
  289                   g.drawLine( w-2, corner+1, w-2, h-corner);
  290                   g.drawLine( corner+1, h-2, w-corner, h-2);
  291   
  292                   g.setColor(shadow);
  293                   // Draw the Long shadow lines
  294                   g.drawLine( corner, 2, w-corner-1, 2);
  295                   g.drawLine( 2, corner, 2, h-corner-1);
  296                   g.drawLine( w-3, corner, w-3, h-corner-1);
  297                   g.drawLine( corner, h-3, w-corner-1, h-3);
  298               }
  299   
  300           }
  301   
  302           public Insets getBorderInsets(Component c, Insets newInsets)
  303           {
  304               newInsets.set(5, 5, 5, 5);
  305               return newInsets;
  306           }
  307       }
  308   
  309       /**
  310        * Border for a Frame.
  311        * @since 1.4
  312        */
  313       static class DialogBorder extends AbstractBorder implements UIResource
  314       {
  315           private static final int corner = 14;
  316   
  317           protected Color getActiveBackground()
  318           {
  319               return MetalLookAndFeel.getPrimaryControlDarkShadow();
  320           }
  321   
  322           protected Color getActiveHighlight()
  323           {
  324               return MetalLookAndFeel.getPrimaryControlShadow();
  325           }
  326   
  327           protected Color getActiveShadow()
  328           {
  329               return MetalLookAndFeel.getPrimaryControlInfo();
  330           }
  331   
  332           protected Color getInactiveBackground()
  333           {
  334               return MetalLookAndFeel.getControlDarkShadow();
  335           }
  336   
  337           protected Color getInactiveHighlight()
  338           {
  339               return MetalLookAndFeel.getControlShadow();
  340           }
  341   
  342           protected Color getInactiveShadow()
  343           {
  344               return MetalLookAndFeel.getControlInfo();
  345           }
  346   
  347           public void paintBorder(Component c, Graphics g, int x, int y, int w, int h)
  348           {
  349               Color background;
  350               Color highlight;
  351               Color shadow;
  352   
  353               Window window = SwingUtilities.getWindowAncestor(c);
  354               if (window != null && window.isActive()) {
  355                   background = getActiveBackground();
  356                   highlight = getActiveHighlight();
  357                   shadow = getActiveShadow();
  358               } else {
  359                   background = getInactiveBackground();
  360                   highlight = getInactiveHighlight();
  361                   shadow = getInactiveShadow();
  362               }
  363   
  364               g.setColor(background);
  365               // Draw outermost lines
  366               g.drawLine( x + 1, y + 0, x + w-2, y + 0);
  367               g.drawLine( x + 0, y + 1, x + 0, y + h - 2);
  368               g.drawLine( x + w - 1, y + 1, x + w - 1, y + h - 2);
  369               g.drawLine( x + 1, y + h - 1, x + w - 2, y + h - 1);
  370   
  371               // Draw the bulk of the border
  372               for (int i = 1; i < 5; i++) {
  373                   g.drawRect(x+i,y+i,w-(i*2)-1, h-(i*2)-1);
  374               }
  375   
  376   
  377               if ((window instanceof Dialog) && ((Dialog) window).isResizable()) {
  378                   g.setColor(highlight);
  379                   // Draw the Long highlight lines
  380                   g.drawLine( corner+1, 3, w-corner, 3);
  381                   g.drawLine( 3, corner+1, 3, h-corner);
  382                   g.drawLine( w-2, corner+1, w-2, h-corner);
  383                   g.drawLine( corner+1, h-2, w-corner, h-2);
  384   
  385                   g.setColor(shadow);
  386                   // Draw the Long shadow lines
  387                   g.drawLine( corner, 2, w-corner-1, 2);
  388                   g.drawLine( 2, corner, 2, h-corner-1);
  389                   g.drawLine( w-3, corner, w-3, h-corner-1);
  390                   g.drawLine( corner, h-3, w-corner-1, h-3);
  391               }
  392   
  393           }
  394   
  395           public Insets getBorderInsets(Component c, Insets newInsets)
  396           {
  397               newInsets.set(5, 5, 5, 5);
  398               return newInsets;
  399           }
  400       }
  401   
  402       /**
  403        * Border for an Error Dialog.
  404        * @since 1.4
  405        */
  406       static class ErrorDialogBorder extends DialogBorder implements UIResource
  407       {
  408           protected Color getActiveBackground() {
  409               return UIManager.getColor("OptionPane.errorDialog.border.background");
  410           }
  411       }
  412   
  413   
  414       /**
  415        * Border for a QuestionDialog.  Also used for a JFileChooser and a
  416        * JColorChooser..
  417        * @since 1.4
  418        */
  419       static class QuestionDialogBorder extends DialogBorder implements UIResource
  420       {
  421           protected Color getActiveBackground() {
  422               return UIManager.getColor("OptionPane.questionDialog.border.background");
  423           }
  424       }
  425   
  426   
  427       /**
  428        * Border for a Warning Dialog.
  429        * @since 1.4
  430        */
  431       static class WarningDialogBorder extends DialogBorder implements UIResource
  432       {
  433           protected Color getActiveBackground() {
  434               return UIManager.getColor("OptionPane.warningDialog.border.background");
  435           }
  436       }
  437   
  438   
  439       /**
  440        * Border for a Palette.
  441        * @since 1.3
  442        */
  443       public static class PaletteBorder extends AbstractBorder implements UIResource {
  444           int titleHeight = 0;
  445   
  446           public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  447   
  448               g.translate(x,y);
  449               g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
  450               g.drawLine(0, 1, 0, h-2);
  451               g.drawLine(1, h-1, w-2, h-1);
  452               g.drawLine(w-1,  1, w-1, h-2);
  453               g.drawLine( 1, 0, w-2, 0);
  454               g.drawRect(1,1, w-3, h-3);
  455               g.translate(-x,-y);
  456   
  457           }
  458   
  459           public Insets getBorderInsets(Component c, Insets newInsets) {
  460               newInsets.set(1, 1, 1, 1);
  461               return newInsets;
  462           }
  463       }
  464   
  465       public static class OptionDialogBorder extends AbstractBorder implements UIResource {
  466           int titleHeight = 0;
  467   
  468           public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  469   
  470               g.translate(x,y);
  471   
  472               int messageType = JOptionPane.PLAIN_MESSAGE;
  473               if (c instanceof JInternalFrame) {
  474                   Object obj = ((JInternalFrame) c).getClientProperty(
  475                                 "JInternalFrame.messageType");
  476                   if (obj != null && (obj instanceof Integer)) {
  477                       messageType = ((Integer) obj).intValue();
  478                   }
  479               }
  480   
  481               Color borderColor;
  482   
  483               switch (messageType) {
  484               case(JOptionPane.ERROR_MESSAGE):
  485                   borderColor = UIManager.getColor(
  486                       "OptionPane.errorDialog.border.background");
  487                   break;
  488               case(JOptionPane.QUESTION_MESSAGE):
  489                   borderColor = UIManager.getColor(
  490                       "OptionPane.questionDialog.border.background");
  491                   break;
  492               case(JOptionPane.WARNING_MESSAGE):
  493                   borderColor = UIManager.getColor(
  494                       "OptionPane.warningDialog.border.background");
  495                   break;
  496               case(JOptionPane.INFORMATION_MESSAGE):
  497               case(JOptionPane.PLAIN_MESSAGE):
  498               default:
  499                   borderColor = MetalLookAndFeel.getPrimaryControlDarkShadow();
  500                   break;
  501               }
  502   
  503               g.setColor(borderColor);
  504   
  505                 // Draw outermost lines
  506                 g.drawLine( 1, 0, w-2, 0);
  507                 g.drawLine( 0, 1, 0, h-2);
  508                 g.drawLine( w-1, 1, w-1, h-2);
  509                 g.drawLine( 1, h-1, w-2, h-1);
  510   
  511                 // Draw the bulk of the border
  512                 for (int i = 1; i < 3; i++) {
  513                     g.drawRect(i, i, w-(i*2)-1, h-(i*2)-1);
  514                 }
  515   
  516               g.translate(-x,-y);
  517   
  518           }
  519   
  520           public Insets getBorderInsets(Component c, Insets newInsets) {
  521               newInsets.set(3, 3, 3, 3);
  522               return newInsets;
  523           }
  524       }
  525   
  526   
  527       public static class MenuBarBorder extends AbstractBorder implements UIResource {
  528           protected static Insets borderInsets = new Insets( 1, 0, 1, 0 );
  529   
  530           public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  531               g.translate( x, y );
  532   
  533               if (MetalLookAndFeel.usingOcean()) {
  534                   // Only paint a border if we're not next to a horizontal
  535                   // toolbar
  536                   if (!MetalToolBarUI.doesMenuBarBorderToolBar((JMenuBar)c)) {
  537                       g.setColor(MetalLookAndFeel.getControl());
  538                       g.drawLine(0, h - 2, w, h - 2);
  539                       g.setColor(UIManager.getColor("MenuBar.borderColor"));
  540                       g.drawLine(0, h - 1, w, h - 1);
  541                   }
  542               }
  543               else {
  544                   g.setColor( MetalLookAndFeel.getControlShadow() );
  545                   g.drawLine( 0, h-1, w, h-1 );
  546               }
  547   
  548               g.translate( -x, -y );
  549   
  550           }
  551   
  552           public Insets getBorderInsets(Component c, Insets newInsets) {
  553               if (MetalLookAndFeel.usingOcean()) {
  554                   newInsets.set(0, 0, 2, 0);
  555               }
  556               else {
  557                   newInsets.set(1, 0, 1, 0);
  558               }
  559               return newInsets;
  560           }
  561       }
  562   
  563       public static class MenuItemBorder extends AbstractBorder implements UIResource {
  564           protected static Insets borderInsets = new Insets( 2, 2, 2, 2 );
  565   
  566           public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  567               JMenuItem b = (JMenuItem) c;
  568               ButtonModel model = b.getModel();
  569   
  570               g.translate( x, y );
  571   
  572               if ( c.getParent() instanceof JMenuBar ) {
  573                   if ( model.isArmed() || model.isSelected() ) {
  574                       g.setColor( MetalLookAndFeel.getControlDarkShadow() );
  575                       g.drawLine( 0, 0, w - 2, 0 );
  576                       g.drawLine( 0, 0, 0, h - 1 );
  577                       g.drawLine( w - 2, 2, w - 2, h - 1 );
  578   
  579                       g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
  580                       g.drawLine( w - 1, 1, w - 1, h - 1 );
  581   
  582                       g.setColor( MetalLookAndFeel.getMenuBackground() );
  583                       g.drawLine( w - 1, 0, w - 1, 0 );
  584                   }
  585               } else {
  586                   if (  model.isArmed() || ( c instanceof JMenu && model.isSelected() ) ) {
  587                       g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
  588                       g.drawLine( 0, 0, w - 1, 0 );
  589   
  590                       g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
  591                       g.drawLine( 0, h - 1, w - 1, h - 1 );
  592                   } else {
  593                       g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
  594                       g.drawLine( 0, 0, 0, h - 1 );
  595                   }
  596               }
  597   
  598               g.translate( -x, -y );
  599           }
  600   
  601           public Insets getBorderInsets(Component c, Insets newInsets) {
  602               newInsets.set(2, 2, 2, 2);
  603               return newInsets;
  604           }
  605       }
  606   
  607       public static class PopupMenuBorder extends AbstractBorder implements UIResource {
  608           protected static Insets borderInsets = new Insets( 3, 1, 2, 1 );
  609   
  610           public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  611               g.translate( x, y );
  612   
  613               g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
  614               g.drawRect( 0, 0, w - 1, h - 1 );
  615   
  616               g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
  617               g.drawLine( 1, 1, w - 2, 1 );
  618               g.drawLine( 1, 2, 1, 2 );
  619               g.drawLine( 1, h - 2, 1, h - 2 );
  620   
  621               g.translate( -x, -y );
  622   
  623           }
  624   
  625           public Insets getBorderInsets(Component c, Insets newInsets) {
  626               newInsets.set(3, 1, 2, 1);
  627               return newInsets;
  628           }
  629       }
  630   
  631   
  632       public static class RolloverButtonBorder extends ButtonBorder {
  633   
  634           public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  635               AbstractButton b = (AbstractButton) c;
  636               ButtonModel model = b.getModel();
  637   
  638               if ( model.isRollover() && !( model.isPressed() && !model.isArmed() ) ) {
  639                   super.paintBorder( c, g, x, y, w, h );
  640               }
  641           }
  642   
  643       }
  644   
  645       /**
  646        * A border which is like a Margin border but it will only honor the margin
  647        * if the margin has been explicitly set by the developer.
  648        *
  649        * Note: This is identical to the package private class
  650        * BasicBorders.RolloverMarginBorder and should probably be consolidated.
  651        */
  652       static class RolloverMarginBorder extends EmptyBorder {
  653   
  654           public RolloverMarginBorder() {
  655               super(3,3,3,3); // hardcoded margin for JLF requirements.
  656           }
  657   
  658           public Insets getBorderInsets(Component c, Insets insets) {
  659               Insets margin = null;
  660   
  661               if (c instanceof AbstractButton) {
  662                   margin = ((AbstractButton)c).getMargin();
  663               }
  664               if (margin == null || margin instanceof UIResource) {
  665                   // default margin so replace
  666                   insets.left = left;
  667                   insets.top = top;
  668                   insets.right = right;
  669                   insets.bottom = bottom;
  670               } else {
  671                   // Margin which has been explicitly set by the user.
  672                   insets.left = margin.left;
  673                   insets.top = margin.top;
  674                   insets.right = margin.right;
  675                   insets.bottom = margin.bottom;
  676               }
  677               return insets;
  678           }
  679       }
  680   
  681       public static class ToolBarBorder extends AbstractBorder implements UIResource, SwingConstants
  682       {
  683           protected MetalBumps bumps = new MetalBumps( 10, 10,
  684                                         MetalLookAndFeel.getControlHighlight(),
  685                                         MetalLookAndFeel.getControlDarkShadow(),
  686                                        UIManager.getColor("ToolBar.background"));
  687   
  688           public void paintBorder( Component c, Graphics g, int x, int y, int w, int h )
  689           {
  690               g.translate( x, y );
  691   
  692               if ( ((JToolBar) c).isFloatable() )
  693               {
  694                   if ( ((JToolBar) c).getOrientation() == HORIZONTAL )
  695                   {
  696                       int shift = MetalLookAndFeel.usingOcean() ? -1 : 0;
  697                       bumps.setBumpArea( 10, h - 4 );
  698                       if( MetalUtils.isLeftToRight(c) ) {
  699                           bumps.paintIcon( c, g, 2, 2 + shift );
  700                       } else {
  701                           bumps.paintIcon( c, g, w-12,
  702                                            2 + shift );
  703                       }
  704                   }
  705                   else // vertical
  706                   {
  707                       bumps.setBumpArea( w - 4, 10 );
  708                       bumps.paintIcon( c, g, 2, 2 );
  709                   }
  710   
  711               }
  712   
  713               if (((JToolBar) c).getOrientation() == HORIZONTAL &&
  714                                  MetalLookAndFeel.usingOcean()) {
  715                   g.setColor(MetalLookAndFeel.getControl());
  716                   g.drawLine(0, h - 2, w, h - 2);
  717                   g.setColor(UIManager.getColor("ToolBar.borderColor"));
  718                   g.drawLine(0, h - 1, w, h - 1);
  719               }
  720   
  721               g.translate( -x, -y );
  722           }
  723   
  724           public Insets getBorderInsets(Component c, Insets newInsets) {
  725               if (MetalLookAndFeel.usingOcean()) {
  726                   newInsets.set(1, 2, 3, 2);
  727               }
  728               else {
  729                   newInsets.top = newInsets.left = newInsets.bottom = newInsets.right = 2;
  730               }
  731   
  732               if ( ((JToolBar) c).isFloatable() ) {
  733                   if ( ((JToolBar) c).getOrientation() == HORIZONTAL ) {
  734                       if (c.getComponentOrientation().isLeftToRight()) {
  735                           newInsets.left = 16;
  736                       } else {
  737                           newInsets.right = 16;
  738                       }
  739                   } else {// vertical
  740                       newInsets.top = 16;
  741                   }
  742               }
  743   
  744               Insets margin = ((JToolBar) c).getMargin();
  745   
  746               if ( margin != null ) {
  747                   newInsets.left   += margin.left;
  748                   newInsets.top    += margin.top;
  749                   newInsets.right  += margin.right;
  750                   newInsets.bottom += margin.bottom;
  751               }
  752   
  753               return newInsets;
  754           }
  755       }
  756   
  757       private static Border buttonBorder;
  758   
  759       /**
  760        * Returns a border instance for a JButton
  761        * @since 1.3
  762        */
  763       public static Border getButtonBorder() {
  764           if (buttonBorder == null) {
  765               buttonBorder = new BorderUIResource.CompoundBorderUIResource(
  766                                                      new MetalBorders.ButtonBorder(),
  767                                                      new BasicBorders.MarginBorder());
  768           }
  769           return buttonBorder;
  770       }
  771   
  772       private static Border textBorder;
  773   
  774       /**
  775        * Returns a border instance for a text component
  776        * @since 1.3
  777        */
  778       public static Border getTextBorder() {
  779           if (textBorder == null) {
  780               textBorder = new BorderUIResource.CompoundBorderUIResource(
  781                                                      new MetalBorders.Flush3DBorder(),
  782                                                      new BasicBorders.MarginBorder());
  783           }
  784           return textBorder;
  785       }
  786   
  787       private static Border textFieldBorder;
  788   
  789       /**
  790        * Returns a border instance for a JTextField
  791        * @since 1.3
  792        */
  793       public static Border getTextFieldBorder() {
  794           if (textFieldBorder == null) {
  795               textFieldBorder = new BorderUIResource.CompoundBorderUIResource(
  796                                                      new MetalBorders.TextFieldBorder(),
  797                                                      new BasicBorders.MarginBorder());
  798           }
  799           return textFieldBorder;
  800       }
  801   
  802       public static class TextFieldBorder extends Flush3DBorder {
  803   
  804           public void paintBorder(Component c, Graphics g, int x, int y,
  805                                   int w, int h) {
  806   
  807             if (!(c instanceof JTextComponent)) {
  808                   // special case for non-text components (bug ID 4144840)
  809                   if (c.isEnabled()) {
  810                       MetalUtils.drawFlush3DBorder(g, x, y, w, h);
  811                   } else {
  812                       MetalUtils.drawDisabledBorder(g, x, y, w, h);
  813                   }
  814                   return;
  815               }
  816   
  817               if (c.isEnabled() && ((JTextComponent)c).isEditable()) {
  818                   MetalUtils.drawFlush3DBorder(g, x, y, w, h);
  819               } else {
  820                   MetalUtils.drawDisabledBorder(g, x, y, w, h);
  821               }
  822   
  823           }
  824       }
  825   
  826       public static class ScrollPaneBorder extends AbstractBorder implements UIResource {
  827           public void paintBorder(Component c, Graphics g, int x, int y,
  828                             int w, int h) {
  829   
  830               JScrollPane scroll = (JScrollPane)c;
  831               JComponent colHeader = scroll.getColumnHeader();
  832               int colHeaderHeight = 0;
  833               if (colHeader != null)
  834                  colHeaderHeight = colHeader.getHeight();
  835   
  836               JComponent rowHeader = scroll.getRowHeader();
  837               int rowHeaderWidth = 0;
  838               if (rowHeader != null)
  839                  rowHeaderWidth = rowHeader.getWidth();
  840   
  841   
  842               g.translate( x, y);
  843   
  844               g.setColor( MetalLookAndFeel.getControlDarkShadow() );
  845               g.drawRect( 0, 0, w-2, h-2 );
  846               g.setColor( MetalLookAndFeel.getControlHighlight() );
  847   
  848               g.drawLine( w-1, 1, w-1, h-1);
  849               g.drawLine( 1, h-1, w-1, h-1);
  850   
  851               g.setColor( MetalLookAndFeel.getControl() );
  852               g.drawLine( w-2, 2+colHeaderHeight, w-2, 2+colHeaderHeight );
  853               g.drawLine( 1+rowHeaderWidth, h-2, 1+rowHeaderWidth, h-2 );
  854   
  855               g.translate( -x, -y);
  856   
  857           }
  858   
  859           public Insets getBorderInsets(Component c, Insets insets) {
  860               insets.set(1, 1, 2, 2);
  861               return insets;
  862           }
  863       }
  864   
  865       private static Border toggleButtonBorder;
  866   
  867       /**
  868        * Returns a border instance for a JToggleButton
  869        * @since 1.3
  870        */
  871       public static Border getToggleButtonBorder() {
  872           if (toggleButtonBorder == null) {
  873               toggleButtonBorder = new BorderUIResource.CompoundBorderUIResource(
  874                                                      new MetalBorders.ToggleButtonBorder(),
  875                                                      new BasicBorders.MarginBorder());
  876           }
  877           return toggleButtonBorder;
  878       }
  879   
  880       /**
  881        * @since 1.3
  882        */
  883       public static class ToggleButtonBorder extends ButtonBorder {
  884           public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
  885               AbstractButton button = (AbstractButton)c;
  886               ButtonModel model = button.getModel();
  887               if (MetalLookAndFeel.usingOcean()) {
  888                   if(model.isArmed() || !button.isEnabled()) {
  889                       super.paintBorder(c, g, x, y, w, h);
  890                   }
  891                   else {
  892                    g.setColor(MetalLookAndFeel.getControlDarkShadow());
  893                    g.drawRect(0, 0, w - 1, h - 1);
  894               }
  895               return;
  896           }
  897               if (! c.isEnabled() ) {
  898                   MetalUtils.drawDisabledBorder( g, x, y, w-1, h-1 );
  899               } else {
  900                   if ( model.isPressed() && model.isArmed() ) {
  901                      MetalUtils.drawPressed3DBorder( g, x, y, w, h );
  902                   } else if ( model.isSelected() ) {
  903                       MetalUtils.drawDark3DBorder( g, x, y, w, h );
  904                   } else {
  905                       MetalUtils.drawFlush3DBorder( g, x, y, w, h );
  906                   }
  907               }
  908           }
  909       }
  910   
  911       /**
  912        * Border for a Table Header
  913        * @since 1.3
  914        */
  915       public static class TableHeaderBorder extends javax.swing.border.AbstractBorder {
  916           protected Insets editorBorderInsets = new Insets( 2, 2, 2, 0 );
  917   
  918           public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
  919               g.translate( x, y );
  920   
  921               g.setColor( MetalLookAndFeel.getControlDarkShadow() );
  922               g.drawLine( w-1, 0, w-1, h-1 );
  923               g.drawLine( 1, h-1, w-1, h-1 );
  924               g.setColor( MetalLookAndFeel.getControlHighlight() );
  925               g.drawLine( 0, 0, w-2, 0 );
  926               g.drawLine( 0, 0, 0, h-2 );
  927   
  928               g.translate( -x, -y );
  929           }
  930   
  931           public Insets getBorderInsets(Component c, Insets insets) {
  932               insets.set(2, 2, 2, 0);
  933               return insets;
  934           }
  935       }
  936   
  937       /**
  938        * Returns a border instance for a Desktop Icon
  939        * @since 1.3
  940        */
  941       public static Border getDesktopIconBorder() {
  942           return new BorderUIResource.CompoundBorderUIResource(
  943                                             new LineBorder(MetalLookAndFeel.getControlDarkShadow(), 1),
  944                                             new MatteBorder (2,2,1,2, MetalLookAndFeel.getControl()));
  945       }
  946   
  947       static Border getToolBarRolloverBorder() {
  948           if (MetalLookAndFeel.usingOcean()) {
  949               return new CompoundBorder(
  950                   new MetalBorders.ButtonBorder(),
  951                   new MetalBorders.RolloverMarginBorder());
  952           }
  953           return new CompoundBorder(new MetalBorders.RolloverButtonBorder(),
  954                                     new MetalBorders.RolloverMarginBorder());
  955       }
  956   
  957       static Border getToolBarNonrolloverBorder() {
  958           if (MetalLookAndFeel.usingOcean()) {
  959               new CompoundBorder(
  960                   new MetalBorders.ButtonBorder(),
  961                   new MetalBorders.RolloverMarginBorder());
  962           }
  963           return new CompoundBorder(new MetalBorders.ButtonBorder(),
  964                                     new MetalBorders.RolloverMarginBorder());
  965       }
  966   }

Save This Page
Home » openjdk-7 » javax » swing » plaf » metal » [javadoc | source]