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