001: package org.columba.contact.gui.box;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Component;
006: import java.awt.Graphics;
007: import java.awt.Insets;
008: import java.util.Iterator;
009: import java.util.List;
010:
011: import javax.swing.BorderFactory;
012: import javax.swing.JLabel;
013: import javax.swing.JList;
014: import javax.swing.JPanel;
015: import javax.swing.JTextField;
016: import javax.swing.ListCellRenderer;
017: import javax.swing.ListModel;
018: import javax.swing.border.AbstractBorder;
019: import javax.swing.border.Border;
020:
021: import org.columba.addressbook.model.IContactModelPartial;
022: import org.columba.core.resourceloader.IconKeys;
023: import org.columba.core.resourceloader.ImageLoader;
024: import org.jdesktop.swingx.JXHyperlink;
025: import org.jdesktop.swingx.JXList;
026: import org.jdesktop.swingx.decorator.Highlighter;
027: import org.jdesktop.swingx.decorator.HighlighterPipeline;
028: import org.jdesktop.swingx.decorator.RolloverHighlighter;
029:
030: class ContactList extends JXList {
031:
032: public ContactList() {
033: super ();
034:
035: setCellRenderer(new MyListCellRenderer());
036:
037: setBorder(null);
038: setHighlighters(new HighlighterPipeline(
039: new Highlighter[] { new RolloverHighlighter(new Color(
040: 248, 248, 248), Color.white) }));
041: setRolloverEnabled(true);
042:
043: }
044:
045: public void addAll(List<IContactModelPartial> list) {
046: Iterator<IContactModelPartial> it = list.iterator();
047: while (it.hasNext()) {
048: addElement(it.next());
049: }
050: }
051:
052: public void add(IContactModelPartial result) {
053: addElement(result);
054: }
055:
056: /**
057: * ********************** filtering
058: * *********************************************
059: */
060:
061: /**
062: * Associates filtering document listener to text component.
063: */
064:
065: public void installJTextField(JTextField input) {
066: if (input != null) {
067: FilteringModel model = (FilteringModel) getModel();
068: input.getDocument().addDocumentListener(model);
069: }
070: }
071:
072: /**
073: * Disassociates filtering document listener from text component.
074: */
075:
076: public void uninstallJTextField(JTextField input) {
077: if (input != null) {
078: FilteringModel model = (FilteringModel) getModel();
079: input.getDocument().removeDocumentListener(model);
080: }
081: }
082:
083: /**
084: * Doesn't let model change to non-filtering variety
085: */
086:
087: public void setModel(ListModel model) {
088: if (!(model instanceof FilteringModel)) {
089: throw new IllegalArgumentException();
090: } else {
091: super .setModel(model);
092: }
093: }
094:
095: /**
096: * Adds item to model of list
097: */
098: public void addElement(IContactModelPartial element) {
099: ((FilteringModel) getModel()).addElement(element);
100: }
101:
102: class MyListCellRenderer extends JPanel implements ListCellRenderer {
103:
104: private JLabel iconLabel = new JLabel();
105:
106: private JLabel titleLabel = new JLabel();
107:
108: private JXHyperlink descriptionLabel = new JXHyperlink();
109:
110: private JPanel centerPanel;
111:
112: private Border lineBorder = new HeaderSeparatorBorder(
113: new Color(230, 230, 230));
114:
115: MyListCellRenderer() {
116: setLayout(new BorderLayout());
117:
118: centerPanel = new JPanel();
119: centerPanel.setLayout(new BorderLayout());
120:
121: centerPanel.add(titleLabel, BorderLayout.NORTH);
122: centerPanel.add(descriptionLabel, BorderLayout.CENTER);
123: add(iconLabel, BorderLayout.WEST);
124: add(centerPanel, BorderLayout.CENTER);
125:
126: setBorder(BorderFactory.createCompoundBorder(lineBorder,
127: BorderFactory.createEmptyBorder(2, 2, 2, 2)));
128: iconLabel.setBorder(BorderFactory.createEmptyBorder(2, 4,
129: 2, 4));
130:
131: centerPanel.setOpaque(false);
132: setOpaque(true);
133:
134: }
135:
136: public Component getListCellRendererComponent(JList list,
137: Object value, int index, boolean isSelected,
138: boolean cellHasFocus) {
139:
140: if (isSelected) {
141: setBackground(list.getSelectionBackground());
142: setForeground(list.getSelectionForeground());
143: } else {
144: setBackground(list.getBackground());
145: setForeground(list.getForeground());
146: }
147:
148: IContactModelPartial result = (IContactModelPartial) value;
149:
150: titleLabel.setText(result.getName());
151: iconLabel.setIcon(ImageLoader.getSmallIcon(IconKeys.USER));
152: descriptionLabel.setText(result.getAddress());
153:
154: return this ;
155: }
156:
157: }
158:
159: class HeaderSeparatorBorder extends AbstractBorder {
160:
161: protected Color color;
162:
163: public HeaderSeparatorBorder(Color color) {
164: super ();
165:
166: this .color = color;
167: }
168:
169: /**
170: * Paints the border for the specified component with the specified
171: * position and size.
172: *
173: * @param c
174: * the component for which this border is being painted
175: * @param g
176: * the paint graphics
177: * @param x
178: * the x position of the painted border
179: * @param y
180: * the y position of the painted border
181: * @param width
182: * the width of the painted border
183: * @param height
184: * the height of the painted border
185: */
186: public void paintBorder(Component c, Graphics g, int x, int y,
187: int width, int height) {
188: Color oldColor = g.getColor();
189: g.setColor(color);
190: g
191: .drawLine(x, y + height - 1, x + width - 1, y
192: + height - 1);
193:
194: g.setColor(oldColor);
195: }
196:
197: /**
198: * Returns the insets of the border.
199: *
200: * @param c
201: * the component for which this border insets value applies
202: */
203: public Insets getBorderInsets(Component c) {
204: return new Insets(0, 0, 1, 0);
205: }
206:
207: /**
208: * Reinitialize the insets parameter with this Border's current Insets.
209: *
210: * @param c
211: * the component for which this border insets value applies
212: * @param insets
213: * the object to be reinitialized
214: */
215: public Insets getBorderInsets(Component c, Insets insets) {
216: insets.left = insets.top = insets.right = insets.bottom = 1;
217: return insets;
218: }
219:
220: }
221: }
|