001    /*
002     * Copyright 2002-2007 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.springframework.richclient.image;
017    
018    import java.awt.Component;
019    import java.awt.Graphics;
020    import java.awt.Insets;
021    
022    import javax.swing.Icon;
023    
024    /**
025     * Code taken from
026     * http://www.jroller.com/santhosh/entry/beautify_swing_applications_toolbar_with
027     * 
028     * @author Santhosh Kumar
029     */
030    public class InsetsIcon implements Icon {
031            private static final Insets DEFAULT_INSETS = new Insets(2, 2, 0, 0);
032    
033            private Icon icon;
034    
035            private Insets insets;
036    
037            public InsetsIcon(Icon icon) {
038                    this(icon, null);
039            }
040    
041            public InsetsIcon(Icon icon, Insets insets) {
042                    this.icon = icon;
043                    this.insets = insets == null ? DEFAULT_INSETS : insets;
044            }
045    
046            public int getIconHeight() {
047                    return icon.getIconHeight() + insets.top + insets.bottom;
048            }
049    
050            public int getIconWidth() {
051                    return icon.getIconWidth() + insets.left + insets.right;
052            }
053    
054            public void paintIcon(Component c, Graphics g, int x, int y) {
055                    icon.paintIcon(c, g, x + insets.left, y + insets.top);
056            }
057    }