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 Dmitry A. Durnev, Pavel Dolgov
19: * @version $Revision$
20: */package org.apache.harmony.awt.theme;
21:
22: import java.awt.Dimension;
23: import java.awt.Graphics;
24: import java.awt.Label;
25:
26: import org.apache.harmony.awt.state.LabelState;
27:
28: /**
29: * Implementation of Label's default visual style
30: */
31: public class DefaultLabel extends DefaultStyle {
32:
33: public static void drawBackground(Graphics g, LabelState s) {
34:
35: Dimension size = s.getSize();
36: g.setColor(s.getBackground());
37: g.fillRect(0, 0, size.width, size.height);
38: }
39:
40: public static void drawText(Graphics g, LabelState s) {
41: String text = s.getText();
42: if (text == null) {
43: return;
44: }
45: int alignment = s.getAlignment();
46: g.setColor(s.getTextColor());
47: g.setFont(s.getFont());
48:
49: Dimension textSize = s.getTextSize();
50: Dimension size = s.getSize();
51:
52: int textCenterX = textSize.width / 2;
53:
54: int lblCenterX = size.width / 2;
55: int lblCenterY = size.height / 2;
56:
57: int baseX = 0;
58: int margin = (int) (REL_MARGIN * textSize.height / 3);
59: switch (alignment) {
60: case Label.LEFT:
61: baseX = margin;
62: break;
63: case Label.CENTER:
64: baseX = lblCenterX - textCenterX;
65: break;
66: case Label.RIGHT:
67: baseX = size.width - textSize.width - margin;
68: break;
69: }
70: int textCenterY = textSize.height / 2;
71: int baseY = lblCenterY + textCenterY;
72:
73: if (s.isEnabled()) {
74: g.drawString(text, baseX, baseY);
75: } else {
76: drawDisabledString(g, text, baseX, baseY);
77: }
78: }
79:
80: public static void calculate(LabelState s) {
81:
82: Dimension textSize = getTextSize(s);
83:
84: Dimension labelSize = new Dimension();
85: labelSize.width = textSize.width
86: + (int) (2 * REL_MARGIN * textSize.height) + 2
87: * ABS_MARGIN;
88: labelSize.height = (int) ((2 * REL_MARGIN + 1) * textSize.height)
89: + 2 * ABS_MARGIN;
90:
91: s.setDefaultMinimumSize(labelSize);
92: s.setTextSize(textSize);
93: }
94:
95: }
|