01: /*
02: * Copyright (c) 2001-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
03: *
04: * Redistribution and use in source and binary forms, with or without
05: * modification, are permitted provided that the following conditions are met:
06: *
07: * o Redistributions of source code must retain the above copyright notice,
08: * this list of conditions and the following disclaimer.
09: *
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;
34: import java.awt.Component;
35: import java.awt.Graphics;
36:
37: import javax.swing.ButtonModel;
38: import javax.swing.Icon;
39: import javax.swing.JMenuItem;
40: import javax.swing.UIManager;
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.3 $
49: *
50: * @see MinimumSizedIcon
51: */
52:
53: public final class MinimumSizedCheckIcon extends MinimumSizedIcon {
54:
55: private final JMenuItem menuItem;
56:
57: public MinimumSizedCheckIcon(Icon icon, JMenuItem menuItem) {
58: super (icon);
59: this .menuItem = menuItem;
60: }
61:
62: public void paintIcon(Component c, Graphics g, int x, int y) {
63: paintState(g, x, y);
64: super .paintIcon(c, g, x, y);
65: }
66:
67: private void paintState(Graphics g, int x, int y) {
68: ButtonModel model = menuItem.getModel();
69: //if (!model.isEnabled()) return;
70:
71: int w = getIconWidth();
72: int h = getIconHeight();
73:
74: g.translate(x, y);
75: if (model.isSelected() || model.isArmed() /* && model.isPressed()*/) {
76: Color background = model.isArmed() ? UIManager
77: .getColor("MenuItem.background") : UIManager
78: .getColor("ScrollBar.track");
79: Color upColor = UIManager.getColor("controlLtHighlight");
80: Color downColor = UIManager.getColor("controlDkShadow");
81:
82: // Background
83: g.setColor(background);
84: g.fillRect(0, 0, w, h);
85: // Top and left border
86: g.setColor(model.isSelected() ? downColor : upColor);
87: g.drawLine(0, 0, w - 2, 0);
88: g.drawLine(0, 0, 0, h - 2);
89: // Bottom and right border
90: g.setColor(model.isSelected() ? upColor : downColor);
91: g.drawLine(0, h - 1, w - 1, h - 1);
92: g.drawLine(w - 1, 0, w - 1, h - 1);
93: }
94: g.translate(-x, -y);
95: g.setColor(UIManager.getColor("textText"));
96: }
97:
98: }
|