001: package com.xoetrope.resource;
002:
003: import com.xoetrope.swing.list.XAltListCellRenderer;
004: import java.awt.Color;
005: import java.awt.Component;
006: import java.awt.Dimension;
007: import java.awt.Font;
008: import java.awt.FontMetrics;
009: import java.awt.Graphics;
010: import java.awt.Rectangle;
011: import javax.swing.JList;
012: import javax.swing.UIManager;
013: import net.xoetrope.xui.data.XBaseModel;
014: import net.xoetrope.xui.helper.XuiUtilities;
015:
016: /**
017: * A cell renderer for displaying download progress
018: * <p> Copyright (c) Xoetrope Ltd., 2001-2007, This software is licensed under
019: * the GNU Public License (GPL), please see license.txt for more details. If
020: * you make commercial use of this software you must purchase a commercial
021: * license from Xoetrope.</p>
022: * @author Luan O'Carroll
023: */
024: public class XResourceLoaderListCellRenderer extends
025: XAltListCellRenderer {
026: private static final int INDICATOR_WIDTH = 50;
027: private int fontHeight = 0;
028: private int enabledSaturation;
029: private int percentage;
030: private Color fgColor, bkColor;
031:
032: private XBaseModel downloadList;
033:
034: /** Creates a new instance of XDownloadProgressListCellRenderer */
035: public XResourceLoaderListCellRenderer(XBaseModel m) {
036: downloadList = m;
037: enabledSaturation = 100;
038: }
039:
040: /**
041: * Get the size of the cell and add some extra width for the progress indicator
042: */
043: public Dimension getPreferredSize() {
044: Dimension d = super .getPreferredSize();
045: return new Dimension(d.width + INDICATOR_WIDTH + 10, d.height);
046: }
047:
048: /*
049: * This method finds the image and text corresponding
050: * to the selected value and returns the label, set-up
051: * to display the text and image.
052: */
053: public Component getListCellRendererComponent(JList list,
054: Object value, int index, boolean isSelected,
055: boolean cellHasFocus) {
056: XBaseModel model = (XBaseModel) downloadList.get(index);
057:
058: if (unselectedForeground == null) {
059: unselectedForeground = list.getForeground();
060: unselectedBackground = list.getBackground();
061: }
062:
063: if (altUnselectedForeground == null) {
064: altUnselectedForeground = list.getForeground();
065: altUnselectedBackground = list.getBackground();
066: }
067:
068: percentage = Integer.parseInt((String) model
069: .getAttribValue(XResourceLoader.DOWNLOAD_PROGRESS));
070: int saturationLevel = 100;
071: if (!list.isEnabled())
072: saturationLevel = 50;
073:
074: if (isSelected) {
075: fgColor = XuiUtilities.unsaturateColor(list
076: .getSelectionForeground(), saturationLevel);
077: bkColor = XuiUtilities.unsaturateColor(list
078: .getSelectionBackground(), saturationLevel);
079: } else {
080: fgColor = XuiUtilities
081: .unsaturateColor(
082: ((index % 2) == 0 ? unselectedForeground
083: : altUnselectedForeground),
084: saturationLevel);
085: bkColor = XuiUtilities
086: .unsaturateColor(
087: ((index % 2) == 0 ? unselectedBackground
088: : altUnselectedBackground),
089: saturationLevel);
090: }
091: super .setForeground(fgColor);
092: super .setBackground(bkColor);
093:
094: setFont(list.getFont());
095: if (cellHasFocus) {
096: setBorder(UIManager
097: .getBorder("List.focusCellHighlightBorder"));
098: } else
099: setBorder(noFocusBorder);
100:
101: setText(value.toString());
102:
103: return this ;
104: }
105:
106: /**
107: * Paint the indicator
108: * @param g the graphics context
109: */
110: public void paintComponent(Graphics g) {
111: Rectangle rect = getBounds();
112:
113: int width = rect.width;
114: int height = rect.height;
115:
116: String text = getText();
117: Font f = g.getFont();
118: FontMetrics fm = g.getFontMetrics(f);
119:
120: // Fill the background
121: g.setColor(bkColor);
122: g.fillRect(0, 0, rect.width, rect.height);
123:
124: // Prepare the color bar
125: if (percentage < 0) {
126: g.setColor(XuiUtilities.unsaturateColor(new Color(204, 64,
127: 64), enabledSaturation));
128: if (percentage == -10000)
129: percentage = 0;
130: } else
131: g.setColor(XuiUtilities.unsaturateColor(new Color(33,
132: 128 + Math.min((percentage * 76) / 100, 127), 33),
133: enabledSaturation));
134:
135: // Determine the width of the bar, limiting it to a max of 150%
136: g.fill3DRect(0, 0, INDICATOR_WIDTH, rect.height, true);
137:
138: // Determine the text position
139: if (fontHeight <= 0) {
140: if (fm != null) {
141: int line_height = fm.getHeight();
142: fontHeight = height - (height - line_height) / 2
143: - fm.getDescent();
144: } else
145: fontHeight = rect.height - 2;
146: }
147:
148: g.setColor(Color.white);
149: g.drawString(Integer.toString(Math.abs(percentage)) + " %", 2,
150: fontHeight);
151:
152: // Draw the text
153: g.setColor(fgColor);
154: g.drawString(text, INDICATOR_WIDTH + 3, fontHeight);
155: }
156: }
|