KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > common > MinimumSizedCheckIcon


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.common;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.Component JavaDoc;
35 import java.awt.Graphics JavaDoc;
36
37 import javax.swing.ButtonModel JavaDoc;
38 import javax.swing.Icon JavaDoc;
39 import javax.swing.JMenuItem JavaDoc;
40 import javax.swing.UIManager JavaDoc;
41
42 /**
43  * An implementation of the <code>Icon</code> interface that has a minimum size
44  * and active border. The minimum size is read from the <code>UIManager</code>
45  * <code>defaultIconSize</code> key.
46  *
47  * @author Karsten Lentzsch
48  * @version $Revision: 1.2 $
49  *
50  * @see MinimumSizedIcon
51  */

52
53 public final class MinimumSizedCheckIcon extends MinimumSizedIcon {
54     
55     private final JMenuItem JavaDoc menuItem;
56     
57     public MinimumSizedCheckIcon(Icon JavaDoc icon, JMenuItem JavaDoc menuItem) {
58         super(icon);
59         this.menuItem = menuItem;
60     }
61     
62     
63     public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
64         paintState(g, x, y);
65         super.paintIcon(c, g, x, y);
66     }
67     
68     private void paintState(Graphics JavaDoc g, int x, int y) {
69         ButtonModel JavaDoc model = menuItem.getModel();
70         //if (!model.isEnabled()) return;
71

72         int w = getIconWidth();
73         int h = getIconHeight();
74         
75         g.translate(x, y);
76         if (model.isSelected() || model.isArmed() /* && model.isPressed()*/) {
77             Color JavaDoc background = model.isArmed()
78                                 ? UIManager.getColor("MenuItem.background")
79                                 : UIManager.getColor("ScrollBar.track");
80             Color JavaDoc upColor = UIManager.getColor("controlLtHighlight");
81             Color JavaDoc downColor = UIManager.getColor("controlDkShadow");
82             
83             // Background
84
g.setColor(background);
85             g.fillRect(0, 0, w, h);
86             // Top and left border
87
g.setColor(model.isSelected() ? downColor : upColor);
88             g.drawLine(0, 0, w-2, 0);
89             g.drawLine(0, 0, 0, h-2);
90             // Bottom and right border
91
g.setColor(model.isSelected() ? upColor: downColor);
92             g.drawLine(0, h-1, w-1, h-1);
93             g.drawLine(w-1, 0, w-1, h-1);
94         }
95         g.translate(-x, -y);
96         g.setColor(UIManager.getColor("textText"));
97     }
98     
99     
100 }
Popular Tags