001: /*
002: * Copyright 2005 Patrick Gotthardt
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 com.pagosoft.plaf;
017:
018: import javax.swing.*;
019: import javax.swing.plaf.*;
020: import java.awt.*;
021:
022: public class ToolBarButtonUI extends PgsButtonUI {
023: private static ToolBarButtonUI INSTANCE = new ToolBarButtonUI();
024:
025: public static ComponentUI createUI(JComponent c) {
026: return INSTANCE;
027: }
028:
029: public void installDefaults(AbstractButton b) {
030: super .installDefaults(b);
031: // Just a hack, but makes life much easier
032: b.setBorderPainted(false);
033: b.setOpaque(false);
034: b.setMargin(UIManager.getInsets("ToolBarButton.margin"));
035: b.putClientProperty("rolloverBackground", UIManager
036: .getColor("ToolBarButton.rolloverBackground"));
037: b.putClientProperty("rolloverBorderColor", UIManager
038: .get("ToolBarButton.rolloverBorderColor"));
039: b.putClientProperty("pgs.isFlat", UIManager
040: .get("ToolBarButton.isFlat"));
041: b.putClientProperty("gradientStart", UIManager
042: .get("ToolBarButton.gradientStart"));
043: b.putClientProperty("gradientEnd", UIManager
044: .get("ToolBarButton.gradientEnd"));
045: b.putClientProperty("rollover.gradientStart", UIManager
046: .get("ToolBarButton.rolloverGradientStart"));
047: b.putClientProperty("rollover.gradientEnd", UIManager
048: .get("ToolBarButton.rolloverGradientEnd"));
049: b.putClientProperty("selected.gradientStart", UIManager
050: .get("ToolBarButton.selectedGradientStart"));
051: b.putClientProperty("selected.gradientEnd", UIManager
052: .get("ToolBarButton.selectedGradientEnd"));
053:
054: Icon ico = b.getIcon();
055: if (ico != null && (ico instanceof ImageIcon)) {
056: Image img = ((ImageIcon) ico).getImage();
057: if (b.getDisabledIcon() == null) {
058: b.setDisabledIcon(PgsUtils.getDisabledButtonIcon(img));
059: }
060: if (b.getDisabledSelectedIcon() == null) {
061: b.setDisabledSelectedIcon(PgsUtils
062: .getDisabledButtonIcon(img));
063: }
064: if (b.getRolloverIcon() == null) {
065: b.setRolloverIcon(PgsUtils.getToolBarIcon(img));
066: }
067: if (b.getRolloverSelectedIcon() == null) {
068: b.setRolloverSelectedIcon(PgsUtils.getToolBarIcon(img));
069: }
070: }
071: }
072:
073: public void update(Graphics g, JComponent c) {
074: AbstractButton b = (AbstractButton) c;
075:
076: // Rollover ?
077: if (c.isEnabled() && b.getModel().isRollover()) {
078: if (Boolean.TRUE.equals(c.getClientProperty("pgs.isFlat"))) {
079: g.setColor((Color) c
080: .getClientProperty("rolloverBackground"));
081: g.fillRect(0, 0, b.getWidth() - 1, b.getHeight() - 1);
082: g.setColor((Color) c
083: .getClientProperty("rolloverBorderColor"));
084: g.drawRect(0, 0, b.getWidth() - 1, b.getHeight() - 1);
085: } else {
086: if (c.isEnabled() && b.getModel().isRollover()) {
087: PgsUtils.drawGradient(g, c, "rollover");
088: } else {
089: PgsUtils.drawGradient(g, c);
090: }
091: g.setColor((Color) c
092: .getClientProperty("rolloverBorderColor"));
093: g.drawRect(0, 0, b.getWidth() - 1, b.getHeight() - 1);
094: }
095: }
096: super .paint(g, c);
097: }
098:
099: protected void paintButtonPressed(Graphics g, AbstractButton b) {
100: if (Boolean.TRUE.equals(b.getClientProperty("pgs.isFlat"))) {
101: g.setColor(getSelectColor());
102: g.fillRect(0, 0, b.getWidth(), b.getHeight());
103: } else {
104: PgsUtils.drawGradient(g, b, "selected");
105: }
106: g.setColor((Color) b.getClientProperty("rolloverBorderColor"));
107: g.drawRect(0, 0, b.getWidth() - 1, b.getHeight() - 1);
108: super.paintButtonPressed(g, b);
109: }
110: }
|