001: package org.acm.seguin.completer.popup;
002:
003: import java.util.Iterator;
004:
005: import java.util.Vector;
006:
007: import java.awt.Component;
008:
009: import java.awt.Graphics;
010:
011: import java.awt.Color;
012:
013: import java.awt.FontMetrics;
014:
015: import java.awt.Font;
016:
017: import java.awt.Rectangle;
018:
019: import javax.swing.ImageIcon;
020:
021: import javax.swing.JList;
022:
023: import javax.swing.DefaultListCellRenderer;
024:
025: public class PopupItemCellRenderer extends DefaultListCellRenderer {
026:
027: PopupItem _popupItem = null;
028:
029: boolean _bSel = false;
030:
031: int _iLeftOffsetStringLen = 0;
032:
033: int _iIconWidth = 0;
034:
035: int _iLeftOffset = -1;
036:
037: public PopupItemCellRenderer(Vector argPopupItems) {
038:
039: String str = null;
040:
041: PopupItem pi;
042:
043: for (Iterator it = argPopupItems.iterator(); it.hasNext();) {
044:
045: pi = (PopupItem) it.next();
046:
047: _iLeftOffsetStringLen = Math.max(pi.getLeftText().length(),
048:
049: _iLeftOffsetStringLen);
050:
051: _iIconWidth = Math.max(pi.getIconWidth(),
052:
053: _iIconWidth);
054:
055: }
056:
057: for (int i = 0, l = _iLeftOffsetStringLen; i < l; i++) {
058:
059: }
060:
061: }
062:
063: int getIconWidth() {
064: return _iIconWidth;
065: }
066:
067: int getLeftOffset(Font argFont) {
068:
069: if (_iLeftOffset == -1) {
070:
071: int iLen = 0;
072:
073: StringBuffer sb = new StringBuffer();
074:
075: if (_iLeftOffsetStringLen != 0) {
076:
077: for (int i = 0, l = _iLeftOffsetStringLen; i < l; i++) {
078:
079: sb.append(" ");
080:
081: }
082:
083: FontMetrics fontMetrics = getFontMetrics(argFont);
084:
085: iLen = fontMetrics.stringWidth(sb.toString());
086:
087: }
088:
089: _iLeftOffset = iLen + _iIconWidth;
090:
091: }
092:
093: return _iLeftOffset;
094:
095: }
096:
097: /**
098:
099: * Gets the listCellRendererComponent attribute of the PopupCellRenderer
100:
101: * object
102:
103: *
104:
105: * @param argList Description of the Parameter
106:
107: * @param argValue Description of the Parameter
108:
109: * @param argIndex Description of the Parameter
110:
111: * @param argIsSelected Description of the Parameter
112:
113: * @param argCellHasFocus Description of the Parameter
114:
115: * @return The listCellRendererComponent value
116:
117: */
118:
119: public Component getListCellRendererComponent(JList argList,
120:
121: Object argValue,
122:
123: int argIndex,
124:
125: boolean argIsSelected,
126:
127: boolean argCellHasFocus) {
128:
129: _popupItem = null;
130:
131: _bSel = argIsSelected;
132:
133: Component comp = null;
134:
135: if (argValue instanceof PopupItem) {
136:
137: _popupItem = (PopupItem) argValue;
138:
139: comp = super .getListCellRendererComponent(
140:
141: argList,
142:
143: _popupItem.toString(_iLeftOffsetStringLen),
144:
145: argIndex, argIsSelected, argCellHasFocus);
146:
147: if (_popupItem.getIcon() != null) {
148:
149: setIcon(_popupItem.getIcon());
150:
151: }
152:
153: } else {
154:
155: comp = super .getListCellRendererComponent(
156:
157: argList, argValue,
158:
159: argIndex, argIsSelected, argCellHasFocus);
160:
161: }
162:
163: return comp;
164:
165: }
166:
167: /**
168:
169: * Description of the Method
170:
171: *
172:
173: * @param argGraphics Description of the Parameter
174:
175: */
176:
177: public void paint(Graphics argGraphics) {
178:
179: super .paint(argGraphics);
180:
181: if (_popupItem != null) {
182:
183: Rectangle r = getBounds();
184:
185: Font font = getFont();
186:
187: FontMetrics fontMetrics = getFontMetrics(font);
188:
189: int iLeftOffset = getLeftOffset(font);
190:
191: if (_popupItem.underLine()) {
192:
193: String strText = getText();
194:
195: int w = fontMetrics.stringWidth(strText);
196:
197: //argGraphics.setColor(MemberRenderer.this.getForeground());
198:
199: argGraphics.drawLine(iLeftOffset, r.height - 2,
200:
201: _iIconWidth + w, r.height - 2);
202:
203: }
204:
205: if (_popupItem.leftHighlight() && !_bSel
206: && _iLeftOffsetStringLen != 0) {
207:
208: argGraphics.setColor(new Color(141, 197, 246, 90));
209:
210: argGraphics.fillRect(_iIconWidth, 0,
211:
212: iLeftOffset - _iIconWidth, r.height);
213:
214: }
215:
216: }
217:
218: }
219:
220: }
|