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 Vadim L. Bogdanov
019: * @version $Revision$
020: */package javax.swing.plaf.basic;
021:
022: import java.awt.Color;
023: import java.awt.Dimension;
024: import java.awt.FontMetrics;
025: import java.awt.Graphics;
026: import java.awt.Insets;
027: import java.awt.Rectangle;
028:
029: import javax.swing.AbstractButton;
030:
031: import org.apache.harmony.x.swing.ButtonCommons;
032: import org.apache.harmony.x.swing.Utilities;
033:
034: /*
035: * Appearence is taken from 1.5 release appearence.
036: * Needs to be kept the same to 1.5 since client applications may use
037: * this functionality in their UIs.
038: */
039: public class BasicGraphicsUtils {
040: public static void drawEtchedRect(final Graphics g, final int x,
041: final int y, final int w, final int h, final Color shadow,
042: final Color darkShadow, final Color highlight,
043: final Color lightHighlight) {
044: Color color = g.getColor();
045:
046: drawTwoLines(g, shadow, x, y + h - 2, x, y, x + w - 2, y);
047: drawTwoLines(g, darkShadow, x + 1, y + h - 3, x + 1, y + 1, x
048: + w - 3, y + 1);
049: drawTwoLines(g, lightHighlight, x, y + h - 1, x + w - 1, y + h
050: - 1, x + w - 1, y);
051: drawTwoLines(g, highlight, x + 1, y + h - 2, x + w - 2, y + h
052: - 2, x + w - 2, y + 1);
053:
054: g.setColor(color);
055: }
056:
057: public static Insets getEtchedInsets() {
058: return new Insets(2, 2, 2, 2);
059: }
060:
061: public static void drawGroove(final Graphics g, final int x,
062: final int y, final int w, final int h, final Color shadow,
063: final Color highlight) {
064: drawLoweredBezel(g, x, y, w, h, highlight, shadow, shadow,
065: highlight);
066: }
067:
068: public static Insets getGrooveInsets() {
069: return new Insets(2, 2, 2, 2);
070: }
071:
072: public static void drawBezel(final Graphics g, final int x,
073: final int y, final int w, final int h,
074: final boolean isPressed, final boolean isDefault,
075: final Color shadow, final Color darkShadow,
076: final Color highlight, final Color lightHighlight) {
077: if (!isDefault) {
078: if (isPressed) {
079: drawLoweredBezel(g, x, y, w, h, shadow, darkShadow,
080: highlight, lightHighlight);
081: } else {
082: drawLoweredBezel(g, x, y, w, h, highlight,
083: lightHighlight, shadow, darkShadow);
084: }
085: return;
086: }
087:
088: Color color = g.getColor();
089: if (isPressed) {
090: g.setColor(shadow);
091: g.drawRect(x + 1, y + 1, w - 3, h - 3);
092: } else {
093: drawLoweredBezel(g, x + 1, y + 1, w - 2, h - 2, highlight,
094: lightHighlight, shadow, darkShadow);
095: }
096: g.setColor(darkShadow);
097: g.drawRect(x, y, w - 1, h - 1);
098: g.setColor(color);
099: }
100:
101: public static void drawLoweredBezel(final Graphics g, final int x,
102: final int y, final int w, final int h, final Color shadow,
103: final Color darkShadow, final Color highlight,
104: final Color lightHighlight) {
105: drawEtchedRect(g, x, y, w, h, darkShadow, shadow, highlight,
106: lightHighlight);
107: }
108:
109: public static void drawString(final Graphics g, final String text,
110: final int underlinedChar, final int x, final int y) {
111:
112: drawStringUnderlineCharAt(g, text, getDisplayedMnemonicIndex(
113: text, underlinedChar), x, y);
114: }
115:
116: public static void drawStringUnderlineCharAt(final Graphics g,
117: final String text, final int underlinedIndex, final int x,
118: final int y) {
119: g.drawString(text, x, y);
120: if (Utilities.insideString(text, underlinedIndex)) {
121: final Rectangle box = getStringUnderscoreBounds(g
122: .getFontMetrics(), text, underlinedIndex);
123: g.fillRect(x + box.x, y + box.y, box.width, box.height);
124: }
125: }
126:
127: public static void drawDashedRect(final Graphics g, final int x,
128: final int y, final int width, final int height) {
129: int maxX = x + width - 1;
130: int maxY = y + height - 1;
131: int dot;
132: for (dot = x; dot < x + width; dot += 2) {
133: g.drawLine(dot, y, dot, y);
134: g.drawLine(dot, maxY, dot, maxY);
135: }
136: for (dot = y; dot < y + height; dot += 2) {
137: g.drawLine(x, dot, x, dot);
138: g.drawLine(maxX, dot, maxX, dot);
139: }
140: }
141:
142: public static Dimension getPreferredButtonSize(
143: final AbstractButton b, final int textIconGap) {
144: return ButtonCommons.getPreferredSize(b, null, textIconGap);
145: }
146:
147: private static void drawTwoLines(final Graphics g, final Color c,
148: final int x1, final int y1, final int x2, final int y2,
149: final int x3, final int y3) {
150: int xs[] = { x1, x2, x3 };
151: int ys[] = { y1, y2, y3 };
152: g.setColor(c);
153: g.drawPolyline(xs, ys, 3);
154: }
155:
156: private static Rectangle getStringUnderscoreBounds(
157: final FontMetrics fm, final String str,
158: final int underscoreIndex) {
159: final int width = fm.charWidth(str.charAt(underscoreIndex));
160: final int height = 1;
161: final int x = underscoreIndex > 0 ? fm.stringWidth(str
162: .substring(0, underscoreIndex)) : 0;
163: final int y = fm.getDescent() - height;
164: return new Rectangle(x, y, width, height);
165: }
166:
167: private static int getDisplayedMnemonicIndex(final String text,
168: final int mnemonicChar) {
169: if (text == null) {
170: return -1;
171: }
172:
173: int index = text.toUpperCase().indexOf(mnemonicChar);
174: return index != -1 ? index : text.toLowerCase().indexOf(
175: mnemonicChar);
176:
177: // TODO: uncomment after moving to 1.5
178: // return text != null
179: // ? text.toUpperCase().indexOf(Character.toUpperCase(mnemonicChar))
180: // : -1;
181: }
182: }
|