001: /*
002: * Copyright (c) 2005-2008 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 Substance 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;
031:
032: import java.awt.*;
033:
034: import javax.swing.JComponent;
035: import javax.swing.JSeparator;
036: import javax.swing.plaf.ComponentUI;
037: import javax.swing.plaf.basic.BasicPopupMenuSeparatorUI;
038:
039: import org.jvnet.lafwidget.layout.TransitionLayout;
040: import org.jvnet.substance.color.ColorScheme;
041: import org.jvnet.substance.theme.SubstanceTheme;
042: import org.jvnet.substance.utils.*;
043: import org.jvnet.substance.utils.SubstanceConstants.MenuGutterFillKind;
044: import org.jvnet.substance.utils.menu.MenuUtilities;
045:
046: /**
047: * UI for popup menu separators in <b>Substance</b> look and feel.
048: *
049: * @author Kirill Grouchnikov
050: */
051: public class SubstancePopupMenuSeparatorUI extends
052: BasicPopupMenuSeparatorUI {
053: /**
054: * Delegate for painting fill background.
055: */
056: private SubstanceFillBackgroundDelegate fillBackgroundDelegate;
057:
058: public static ComponentUI createUI(JComponent c) {
059: return new SubstancePopupMenuSeparatorUI();
060: }
061:
062: /**
063: * Creates a new UI delegate.
064: */
065: public SubstancePopupMenuSeparatorUI() {
066: super ();
067: this .fillBackgroundDelegate = new SubstanceFillBackgroundDelegate();
068: }
069:
070: @Override
071: public void paint(Graphics g, JComponent c) {
072: Graphics2D graphics = (Graphics2D) g.create();
073:
074: SubstanceTheme theme = SubstanceThemeUtilities.getTheme(c)
075: .getDefaultTheme();
076:
077: boolean isDark = SubstanceCoreUtilities.isThemeDark(theme
078: .getActiveTheme());
079: ColorScheme colorScheme = theme.getColorScheme();
080: try {
081: JSeparator sep = (JSeparator) c;
082:
083: this .fillBackgroundDelegate.updateIfOpaque(graphics, c);
084: int xOffset = MenuUtilities.getTextOffset(sep, sep
085: .getParent());
086:
087: Dimension s = c.getSize();
088: int startX = 0;
089: int width = s.width;
090: if (c.getComponentOrientation().isLeftToRight()) {
091: MenuGutterFillKind fillKind = SubstanceCoreUtilities
092: .getMenuGutterFillKind();
093: if (fillKind != MenuGutterFillKind.NONE) {
094: ColorScheme scheme = SubstanceThemeUtilities
095: .getTheme(sep, ComponentState.DEFAULT)
096: .getColorScheme();
097: Color leftColor = ((fillKind == MenuGutterFillKind.SOFT_FILL) || (fillKind == MenuGutterFillKind.HARD)) ? scheme
098: .getUltraLightColor()
099: : scheme.getLightColor();
100: Color rightColor = ((fillKind == MenuGutterFillKind.SOFT_FILL) || (fillKind == MenuGutterFillKind.SOFT)) ? scheme
101: .getUltraLightColor()
102: : scheme.getLightColor();
103: GradientPaint gp = new GradientPaint(0, 0,
104: leftColor, xOffset, 0, rightColor);
105:
106: graphics.setComposite(TransitionLayout
107: .getAlphaComposite(sep, 0.7f, g));
108: graphics.setPaint(gp);
109: graphics.fillRect(0, 0, xOffset - 2, c.getHeight());
110: }
111:
112: // graphics.setComposite(TransitionLayout.getAlphaComposite(sep));
113: // graphics.setColor(c.getForeground());
114: // graphics.drawLine(xOffset, 1, s.width, 1);
115: // graphics.setColor(c.getBackground());
116: // graphics.drawLine(xOffset, 2, s.width, 2);
117: // graphics.drawLine(xOffset, 0, xOffset, 0);
118: // graphics.drawLine(xOffset, 3, xOffset, 3);
119: startX = xOffset - 2;
120: width = s.width - startX;
121: } else {
122: // fix for defect 125 - support of RTL menus
123: MenuGutterFillKind fillKind = SubstanceCoreUtilities
124: .getMenuGutterFillKind();
125: if (fillKind != MenuGutterFillKind.NONE) {
126: ColorScheme scheme = SubstanceThemeUtilities
127: .getTheme(sep, ComponentState.DEFAULT)
128: .getColorScheme();
129: Color leftColor = ((fillKind == MenuGutterFillKind.HARD_FILL) || (fillKind == MenuGutterFillKind.HARD)) ? scheme
130: .getLightColor()
131: : scheme.getUltraLightColor();
132: Color rightColor = ((fillKind == MenuGutterFillKind.HARD_FILL) || (fillKind == MenuGutterFillKind.SOFT)) ? scheme
133: .getLightColor()
134: : scheme.getUltraLightColor();
135: GradientPaint gp = new GradientPaint(xOffset, 0,
136: leftColor, s.width, 0, rightColor);
137:
138: graphics.setComposite(TransitionLayout
139: .getAlphaComposite(sep, 0.7f, g));
140: graphics.setPaint(gp);
141: graphics.fillRect(xOffset - 2, 0, s.width, c
142: .getHeight());
143: }
144: // graphics.setComposite(TransitionLayout.getAlphaComposite(sep));
145: // graphics.setColor(c.getForeground());
146: // graphics.drawLine(0, 1, s.width - xOffset - 4, 1);
147: // graphics.setColor(c.getBackground());
148: // graphics.drawLine(0, 2, s.width - xOffset - 4, 2);
149: // graphics.drawLine(0, 0, 0, 0);
150: // graphics.drawLine(0, 3, 0, 3);
151: startX = 0;
152: width = xOffset - 4;
153: }
154: graphics.translate(startX, 0);
155: graphics.setComposite(TransitionLayout
156: .getAlphaComposite(sep));
157: SubstanceCoreUtilities.paintSeparator(sep, graphics,
158: colorScheme, isDark, width, s.height, sep
159: .getOrientation(), true, 2);
160: } finally {
161: graphics.dispose();
162: }
163: }
164:
165: /*
166: * (non-Javadoc)
167: *
168: * @see javax.swing.plaf.basic.BasicPopupMenuSeparatorUI#getPreferredSize(javax.swing.JComponent)
169: */
170: @Override
171: public Dimension getPreferredSize(JComponent c) {
172: float borderStrokeWidth = SubstanceSizeUtils
173: .getBorderStrokeWidth(SubstanceSizeUtils
174: .getComponentFontSize(c));
175: int prefSize = (int) (Math.ceil(2.0 * borderStrokeWidth));
176: return new Dimension(0, prefSize);
177: }
178: }
|