src/share/classes/javax/swing/plaf/nimbus/NimbusIcon.java

Print this page




  33 import java.awt.image.BufferedImage;
  34 import javax.swing.plaf.UIResource;
  35 
  36 /**
  37  * An icon that delegates to a painter.
  38  * @author rbair
  39  */
  40 class NimbusIcon extends SynthIcon {
  41     private int width;
  42     private int height;
  43     private String prefix;
  44     private String key;
  45 
  46     NimbusIcon(String prefix, String key, int w, int h) {
  47         this.width = w;
  48         this.height = h;
  49         this.prefix = prefix;
  50         this.key = key;
  51     }
  52 





  53     @Override
  54     public void paintIcon(SynthContext context, Graphics g, int x, int y,
  55                           int w, int h) {
  56         Painter painter = null;
  57         if (context != null) {
  58             painter = (Painter)context.getStyle().get(context, key);
  59         }
  60         if (painter == null){
  61             painter = (Painter) UIManager.get(prefix + "[Enabled]." + key);
  62         }
  63 
  64         if (painter != null && context != null) {
  65             JComponent c = context.getComponent();
  66             boolean rotate = false;
  67             boolean flip = false;
  68             //translatex and translatey are additional translations that
  69             //must occur on the graphics context when rendering a toolbar
  70             //icon
  71             int translatex = 0;
  72             int translatey = 0;
  73             if (c instanceof JToolBar) {
  74                 JToolBar toolbar = (JToolBar)c;
  75                 rotate = toolbar.getOrientation() == JToolBar.VERTICAL;
  76                 flip = !toolbar.getComponentOrientation().isLeftToRight();
  77                 Object o = NimbusLookAndFeel.resolveToolbarConstraint(toolbar);
  78                 //we only do the +1 hack for UIResource borders, assuming
  79                 //that the border is probably going to be our border
  80                 if (toolbar.getBorder() instanceof UIResource) {
  81                     if (o == BorderLayout.SOUTH) {


 123                     gfx.translate(-w,0);
 124                     painter.paint(gfx, context.getComponent(), w, h);
 125                 } else {
 126                     painter.paint(gfx, context.getComponent(), w, h);
 127                 }
 128                 gfx.dispose();
 129                 g.drawImage(img,x,y,null);
 130                 img = null;
 131             }
 132         }
 133     }
 134 
 135     /**
 136      * Implements the standard Icon interface's paintIcon method as the standard
 137      * synth stub passes null for the context and this will cause us to not
 138      * paint any thing, so we override here so that we can paint the enabled
 139      * state if no synth context is available
 140      */
 141     @Override
 142     public void paintIcon(Component c, Graphics g, int x, int y) {
 143         Painter painter = (Painter)UIManager.get(prefix + "[Enabled]." + key);

 144         if (painter != null){
 145             JComponent jc = (c instanceof JComponent) ? (JComponent)c : null;
 146             Graphics2D gfx = (Graphics2D)g;
 147             gfx.translate(x, y);
 148             painter.paint(gfx, jc , width, height);
 149             gfx.translate(-x, -y);
 150         }
 151     }
 152 
 153     @Override
 154     public int getIconWidth(SynthContext context) {
 155         if (context == null) {
 156             return width;
 157         }
 158         JComponent c = context.getComponent();
 159         if (c instanceof JToolBar && ((JToolBar)c).getOrientation() == JToolBar.VERTICAL) {
 160             //we only do the -1 hack for UIResource borders, assuming
 161             //that the border is probably going to be our border
 162             if (c.getBorder() instanceof UIResource) {
 163                 return c.getWidth() - 1;




  33 import java.awt.image.BufferedImage;
  34 import javax.swing.plaf.UIResource;
  35 
  36 /**
  37  * An icon that delegates to a painter.
  38  * @author rbair
  39  */
  40 class NimbusIcon extends SynthIcon {
  41     private int width;
  42     private int height;
  43     private String prefix;
  44     private String key;
  45 
  46     NimbusIcon(String prefix, String key, int w, int h) {
  47         this.width = w;
  48         this.height = h;
  49         this.prefix = prefix;
  50         this.key = key;
  51     }
  52 
  53     @SuppressWarnings("unchecked")
  54     private static Painter<JComponent> paintFilter(@SuppressWarnings("rawtypes") Painter painter) {
  55         return (Painter<JComponent>) painter;
  56     }
  57 
  58     @Override
  59     public void paintIcon(SynthContext context, Graphics g, int x, int y,
  60                           int w, int h) {
  61         Painter<JComponent> painter = null;
  62         if (context != null) {
  63             painter = paintFilter((Painter)context.getStyle().get(context, key));
  64         }
  65         if (painter == null){
  66             painter = paintFilter((Painter)UIManager.get(prefix + "[Enabled]." + key));
  67         }
  68 
  69         if (painter != null && context != null) {
  70             JComponent c = context.getComponent();
  71             boolean rotate = false;
  72             boolean flip = false;
  73             //translatex and translatey are additional translations that
  74             //must occur on the graphics context when rendering a toolbar
  75             //icon
  76             int translatex = 0;
  77             int translatey = 0;
  78             if (c instanceof JToolBar) {
  79                 JToolBar toolbar = (JToolBar)c;
  80                 rotate = toolbar.getOrientation() == JToolBar.VERTICAL;
  81                 flip = !toolbar.getComponentOrientation().isLeftToRight();
  82                 Object o = NimbusLookAndFeel.resolveToolbarConstraint(toolbar);
  83                 //we only do the +1 hack for UIResource borders, assuming
  84                 //that the border is probably going to be our border
  85                 if (toolbar.getBorder() instanceof UIResource) {
  86                     if (o == BorderLayout.SOUTH) {


 128                     gfx.translate(-w,0);
 129                     painter.paint(gfx, context.getComponent(), w, h);
 130                 } else {
 131                     painter.paint(gfx, context.getComponent(), w, h);
 132                 }
 133                 gfx.dispose();
 134                 g.drawImage(img,x,y,null);
 135                 img = null;
 136             }
 137         }
 138     }
 139 
 140     /**
 141      * Implements the standard Icon interface's paintIcon method as the standard
 142      * synth stub passes null for the context and this will cause us to not
 143      * paint any thing, so we override here so that we can paint the enabled
 144      * state if no synth context is available
 145      */
 146     @Override
 147     public void paintIcon(Component c, Graphics g, int x, int y) {
 148         Painter<JComponent> painter =
 149             paintFilter((Painter)UIManager.get(prefix + "[Enabled]." + key));
 150         if (painter != null){
 151             JComponent jc = (c instanceof JComponent) ? (JComponent)c : null;
 152             Graphics2D gfx = (Graphics2D)g;
 153             gfx.translate(x, y);
 154             painter.paint(gfx, jc , width, height);
 155             gfx.translate(-x, -y);
 156         }
 157     }
 158 
 159     @Override
 160     public int getIconWidth(SynthContext context) {
 161         if (context == null) {
 162             return width;
 163         }
 164         JComponent c = context.getComponent();
 165         if (c instanceof JToolBar && ((JToolBar)c).getOrientation() == JToolBar.VERTICAL) {
 166             //we only do the -1 hack for UIResource borders, assuming
 167             //that the border is probably going to be our border
 168             if (c.getBorder() instanceof UIResource) {
 169                 return c.getWidth() - 1;