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
019: * @version $Revision$
020: */package org.apache.harmony.awt.theme;
021:
022: import java.awt.Dimension;
023: import java.awt.Graphics;
024: import java.awt.Point;
025: import java.awt.Rectangle;
026: import java.awt.Shape;
027: import java.awt.SystemColor;
028:
029: import org.apache.harmony.awt.state.ChoiceState;
030:
031: /**
032: * DefaultChoice
033: */
034: public class DefaultChoice extends DefaultStyle {
035: private final static int BORDER_SIZE = 2;
036:
037: public static Rectangle drawButton(Graphics g, ChoiceState s) {
038: Dimension size = s.getSize();
039: Rectangle buttonRect = getButtonRect(size);
040: int dx = buttonRect.x;
041: int dy = buttonRect.y;
042: g.translate(dx, dy);
043: g.setColor(SystemColor.control);
044: int buttonWidth = buttonRect.width;
045: int buttonHeight = buttonRect.height;
046: g.fillRect(0, 0, buttonWidth, buttonHeight);
047: DefaultScrollbar
048: .paintArrowButton(g, DefaultScrollbar.SOUTH,
049: buttonWidth, buttonHeight, s.isPressed(), s
050: .isEnabled());
051: g.translate(-dx, -dy);
052: return buttonRect;
053: }
054:
055: private static Rectangle getButtonRect(Dimension size) {
056: Rectangle buttonRect = new Rectangle();
057: int gap = 2 * BORDER_SIZE;
058: buttonRect.width = Math.min(size.width - gap,
059: DefaultScrollbar.BUTTON_SIZE + 2);
060: buttonRect.height = size.height - gap;
061: buttonRect.x = size.width - buttonRect.width - BORDER_SIZE;
062: buttonRect.y = BORDER_SIZE;
063: return buttonRect;
064: }
065:
066: public static Rectangle drawBackground(Graphics g, ChoiceState s) {
067: Dimension size = s.getSize();
068: g.setColor(s.getBackground());
069: g.fillRect(0, 0, size.width, size.height);
070: DefaultButton.drawButtonFrame(g, new Rectangle(new Point(),
071: size), true);
072: Rectangle buttonRect = drawButton(g, s);
073: drawFocus(g, s);
074: return buttonRect;
075: }
076:
077: private static void drawFocus(Graphics g, ChoiceState s) {
078: if (s.isFocused()) {
079: Rectangle r = new Rectangle(new Point(), s.getSize());
080: r.width -= DefaultScrollbar.BUTTON_SIZE + 2;
081: r.grow(-2 * BORDER_SIZE, -2 * BORDER_SIZE);
082: drawFocusRect(g, r.x, r.y, r.width, r.height);
083: r.grow(-1, -1);
084: g.setColor(SystemColor.textHighlight);
085: g.fillRect(r.x, r.y, r.width, r.height);
086: }
087: }
088:
089: public static void drawText(Graphics g, ChoiceState s) {
090: String text = s.getText();
091: if (text == null) {
092: return;
093: }
094:
095: Rectangle r = s.getTextBounds();
096:
097: Shape oldClip = g.getClip();
098: g.clipRect(r.x, r.y, r.width, r.height);
099:
100: g.setFont(s.getFont());
101: g.setColor(s.isFocused() ? SystemColor.textHighlightText : s
102: .getTextColor());
103: int baseX = r.x;
104: int baseY = r.y + r.height - s.getFontMetrics().getDescent();
105: if (s.isEnabled()) {
106: g.drawString(text, baseX, baseY);
107: } else {
108: drawDisabledString(g, text, baseX, baseY);
109: }
110: g.setClip(oldClip);
111: }
112: }
|