components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/misc/IconUtil.java
changeset 827 0944d8c0158b
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.util.misc;
       
    27 
       
    28 import java.awt.*;
       
    29 import java.util.*;
       
    30 import java.util.List;
       
    31 import javax.swing.*;
       
    32 import com.oracle.solaris.vp.util.swing.*;
       
    33 
       
    34 public class IconUtil {
       
    35     //
       
    36     // Static methods
       
    37     //
       
    38 
       
    39     /**
       
    40      * Creates, from the given icon, a new icon with the given height.	If the
       
    41      * given height is greater than the height of the given icon, the icon will
       
    42      * be padded.  If the given height is less than the height of the given
       
    43      * icon, the icon will be shrunk.
       
    44      *
       
    45      * @param	    icon
       
    46      *		    the icon to crop or pad
       
    47      *
       
    48      * @param	    height
       
    49      *		    a height to apply to the given icon, or -1 if no change
       
    50      *		    should be made to this icon's height
       
    51      *
       
    52      * @return	    a new {@code Icon}
       
    53      */
       
    54     public static Icon ensureIconHeight(Icon icon, int height) {
       
    55 	if (icon != null) {
       
    56 	    int iWidth = icon.getIconWidth();
       
    57 	    int iHeight = icon.getIconHeight();
       
    58 
       
    59 	    if (iHeight > height) {
       
    60 		// Maintain aspect ratio
       
    61 		icon = new ScaledIcon(
       
    62 		    icon, (iWidth * height) / iHeight, height);
       
    63 	    } else if (iHeight < height) {
       
    64 		icon = new CroppedPaddedIcon(icon, iWidth, height);
       
    65 	    }
       
    66 	}
       
    67 
       
    68 	return icon;
       
    69     }
       
    70 
       
    71     public static Icon getBadgedIcon(Icon icon, Icon badge) {
       
    72 	int sHeight = badge.getIconHeight();
       
    73 	int sWidth = badge.getIconWidth();
       
    74 
       
    75 	return getBadgedIcon(icon, badge, sWidth / 2, sHeight / 2);
       
    76     }
       
    77 
       
    78     public static Icon getBadgedIcon(final Icon icon, final Icon badge,
       
    79 	int xOffset, int yOffset) {
       
    80 
       
    81 	// Calculate once
       
    82 	final int sHeight = badge.getIconHeight();
       
    83 	final int sWidth = badge.getIconWidth();
       
    84 	final int height = icon.getIconHeight() + yOffset;
       
    85 	final int width = icon.getIconWidth() + xOffset;
       
    86 
       
    87 	return new Icon() {
       
    88 	    public int getIconHeight() {
       
    89 		return height;
       
    90 	    }
       
    91 
       
    92 	    public int getIconWidth() {
       
    93 		return width;
       
    94 	    }
       
    95 
       
    96 	    public void paintIcon(Component c, Graphics g, int x, int y) {
       
    97 		icon.paintIcon(c, g, x, y);
       
    98 		badge.paintIcon(c, g, x + width - sWidth, y + height - sHeight);
       
    99 	    }
       
   100 	};
       
   101     }
       
   102 
       
   103     /**
       
   104      * From the given icons, chooses the icon taller than (if available) and
       
   105      * closest to the given height.
       
   106      *
       
   107      * @param	    icons
       
   108      *		    the icons to choose from
       
   109      *
       
   110      * @param	    height
       
   111      *		    the desired height of the icon
       
   112      */
       
   113     public static <I extends Icon> I getClosestIcon(Collection<I> icons,
       
   114 	int height) {
       
   115 
       
   116 	I icon = null;
       
   117 
       
   118 	if (icons != null) {
       
   119 	    int diff = 0;
       
   120 
       
   121 	    for (I i : icons) {
       
   122 		int d = i.getIconHeight() - height;
       
   123 		if (d == 0) {
       
   124 		    return i;
       
   125 		}
       
   126 
       
   127 		if (icon == null ||
       
   128 		    (diff < 0 && d > diff) ||
       
   129 		    (diff > 0 && d < diff && d >= 0)) {
       
   130 
       
   131 		    icon = i;
       
   132 		    diff = d;
       
   133 		}
       
   134 	    }
       
   135 	}
       
   136 
       
   137 	return icon;
       
   138     }
       
   139 
       
   140     /**
       
   141      * Converts the given {@code Icon}s to {@code Image}s.
       
   142      *
       
   143      * @param	    icons
       
   144      *		    a {@code List} of {@code Icon}s
       
   145      *
       
   146      * @return	    a (possibly empty) {@code List} of {@code Image}s, or {@code
       
   147      *		    null} if {@code icons} is {@code null}
       
   148      */
       
   149     public static List<Image> toImages(List<? extends Icon> icons) {
       
   150 	// This simple implementation only works with ImageIcons
       
   151 	List<Image> images = null;
       
   152 	if (icons != null) {
       
   153 	    images = new ArrayList<Image>(icons.size());
       
   154 	    for (Icon icon : icons) {
       
   155 		if (icon instanceof ImageIcon) {
       
   156 		    Image image = ((ImageIcon)icon).getImage();
       
   157 		    images.add(image);
       
   158 		}
       
   159 	    }
       
   160 	}
       
   161 	return images;
       
   162     }
       
   163 }