src/macosx/classes/com/apple/laf/AquaUtils.java

Print this page




  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.apple.laf;
  27 
  28 import java.awt.*;
  29 import java.awt.image.*;
  30 import java.lang.ref.SoftReference;
  31 import java.lang.ref.WeakReference;
  32 import java.lang.reflect.Method;
  33 import java.security.PrivilegedAction;
  34 import java.util.*;
  35 
  36 import javax.swing.*;
  37 import javax.swing.border.Border;

  38 
  39 import sun.awt.AppContext;
  40 
  41 import sun.lwawt.macosx.CImage;
  42 import sun.lwawt.macosx.CImage.Creator;

  43 import sun.swing.SwingUtilities2;
  44 
  45 import com.apple.laf.AquaImageFactory.SlicedImageControl;
  46 
  47 public class AquaUtils {
  48     final static String ANIMATIONS_SYSTEM_PROPERTY = "swing.enableAnimations";
  49 
  50     /*
  51      * Convenience function for determining ComponentOrientation.  Helps us
  52      * avoid having Munge directives throughout the code.
  53      */
  54     public static boolean isLeftToRight(final Component c) {
  55         return c.getComponentOrientation().isLeftToRight();
  56     }
  57 
  58     public static void enforceComponentOrientation(Component c, ComponentOrientation orientation) {
  59         c.setComponentOrientation(orientation);
  60         if (c instanceof Container) {
  61             for (Component child : ((Container)c).getComponents()) {
  62                 enforceComponentOrientation(child, orientation);


 372                             return method;
 373                         } catch (final Throwable e) {
 374                             return null;
 375                         }
 376                     }
 377                 }
 378             );
 379         }
 380     };
 381 
 382     private static final Integer OPAQUE_SET_FLAG = new Integer(24); // private int JComponent.OPAQUE_SET
 383     protected static boolean hasOpaqueBeenExplicitlySet(final JComponent c) {
 384         final Method method = getJComponentGetFlagMethod.get();
 385         if (method == null) return false;
 386         try {
 387             return Boolean.TRUE.equals(method.invoke(c, OPAQUE_SET_FLAG));
 388         } catch (final Throwable e) {
 389             return false;
 390         }
 391     }














































 392 }



  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.apple.laf;
  27 
  28 import java.awt.*;
  29 import java.awt.image.*;
  30 import java.lang.ref.SoftReference;

  31 import java.lang.reflect.Method;
  32 import java.security.PrivilegedAction;
  33 import java.util.*;
  34 
  35 import javax.swing.*;
  36 import javax.swing.border.Border;
  37 import javax.swing.plaf.UIResource;
  38 
  39 import sun.awt.AppContext;
  40 
  41 import sun.lwawt.macosx.CImage;
  42 import sun.lwawt.macosx.CImage.Creator;
  43 import sun.lwawt.macosx.CPlatformWindow;
  44 import sun.swing.SwingUtilities2;
  45 
  46 import com.apple.laf.AquaImageFactory.SlicedImageControl;
  47 
  48 public class AquaUtils {
  49     final static String ANIMATIONS_SYSTEM_PROPERTY = "swing.enableAnimations";
  50 
  51     /*
  52      * Convenience function for determining ComponentOrientation.  Helps us
  53      * avoid having Munge directives throughout the code.
  54      */
  55     public static boolean isLeftToRight(final Component c) {
  56         return c.getComponentOrientation().isLeftToRight();
  57     }
  58 
  59     public static void enforceComponentOrientation(Component c, ComponentOrientation orientation) {
  60         c.setComponentOrientation(orientation);
  61         if (c instanceof Container) {
  62             for (Component child : ((Container)c).getComponents()) {
  63                 enforceComponentOrientation(child, orientation);


 373                             return method;
 374                         } catch (final Throwable e) {
 375                             return null;
 376                         }
 377                     }
 378                 }
 379             );
 380         }
 381     };
 382 
 383     private static final Integer OPAQUE_SET_FLAG = new Integer(24); // private int JComponent.OPAQUE_SET
 384     protected static boolean hasOpaqueBeenExplicitlySet(final JComponent c) {
 385         final Method method = getJComponentGetFlagMethod.get();
 386         if (method == null) return false;
 387         try {
 388             return Boolean.TRUE.equals(method.invoke(c, OPAQUE_SET_FLAG));
 389         } catch (final Throwable e) {
 390             return false;
 391         }
 392     }
 393 
 394     protected static boolean isWindowTextured(final Component c) {
 395         if (!(c instanceof JComponent)) {
 396             return false;
 397         }
 398         final JRootPane pane = ((JComponent) c).getRootPane();
 399         if (pane == null) {
 400             return false;
 401         }
 402         Object prop = pane.getClientProperty(
 403                 CPlatformWindow.WINDOW_BRUSH_METAL_LOOK);
 404         if (prop != null) {
 405             return Boolean.parseBoolean(prop.toString());
 406         }
 407         prop = pane.getClientProperty(CPlatformWindow.WINDOW_STYLE);
 408         return prop != null && "textured".equals(prop);
 409     }
 410 
 411     private static Color resetAlpha(final Color color) {
 412         return new Color(color.getRed(), color.getGreen(), color.getBlue(), 0);
 413     }
 414 
 415     protected static void fillRect(final Graphics g, final Component c) {
 416         fillRect(g, c, c.getBackground(), 0, 0, c.getWidth(), c.getHeight());
 417     }
 418 
 419     protected static void fillRect(final Graphics g, final Component c,
 420                                    final Color color, final int x, final int y,
 421                                    final int w, final int h) {
 422         if (!(g instanceof Graphics2D)) {
 423             return;
 424         }
 425         final Graphics2D cg = (Graphics2D) g.create();
 426         try {
 427             if (color instanceof UIResource && isWindowTextured(c)
 428                     && color.equals(SystemColor.window)) {
 429                 cg.setComposite(AlphaComposite.Src);
 430                 cg.setColor(resetAlpha(color));
 431             } else {
 432                 cg.setColor(color);
 433             }
 434             cg.fillRect(x, y, w, h);
 435         } finally {
 436             cg.dispose();
 437         }
 438     }
 439 }
 440