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 Dmitry A. Durnev, Pavel Dolgov
019: * @version $Revision$
020: */package org.apache.harmony.awt.theme;
021:
022: import java.awt.BasicStroke;
023: import java.awt.Color;
024: import java.awt.Dimension;
025: import java.awt.Graphics;
026: import java.awt.Graphics2D;
027: import java.awt.Point;
028: import java.awt.Rectangle;
029: import java.awt.Stroke;
030: import java.awt.SystemColor;
031:
032: import org.apache.harmony.awt.state.CheckboxState;
033: import org.apache.harmony.awt.state.TextState;
034:
035: /**
036: * Implementation of Checkbox' default visual style
037: */
038: public class DefaultCheckbox extends DefaultStyle {
039: static Color pressedColor = new Color(236, 233, 216);
040:
041: public static void drawBackground(Graphics g, CheckboxState s,
042: Rectangle r) {
043:
044: Dimension size = s.getSize();
045: boolean group = s.isInGroup();
046:
047: g.setColor(s.getBackground());
048: g.fillRect(0, 0, size.width, size.height);
049: Rectangle checkRect = drawCheckBox(g, s);
050: g.setColor(s.getTextColor());
051: if (s.isChecked()) {
052: if (group) {
053: drawRoundCheckMark(g, checkRect);
054: } else {
055: drawCheckMark(g, checkRect);
056: }
057: }
058:
059: if (s.isFocused()) {
060: r.grow(1, 1);
061: drawFocusRect(g, r.x, r.y, r.width, r.height);
062: }
063: }
064:
065: public static Rectangle getTextRect(CheckboxState s) {
066:
067: if (s.getText() == null) {
068: return s.getBounds();
069: }
070:
071: Dimension size = s.getSize();
072: Dimension textSize = getTextSize(s);
073:
074: int centerY = size.height / 2;
075: int textCenterY = textSize.height / 2;
076:
077: int margin = ABS_MARGIN + CB_SIZE
078: + (int) (REL_MARGIN * textSize.height / 2);
079: int baseX = margin; // + textSize.width;
080:
081: int baseY = centerY + textCenterY;
082:
083: return new Rectangle(baseX, baseY - textSize.height,
084: textSize.width, textSize.height);
085:
086: }
087:
088: public static void drawText(Graphics g, TextState s, Rectangle r) {
089: String text = s.getText();
090: if (text == null) {
091: return;
092: }
093: int baseX = r.x;
094: int h = getTextSize(s).height;
095: int baseY = r.y + r.height - h / 5;
096: g.setFont(s.getFont());
097: g.setColor(s.getTextColor());
098: if (s.isEnabled()) {
099: g.drawString(text, baseX, baseY);
100: } else {
101: drawDisabledString(g, text, baseX, baseY);
102: }
103: }
104:
105: public static void calculate(CheckboxState s) {
106: Dimension textSize = getTextSize(s);
107:
108: Dimension cbSize = new Dimension();
109: cbSize.width = textSize.width
110: + (int) (2 * REL_MARGIN * textSize.height) + 2
111: * ABS_MARGIN;
112: cbSize.width += CB_SIZE;
113: cbSize.height = (int) ((2 * REL_MARGIN + 1) * textSize.height)
114: + 2 * ABS_MARGIN;
115: s.setDefaultMinimumSize(cbSize);
116: s.setTextSize(textSize);
117: }
118:
119: private static void drawRoundCheckMark(Graphics g, Rectangle rect) {
120: int dx = rect.width / 3;
121: int dy = rect.height / 3;
122: g.fillOval(rect.x + dx, rect.y + dy, dx + 1, dy + 1);
123: }
124:
125: private static void drawCheckMark(Graphics g, Rectangle rect) {
126:
127: Graphics2D g2d = (Graphics2D) g;
128: Stroke stroke = g2d.getStroke();
129: g2d.setStroke(new BasicStroke(3));
130: int w = rect.width;
131: int h = rect.height;
132: Point p2 = new Point(rect.x + w / 2 - 1, rect.y + h - 3);
133: int x0 = p2.x;
134: int y0 = p2.y;
135: Point p1 = new Point(x0 - w / 4 + 1, y0 - h / 4 + 1);
136: Point p3 = new Point(x0 + w / 3, y0 - h / 3);
137: g.drawLine(p1.x, p1.y, x0, y0);
138: g.drawLine(x0, y0, p3.x, p3.y);
139:
140: g2d.setStroke(stroke);
141: }
142:
143: private static Rectangle drawCheckBox(Graphics g, CheckboxState s) {
144: Dimension size = s.getSize();
145: boolean group = s.isInGroup();
146: boolean pressed = s.isPressed();
147: int x = ABS_MARGIN;
148: int y = size.height / 2 - CB_SIZE / 2;
149: Rectangle r = new Rectangle(x, y, CB_SIZE, CB_SIZE);
150: if (!group) {
151: drawPressedRect(g, r, pressed);
152: } else {
153: drawPressedCircle(g, r, pressed);
154: }
155:
156: return r;
157: }
158:
159: private static void drawPressedRect(Graphics g, Rectangle rect,
160: boolean pressed) {
161: int w = rect.width;
162: int h = rect.height;
163: int x = rect.x;
164: int y = rect.y;
165: g.setColor(SystemColor.controlHighlight);
166:
167: g.drawLine(x + w - 1, y, x + w - 1, y + h - 1);
168: g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);
169: g.setColor(SystemColor.controlShadow);
170: g.drawLine(x + 1, y + 1, x + w - 3, y + 1);
171: g.drawLine(x + 1, y + 1, x + 1, y + h - 3);
172: g.setColor(SystemColor.controlDkShadow);
173: g.drawLine(x, y, x + w - 2, y);
174: g.drawLine(x, y, x, y + h - 2);
175: g.setColor(pressed ? pressedColor : SystemColor.window);
176: g.fillRect(x + 2, y + 2, CB_SIZE - 2, CB_SIZE - 2);
177: }
178:
179: private static void drawPressedCircle(Graphics g, Rectangle rect,
180: boolean pressed) {
181: int w = rect.width;
182: int h = rect.height;
183: int x = rect.x;
184: int y = rect.y;
185:
186: g.setColor(SystemColor.controlShadow);
187: int startAngle = 45;
188: int angle = 180;
189: int endAngle = startAngle + angle;
190: g.drawArc(x, y, w, h, startAngle, angle);
191: g.setColor(SystemColor.controlDkShadow);
192: g.drawArc(x + 1, y + 1, w - 1, h - 1, startAngle, angle);
193:
194: g.setColor(SystemColor.controlHighlight);
195: g.drawArc(x, y, w, h, endAngle, angle);
196: g.setColor(SystemColor.controlLtHighlight);
197: g.drawArc(x + 1, y + 1, w - 1, h - 1, endAngle, angle);
198: g.setColor(pressed ? pressedColor : SystemColor.window);
199: g.fillOval(x + 2, y + 2, CB_SIZE - 2, CB_SIZE - 2);
200:
201: }
202:
203: }
|