src/share/classes/sun/awt/SunToolkit.java

Print this page




  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 sun.awt;
  27 
  28 import java.awt.*;
  29 import static java.awt.RenderingHints.*;
  30 import java.awt.dnd.*;
  31 import java.awt.dnd.peer.DragSourceContextPeer;
  32 import java.awt.peer.*;
  33 import java.awt.event.WindowEvent;
  34 import java.awt.event.KeyEvent;
  35 import java.awt.image.*;
  36 import java.awt.TrayIcon;
  37 import java.awt.SystemTray;
  38 import java.awt.event.InputEvent;



  39 import java.net.URL;
  40 import java.util.*;
  41 import java.util.concurrent.TimeUnit;
  42 import java.util.concurrent.locks.Condition;
  43 import java.util.concurrent.locks.Lock;
  44 import java.util.concurrent.locks.ReentrantLock;
  45 
  46 import sun.security.util.SecurityConstants;
  47 import sun.util.logging.PlatformLogger;
  48 import sun.misc.SoftCache;
  49 import sun.font.FontDesignMetrics;
  50 import sun.awt.im.InputContext;
  51 import sun.awt.image.*;
  52 import sun.security.action.GetPropertyAction;
  53 import sun.security.action.GetBooleanAction;
  54 import java.lang.reflect.InvocationTargetException;
  55 import java.security.AccessController;
  56 
  57 public abstract class SunToolkit extends Toolkit
  58     implements WindowClosingSupport, WindowClosingListener,


 698     /**
 699      * Returns the value of "sun.awt.noerasebackground" property. Default
 700      * value is {@code false}.
 701      */
 702     public static boolean getSunAwtNoerasebackground() {
 703         return AccessController.doPrivileged(new GetBooleanAction("sun.awt.noerasebackground"));
 704     }
 705 
 706     /**
 707      * Returns the value of "sun.awt.erasebackgroundonresize" property. Default
 708      * value is {@code false}.
 709      */
 710     public static boolean getSunAwtErasebackgroundonresize() {
 711         return AccessController.doPrivileged(new GetBooleanAction("sun.awt.erasebackgroundonresize"));
 712     }
 713 
 714 
 715     static final SoftCache imgCache = new SoftCache();
 716 
 717     static Image getImageFromHash(Toolkit tk, URL url) {
 718         SecurityManager sm = System.getSecurityManager();
 719         if (sm != null) {
 720             try {
 721                 java.security.Permission perm =
 722                     url.openConnection().getPermission();
 723                 if (perm != null) {
 724                     try {
 725                         sm.checkPermission(perm);
 726                     } catch (SecurityException se) {
 727                         // fallback to checkRead/checkConnect for pre 1.2
 728                         // security managers
 729                         if ((perm instanceof java.io.FilePermission) &&
 730                             perm.getActions().indexOf("read") != -1) {
 731                             sm.checkRead(perm.getName());
 732                         } else if ((perm instanceof
 733                             java.net.SocketPermission) &&
 734                             perm.getActions().indexOf("connect") != -1) {
 735                             sm.checkConnect(url.getHost(), url.getPort());
 736                         } else {
 737                             throw se;
 738                         }
 739                     }
 740                 }
 741             } catch (java.io.IOException ioe) {
 742                     sm.checkConnect(url.getHost(), url.getPort());
 743             }
 744         }
 745         synchronized (imgCache) {
 746             Image img = (Image)imgCache.get(url);
 747             if (img == null) {
 748                 try {
 749                     img = tk.createImage(new URLImageSource(url));
 750                     imgCache.put(url, img);
 751                 } catch (Exception e) {
 752                 }
 753             }
 754             return img;
 755         }
 756     }
 757 
 758     static Image getImageFromHash(Toolkit tk,
 759                                                String filename) {
 760         SecurityManager security = System.getSecurityManager();
 761         if (security != null) {
 762             security.checkRead(filename);
 763         }
 764         synchronized (imgCache) {
 765             Image img = (Image)imgCache.get(filename);
 766             if (img == null) {
 767                 try {
 768                     img = tk.createImage(new FileImageSource(filename));
 769                     imgCache.put(filename, img);
 770                 } catch (Exception e) {
 771                 }
 772             }
 773             return img;
 774         }
 775     }
 776 
 777     public Image getImage(String filename) {
 778         return getImageFromHash(this, filename);
 779     }
 780 
 781     public Image getImage(URL url) {
 782         return getImageFromHash(this, url);
 783     }
 784 
 785     public Image createImage(String filename) {
 786         SecurityManager security = System.getSecurityManager();
 787         if (security != null) {
 788             security.checkRead(filename);


 789         }
 790         return createImage(new FileImageSource(filename));



 791     }
 792 
 793     public Image createImage(URL url) {
 794         SecurityManager sm = System.getSecurityManager();
 795         if (sm != null) {
 796             try {
 797                 java.security.Permission perm =
 798                     url.openConnection().getPermission();
 799                 if (perm != null) {
 800                     try {
 801                         sm.checkPermission(perm);
 802                     } catch (SecurityException se) {
 803                         // fallback to checkRead/checkConnect for pre 1.2
 804                         // security managers
 805                         if ((perm instanceof java.io.FilePermission) &&
 806                             perm.getActions().indexOf("read") != -1) {
 807                             sm.checkRead(perm.getName());
 808                         } else if ((perm instanceof
 809                             java.net.SocketPermission) &&
 810                             perm.getActions().indexOf("connect") != -1) {
 811                             sm.checkConnect(url.getHost(), url.getPort());
 812                         } else {
 813                             throw se;
 814                         }












 815                     }
 816                 }
 817             } catch (java.io.IOException ioe) {
 818                     sm.checkConnect(url.getHost(), url.getPort());
 819             }


 820         }



 821         return createImage(new URLImageSource(url));
 822     }
 823 
 824     public Image createImage(byte[] data, int offset, int length) {
 825         return createImage(new ByteArrayImageSource(data, offset, length));
 826     }
 827 
 828     public Image createImage(ImageProducer producer) {
 829         return new ToolkitImage(producer);
 830     }
 831 





 832     public int checkImage(Image img, int w, int h, ImageObserver o) {
 833         if (!(img instanceof ToolkitImage)) {
 834             return ImageObserver.ALLBITS;
 835         }
 836 
 837         ToolkitImage tkimg = (ToolkitImage)img;
 838         int repbits;
 839         if (w == 0 || h == 0) {
 840             repbits = ImageObserver.ALLBITS;
 841         } else {
 842             repbits = tkimg.getImageRep().check(o);
 843         }
 844         return tkimg.check(o) | repbits;
 845     }
 846 
 847     public boolean prepareImage(Image img, int w, int h, ImageObserver o) {
 848         if (w == 0 || h == 0) {
 849             return true;
 850         }
 851 
 852         // Must be a ToolkitImage
 853         if (!(img instanceof ToolkitImage)) {
 854             return true;
 855         }
 856 
 857         ToolkitImage tkimg = (ToolkitImage)img;
 858         if (tkimg.hasError()) {
 859             if (o != null) {
 860                 o.imageUpdate(img, ImageObserver.ERROR|ImageObserver.ABORT,
 861                               -1, -1, -1, -1);
 862             }
 863             return false;
 864         }
 865         ImageRepresentation ir = tkimg.getImageRep();
 866         return ir.prepare(o);


























































































 867     }
 868 
 869     /**
 870      * Scans {@code imageList} for best-looking image of specified dimensions.
 871      * Image can be scaled and/or padded with transparency.
 872      */
 873     public static BufferedImage getScaledIconImage(java.util.List<Image> imageList, int width, int height) {
 874         if (width == 0 || height == 0) {
 875             return null;
 876         }
 877         Image bestImage = null;
 878         int bestWidth = 0;
 879         int bestHeight = 0;
 880         double bestSimilarity = 3; //Impossibly high value
 881         double bestScaleFactor = 0;
 882         for (Iterator<Image> i = imageList.iterator();i.hasNext();) {
 883             //Iterate imageList looking for best matching image.
 884             //'Similarity' measure is defined as good scale factor and small insets.
 885             //best possible similarity is 0 (no scale, no insets).
 886             //It's found while the experiments that good-looking result is achieved




  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 sun.awt;
  27 
  28 import java.awt.*;
  29 import static java.awt.RenderingHints.*;
  30 import java.awt.dnd.*;
  31 import java.awt.dnd.peer.DragSourceContextPeer;
  32 import java.awt.peer.*;
  33 import java.awt.event.WindowEvent;
  34 import java.awt.event.KeyEvent;
  35 import java.awt.image.*;
  36 import java.awt.TrayIcon;
  37 import java.awt.SystemTray;
  38 import java.awt.event.InputEvent;
  39 import java.io.File;
  40 import java.io.IOException;
  41 import java.io.InputStream;
  42 import java.net.URL;
  43 import java.util.*;
  44 import java.util.concurrent.TimeUnit;
  45 import java.util.concurrent.locks.Condition;
  46 import java.util.concurrent.locks.Lock;
  47 import java.util.concurrent.locks.ReentrantLock;
  48 
  49 import sun.security.util.SecurityConstants;
  50 import sun.util.logging.PlatformLogger;
  51 import sun.misc.SoftCache;
  52 import sun.font.FontDesignMetrics;
  53 import sun.awt.im.InputContext;
  54 import sun.awt.image.*;
  55 import sun.security.action.GetPropertyAction;
  56 import sun.security.action.GetBooleanAction;
  57 import java.lang.reflect.InvocationTargetException;
  58 import java.security.AccessController;
  59 
  60 public abstract class SunToolkit extends Toolkit
  61     implements WindowClosingSupport, WindowClosingListener,


 701     /**
 702      * Returns the value of "sun.awt.noerasebackground" property. Default
 703      * value is {@code false}.
 704      */
 705     public static boolean getSunAwtNoerasebackground() {
 706         return AccessController.doPrivileged(new GetBooleanAction("sun.awt.noerasebackground"));
 707     }
 708 
 709     /**
 710      * Returns the value of "sun.awt.erasebackgroundonresize" property. Default
 711      * value is {@code false}.
 712      */
 713     public static boolean getSunAwtErasebackgroundonresize() {
 714         return AccessController.doPrivileged(new GetBooleanAction("sun.awt.erasebackgroundonresize"));
 715     }
 716 
 717 
 718     static final SoftCache imgCache = new SoftCache();
 719 
 720     static Image getImageFromHash(Toolkit tk, URL url) {
 721         checkPermissions(url);


























 722         synchronized (imgCache) {
 723             Image img = (Image)imgCache.get(url);
 724             if (img == null) {
 725                 try {
 726                     img = tk.createImage(new URLImageSource(url));
 727                     imgCache.put(url, img);
 728                 } catch (Exception e) {
 729                 }
 730             }
 731             return img;
 732         }
 733     }
 734 
 735     static Image getImageFromHash(Toolkit tk,
 736                                                String filename) {
 737         checkPermissions(filename);



 738         synchronized (imgCache) {
 739             Image img = (Image)imgCache.get(filename);
 740             if (img == null) {
 741                 try {
 742                     img = tk.createImage(new FileImageSource(filename));
 743                     imgCache.put(filename, img);
 744                 } catch (Exception e) {
 745                 }
 746             }
 747             return img;
 748         }
 749     }
 750 
 751     public Image getImage(String filename) {
 752         return getImageFromHash(this, filename);
 753     }
 754 
 755     public Image getImage(URL url) {
 756         return getImageFromHash(this, url);
 757     }
 758 
 759     protected Image getImageWithResolutionVariant(String fileName,
 760             String resolutionVariantName) {
 761         synchronized (imgCache) {
 762             Image image = getImageFromHash(this, fileName);
 763             if (image instanceof MultiResolutionImage) {
 764                 return image;
 765             }
 766             Image resolutionVariant = getImageFromHash(this, resolutionVariantName);
 767             image = createImageWithResolutionVariant(image, resolutionVariant);
 768             imgCache.put(fileName, image);
 769             return image;
 770         }






















 771     }
 772 
 773     protected Image getImageWithResolutionVariant(URL url,
 774             URL resolutionVariantURL) {
 775         synchronized (imgCache) {
 776             Image image = getImageFromHash(this, url);
 777             if (image instanceof MultiResolutionImage) {
 778                 return image;
 779             }
 780             Image resolutionVariant = getImageFromHash(this, resolutionVariantURL);
 781             image = createImageWithResolutionVariant(image, resolutionVariant);
 782             imgCache.put(url, image);
 783             return image;
 784         }
 785     }
 786 
 787 
 788     public Image createImage(String filename) {
 789         checkPermissions(filename);
 790         return createImage(new FileImageSource(filename));
 791     }
 792 
 793     public Image createImage(URL url) {
 794         checkPermissions(url);
 795         return createImage(new URLImageSource(url));
 796     }
 797 
 798     public Image createImage(byte[] data, int offset, int length) {
 799         return createImage(new ByteArrayImageSource(data, offset, length));
 800     }
 801 
 802     public Image createImage(ImageProducer producer) {
 803         return new ToolkitImage(producer);
 804     }
 805 
 806     public static Image createImageWithResolutionVariant(Image image,
 807             Image resolutionVariant) {
 808         return new MultiResolutionToolkitImage(image, resolutionVariant);
 809     }
 810 
 811     public int checkImage(Image img, int w, int h, ImageObserver o) {
 812         if (!(img instanceof ToolkitImage)) {
 813             return ImageObserver.ALLBITS;
 814         }
 815 
 816         ToolkitImage tkimg = (ToolkitImage)img;
 817         int repbits;
 818         if (w == 0 || h == 0) {
 819             repbits = ImageObserver.ALLBITS;
 820         } else {
 821             repbits = tkimg.getImageRep().check(o);
 822         }
 823         return (tkimg.check(o) | repbits) & checkResolutionVariant(img, w, h, o);
 824     }
 825 
 826     public boolean prepareImage(Image img, int w, int h, ImageObserver o) {
 827         if (w == 0 || h == 0) {
 828             return true;
 829         }
 830 
 831         // Must be a ToolkitImage
 832         if (!(img instanceof ToolkitImage)) {
 833             return true;
 834         }
 835 
 836         ToolkitImage tkimg = (ToolkitImage)img;
 837         if (tkimg.hasError()) {
 838             if (o != null) {
 839                 o.imageUpdate(img, ImageObserver.ERROR|ImageObserver.ABORT,
 840                               -1, -1, -1, -1);
 841             }
 842             return false;
 843         }
 844         ImageRepresentation ir = tkimg.getImageRep();
 845         return ir.prepare(o) & prepareResolutionVariant(img, w, h, o);
 846     }
 847 
 848     private int checkResolutionVariant(Image img, int w, int h, ImageObserver o) {
 849         ToolkitImage rvImage = getResolutionVariant(img);
 850         // Ignore the resolution variant in case of error
 851         return (rvImage == null || rvImage.hasError()) ? 0xFFFF :
 852                 checkImage(rvImage, 2 * w, 2 * h, MultiResolutionToolkitImage.
 853                                 getResolutionVariantObserver(
 854                                         img, o, w, h, 2 * w, 2 * h));
 855     }
 856 
 857     private boolean prepareResolutionVariant(Image img, int w, int h,
 858             ImageObserver o) {
 859 
 860         ToolkitImage rvImage = getResolutionVariant(img);
 861         // Ignore the resolution variant in case of error
 862         return rvImage == null || rvImage.hasError() || prepareImage(
 863                 rvImage, 2 * w, 2 * h,
 864                 MultiResolutionToolkitImage.getResolutionVariantObserver(
 865                         img, o, w, h, 2 * w, 2 * h));
 866     }
 867 
 868     private static ToolkitImage getResolutionVariant(Image image) {
 869         if (image instanceof MultiResolutionToolkitImage) {
 870             Image resolutionVariant = ((MultiResolutionToolkitImage) image).
 871                     getResolutionVariant();
 872             if (resolutionVariant instanceof ToolkitImage) {
 873                 return (ToolkitImage) resolutionVariant;
 874             }
 875         }
 876         return null;
 877     }
 878 
 879     protected static boolean imageCached(Object key) {
 880         return imgCache.containsKey(key);
 881     }
 882 
 883     protected static boolean imageExists(String filename) {
 884         checkPermissions(filename);
 885         return filename != null && new File(filename).exists();
 886     }
 887 
 888     @SuppressWarnings("try")
 889     protected static boolean imageExists(URL url) {
 890         checkPermissions(url);
 891         if (url != null) {
 892             try (InputStream is = url.openStream()) {
 893                 return true;
 894             }catch(IOException e){
 895                 return false;
 896             }
 897         }
 898         return false;
 899     }
 900 
 901     private static void checkPermissions(String filename) {
 902         SecurityManager security = System.getSecurityManager();
 903         if (security != null) {
 904             security.checkRead(filename);
 905         }
 906     }
 907 
 908     private static void checkPermissions(URL url) {
 909         SecurityManager sm = System.getSecurityManager();
 910         if (sm != null) {
 911             try {
 912                 java.security.Permission perm =
 913                     url.openConnection().getPermission();
 914                 if (perm != null) {
 915                     try {
 916                         sm.checkPermission(perm);
 917                     } catch (SecurityException se) {
 918                         // fallback to checkRead/checkConnect for pre 1.2
 919                         // security managers
 920                         if ((perm instanceof java.io.FilePermission) &&
 921                             perm.getActions().indexOf("read") != -1) {
 922                             sm.checkRead(perm.getName());
 923                         } else if ((perm instanceof
 924                             java.net.SocketPermission) &&
 925                             perm.getActions().indexOf("connect") != -1) {
 926                             sm.checkConnect(url.getHost(), url.getPort());
 927                         } else {
 928                             throw se;
 929                         }
 930                     }
 931                 }
 932             } catch (java.io.IOException ioe) {
 933                     sm.checkConnect(url.getHost(), url.getPort());
 934             }
 935         }
 936     }
 937 
 938     /**
 939      * Scans {@code imageList} for best-looking image of specified dimensions.
 940      * Image can be scaled and/or padded with transparency.
 941      */
 942     public static BufferedImage getScaledIconImage(java.util.List<Image> imageList, int width, int height) {
 943         if (width == 0 || height == 0) {
 944             return null;
 945         }
 946         Image bestImage = null;
 947         int bestWidth = 0;
 948         int bestHeight = 0;
 949         double bestSimilarity = 3; //Impossibly high value
 950         double bestScaleFactor = 0;
 951         for (Iterator<Image> i = imageList.iterator();i.hasNext();) {
 952             //Iterate imageList looking for best matching image.
 953             //'Similarity' measure is defined as good scale factor and small insets.
 954             //best possible similarity is 0 (no scale, no insets).
 955             //It's found while the experiments that good-looking result is achieved