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-2007 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.uml.ui.swing.plaf.basic;
043:
044: import java.awt.Component;
045: import java.awt.Graphics;
046: import java.awt.Insets;
047:
048: import javax.swing.AbstractButton;
049: import javax.swing.ButtonModel;
050: import javax.swing.border.AbstractBorder;
051: import javax.swing.plaf.metal.MetalLookAndFeel;
052:
053: import org.netbeans.modules.uml.ui.swing.pulldownbutton.JPullDownButton;
054:
055: /**
056: *
057: * @author Trey Spiva
058: */
059: public class BasicPullDownButtonBorder extends AbstractBorder {
060: protected Insets borderInsets = null;
061:
062: private boolean m_DrawBorder = true;
063:
064: public BasicPullDownButtonBorder(boolean drawBorder) {
065: setDrawBorderAround(drawBorder);
066: }
067:
068: public void setDrawBorderAround(boolean value) {
069: m_DrawBorder = value;
070:
071: if (m_DrawBorder == true) {
072: borderInsets = new Insets(4, 4, 4, 4);
073: } else {
074: borderInsets = new Insets(4, 4, 4, 4);
075: }
076: }
077:
078: public boolean getDrawBorderAround() {
079: return m_DrawBorder;
080: }
081:
082: public void paintBorder(Component c, Graphics g, int x, int y,
083: int w, int h) {
084: if (c instanceof AbstractButton) {
085: AbstractButton button = (AbstractButton) c;
086: ButtonModel model = button.getModel();
087:
088: if (model.isEnabled()) {
089: boolean isPressed = model.isPressed()
090: && model.isArmed();
091: if (isPressed) {
092: drawPressed3DBorder(g, x, y, w, h);
093: } else if (c instanceof JPullDownButton) {
094: JPullDownButton btn = (JPullDownButton) c;
095: if (btn.isArrowButtonPressed() == true) {
096: drawPressed3DBorder(g, x, y, w, h);
097: } else if (getDrawBorderAround() == true) {
098: drawButtonBorder(g, x, y, w, h, false);
099: }
100: } else if (getDrawBorderAround() == true) {
101: drawButtonBorder(g, x, y, w, h, false);
102: }
103: } else if (getDrawBorderAround() == true) { // disabled state
104: drawDisabledBorder(g, x, y, w - 1, h - 1);
105: }
106: }
107: }
108:
109: public Insets getBorderInsets(Component c) {
110: return borderInsets;
111: }
112:
113: public Insets getBorderInsets(Component c, Insets newInsets) {
114: newInsets.top = borderInsets.top;
115: newInsets.left = borderInsets.left;
116: newInsets.bottom = borderInsets.bottom;
117: newInsets.right = borderInsets.right;
118: return newInsets;
119: }
120:
121: /**
122: * This draws a variant "Flush 3D Border"
123: * It is used for things like pressed buttons.
124: */
125: static void drawPressed3DBorder(Graphics g, int x, int y, int w,
126: int h) {
127: g.translate(x, y);
128:
129: drawFlush3DBorder(g, 0, 0, w, h);
130:
131: g.setColor(MetalLookAndFeel.getControlShadow());
132: g.drawLine(1, 1, 1, h - 2);
133: g.drawLine(1, 1, w - 2, 1);
134: g.translate(-x, -y);
135: }
136:
137: static void drawButtonBorder(Graphics g, int x, int y, int w,
138: int h, boolean active) {
139: if (active) {
140: drawActiveButtonBorder(g, x, y, w, h);
141: } else {
142: drawFlush3DBorder(g, x, y, w, h);
143: }
144: }
145:
146: /**
147: * This draws the "Flush 3D Border" which is used throughout the Metal L&F
148: */
149: static void drawFlush3DBorder(Graphics g, int x, int y, int w, int h) {
150: g.translate(x, y);
151: g.setColor(MetalLookAndFeel.getControlDarkShadow());
152: g.drawRect(0, 0, w - 2, h - 2);
153: g.setColor(MetalLookAndFeel.getControlHighlight());
154: g.drawRect(1, 1, w - 2, h - 2);
155: g.setColor(MetalLookAndFeel.getControl());
156: g.drawLine(0, h - 1, 1, h - 2);
157: g.drawLine(w - 1, 0, w - 2, 1);
158: g.translate(-x, -y);
159: }
160:
161: static void drawActiveButtonBorder(Graphics g, int x, int y, int w,
162: int h) {
163: drawFlush3DBorder(g, x, y, w, h);
164: g.setColor(MetalLookAndFeel.getPrimaryControl());
165: g.drawLine(x + 1, y + 1, x + 1, h - 3);
166: g.drawLine(x + 1, y + 1, w - 3, x + 1);
167: g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
168: g.drawLine(x + 2, h - 2, w - 2, h - 2);
169: g.drawLine(w - 2, y + 2, w - 2, h - 2);
170: }
171:
172: static void drawDisabledBorder(Graphics g, int x, int y, int w,
173: int h) {
174: g.translate(x, y);
175: g.setColor(MetalLookAndFeel.getControlShadow());
176: g.drawRect(0, 0, w - 1, h - 1);
177: g.translate(-x, -y);
178: }
179: }
|