01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /**
18: * @author Pavel Dolgov, Michael Danilov
19: * @version $Revision$
20: */package org.apache.harmony.awt.theme;
21:
22: import java.awt.BasicStroke;
23: import java.awt.Dimension;
24: import java.awt.FontMetrics;
25: import java.awt.Graphics;
26: import java.awt.Graphics2D;
27: import java.awt.Stroke;
28: import java.awt.SystemColor;
29:
30: import org.apache.harmony.awt.state.TextState;
31:
32: /**
33: * Common functionality for default visual style
34: */
35: public class DefaultStyle {
36:
37: static final int ABS_MARGIN = 4;
38: static final double REL_MARGIN = 1. / 3.;
39: static final int CB_SIZE = 12;
40:
41: /**
42: * Draw dotted rectangle that indicates the focused component
43: *
44: */
45: public static void drawFocusRect(Graphics g, int x, int y, int w,
46: int h) {
47: if ((w <= 0) || (h <= 0)) {
48: return;
49: }
50: Graphics2D g2d = (Graphics2D) g;
51: Stroke oldStroke = g2d.getStroke();
52: g2d.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
53: BasicStroke.JOIN_ROUND, 1, new float[] { 1.0f, 1.0f },
54: 0.0f));
55: //g.drawRect(x, y, w, h);
56: // workaround:
57: int x1 = x + w;
58: int y1 = y + h;
59: g.drawLine(x, y, x, y1);
60: g.drawLine(x, y1, x1, y1);
61: g.drawLine(x, y, x1, y);
62: g.drawLine(x1, y, x1, y1);
63: g2d.setStroke(oldStroke);
64: }
65:
66: /**
67: * Draw text for disabled standard component.
68: * It's assumed that desired font is already selected in passed Graphics
69: * @param g
70: * @param label
71: * @param baseX
72: * @param baseY
73: */
74: public static void drawDisabledString(Graphics g, String label,
75: int baseX, int baseY) {
76: g.setColor(SystemColor.controlHighlight);
77: g.drawString(label, baseX + 1, baseY + 1);
78: g.setColor(SystemColor.controlShadow);
79: g.drawString(label, baseX, baseY);
80: }
81:
82: /**
83: * Calculate the size of the text
84: * @param s
85: */
86: public static Dimension getTextSize(TextState s) {
87: Dimension textSize = new Dimension(0, 0);
88: String text = s.getText();
89: if (text != null) {
90: FontMetrics metrics = s.getFontMetrics();
91: textSize.width = metrics.stringWidth(text);
92: textSize.height = metrics.getHeight();
93: }
94: return textSize;
95: }
96: }
|