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.Color;
023: import java.awt.Dimension;
024: import java.awt.Font;
025: import java.awt.FontMetrics;
026: import java.awt.Graphics;
027: import java.awt.Graphics2D;
028: import java.awt.Point;
029: import java.awt.Rectangle;
030: import java.awt.Shape;
031: import java.awt.SystemColor;
032:
033: import org.apache.harmony.awt.state.ListState;
034:
035: /**
036: * DefaultList
037: */
038: public class DefaultList extends DefaultStyle {
039: public static void drawBackground(Graphics g, ListState s,
040: boolean flat) {
041:
042: Dimension size = s.getSize();
043: g.setColor(s.isBackgroundSet() ? s.getBackground()
044: : SystemColor.text);
045: Rectangle client = s.getClient();
046: g.fillRect(client.x, client.y, client.width, client.height);
047: int x = client.x + client.width;
048: int y = client.y + client.height;
049: int w = size.width - x;
050: int h = size.height - y;
051: // fill areas outside of client area &&
052: // scrollbars
053: g.fillRect(x, y, w, h);
054: g.fillRect(0, size.height - 2, size.width, 2);
055: g.fillRect(size.width - 2, 0, 2, size.height);
056: if (flat) {
057: g.setColor(Color.black);
058: g.drawRect(0, 0, size.width - 1, size.height - 1);
059: } else {
060: //draw pressed button frame:
061: DefaultButton.drawButtonFrame(g, new Rectangle(new Point(),
062: size), true);
063: }
064: }
065:
066: public static void drawItems(Graphics g, ListState s) {
067: // draw items:
068: Shape oldClip = g.getClip();
069: Rectangle client = s.getClient();
070: g.clipRect(client.x, client.y, client.width, client.height);
071: g.setColor(s.getTextColor());
072: Font oldFont = g.getFont();
073: g.setFont(s.getFont());
074: int firstItemIdx = 0;
075: int lastItemIdx = 0;
076: if (s.getItemCount() > 0) {
077: Rectangle itemRect = s.getItemBounds(0);
078: int itemHeight = itemRect.height;
079: firstItemIdx = (client.y - itemRect.y) / itemHeight;
080: lastItemIdx = firstItemIdx + (client.height / itemHeight)
081: + 2;
082: lastItemIdx = Math.min(lastItemIdx, s.getItemCount());
083: }
084: for (int i = firstItemIdx; i < lastItemIdx; i++) {
085: drawItem(i, g, s);
086: }
087: if (s.isFocused()) {
088: paintFocus(g, s);
089: }
090: g.setFont(oldFont);
091: g.setClip(oldClip);
092: }
093:
094: private static void drawItem(int idx, Graphics g, ListState s) {
095: Rectangle itemRect = s.getItemBounds(idx);
096: itemRect.width = s.getClient().width - itemRect.x + 1;
097: int itemHeight = itemRect.height;
098: if (s.isSelected(idx)) {
099: g.setColor(SystemColor.textHighlight);
100: ((Graphics2D) g).fill(itemRect);
101: g.setColor(SystemColor.textHighlightText);
102: }
103: String item = s.getItem(idx);
104: FontMetrics fm = s.getFontMetrics();
105: int baseLineOffset = itemHeight - fm.getDescent() - 1;
106: g.drawString(item, itemRect.x + 1, baseLineOffset + itemRect.y);
107:
108: if (s.isSelected(idx)) {
109: g.setColor(s.getTextColor());
110: }
111: }
112:
113: private static void paintFocus(Graphics g, ListState s) {
114: int curIdx = s.getCurrentIndex();
115: if ((curIdx >= 0) && (curIdx < s.getItemCount())) {
116: Rectangle curRect = s.getItemBounds(curIdx);
117: Color oldColor = g.getColor();
118: g.setColor(s.isSelected(curIdx) ? Color.YELLOW : s
119: .getTextColor());
120: int w = s.getClient().width - curRect.x - 1;
121: drawFocusRect(g, curRect.x + 1, curRect.y + 1, w,
122: curRect.height - 3);
123: g.setColor(oldColor);
124: }
125: }
126: }
|