KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > addressbook > gui > search > SearchResultList


1 package org.columba.addressbook.gui.search;
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.awt.Point JavaDoc;
9 import java.awt.event.ActionEvent JavaDoc;
10 import java.awt.event.ActionListener JavaDoc;
11 import java.awt.event.MouseAdapter JavaDoc;
12 import java.awt.event.MouseEvent JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import javax.swing.BorderFactory JavaDoc;
17 import javax.swing.DefaultListModel JavaDoc;
18 import javax.swing.JLabel JavaDoc;
19 import javax.swing.JList JavaDoc;
20 import javax.swing.JMenuItem JavaDoc;
21 import javax.swing.JPanel JavaDoc;
22 import javax.swing.JPopupMenu JavaDoc;
23 import javax.swing.ListCellRenderer JavaDoc;
24 import javax.swing.border.AbstractBorder JavaDoc;
25 import javax.swing.border.Border JavaDoc;
26
27 import org.columba.addressbook.facade.DialogFacade;
28 import org.columba.api.exception.ServiceNotFoundException;
29 import org.columba.contact.search.ContactSearchResult;
30 import org.columba.core.facade.ServiceFacadeRegistry;
31 import org.columba.core.gui.base.DoubleClickListener;
32 import org.columba.core.resourceloader.IconKeys;
33 import org.columba.core.resourceloader.ImageLoader;
34 import org.columba.core.search.api.ISearchResult;
35 import org.jdesktop.swingx.JXHyperlink;
36 import org.jdesktop.swingx.JXList;
37 import org.jdesktop.swingx.decorator.Highlighter;
38 import org.jdesktop.swingx.decorator.HighlighterPipeline;
39 import org.jdesktop.swingx.decorator.RolloverHighlighter;
40
41 public class SearchResultList extends JXList {
42
43     private DefaultListModel JavaDoc listModel;
44     private JPopupMenu JavaDoc contextMenu;
45     
46     public SearchResultList() {
47         super();
48
49         listModel = new DefaultListModel JavaDoc();
50         setModel(listModel);
51         setCellRenderer(new MyListCellRenderer());
52
53         setBorder(null);
54         setHighlighters(new HighlighterPipeline(
55                 new Highlighter[] { new RolloverHighlighter(new Color JavaDoc(248, 248,
56                         248), Color.white) }));
57         setRolloverEnabled(true);
58
59         addMouseListener(new DoubleClickListener() {
60             @Override JavaDoc
61             public void doubleClick(MouseEvent JavaDoc event) {
62                 ISearchResult selected = (ISearchResult) getSelectedValue();
63                 new DialogFacade().openContactDialog(selected.getLocation());
64             }
65         });
66
67         add(getPopupMenu());
68         addMouseListener(new MyMouseListener());
69     }
70
71     private JPopupMenu JavaDoc getPopupMenu() {
72         if (contextMenu != null)
73             return contextMenu;
74
75         contextMenu = new JPopupMenu JavaDoc();
76
77         JMenuItem JavaDoc item = new JMenuItem JavaDoc("Open..");
78         item.addActionListener(new ActionListener JavaDoc() {
79             public void actionPerformed(ActionEvent JavaDoc event) {
80                 ISearchResult selected = (ISearchResult)getSelectedValue();
81                 new DialogFacade().openContactDialog(selected.getLocation());
82             }
83         });
84         contextMenu.add(item);
85
86         item = new JMenuItem JavaDoc("Compose Message..");
87         item.addActionListener(new ActionListener JavaDoc() {
88             public void actionPerformed(ActionEvent JavaDoc event) {
89                 ContactSearchResult selected = (ContactSearchResult) getSelectedValue();
90                 
91                 String JavaDoc address = selected.getDescription();
92
93                 try {
94                     org.columba.mail.facade.IDialogFacade facade = (org.columba.mail.facade.IDialogFacade) ServiceFacadeRegistry
95                             .getInstance()
96                             .getService(
97                                     org.columba.mail.facade.IDialogFacade.class);
98                     facade.openComposer(address);
99                 } catch (ServiceNotFoundException e) {
100                     e.printStackTrace();
101                 }
102             }
103         });
104
105         contextMenu.add(item);
106         return contextMenu;
107     }
108     
109     public void addAll(List JavaDoc<ISearchResult> result) {
110         Iterator JavaDoc<ISearchResult> it = result.iterator();
111         while (it.hasNext()) {
112             listModel.addElement(it.next());
113         }
114     }
115
116     public void add(ISearchResult result) {
117         listModel.addElement(result);
118     }
119
120     public void clear() {
121         listModel.clear();
122     }
123
124
125     class MyMouseListener extends MouseAdapter JavaDoc {
126
127         @Override JavaDoc
128         public void mouseClicked(MouseEvent JavaDoc e) {
129             handleEvent(e);
130         }
131
132         @Override JavaDoc
133         public void mousePressed(MouseEvent JavaDoc e) {
134             handlePopupEvent(e);
135         }
136
137         @Override JavaDoc
138         public void mouseReleased(MouseEvent JavaDoc e) {
139             handlePopupEvent(e);
140         }
141
142         /**
143          * @param e
144          */

145         private void handlePopupEvent(MouseEvent JavaDoc e) {
146             Point JavaDoc p = e.getPoint();
147             if (e.isPopupTrigger()) {
148                 // check if a single entry is selected
149
if (getSelectedIndices().length <= 1) {
150                     // select new item
151
int index = locationToIndex(p);
152                     setSelectedIndex(index);
153                 }
154                 // show context menu
155
getPopupMenu().show(e.getComponent(), p.x, p.y);
156             }
157         }
158
159         /**
160          * @param e
161          */

162         private void handleEvent(MouseEvent JavaDoc e) {
163         }
164     }
165     
166     class MyListCellRenderer extends JPanel JavaDoc implements ListCellRenderer JavaDoc {
167
168         private JLabel JavaDoc iconLabel = new JLabel JavaDoc();
169
170         private JLabel JavaDoc titleLabel = new JLabel JavaDoc();
171
172         private JXHyperlink descriptionLabel = new JXHyperlink();
173
174         private JPanel JavaDoc centerPanel;
175
176         private Border JavaDoc lineBorder = new HeaderSeparatorBorder(new Color JavaDoc(230,
177                 230, 230));
178
179         MyListCellRenderer() {
180             setLayout(new BorderLayout JavaDoc());
181
182             centerPanel = new JPanel JavaDoc();
183             centerPanel.setLayout(new BorderLayout JavaDoc());
184
185             centerPanel.add(titleLabel, BorderLayout.NORTH);
186             centerPanel.add(descriptionLabel, BorderLayout.CENTER);
187             add(iconLabel, BorderLayout.WEST);
188             add(centerPanel, BorderLayout.CENTER);
189
190             setBorder(BorderFactory.createCompoundBorder(lineBorder,
191                     BorderFactory.createEmptyBorder(2, 2, 2, 2)));
192             iconLabel.setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 8));
193
194             centerPanel.setOpaque(false);
195             setOpaque(true);
196
197         }
198
199         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value,
200                 int index, boolean isSelected, boolean cellHasFocus) {
201
202             if (isSelected) {
203                  setBackground(list.getSelectionBackground());
204                  setForeground(list.getSelectionForeground());
205
206             } else {
207                 setBackground(list.getBackground());
208                 setForeground(list.getForeground());
209
210             }
211
212             ISearchResult result = (ISearchResult) value;
213
214             titleLabel.setText(result.getTitle());
215             iconLabel.setIcon(ImageLoader.getSmallIcon(IconKeys.USER));
216             descriptionLabel.setText(result.getDescription());
217
218             return this;
219         }
220
221     }
222
223     class HeaderSeparatorBorder extends AbstractBorder JavaDoc {
224
225         protected Color JavaDoc color;
226
227         public HeaderSeparatorBorder(Color JavaDoc color) {
228             super();
229
230             this.color = color;
231         }
232
233         /**
234          * Paints the border for the specified component with the specified
235          * position and size.
236          *
237          * @param c
238          * the component for which this border is being painted
239          * @param g
240          * the paint graphics
241          * @param x
242          * the x position of the painted border
243          * @param y
244          * the y position of the painted border
245          * @param width
246          * the width of the painted border
247          * @param height
248          * the height of the painted border
249          */

250         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
251                 int width, int height) {
252             Color JavaDoc oldColor = g.getColor();
253             g.setColor(color);
254             g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);
255
256             g.setColor(oldColor);
257         }
258
259         /**
260          * Returns the insets of the border.
261          *
262          * @param c
263          * the component for which this border insets value applies
264          */

265         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
266             return new Insets JavaDoc(0, 0, 1, 0);
267         }
268
269         /**
270          * Reinitialize the insets parameter with this Border's current Insets.
271          *
272          * @param c
273          * the component for which this border insets value applies
274          * @param insets
275          * the object to be reinitialized
276          */

277         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
278             insets.left = insets.top = insets.right = insets.bottom = 1;
279             return insets;
280         }
281
282     }
283 }
284
Popular Tags