001: /*
002: * Copyright (c) 2001-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.looks.common;
032:
033: import java.awt.Color;
034: import java.awt.Dimension;
035: import java.awt.Font;
036: import java.awt.Graphics;
037:
038: import javax.swing.Icon;
039: import javax.swing.JComponent;
040: import javax.swing.JMenuItem;
041: import javax.swing.UIManager;
042: import javax.swing.plaf.ComponentUI;
043: import javax.swing.plaf.basic.BasicMenuItemUI;
044:
045: /**
046: * An implementation of <code>MenuItemUI</code> used by the
047: * JGoodies Windows and Plastic looks.
048: * Unlike it's superclass it aligns menu items, uses a slightly
049: * smaller gap between text and icon, which you can override
050: * in the UI defaults.
051: *
052: * @author Karsten Lentzsch
053: * @version $Revision: 1.4 $
054: */
055:
056: public class ExtBasicMenuItemUI extends BasicMenuItemUI {
057:
058: private static final int MINIMUM_WIDTH = 80;
059:
060: private MenuItemRenderer renderer;
061:
062: public static ComponentUI createUI(JComponent b) {
063: return new ExtBasicMenuItemUI();
064: }
065:
066: protected void installDefaults() {
067: super .installDefaults();
068: renderer = createRenderer(menuItem, iconBorderEnabled(),
069: acceleratorFont, selectionForeground,
070: disabledForeground, acceleratorForeground,
071: acceleratorSelectionForeground);
072: Integer gap = (Integer) UIManager.get(getPropertyPrefix()
073: + ".textIconGap");
074: defaultTextIconGap = gap != null ? gap.intValue() : 2;
075: }
076:
077: // RadioButtonMenuItems and CheckBoxMenuItems will override
078: protected boolean iconBorderEnabled() {
079: return false;
080: }
081:
082: protected void uninstallDefaults() {
083: super .uninstallDefaults();
084: renderer = null;
085: }
086:
087: protected Dimension getPreferredMenuItemSize(JComponent c,
088: Icon aCheckIcon, Icon anArrowIcon, int textIconGap) {
089: //if (storedCheckIcon == null) replaceIcons();
090: Dimension size = renderer.getPreferredMenuItemSize(c,
091: aCheckIcon, anArrowIcon, textIconGap);
092: int width = Math.max(MINIMUM_WIDTH, size.width);
093: int height = size.height;
094: return new Dimension(width, height);
095: }
096:
097: protected void paintMenuItem(Graphics g, JComponent c,
098: Icon aCheckIcon, Icon anArrowIcon, Color background,
099: Color foreground, int textIconGap) {
100: renderer.paintMenuItem(g, c, aCheckIcon, anArrowIcon,
101: background, foreground, textIconGap);
102: }
103:
104: protected MenuItemRenderer createRenderer(JMenuItem menuItem,
105: boolean iconBorderEnabled, Font acceleratorFont,
106: Color selectionForeground, Color disabledForeground,
107: Color acceleratorForeground,
108: Color acceleratorSelectionForeground) {
109: return new MenuItemRenderer(menuItem, iconBorderEnabled(),
110: acceleratorFont, selectionForeground,
111: disabledForeground, acceleratorForeground,
112: acceleratorSelectionForeground);
113: }
114:
115: }
|