01: /*
02: * Copyright 2005 Patrick Gotthardt
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package com.pagosoft.plaf;
17:
18: import javax.swing.*;
19: import javax.swing.plaf.*;
20: import javax.swing.plaf.basic.*;
21: import java.awt.*;
22: import java.beans.PropertyChangeEvent;
23: import java.beans.PropertyChangeListener;
24:
25: public class PgsMenuItemUI extends BasicMenuItemUI {
26: public static ComponentUI createUI(JComponent c) {
27: return new PgsMenuItemUI();
28: }
29:
30: private PropertyChangeListener menuItemIconChangeListener;
31:
32: protected void installDefaults() {
33: super .installDefaults();
34: if (menuItem.getIcon() == null) {
35: menuItem.setIcon(PgsIconFactory.getEmptyIcon());
36: }
37: }
38:
39: protected void installListeners() {
40: super .installListeners();
41: if (menuItemIconChangeListener == null) {
42: menuItemIconChangeListener = new IconChangeListener(
43: menuItem);
44: }
45: menuItem.addPropertyChangeListener(
46: JMenuItem.ICON_CHANGED_PROPERTY,
47: menuItemIconChangeListener);
48: }
49:
50: protected void uninstallListeners() {
51: super .uninstallListeners();
52: menuItem.removePropertyChangeListener(
53: JMenuItem.ICON_CHANGED_PROPERTY,
54: menuItemIconChangeListener);
55: }
56:
57: protected void paintBackground(Graphics g, JMenuItem menuItem,
58: Color bgColor) {
59: PgsUtils.paintMenuItemBackground(g, menuItem, bgColor,
60: getPropertyPrefix());
61: }
62:
63: protected void paintText(Graphics g, JMenuItem c,
64: Rectangle textRect, String text) {
65: PgsUtils.installAntialiasing(g);
66: super .paintText(g, c, textRect, text);
67: PgsUtils.uninstallAntialiasing(g);
68: }
69:
70: public static class IconChangeListener implements
71: PropertyChangeListener {
72: private AbstractButton button;
73:
74: public IconChangeListener(AbstractButton item) {
75: button = item;
76: }
77:
78: public void propertyChange(PropertyChangeEvent evt) {
79: if (button.getClientProperty("allow.no.icon") == null
80: && button.getIcon() == null) {
81: button.setIcon(PgsIconFactory.getEmptyIcon());
82: }
83: }
84: }
85: }
|