1 //
   2 //  AquaFocus.java
   3 //  Copyright (c) 2009 Apple Inc. All rights reserved.
   4 //
   5 
   6 package com.apple.laf;
   7 
   8 import java.awt.*;
   9 
  10 import javax.swing.*;
  11 
  12 import sun.java2d.*;
  13 import apple.laf.JRSUIFocus;
  14 
  15 import com.apple.laf.AquaUtils.Painter;
  16 
  17 public class AquaFocus {
  18     interface Drawable {
  19         public void draw(final Graphics2D sg2d);
  20     }
  21 
  22     static boolean paintFocus(final Graphics g, final Drawable drawable) {
  23         // TODO: requires OSXSurfaceData
  24         return false;
  25         /*if (!(g instanceof SunGraphics2D)) return false;
  26         final SunGraphics2D sg2d = (SunGraphics2D)g;
  27         
  28         final SurfaceData surfaceData = sg2d.getSurfaceData();
  29         if (!(surfaceData instanceof OSXSurfaceData)) return false;
  30 
  31         try {
  32             ((OSXSurfaceData)surfaceData).performCocoaDrawing(sg2d, new OSXSurfaceData.CGContextDrawable() {
  33                 @Override
  34                 public void drawIntoCGContext(final long cgContext) {
  35                     final JRSUIFocus focus = new JRSUIFocus(cgContext);
  36                     focus.beginFocus(JRSUIFocus.RING_BELOW);
  37                     drawable.draw(sg2d);
  38                     focus.endFocus();
  39                 }
  40             });
  41         } finally {
  42             sg2d.dispose();
  43         }
  44         return true;*/
  45     }
  46     
  47     public static Icon createFocusedIcon(final Icon tmpIcon, final Component c, final int slack) {
  48         return new FocusedIcon(tmpIcon, slack);
  49     }
  50     
  51 /* -- disabled until we can get the real JRSUI focus painter working
  52 
  53     static class FocusedIcon implements Icon {
  54         final Icon icon;
  55         final int slack;
  56         
  57         public FocusedIcon(final Icon icon, final int slack) {
  58             this.icon = icon;
  59             this.slack = slack;
  60         }
  61 
  62         @Override
  63         public int getIconHeight() {
  64             return icon.getIconHeight() + slack + slack;
  65         }
  66 
  67         @Override
  68         public int getIconWidth() {
  69             return icon.getIconWidth() + slack + slack;
  70         }
  71 
  72         @Override
  73         public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
  74             final boolean painted = paintFocus(g, new Drawable() {
  75                 @Override
  76                 public void draw(SunGraphics2D sg2d) {
  77                     icon.paintIcon(c, sg2d, x + slack, y + slack);
  78                 }
  79             });
  80             if (!painted) {
  81                 icon.paintIcon(c, g, x + slack, y + slack);
  82             }
  83         }
  84     }
  85  */
  86     
  87     static class FocusedIcon extends AquaUtils.ShadowBorder implements Icon {
  88         final Icon icon;
  89         final int slack;
  90         
  91         public FocusedIcon(final Icon icon, final int slack) {
  92             super(
  93                 new Painter() {
  94                     public void paint(Graphics g, int x, int y, int w, int h) {
  95                         Graphics2D imgG = (Graphics2D)g;
  96                         imgG.setComposite(AlphaComposite.Src);
  97                         imgG.setColor(UIManager.getColor("Focus.color"));
  98                         imgG.fillRect(x, y, w - (slack * 2), h - (slack * 2));
  99                         imgG.setComposite(AlphaComposite.DstAtop);
 100                         icon.paintIcon(null, imgG, x, y);
 101                     }
 102                 },
 103                 new Painter() {
 104                     public void paint(Graphics g, int x, int y, int w, int h) {
 105                         ((Graphics2D)g).setComposite(AlphaComposite.SrcAtop);
 106                         icon.paintIcon(null, g, x, y);
 107                     }
 108                 },
 109                 slack, slack, 0.0f, 1.8f, 7
 110             );
 111             this.icon = icon;
 112             this.slack = slack;
 113         }
 114         
 115         @Override
 116         public int getIconHeight() {
 117             return icon.getIconHeight() + slack + slack;
 118         }
 119 
 120         @Override
 121         public int getIconWidth() {
 122             return icon.getIconWidth() + slack + slack;
 123         }
 124 
 125         @Override
 126         public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
 127             paintBorder(c, g, x, y, getIconWidth(), getIconHeight());
 128             icon.paintIcon(c, g, x + slack, y + slack);
 129         }
 130     }
 131 }