001: /*
002: * Copyright (c) 2005-2008 Flamingo / Substance Kirill Grouchnikov. 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 Flamingo Kirill Grouchnikov 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: package org.jvnet.substance.flamingo.bcb.ui;
031:
032: import java.awt.Dimension;
033: import java.awt.Graphics;
034: import java.util.HashMap;
035: import java.util.Map;
036:
037: import javax.swing.*;
038: import javax.swing.plaf.ComponentUI;
039:
040: import org.jvnet.flamingo.bcb.ui.BasicScrollablePopupUI;
041: import org.jvnet.substance.SubstanceImageCreator;
042: import org.jvnet.substance.theme.SubstanceTheme;
043: import org.jvnet.substance.utils.*;
044:
045: /**
046: * UI delegate for scrollable popup.
047: *
048: * @author Kirill Grouchnikov
049: */
050: public class SubstanceScrollablePopupUI extends BasicScrollablePopupUI {
051: /**
052: * Up-pointing icons.
053: */
054: protected static Map<SubstanceTheme, Icon> upIcons = new HashMap<SubstanceTheme, Icon>();
055:
056: /**
057: * Down-pointing icons.
058: */
059: protected static Map<SubstanceTheme, Icon> downIcons = new HashMap<SubstanceTheme, Icon>();
060:
061: /*
062: * (non-Javadoc)
063: *
064: * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
065: */
066: public static ComponentUI createUI(JComponent c) {
067: return new SubstanceScrollablePopupUI();
068: }
069:
070: /**
071: * Returns up-pointing icon.
072: *
073: * @param theme
074: * Theme.
075: * @return Up-pointing icon.
076: */
077: protected static synchronized Icon getUpIcon(SubstanceTheme theme) {
078: if (!upIcons.containsKey(theme))
079: upIcons.put(theme, SubstanceImageCreator.getArrowIcon(
080: SubstanceSizeUtils.getControlFontSize(),
081: SwingConstants.NORTH, theme));
082: return upIcons.get(theme);
083: }
084:
085: /**
086: * Returns down-pointing icon.
087: *
088: * @param theme
089: * Theme.
090: * @return Down-pointing icon.
091: */
092: protected static synchronized Icon getDownIcon(SubstanceTheme theme) {
093: if (!downIcons.containsKey(theme))
094: downIcons.put(theme, SubstanceImageCreator.getArrowIcon(
095: SubstanceSizeUtils.getControlFontSize(),
096: SwingConstants.SOUTH, theme));
097: return downIcons.get(theme);
098: }
099:
100: /*
101: * (non-Javadoc)
102: *
103: * @see org.jvnet.flamingo.bcb.ui.BasicScrollablePopupUI#createScroller(boolean)
104: */
105: @Override
106: protected JMenuItem createScroller(final boolean up) {
107: // System.out.println("Creating scroller");
108: final JMenuItem scroller = new JMenuItem("") {
109: @Override
110: public void paint(Graphics g) {
111: super .paint(g);
112: SubstanceTheme theme = SubstanceThemeUtilities
113: .getTheme(null, ComponentState.DEFAULT);
114: Icon arrow = up ? getUpIcon(theme) : getDownIcon(theme);
115: if (!this .isEnabled())
116: arrow = SubstanceImageCreator.makeTransparent(null,
117: arrow, 0.5);
118: int x = (this .getWidth() - arrow.getIconWidth()) / 2;
119: int y = (this .getHeight() - arrow.getIconHeight()) / 2;
120: arrow.paintIcon(null, g, x, y);
121: // paintArrow(g, getWidth(), getHeight(),
122: // isEnabled() ? getForeground() : Color.lightGray, up);
123: }
124:
125: @Override
126: public void menuSelectionChanged(boolean isIncluded) {
127: // System.out.println("Menu selection on @" + this.hashCode()
128: // + (up ? " up" : " down"));
129: super .menuSelectionChanged(isIncluded);
130: if (isIncluded) {
131: SubstanceScrollablePopupUI.this .scrollAction(this );
132: } else {
133: SubstanceScrollablePopupUI.this .isPressed = false;
134: }
135: }
136: };
137:
138: // scroller.setForeground(this.popupMenu.getForeground());
139: scroller.setEnabled(false);
140: scroller.addMenuKeyListener(this .getMenuListener());
141: scroller.setOpaque(true);
142: scroller.setBorder(BorderFactory.createEmptyBorder());
143: scroller.setSize(new Dimension(0, 16));
144: scroller.setPreferredSize(new Dimension(0, 16));
145: return scroller;
146: }
147: }
|