Save This Page
Home » openjdk-7 » javax » swing » plaf » metal » [javadoc | source]
    1   /*
    2    * Copyright 1998-2005 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.plaf;
   29   import javax.swing;
   30   import java.awt;
   31   import java.awt.image;
   32   import java.lang.ref;
   33   import java.util;
   34   import sun.swing.CachedPainter;
   35   import sun.swing.ImageIconUIResource;
   36   
   37   /**
   38    * This is a dumping ground for random stuff we want to use in several places.
   39    *
   40    * @author Steve Wilson
   41    */
   42   
   43   class MetalUtils {
   44   
   45       static void drawFlush3DBorder(Graphics g, Rectangle r) {
   46           drawFlush3DBorder(g, r.x, r.y, r.width, r.height);
   47       }
   48   
   49       /**
   50         * This draws the "Flush 3D Border" which is used throughout the Metal L&F
   51         */
   52       static void drawFlush3DBorder(Graphics g, int x, int y, int w, int h) {
   53           g.translate( x, y);
   54           g.setColor( MetalLookAndFeel.getControlDarkShadow() );
   55           g.drawRect( 0, 0, w-2, h-2 );
   56           g.setColor( MetalLookAndFeel.getControlHighlight() );
   57           g.drawRect( 1, 1, w-2, h-2 );
   58           g.setColor( MetalLookAndFeel.getControl() );
   59           g.drawLine( 0, h-1, 1, h-2 );
   60           g.drawLine( w-1, 0, w-2, 1 );
   61           g.translate( -x, -y);
   62       }
   63   
   64       /**
   65         * This draws a variant "Flush 3D Border"
   66         * It is used for things like pressed buttons.
   67         */
   68       static void drawPressed3DBorder(Graphics g, Rectangle r) {
   69           drawPressed3DBorder( g, r.x, r.y, r.width, r.height );
   70       }
   71   
   72       static void drawDisabledBorder(Graphics g, int x, int y, int w, int h) {
   73           g.translate( x, y);
   74           g.setColor( MetalLookAndFeel.getControlShadow() );
   75           g.drawRect( 0, 0, w-1, h-1 );
   76           g.translate(-x, -y);
   77       }
   78   
   79       /**
   80         * This draws a variant "Flush 3D Border"
   81         * It is used for things like pressed buttons.
   82         */
   83       static void drawPressed3DBorder(Graphics g, int x, int y, int w, int h) {
   84           g.translate( x, y);
   85   
   86           drawFlush3DBorder(g, 0, 0, w, h);
   87   
   88           g.setColor( MetalLookAndFeel.getControlShadow() );
   89           g.drawLine( 1, 1, 1, h-2 );
   90           g.drawLine( 1, 1, w-2, 1 );
   91           g.translate( -x, -y);
   92       }
   93   
   94       /**
   95         * This draws a variant "Flush 3D Border"
   96         * It is used for things like active toggle buttons.
   97         * This is used rarely.
   98         */
   99       static void drawDark3DBorder(Graphics g, Rectangle r) {
  100           drawDark3DBorder(g, r.x, r.y, r.width, r.height);
  101       }
  102   
  103       /**
  104         * This draws a variant "Flush 3D Border"
  105         * It is used for things like active toggle buttons.
  106         * This is used rarely.
  107         */
  108       static void drawDark3DBorder(Graphics g, int x, int y, int w, int h) {
  109           g.translate( x, y);
  110   
  111           drawFlush3DBorder(g, 0, 0, w, h);
  112   
  113           g.setColor( MetalLookAndFeel.getControl() );
  114           g.drawLine( 1, 1, 1, h-2 );
  115           g.drawLine( 1, 1, w-2, 1 );
  116           g.setColor( MetalLookAndFeel.getControlShadow() );
  117           g.drawLine( 1, h-2, 1, h-2 );
  118           g.drawLine( w-2, 1, w-2, 1 );
  119           g.translate( -x, -y);
  120       }
  121   
  122       static void drawButtonBorder(Graphics g, int x, int y, int w, int h, boolean active) {
  123           if (active) {
  124               drawActiveButtonBorder(g, x, y, w, h);
  125           } else {
  126               drawFlush3DBorder(g, x, y, w, h);
  127           }
  128       }
  129   
  130       static void drawActiveButtonBorder(Graphics g, int x, int y, int w, int h) {
  131           drawFlush3DBorder(g, x, y, w, h);
  132           g.setColor( MetalLookAndFeel.getPrimaryControl() );
  133           g.drawLine( x+1, y+1, x+1, h-3 );
  134           g.drawLine( x+1, y+1, w-3, x+1 );
  135           g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
  136           g.drawLine( x+2, h-2, w-2, h-2 );
  137           g.drawLine( w-2, y+2, w-2, h-2 );
  138       }
  139   
  140       static void drawDefaultButtonBorder(Graphics g, int x, int y, int w, int h, boolean active) {
  141           drawButtonBorder(g, x+1, y+1, w-1, h-1, active);
  142           g.translate(x, y);
  143           g.setColor( MetalLookAndFeel.getControlDarkShadow() );
  144           g.drawRect( 0, 0, w-3, h-3 );
  145           g.drawLine( w-2, 0, w-2, 0);
  146           g.drawLine( 0, h-2, 0, h-2);
  147           g.translate(-x, -y);
  148       }
  149   
  150       static void drawDefaultButtonPressedBorder(Graphics g, int x, int y, int w, int h) {
  151           drawPressed3DBorder(g, x + 1, y + 1, w - 1, h - 1);
  152           g.translate(x, y);
  153           g.setColor(MetalLookAndFeel.getControlDarkShadow());
  154           g.drawRect(0, 0, w - 3, h - 3);
  155           g.drawLine(w - 2, 0, w - 2, 0);
  156           g.drawLine(0, h - 2, 0, h - 2);
  157           g.setColor(MetalLookAndFeel.getControl());
  158           g.drawLine(w - 1, 0, w - 1, 0);
  159           g.drawLine(0, h - 1, 0, h - 1);
  160           g.translate(-x, -y);
  161       }
  162   
  163       /*
  164        * Convenience function for determining ComponentOrientation.  Helps us
  165        * avoid having Munge directives throughout the code.
  166        */
  167       static boolean isLeftToRight( Component c ) {
  168           return c.getComponentOrientation().isLeftToRight();
  169       }
  170   
  171       static int getInt(Object key, int defaultValue) {
  172           Object value = UIManager.get(key);
  173   
  174           if (value instanceof Integer) {
  175               return ((Integer)value).intValue();
  176           }
  177           if (value instanceof String) {
  178               try {
  179                   return Integer.parseInt((String)value);
  180               } catch (NumberFormatException nfe) {}
  181           }
  182           return defaultValue;
  183       }
  184   
  185       //
  186       // Ocean specific stuff.
  187       //
  188       /**
  189        * Draws a radial type gradient. The gradient will be drawn vertically if
  190        * <code>vertical</code> is true, otherwise horizontally.
  191        * The UIManager key consists of five values:
  192        * r1 r2 c1 c2 c3. The gradient is broken down into four chunks drawn
  193        * in order from the origin.
  194        * <ol>
  195        * <li>Gradient r1 % of the size from c1 to c2
  196        * <li>Rectangle r2 % of the size in c2.
  197        * <li>Gradient r1 % of the size from c2 to c1
  198        * <li>The remaining size will be filled with a gradient from c1 to c3.
  199        * </ol>
  200        *
  201        * @param c Component rendering to
  202        * @param g Graphics to draw to.
  203        * @param key UIManager key used to look up gradient values.
  204        * @param x X coordinate to draw from
  205        * @param y Y coordinate to draw from
  206        * @param w Width to draw to
  207        * @param h Height to draw to
  208        * @param vertical Direction of the gradient
  209        * @return true if <code>key</code> exists, otherwise false.
  210        */
  211       static boolean drawGradient(Component c, Graphics g, String key,
  212                                   int x, int y, int w, int h, boolean vertical) {
  213           java.util.List gradient = (java.util.List)UIManager.get(key);
  214           if (gradient == null || !(g instanceof Graphics2D)) {
  215               return false;
  216           }
  217   
  218           if (w <= 0 || h <= 0) {
  219               return true;
  220           }
  221   
  222           GradientPainter.INSTANCE.paint(
  223                   c, (Graphics2D)g, gradient, x, y, w, h, vertical);
  224           return true;
  225       }
  226   
  227   
  228       private static class GradientPainter extends CachedPainter {
  229           /**
  230            * Instance used for painting.  This is the only instance that is
  231            * ever created.
  232            */
  233           public static final GradientPainter INSTANCE = new GradientPainter(8);
  234   
  235           // Size of images to create. For vertical gradients this is the width,
  236           // otherwise it's the height.
  237           private static final int IMAGE_SIZE = 64;
  238   
  239           /**
  240            * This is the actual width we're painting in, or last painted to.
  241            */
  242           private int w;
  243           /**
  244            * This is the actual height we're painting in, or last painted to
  245            */
  246           private int h;
  247   
  248   
  249           GradientPainter(int count) {
  250               super(count);
  251           }
  252   
  253           public void paint(Component c, Graphics2D g,
  254                             java.util.List gradient, int x, int y, int w,
  255                             int h, boolean isVertical) {
  256               int imageWidth;
  257               int imageHeight;
  258               if (isVertical) {
  259                   imageWidth = IMAGE_SIZE;
  260                   imageHeight = h;
  261               }
  262               else {
  263                   imageWidth = w;
  264                   imageHeight = IMAGE_SIZE;
  265               }
  266               synchronized(c.getTreeLock()) {
  267                   this.w = w;
  268                   this.h = h;
  269                   paint(c, g, x, y, imageWidth, imageHeight,
  270                         gradient, isVertical);
  271               }
  272           }
  273   
  274           protected void paintToImage(Component c, Image image, Graphics g,
  275                                       int w, int h, Object[] args) {
  276               Graphics2D g2 = (Graphics2D)g;
  277               java.util.List gradient = (java.util.List)args[0];
  278               boolean isVertical = ((Boolean)args[1]).booleanValue();
  279               // Render to the VolatileImage
  280               if (isVertical) {
  281                   drawVerticalGradient(g2,
  282                                        ((Number)gradient.get(0)).floatValue(),
  283                                        ((Number)gradient.get(1)).floatValue(),
  284                                        (Color)gradient.get(2),
  285                                        (Color)gradient.get(3),
  286                                        (Color)gradient.get(4), w, h);
  287               }
  288               else {
  289                   drawHorizontalGradient(g2,
  290                                         ((Number)gradient.get(0)).floatValue(),
  291                                         ((Number)gradient.get(1)).floatValue(),
  292                                         (Color)gradient.get(2),
  293                                         (Color)gradient.get(3),
  294                                         (Color)gradient.get(4), w, h);
  295               }
  296           }
  297   
  298           protected void paintImage(Component c, Graphics g,
  299                                     int x, int y, int imageW, int imageH,
  300                                     Image image, Object[] args) {
  301               boolean isVertical = ((Boolean)args[1]).booleanValue();
  302               // Render to the screen
  303               g.translate(x, y);
  304               if (isVertical) {
  305                   for (int counter = 0; counter < w; counter += IMAGE_SIZE) {
  306                       int tileSize = Math.min(IMAGE_SIZE, w - counter);
  307                       g.drawImage(image, counter, 0, counter + tileSize, h,
  308                                   0, 0, tileSize, h, null);
  309                   }
  310               }
  311               else {
  312                   for (int counter = 0; counter < h; counter += IMAGE_SIZE) {
  313                       int tileSize = Math.min(IMAGE_SIZE, h - counter);
  314                       g.drawImage(image, 0, counter, w, counter + tileSize,
  315                                   0, 0, w, tileSize, null);
  316                   }
  317               }
  318               g.translate(-x, -y);
  319           }
  320   
  321           private void drawVerticalGradient(Graphics2D g, float ratio1,
  322                                             float ratio2, Color c1,Color c2,
  323                                             Color c3, int w, int h) {
  324               int mid = (int)(ratio1 * h);
  325               int mid2 = (int)(ratio2 * h);
  326               if (mid > 0) {
  327                   g.setPaint(getGradient((float)0, (float)0, c1, (float)0,
  328                                          (float)mid, c2));
  329                   g.fillRect(0, 0, w, mid);
  330               }
  331               if (mid2 > 0) {
  332                   g.setColor(c2);
  333                   g.fillRect(0, mid, w, mid2);
  334               }
  335               if (mid > 0) {
  336                   g.setPaint(getGradient((float)0, (float)mid + mid2, c2,
  337                                          (float)0, (float)mid * 2 + mid2, c1));
  338                   g.fillRect(0, mid + mid2, w, mid);
  339               }
  340               if (h - mid * 2 - mid2 > 0) {
  341                   g.setPaint(getGradient((float)0, (float)mid * 2 + mid2, c1,
  342                                          (float)0, (float)h, c3));
  343                   g.fillRect(0, mid * 2 + mid2, w, h - mid * 2 - mid2);
  344               }
  345           }
  346   
  347           private void drawHorizontalGradient(Graphics2D g, float ratio1,
  348                                               float ratio2, Color c1,Color c2,
  349                                               Color c3, int w, int h) {
  350               int mid = (int)(ratio1 * w);
  351               int mid2 = (int)(ratio2 * w);
  352               if (mid > 0) {
  353                   g.setPaint(getGradient((float)0, (float)0, c1,
  354                                          (float)mid, (float)0, c2));
  355                   g.fillRect(0, 0, mid, h);
  356               }
  357               if (mid2 > 0) {
  358                   g.setColor(c2);
  359                   g.fillRect(mid, 0, mid2, h);
  360               }
  361               if (mid > 0) {
  362                   g.setPaint(getGradient((float)mid + mid2, (float)0, c2,
  363                                          (float)mid * 2 + mid2, (float)0, c1));
  364                   g.fillRect(mid + mid2, 0, mid, h);
  365               }
  366               if (w - mid * 2 - mid2 > 0) {
  367                   g.setPaint(getGradient((float)mid * 2 + mid2, (float)0, c1,
  368                                          w, (float)0, c3));
  369                   g.fillRect(mid * 2 + mid2, 0, w - mid * 2 - mid2, h);
  370               }
  371           }
  372   
  373           private GradientPaint getGradient(float x1, float y1,
  374                                                    Color c1, float x2, float y2,
  375                                                    Color c2) {
  376               return new GradientPaint(x1, y1, c1, x2, y2, c2, true);
  377           }
  378       }
  379   
  380   
  381       /**
  382        * Returns true if the specified widget is in a toolbar.
  383        */
  384       static boolean isToolBarButton(JComponent c) {
  385           return (c.getParent() instanceof JToolBar);
  386       }
  387   
  388       static Icon getOceanToolBarIcon(Image i) {
  389           ImageProducer prod = new FilteredImageSource(i.getSource(),
  390                                new OceanToolBarImageFilter());
  391           return new ImageIconUIResource(Toolkit.getDefaultToolkit().createImage(prod));
  392       }
  393   
  394       static Icon getOceanDisabledButtonIcon(Image image) {
  395           Object[] range = (Object[])UIManager.get("Button.disabledGrayRange");
  396           int min = 180;
  397           int max = 215;
  398           if (range != null) {
  399               min = ((Integer)range[0]).intValue();
  400               max = ((Integer)range[1]).intValue();
  401           }
  402           ImageProducer prod = new FilteredImageSource(image.getSource(),
  403                         new OceanDisabledButtonImageFilter(min , max));
  404           return new ImageIconUIResource(Toolkit.getDefaultToolkit().createImage(prod));
  405       }
  406   
  407   
  408   
  409   
  410       /**
  411        * Used to create a disabled Icon with the ocean look.
  412        */
  413       private static class OceanDisabledButtonImageFilter extends RGBImageFilter{
  414           private float min;
  415           private float factor;
  416   
  417           OceanDisabledButtonImageFilter(int min, int max) {
  418               canFilterIndexColorModel = true;
  419               this.min = (float)min;
  420               this.factor = (max - min) / 255f;
  421           }
  422   
  423           public int filterRGB(int x, int y, int rgb) {
  424               // Coefficients are from the sRGB color space:
  425               int gray = Math.min(255, (int)(((0.2125f * ((rgb >> 16) & 0xFF)) +
  426                       (0.7154f * ((rgb >> 8) & 0xFF)) +
  427                       (0.0721f * (rgb & 0xFF)) + .5f) * factor + min));
  428   
  429               return (rgb & 0xff000000) | (gray << 16) | (gray << 8) |
  430                   (gray << 0);
  431           }
  432       }
  433   
  434   
  435       /**
  436        * Used to create the rollover icons with the ocean look.
  437        */
  438       private static class OceanToolBarImageFilter extends RGBImageFilter {
  439           OceanToolBarImageFilter() {
  440               canFilterIndexColorModel = true;
  441           }
  442   
  443           public int filterRGB(int x, int y, int rgb) {
  444               int r = ((rgb >> 16) & 0xff);
  445               int g = ((rgb >> 8) & 0xff);
  446               int b = (rgb & 0xff);
  447               int gray = Math.max(Math.max(r, g), b);
  448               return (rgb & 0xff000000) | (gray << 16) | (gray << 8) |
  449                   (gray << 0);
  450           }
  451       }
  452   }

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