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 javax.swing.plaf.basic.*;
021: import java.awt.*;
022:
023: public class PgsMenuUI extends BasicMenuUI {
024: public static ComponentUI createUI(JComponent c) {
025: return new PgsMenuUI();
026: }
027:
028: protected void installListeners() {
029: super .installListeners();
030: menuItem.addMouseListener(createButtonListener(menuItem));
031: }
032:
033: protected void uninstallListeners() {
034: super .uninstallListeners();
035: }
036:
037: protected BasicButtonListener createButtonListener(AbstractButton b) {
038: return new RolloverButtonListener(b);
039: }
040:
041: protected void paintBackground(Graphics g, JMenuItem menuItem,
042: Color bgColor) {
043: ButtonModel model = menuItem.getModel();
044: Color oldColor = g.getColor();
045: Dimension size = menuItem.getSize();
046:
047: //if(menuItem.isOpaque()) {
048: if (menuItem.getParent() instanceof JMenuBar) {
049: // Not really a nice hack, but it works. ^^"
050: if (menuItem.isEnabled()
051: && (model.isArmed()
052: || (menuItem instanceof JMenu && model
053: .isSelected()) || model
054: .isRollover())) {
055: if (PgsUtils.isFlat("MenuBarMenu")) {
056: if (model.isSelected()) {
057: g
058: .setColor(UIManager
059: .getColor("MenuBarMenu.selectedBackground"));
060: } else {
061: g
062: .setColor(UIManager
063: .getColor("MenuBarMenu.rolloverBackground"));
064: }
065: g.fillRect(0, 0, size.width, size.height);
066: } else if (PlafOptions.isVistaStyle()) {
067: if (model.isRollover() && !model.isSelected()) {
068: PgsUtils.drawVistaBackground(g, menuItem,
069: "MenuBarMenu.rolloverBackground");
070: } else {
071: PgsUtils.drawVistaBackground(g, menuItem,
072: "MenuBarMenu.selectedBackground");
073: }
074: } else {
075: if (model.isRollover() && !model.isSelected()) {
076: PgsUtils
077: .drawGradient(
078: g,
079: menuItem.getWidth(),
080: menuItem.getHeight(),
081: (Color) UIManager
082: .getColor("MenuBarMenu.rolloverBackgroundGradientStart"),
083: (Color) UIManager
084: .getColor("MenuBarMenu.rolloverBackgroundGradientEnd"));
085: } else {
086: PgsUtils
087: .drawGradient(
088: g,
089: menuItem.getWidth(),
090: menuItem.getHeight(),
091: (Color) UIManager
092: .getColor("MenuBarMenu.selectedBackgroundGradientStart"),
093: (Color) UIManager
094: .getColor("MenuBarMenu.selectedBackgroundGradientEnd"));
095: }
096: }
097: if (model.isRollover() && !model.isSelected()) {
098: // draw a border if the menu is hovered but not selected
099: g
100: .setColor(UIManager
101: .getColor("MenuBarMenu.rolloverBorderColor"));
102: g.drawRect(0, 0, size.width - 1, size.height - 1);
103: } else {
104: g
105: .setColor(UIManager
106: .getColor("MenuBarMenu.selectedBorderColor"));
107: g.drawLine(0, 0, 0, size.height - 1);
108: g.drawLine(size.width - 1, 0, size.width - 1,
109: size.height - 1);
110: g.drawLine(0, 0, size.width - 1, 0);
111: }
112: } else {
113: if (PgsUtils.isFlat("MenuBar")) {
114: g
115: .setColor(UIManager
116: .getColor("MenuBar.background"));
117: g.fillRect(0, 0, size.width, size.height);
118: } else if (PlafOptions.isVistaStyle()) {
119: PgsUtils
120: .drawVistaBackground(g, menuItem, "MenuBar");
121: } else {
122: PgsUtils.drawGradient(g, menuItem.getWidth(),
123: menuItem.getHeight(), (Color) UIManager
124: .getColor("MenuBar.gradientStart"),
125: (Color) UIManager
126: .getColor("MenuBar.gradientEnd"));
127: }
128: }
129: } else {
130: if (menuItem.getIcon() == null) {
131: menuItem.setIcon(PgsIconFactory.getEmptyIcon());
132: }
133: PgsUtils.paintMenuItemBackground(g, menuItem, bgColor,
134: getPropertyPrefix());
135: }
136: g.setColor(oldColor);
137: //}
138: }
139:
140: protected void paintText(Graphics g, JMenuItem c,
141: Rectangle textRect, String text) {
142: PgsUtils.installAntialiasing(g);
143: if (c.getParent() instanceof JMenuBar) {
144: c.setForeground(UIManager
145: .getColor("MenuBarMenu.foreground"));
146: selectionForeground = UIManager
147: .getColor("MenuBarMenu.foreground");
148: }
149: super.paintText(g, c, textRect, text);
150: PgsUtils.uninstallAntialiasing(g);
151: }
152: }
|