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 Michael Danilov, Pavel Dolgov
019: * @version $Revision$
020: */package org.apache.harmony.awt.theme;
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.Rectangle;
027: import java.awt.SystemColor;
028:
029: import org.apache.harmony.awt.state.*;
030:
031: /**
032: * Implementation of Button's default visual style
033: */
034: public class DefaultButton extends DefaultStyle {
035:
036: public static void drawBackground(Graphics g, ButtonState s) {
037: Rectangle rect = s.getBounds();
038: Color bkColor = s.isBackgroundSet() ? s.getBackground()
039: : SystemColor.control;
040: g.setColor(bkColor);
041: g.fillRect(0, 0, rect.width, rect.height);
042:
043: drawButtonFrame(g, s.getBounds(), s.isPressed());
044:
045: if (s.isFocused()) {
046: Color foreColor = s.isTextColorSet() ? s.getTextColor()
047: : SystemColor.controlText;
048: g.setColor(foreColor);
049:
050: drawFocusRect(g, 3, 3, rect.width - 7, rect.height - 7);
051: }
052: }
053:
054: public static void drawButtonFrame(Graphics g, Rectangle rect,
055: boolean pressed) {
056: if (pressed) {
057: g.setColor(SystemColor.controlHighlight);
058: g.drawLine(rect.width - 1, 0, rect.width - 1,
059: rect.height - 1);
060: g.drawLine(0, rect.height - 1, rect.width - 1,
061: rect.height - 1);
062: g.setColor(SystemColor.controlShadow);
063: g.drawLine(1, 1, rect.width - 3, 1);
064: g.drawLine(1, 1, 1, rect.height - 3);
065: g.setColor(SystemColor.controlDkShadow);
066: g.drawLine(0, 0, rect.width - 2, 0);
067: g.drawLine(0, 0, 0, rect.height - 2);
068: } else {
069: g.setColor(SystemColor.controlHighlight);
070: g.drawLine(0, 0, rect.width - 1, 0);
071: g.drawLine(0, 0, 0, rect.height - 1);
072: g.setColor(SystemColor.controlShadow);
073: g.drawLine(rect.width - 2, 1, rect.width - 2,
074: rect.height - 2);
075: g.drawLine(1, rect.height - 2, rect.width - 2,
076: rect.height - 2);
077: g.setColor(SystemColor.controlDkShadow);
078: g.drawLine(rect.width - 1, 0, rect.width - 1,
079: rect.height - 1);
080: g.drawLine(0, rect.height - 1, rect.width - 1,
081: rect.height - 1);
082: }
083: }
084:
085: public static void drawText(Graphics g, ButtonState s) {
086: String text = s.getText();
087: boolean pressed = s.isPressed();
088: Color foreColor = s.isTextColorSet() ? s.getTextColor()
089: : SystemColor.controlText;
090:
091: if (text != null) {
092: int labelHeight = s.getTextSize().height;
093: Dimension prefSize = s.getDefaultMinimumSize();
094: Dimension realSize = s.getSize();
095: int baseX = ABS_MARGIN + (int) (REL_MARGIN * labelHeight)
096: + (realSize.width - prefSize.width) / 2;
097: int baseY = ABS_MARGIN
098: + (int) ((REL_MARGIN + 1) * labelHeight)
099: + (realSize.height - prefSize.height) / 2;
100:
101: g.setFont(s.getFont());
102: if (s.isEnabled()) {
103: g.setColor(foreColor);
104: if (pressed) {
105: g.drawString(text, baseX + 1, baseY + 1);
106: } else {
107: g.drawString(text, baseX, baseY);
108: }
109: } else {
110: drawDisabledString(g, text, baseX, baseY);
111: }
112: }
113:
114: }
115:
116: public static void calculate(ButtonState s) {
117: FontMetrics metrics = s.getFontMetrics();
118: Dimension textSize = getTextSize(s);
119:
120: textSize.height = metrics.getAscent();
121:
122: Dimension buttonSize = new Dimension();
123: buttonSize.width = textSize.width
124: + (int) (2 * REL_MARGIN * textSize.height) + 2
125: * ABS_MARGIN;
126: buttonSize.height = (int) ((2 * REL_MARGIN + 1) * textSize.height)
127: + 2 * ABS_MARGIN;
128:
129: s.setDefaultMinimumSize(buttonSize);
130: s.setTextSize(textSize);
131: }
132:
133: }
|