components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/ScaledIconPanel.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.swing;
       
    27 
       
    28 import java.awt.*;
       
    29 import javax.swing.*;
       
    30 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    31 
       
    32 @SuppressWarnings({"serial"})
       
    33 public class ScaledIconPanel extends JPanel {
       
    34     //
       
    35     // Private methods
       
    36     //
       
    37 
       
    38     private ScaledIcon sIcon;
       
    39     private boolean presAspectRatio;
       
    40     private int hAlign;
       
    41     private int vAlign;
       
    42 
       
    43     //
       
    44     // Constructors
       
    45     //
       
    46 
       
    47     public ScaledIconPanel(Icon icon, boolean presAspectRatio, int hAlign,
       
    48 	int vAlign) {
       
    49 
       
    50 	sIcon = new ScaledIcon(icon);
       
    51 	setPreserveAspectRatio(presAspectRatio);
       
    52 	setHorizontalAlignment(hAlign);
       
    53 	setVerticalAlignment(vAlign);
       
    54     }
       
    55 
       
    56     public ScaledIconPanel(Icon icon, boolean presAspectRatio) {
       
    57 	this(icon, presAspectRatio, SwingConstants.CENTER,
       
    58 	    SwingConstants.CENTER);
       
    59     }
       
    60 
       
    61     public ScaledIconPanel(Icon icon) {
       
    62 	this(icon, false);
       
    63     }
       
    64 
       
    65     public ScaledIconPanel() {
       
    66 	this(null);
       
    67     }
       
    68 
       
    69     //
       
    70     // Component methods
       
    71     //
       
    72 
       
    73     @Override
       
    74     public Dimension getPreferredSize() {
       
    75 	if (isPreferredSizeSet()) {
       
    76 	    return super.getPreferredSize();
       
    77 	}
       
    78 
       
    79 	Insets insets = getInsets();
       
    80 	Icon icon = sIcon.getIcon();
       
    81 	int width = icon.getIconWidth() + insets.left + insets.right;
       
    82 	int height = icon.getIconHeight() + insets.top + insets.bottom;
       
    83 
       
    84 	return new Dimension(width, height);
       
    85     }
       
    86 
       
    87     //
       
    88     // JComponent methods
       
    89     //
       
    90 
       
    91     @Override
       
    92     protected void paintComponent(Graphics g) {
       
    93 	super.paintComponent(g);
       
    94 
       
    95 	Insets insets = getInsets();
       
    96 	int width = getWidth() - insets.left - insets.right;
       
    97 	int height = getHeight() - insets.top - insets.bottom;
       
    98 
       
    99 	int iWidth = width;
       
   100 	int iHeight = height;
       
   101 
       
   102 	int x = insets.left;
       
   103 	int y = insets.top;
       
   104 
       
   105 	if (getPreserveAspectRatio()) {
       
   106 	    Icon icon = sIcon.getIcon();
       
   107 	    float ratio = (float)icon.getIconWidth() / icon.getIconHeight();
       
   108 	    float curRatio = (float)iWidth / iHeight;
       
   109 
       
   110 	    if (ratio > curRatio) {
       
   111 		iHeight = (int)(iWidth / ratio);
       
   112 	    } else {
       
   113 		iWidth = (int)(iHeight * ratio);
       
   114 	    }
       
   115 
       
   116 	    switch (getHorizontalAlignment()) {
       
   117 		case SwingConstants.LEFT:
       
   118 		    // Do nothing
       
   119 		break;
       
   120 
       
   121 		case SwingConstants.RIGHT:
       
   122 		    x += (width - iWidth);
       
   123 		break;
       
   124 
       
   125 		default:
       
   126 		case SwingConstants.CENTER:
       
   127 		    x += (width - iWidth) / 2;
       
   128 		break;
       
   129 	    }
       
   130 
       
   131 	    switch (getVerticalAlignment()) {
       
   132 		case SwingConstants.TOP:
       
   133 		    // Do nothing
       
   134 		break;
       
   135 
       
   136 		case SwingConstants.BOTTOM:
       
   137 		    y += (height - iHeight);
       
   138 		break;
       
   139 
       
   140 		default:
       
   141 		case SwingConstants.CENTER:
       
   142 		    y += (height - iHeight) / 2;
       
   143 		break;
       
   144 	    }
       
   145 	}
       
   146 
       
   147 	sIcon.setIconWidth(iWidth);
       
   148 	sIcon.setIconHeight(iHeight);
       
   149 	sIcon.paintIcon(this, g, x, y);
       
   150     }
       
   151 
       
   152     //
       
   153     // ScaledIconPanel methods
       
   154     //
       
   155 
       
   156     public int getHorizontalAlignment() {
       
   157 	return hAlign;
       
   158     }
       
   159 
       
   160     public boolean getPreserveAspectRatio() {
       
   161 	return presAspectRatio;
       
   162     }
       
   163 
       
   164     public ScaledIcon getScaledIcon() {
       
   165 	return sIcon;
       
   166     }
       
   167 
       
   168     public int getVerticalAlignment() {
       
   169 	return vAlign;
       
   170     }
       
   171 
       
   172     public void setHorizontalAlignment(int hAlign) {
       
   173 	this.hAlign = hAlign;
       
   174     }
       
   175 
       
   176     public void setIcon(Icon icon) {
       
   177 	sIcon.setIcon(icon);
       
   178     }
       
   179 
       
   180     public void setPreserveAspectRatio(boolean presAspectRatio) {
       
   181 	this.presAspectRatio = presAspectRatio;
       
   182     }
       
   183 
       
   184     public void setVerticalAlignment(int vAlign) {
       
   185 	this.vAlign = vAlign;
       
   186     }
       
   187 
       
   188     //
       
   189     // Static methods
       
   190     //
       
   191 
       
   192     /**
       
   193      * Unit test.
       
   194      */
       
   195     public static void main(String[] args) {
       
   196 	Icon icon = Finder.getIcon("images/dialog/dialog-error.png");
       
   197 
       
   198 	ScaledIconPanel panel = new ScaledIconPanel(icon, true);
       
   199 	panel.setBorder(GUIUtil.getEmptyBorder());
       
   200 
       
   201 	JFrame frame = new JFrame();
       
   202 	Container c = frame.getContentPane();
       
   203 	c.setLayout(new BorderLayout());
       
   204 	c.add(panel, BorderLayout.CENTER);
       
   205 	frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       
   206 	frame.pack();
       
   207 	frame.setVisible(true);
       
   208     }
       
   209 }