001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.options.ui;
043:
044: import java.awt.Color;
045: import java.awt.Component;
046: import java.awt.Graphics;
047: import java.awt.Graphics2D;
048: import java.awt.Insets;
049: import java.awt.Shape;
050: import java.awt.geom.Line2D;
051: import javax.swing.JToggleButton;
052: import javax.swing.JToolBar;
053: import javax.swing.border.Border;
054:
055: /**
056: * VariableBorder is a simple Border that only draw a line if a color is given.
057: *
058: * @author Christopher Atlan
059: */
060: public class VariableBorder implements Border {
061: private Color topColor;
062: private Color leftColor;
063: private Color bottomColor;
064: private Color rightColor;
065:
066: /** Creates a new instance of VariableBorder */
067: public VariableBorder(final Color topColor, final Color leftColor,
068: final Color bottomColor, final Color rightColor) {
069: this .topColor = topColor;
070: this .leftColor = leftColor;
071: this .bottomColor = bottomColor;
072: this .rightColor = rightColor;
073: }
074:
075: public void paintBorder(Component c, Graphics g, int x, int y,
076: int width, int height) {
077: Graphics2D g2d = (Graphics2D) g;
078: Shape s;
079:
080: if (topColor != null) {
081: s = new Line2D.Double(x, y, x + width, y);
082: g2d.setColor(topColor);
083: g2d.fill(s);
084: }
085:
086: if (leftColor != null) {
087: s = new Line2D.Double(x, y, x, y + height);
088: g2d.setColor(leftColor);
089: g2d.fill(s);
090: }
091:
092: if (bottomColor != null) {
093: s = new Line2D.Double(x, y + height - 1, x + width, y
094: + height - 1);
095: g2d.setColor(bottomColor);
096: g2d.fill(s);
097: }
098:
099: if (rightColor != null) {
100: s = new Line2D.Double(x + width - 1, y, x + width - 1, y
101: + height);
102: g2d.setColor(rightColor);
103: g2d.fill(s);
104: }
105:
106: }
107:
108: public Insets getBorderInsets(Component c) {
109: Insets i = new Insets(0, 0, 0, 0);
110:
111: if (topColor != null)
112: i.top = 1;
113:
114: if (leftColor != null)
115: i.left = 1;
116:
117: if (bottomColor != null)
118: i.bottom = 1;
119:
120: if (rightColor != null)
121: i.right = 1;
122:
123: if (c instanceof JToolBar) {
124: Insets toolBarInsets = ((JToolBar) c).getMargin();
125: i.top += toolBarInsets.top;
126: i.left += toolBarInsets.left;
127: i.right += toolBarInsets.right;
128: i.bottom += toolBarInsets.bottom;
129: }
130:
131: if (c instanceof JToggleButton) {
132: Insets buttonInsets = ((JToggleButton) c).getMargin();
133: i.top += buttonInsets.top;
134: i.left += buttonInsets.left;
135: i.right += buttonInsets.right;
136: i.bottom += buttonInsets.bottom;
137: }
138:
139: return i;
140: }
141:
142: public boolean isBorderOpaque() {
143: return false;
144: }
145: }
|