001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing.border;
021:
022: import java.awt.Color;
023: import java.awt.Component;
024: import java.awt.Graphics;
025: import java.awt.Insets;
026:
027: import org.apache.harmony.x.swing.internal.nls.Messages;
028:
029: public class BevelBorder extends AbstractBorder {
030:
031: public static final int RAISED = 0;
032: public static final int LOWERED = 1;
033:
034: private static final int INSETS_SIZE = 2;
035:
036: protected int bevelType;
037:
038: protected Color highlightOuter;
039: protected Color highlightInner;
040: protected Color shadowInner;
041: protected Color shadowOuter;
042:
043: public BevelBorder(final int bevelType) {
044: this .bevelType = bevelType;
045: }
046:
047: public BevelBorder(final int bevelType,
048: final Color highlightOuterColor,
049: final Color highlightInnerColor,
050: final Color shadowOuterColor, final Color shadowInnerColor) {
051: this (bevelType);
052:
053: if (highlightOuterColor == null || highlightInnerColor == null
054: || shadowOuterColor == null || shadowInnerColor == null) {
055: throw new NullPointerException(Messages
056: .getString("swing.68")); //$NON-NLS-1$
057: }
058: highlightOuter = highlightOuterColor;
059: highlightInner = highlightInnerColor;
060: shadowOuter = shadowOuterColor;
061: shadowInner = shadowInnerColor;
062: }
063:
064: public BevelBorder(final int bevelType, final Color highlight,
065: final Color shadow) {
066: this (bevelType, highlight, highlight, shadow, shadow);
067: }
068:
069: int getInsetSize() {
070: return INSETS_SIZE;
071: }
072:
073: public Insets getBorderInsets(final Component c, final Insets insets) {
074: int insetSize = getInsetSize();
075:
076: insets.left = insetSize;
077: insets.top = insetSize;
078: insets.bottom = insetSize;
079: insets.right = insetSize;
080:
081: return insets;
082: }
083:
084: public Insets getBorderInsets(final Component c) {
085: int insetSize = getInsetSize();
086: return new Insets(insetSize, insetSize, insetSize, insetSize);
087: }
088:
089: protected void paintRaisedBevel(final Component c,
090: final Graphics g, final int x, final int y,
091: final int width, final int height) {
092: Color oldColor = g.getColor();
093: Color color = getShadowOuterColor(c);
094: g.setColor(color);
095: g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);
096: g.drawLine(x + width - 1, y, x + width - 1, y + height - 1);
097:
098: color = getShadowInnerColor(c);
099: g.setColor(color);
100: g
101: .drawLine(x + 1, y + height - 2, x + width - 2, y
102: + height - 2);
103: g.drawLine(x + width - 2, y + 1, x + width - 2, y + height - 2);
104:
105: color = getHighlightOuterColor(c);
106: g.setColor(color);
107: g.drawLine(x + 1, y, x + width - 2, y);
108: g.drawLine(x, y + 1, x, y + height - 2);
109:
110: color = getHighlightInnerColor(c);
111: g.setColor(color);
112: g.drawLine(x + 2, y + 1, x + width - 3, y + 1);
113: g.drawLine(x + 1, y + 2, x + 1, y + height - 3);
114: g.setColor(oldColor);
115: }
116:
117: protected void paintLoweredBevel(final Component c,
118: final Graphics g, final int x, final int y,
119: final int width, final int height) {
120: Color oldColor = g.getColor();
121: Color color = getShadowInnerColor(c);
122: g.setColor(color);
123: g.drawLine(x, y, x + width - 1, y);
124: g.drawLine(x, y, x, y + height - 1);
125:
126: color = getShadowOuterColor(c);
127: g.setColor(color);
128: g.drawLine(x + 1, y + 1, x + width - 2, y + 1);
129: g.drawLine(x + 1, y + 1, x + 1, y + height - 2);
130:
131: color = getHighlightOuterColor(c);
132: g.setColor(color);
133: g
134: .drawLine(x + 1, y + height - 1, x + width - 1, y
135: + height - 1);
136: g.drawLine(x + width - 1, y + 1, x + width - 1, y + height - 2);
137:
138: color = getHighlightInnerColor(c);
139: g.setColor(color);
140: g
141: .drawLine(x + 2, y + height - 2, x + width - 2, y
142: + height - 2);
143: g.drawLine(x + width - 2, y + 2, x + width - 2, y + height - 3);
144: g.setColor(oldColor);
145: }
146:
147: public void paintBorder(final Component c, final Graphics g,
148: final int x, final int y, final int width, final int height) {
149: if (bevelType == BevelBorder.LOWERED) {
150: paintLoweredBevel(c, g, x, y, width, height);
151: } else if (bevelType == BevelBorder.RAISED) {
152: paintRaisedBevel(c, g, x, y, width, height);
153: }
154: }
155:
156: public Color getShadowOuterColor(final Component c) {
157: return (shadowOuter != null) ? shadowOuter : c.getBackground()
158: .darker().darker();
159: }
160:
161: public Color getShadowInnerColor(final Component c) {
162: return (shadowInner != null) ? shadowInner : c.getBackground()
163: .darker();
164: }
165:
166: public Color getHighlightOuterColor(final Component c) {
167: return (highlightOuter != null) ? highlightOuter : c
168: .getBackground().brighter().brighter();
169: }
170:
171: public Color getHighlightInnerColor(final Component c) {
172: return (highlightInner != null) ? highlightInner : c
173: .getBackground().brighter();
174: }
175:
176: public Color getShadowOuterColor() {
177: return shadowOuter;
178: }
179:
180: public Color getShadowInnerColor() {
181: return shadowInner;
182: }
183:
184: public Color getHighlightOuterColor() {
185: return highlightOuter;
186: }
187:
188: public Color getHighlightInnerColor() {
189: return highlightInner;
190: }
191:
192: public boolean isBorderOpaque() {
193: return true;
194: }
195:
196: public int getBevelType() {
197: return bevelType;
198: }
199:
200: }
|