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.scroll;
031:
032: import java.awt.*;
033:
034: import javax.swing.*;
035: import javax.swing.border.Border;
036: import javax.swing.plaf.UIResource;
037:
038: import org.jvnet.substance.border.SimplisticSoftBorderPainter;
039: import org.jvnet.substance.border.StandardBorderPainter;
040: import org.jvnet.substance.color.ColorScheme;
041: import org.jvnet.substance.utils.*;
042:
043: /**
044: * Default border on {@link JScrollPane}s. Provides continuous appearance of
045: * the border + scroll bars.
046: *
047: * @author Kirill Grouchnikov
048: */
049: public class SubstanceScrollPaneBorder implements Border, UIResource {
050: /*
051: * (non-Javadoc)
052: *
053: * @see javax.swing.border.Border#getBorderInsets(java.awt.Component)
054: */
055: public Insets getBorderInsets(Component c) {
056: float borderStrokeWidth = SubstanceSizeUtils
057: .getBorderStrokeWidth(SubstanceSizeUtils
058: .getComponentFontSize(c));
059: int ins = (int) borderStrokeWidth;
060: return new Insets(ins, ins, ins, ins);
061: }
062:
063: /*
064: * (non-Javadoc)
065: *
066: * @see javax.swing.border.Border#isBorderOpaque()
067: */
068: public boolean isBorderOpaque() {
069: return false;
070: }
071:
072: /*
073: * (non-Javadoc)
074: *
075: * @see javax.swing.border.Border#paintBorder(java.awt.Component,
076: * java.awt.Graphics, int, int, int, int)
077: */
078: public void paintBorder(Component c, Graphics g, int x, int y,
079: int width, int height) {
080: if (!(c instanceof JScrollPane)) {
081: // Applications (such as NetBeans RCP) may incorrectly assume
082: // that scroll pane border specified by the ""ScrollPane.border"
083: // UIManager key by a look-and-feel can be installed on any
084: // component. In case this component is not JScrollPane, do
085: // nothing.
086: return;
087: }
088:
089: JScrollPane scrollPane = (JScrollPane) c;
090: JScrollBar vertical = scrollPane.getVerticalScrollBar();
091: JScrollBar horizontal = scrollPane.getHorizontalScrollBar();
092: JViewport columnHeader = scrollPane.getColumnHeader();
093:
094: StandardBorderPainter painter = new SimplisticSoftBorderPainter();
095: ColorScheme scheme = SubstanceThemeUtilities.getTheme(c,
096: ComponentState.DEFAULT).getColorScheme();
097:
098: float borderStrokeWidth = SubstanceSizeUtils
099: .getBorderStrokeWidth(SubstanceSizeUtils
100: .getComponentFontSize(c));
101: int borderDelta = (int) Math.floor(SubstanceSizeUtils
102: .getBorderStrokeWidth(SubstanceSizeUtils
103: .getComponentFontSize(c)) / 2.0);
104: Graphics2D g2d = (Graphics2D) g.create();
105: g2d.setStroke(new BasicStroke(borderStrokeWidth,
106: BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));
107: g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
108: RenderingHints.VALUE_ANTIALIAS_ON);
109: x += borderDelta;
110: y += borderDelta;
111: width -= 2 * borderDelta;
112: height -= 2 * borderDelta;
113:
114: if (scrollPane.getComponentOrientation().isLeftToRight()) {
115: // top portion
116: g2d.setColor(painter.getTopBorderColor(scheme, scheme, 0,
117: false));
118: if (vertical.isVisible() && (columnHeader == null)) {
119: g2d.drawLine(x, y, x + width - vertical.getWidth(), y);
120: } else {
121: g2d.drawLine(x, y, x + width, y);
122: }
123:
124: // left portion
125: g2d.setColor(painter.getTopBorderColor(scheme, scheme, 0,
126: false));
127: if (horizontal.isVisible()) {
128: g2d.drawLine(x, y, x, y + height
129: - horizontal.getHeight());
130: } else {
131: g2d.drawLine(x, y, x, y + height);
132: }
133:
134: // bottom portion
135: g2d.setColor(painter.getBottomBorderColor(scheme, scheme,
136: 0, false));
137: if (horizontal.isVisible()) {
138: // g.drawLine(x + horizontal.getWidth(), y + height - 1,
139: // x + width, y + height - 1);
140: } else {
141: if (vertical.isVisible()) {
142: g2d.drawLine(x, y + height - 1, x + width
143: - vertical.getWidth(), y + height - 1);
144: } else {
145: g2d.drawLine(x, y + height - 1, x + width, y
146: + height - 1);
147: }
148: }
149:
150: // right portion
151: g2d.setColor(painter.getBottomBorderColor(scheme, scheme,
152: 0, false));
153: if (vertical.isVisible()) {
154: // g.drawLine(x + width - 1, y + vertical.getHeight(), x + width
155: // - 1, y + height);
156:
157: if (columnHeader != null) {
158: g2d.drawLine(x + width - 1, y, x + width - 1, y
159: + columnHeader.getHeight());
160: }
161: } else {
162: if (horizontal.isVisible())
163: g2d.drawLine(x + width - 1, y, x + width - 1, y
164: + height - horizontal.getHeight());
165: else
166: g2d.drawLine(x + width - 1, y, x + width - 1, y
167: + height);
168: }
169: } else {
170: // top portion
171: g2d.setColor(painter.getTopBorderColor(scheme, scheme, 0,
172: false));
173: if (vertical.isVisible() && (columnHeader == null)) {
174: g2d.drawLine(x + vertical.getWidth(), y, x + width, y);
175: } else {
176: g2d.drawLine(x, y, x + width, y);
177: }
178:
179: // left portion
180: g2d.setColor(painter.getBottomBorderColor(scheme, scheme,
181: 0, false));
182: if (vertical.isVisible()) {
183: // g.drawLine(x, y, x, y + height - horizontal.getHeight());
184: if (columnHeader != null) {
185: g2d.drawLine(x, y, x, y + columnHeader.getHeight());
186: }
187: } else {
188: if (horizontal.isVisible()) {
189: g2d.drawLine(x, y, x, y + height
190: - horizontal.getHeight());
191: } else {
192: g2d.drawLine(x, y, x, y + height - 1);
193: }
194: }
195:
196: // bottom portion
197: g2d.setColor(painter.getBottomBorderColor(scheme, scheme,
198: 0, false));
199: if (horizontal.isVisible()) {
200: // g.drawLine(x + horizontal.getWidth(), y + height - 1,
201: // x + width, y + height - 1);
202: } else {
203: if (vertical.isVisible()) {
204: g2d.drawLine(x + vertical.getWidth(), y + height
205: - 1, x + width, y + height - 1);
206: } else {
207: g2d.drawLine(x, y + height - 1, x + width, y
208: + height - 1);
209: }
210: }
211:
212: // right portion
213: g2d.setColor(painter.getTopBorderColor(scheme, scheme, 0,
214: false));
215: if (horizontal.isVisible()) {
216: g2d.drawLine(x + width - 1, y, x + width - 1, y
217: + height - horizontal.getHeight());
218: } else {
219: g2d.drawLine(x + width - 1, y, x + width - 1, y
220: + height);
221: }
222: }
223: g2d.dispose();
224: }
225: }
|