KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > contact > gui > box > ContactList


1 package org.columba.contact.gui.box;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Color JavaDoc;
5 import java.awt.Component JavaDoc;
6 import java.awt.Graphics JavaDoc;
7 import java.awt.Insets JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10
11 import javax.swing.BorderFactory JavaDoc;
12 import javax.swing.JLabel JavaDoc;
13 import javax.swing.JList JavaDoc;
14 import javax.swing.JPanel JavaDoc;
15 import javax.swing.JTextField JavaDoc;
16 import javax.swing.ListCellRenderer JavaDoc;
17 import javax.swing.ListModel JavaDoc;
18 import javax.swing.border.AbstractBorder JavaDoc;
19 import javax.swing.border.Border JavaDoc;
20
21 import org.columba.addressbook.model.IContactModelPartial;
22 import org.columba.core.resourceloader.IconKeys;
23 import org.columba.core.resourceloader.ImageLoader;
24 import org.jdesktop.swingx.JXHyperlink;
25 import org.jdesktop.swingx.JXList;
26 import org.jdesktop.swingx.decorator.Highlighter;
27 import org.jdesktop.swingx.decorator.HighlighterPipeline;
28 import org.jdesktop.swingx.decorator.RolloverHighlighter;
29
30 class ContactList extends JXList {
31
32     public ContactList() {
33         super();
34
35         setCellRenderer(new MyListCellRenderer());
36
37         setBorder(null);
38         setHighlighters(new HighlighterPipeline(
39                 new Highlighter[] { new RolloverHighlighter(new Color JavaDoc(248, 248,
40                         248), Color.white) }));
41         setRolloverEnabled(true);
42
43     }
44
45     public void addAll(List JavaDoc<IContactModelPartial> list) {
46         Iterator JavaDoc<IContactModelPartial> it = list.iterator();
47         while (it.hasNext()) {
48             addElement(it.next());
49         }
50     }
51
52     public void add(IContactModelPartial result) {
53         addElement(result);
54     }
55
56     /**
57      * ********************** filtering
58      * *********************************************
59      */

60
61     /**
62      * Associates filtering document listener to text component.
63      */

64
65     public void installJTextField(JTextField JavaDoc input) {
66         if (input != null) {
67             FilteringModel model = (FilteringModel) getModel();
68             input.getDocument().addDocumentListener(model);
69         }
70     }
71
72     /**
73      * Disassociates filtering document listener from text component.
74      */

75
76     public void uninstallJTextField(JTextField JavaDoc input) {
77         if (input != null) {
78             FilteringModel model = (FilteringModel) getModel();
79             input.getDocument().removeDocumentListener(model);
80         }
81     }
82
83     /**
84      * Doesn't let model change to non-filtering variety
85      */

86
87     public void setModel(ListModel JavaDoc model) {
88         if (!(model instanceof FilteringModel)) {
89             throw new IllegalArgumentException JavaDoc();
90         } else {
91             super.setModel(model);
92         }
93     }
94
95     /**
96      * Adds item to model of list
97      */

98     public void addElement(IContactModelPartial element) {
99         ((FilteringModel) getModel()).addElement(element);
100     }
101
102     class MyListCellRenderer extends JPanel JavaDoc implements ListCellRenderer JavaDoc {
103
104         private JLabel JavaDoc iconLabel = new JLabel JavaDoc();
105
106         private JLabel JavaDoc titleLabel = new JLabel JavaDoc();
107
108         private JXHyperlink descriptionLabel = new JXHyperlink();
109
110         private JPanel JavaDoc centerPanel;
111
112         private Border JavaDoc lineBorder = new HeaderSeparatorBorder(new Color JavaDoc(230,
113                 230, 230));
114
115         MyListCellRenderer() {
116             setLayout(new BorderLayout JavaDoc());
117
118             centerPanel = new JPanel JavaDoc();
119             centerPanel.setLayout(new BorderLayout JavaDoc());
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, 2, 4));
129
130             centerPanel.setOpaque(false);
131             setOpaque(true);
132
133         }
134
135         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value,
136                 int index, boolean isSelected, boolean cellHasFocus) {
137
138             if (isSelected) {
139                 setBackground(list.getSelectionBackground());
140                 setForeground(list.getSelectionForeground());
141             } else {
142                 setBackground(list.getBackground());
143                 setForeground(list.getForeground());
144             }
145
146             IContactModelPartial result = (IContactModelPartial) value;
147
148             titleLabel.setText(result.getName());
149             iconLabel.setIcon(ImageLoader.getSmallIcon(IconKeys.USER));
150             descriptionLabel.setText(result.getAddress());
151
152             return this;
153         }
154
155     }
156
157     class HeaderSeparatorBorder extends AbstractBorder JavaDoc {
158
159         protected Color JavaDoc color;
160
161         public HeaderSeparatorBorder(Color JavaDoc color) {
162             super();
163
164             this.color = color;
165         }
166
167         /**
168          * Paints the border for the specified component with the specified
169          * position and size.
170          *
171          * @param c
172          * the component for which this border is being painted
173          * @param g
174          * the paint graphics
175          * @param x
176          * the x position of the painted border
177          * @param y
178          * the y position of the painted border
179          * @param width
180          * the width of the painted border
181          * @param height
182          * the height of the painted border
183          */

184         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
185                 int width, int height) {
186             Color JavaDoc oldColor = g.getColor();
187             g.setColor(color);
188             g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);
189
190             g.setColor(oldColor);
191         }
192
193         /**
194          * Returns the insets of the border.
195          *
196          * @param c
197          * the component for which this border insets value applies
198          */

199         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
200             return new Insets JavaDoc(0, 0, 1, 0);
201         }
202
203         /**
204          * Reinitialize the insets parameter with this Border's current Insets.
205          *
206          * @param c
207          * the component for which this border insets value applies
208          * @param insets
209          * the object to be reinitialized
210          */

211         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
212             insets.left = insets.top = insets.right = insets.bottom = 1;
213             return insets;
214         }
215
216     }
217 }
218
Popular Tags