001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.swing.plaf.misc;
018:
019: import com.l2fprod.common.swing.plaf.ButtonBarButtonUI;
020: import com.l2fprod.common.swing.plaf.basic.BasicButtonBarUI;
021:
022: import java.awt.Color;
023: import java.awt.FontMetrics;
024: import java.awt.Graphics;
025: import java.awt.Rectangle;
026:
027: import javax.swing.AbstractButton;
028: import javax.swing.BorderFactory;
029: import javax.swing.ButtonModel;
030: import javax.swing.JButton;
031: import javax.swing.JComponent;
032: import javax.swing.border.Border;
033: import javax.swing.border.CompoundBorder;
034: import javax.swing.plaf.BorderUIResource;
035: import javax.swing.plaf.ColorUIResource;
036: import javax.swing.plaf.ComponentUI;
037: import javax.swing.plaf.UIResource;
038: import javax.swing.plaf.basic.BasicButtonUI;
039: import javax.swing.plaf.basic.BasicGraphicsUtils;
040: import javax.swing.plaf.basic.BasicHTML;
041:
042: /**
043: * An implementation for the JButtonBar UI which looks like the one found in
044: * <a href="http://www.stardock.com/products/iconpackager/">IconPackager 2.5
045: * </a>.
046: */
047: public class IconPackagerButtonBarUI extends BasicButtonBarUI {
048:
049: public static ComponentUI createUI(JComponent c) {
050: return new IconPackagerButtonBarUI();
051: }
052:
053: protected void installDefaults() {
054: Border b = bar.getBorder();
055: if (b == null || b instanceof UIResource) {
056: bar.setBorder(new BorderUIResource(new CompoundBorder(
057: BorderFactory.createEtchedBorder(), BorderFactory
058: .createEmptyBorder(2, 2, 2, 2))));
059: }
060:
061: if (bar.getBackground() == null
062: || bar.getBackground() instanceof UIResource) {
063: bar.setBackground(new ColorUIResource(128, 128, 128));
064: bar.setOpaque(true);
065: }
066: }
067:
068: public void installButtonBarUI(AbstractButton button) {
069: button.setUI(new ButtonUI());
070: button.setHorizontalTextPosition(JButton.CENTER);
071: button.setVerticalTextPosition(JButton.BOTTOM);
072: }
073:
074: static class ButtonUI extends BasicButtonUI implements
075: ButtonBarButtonUI {
076: private static Color selectedBackground = Color.white;
077: private static Color selectedBorder = Color.black;
078:
079: private static Color selectedForeground = Color.black;
080: private static Color unselectedForeground = Color.white;
081:
082: public void installUI(JComponent c) {
083: super .installUI(c);
084:
085: AbstractButton button = (AbstractButton) c;
086: button.setOpaque(false);
087: button.setRolloverEnabled(true);
088: button.setBorder(BorderFactory
089: .createEmptyBorder(3, 3, 3, 3));
090: }
091:
092: public void paint(Graphics g, JComponent c) {
093: AbstractButton button = (AbstractButton) c;
094:
095: if (button.getModel().isSelected()) {
096: Color oldColor = g.getColor();
097: g.setColor(selectedBackground);
098: g.fillRoundRect(0, 0, c.getWidth() - 1,
099: c.getHeight() - 1, 5, 5);
100:
101: g.setColor(selectedBorder);
102: g.drawRoundRect(0, 0, c.getWidth() - 1,
103: c.getHeight() - 1, 5, 5);
104:
105: g.setColor(oldColor);
106: }
107:
108: // this is a tweak to get the View with the color we expect it to be. We
109: // change directly the color of the button
110: if (c.getClientProperty(BasicHTML.propertyKey) != null) {
111: ButtonModel model = button.getModel();
112: if (model.isEnabled()) {
113: if (model.isSelected()) {
114: button.setForeground(selectedForeground);
115: } else {
116: button.setForeground(unselectedForeground);
117: }
118: } else {
119: button.setForeground(unselectedForeground.darker());
120: }
121: }
122:
123: super .paint(g, c);
124: }
125:
126: protected void paintText(Graphics g, AbstractButton b,
127: Rectangle textRect, String text) {
128: ButtonModel model = b.getModel();
129: FontMetrics fm = g.getFontMetrics();
130: int mnemonicIndex = b.getDisplayedMnemonicIndex();
131:
132: Color oldColor = g.getColor();
133:
134: /* Draw the Text */
135: if (model.isEnabled()) {
136: /** * paint the text normally */
137: if (model.isSelected()) {
138: g.setColor(selectedForeground);
139: } else {
140: g.setColor(unselectedForeground);
141: }
142: } else {
143: g.setColor(unselectedForeground.darker());
144: }
145:
146: //
147: BasicGraphicsUtils.drawStringUnderlineCharAt(g, text,
148: mnemonicIndex, textRect.x + getTextShiftOffset(),
149: textRect.y + fm.getAscent() + getTextShiftOffset());
150: //
151: // } else {
152: // g.setColor(b.getParent().getBackground().brighter());
153: // BasicGraphicsUtils.drawStringUnderlineCharAt(
154: // g,
155: // text,
156: // mnemonicIndex,
157: // textRect.x,
158: // textRect.y + fm.getAscent());
159: // g.setColor(b.getParent().getBackground().darker());
160: // BasicGraphicsUtils.drawStringUnderlineCharAt(
161: // g,
162: // text,
163: // mnemonicIndex,
164: // textRect.x - 1,
165: // textRect.y + fm.getAscent() - 1);
166: // }
167: g.setColor(oldColor);
168: }
169: }
170:
171: }
|