KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > search > ResultList


1 package org.columba.mail.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.ImageIcon JavaDoc;
19 import javax.swing.JLabel JavaDoc;
20 import javax.swing.JList JavaDoc;
21 import javax.swing.JMenuItem JavaDoc;
22 import javax.swing.JPanel JavaDoc;
23 import javax.swing.JPopupMenu JavaDoc;
24 import javax.swing.ListCellRenderer JavaDoc;
25 import javax.swing.border.AbstractBorder JavaDoc;
26 import javax.swing.border.Border JavaDoc;
27
28 import org.columba.api.exception.ServiceNotFoundException;
29 import org.columba.core.facade.ServiceFacadeRegistry;
30 import org.columba.core.gui.base.DoubleClickListener;
31 import org.columba.core.gui.base.EmptyIcon;
32 import org.columba.core.search.api.ISearchResult;
33 import org.columba.mail.facade.IDialogFacade;
34 import org.columba.mail.resourceloader.MailImageLoader;
35 import org.columba.mail.search.MailSearchResult;
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 ResultList extends JXList {
42
43     private DefaultListModel JavaDoc listModel;
44
45     private JPopupMenu JavaDoc contextMenu;
46
47     public ResultList() {
48         listModel = new DefaultListModel JavaDoc();
49         setModel(listModel);
50         setCellRenderer(new MyListCellRenderer());
51
52         setBorder(null);
53         setHighlighters(new HighlighterPipeline(
54                 new Highlighter[] { new RolloverHighlighter(new Color JavaDoc(248, 248,
55                         248), Color.white) }));
56         setRolloverEnabled(true);
57
58         addMouseListener(new DoubleClickListener() {
59
60             @Override JavaDoc
61             public void doubleClick(MouseEvent JavaDoc event) {
62                 ISearchResult result = (ISearchResult) getSelectedValue();
63
64                 try {
65                     IDialogFacade facade = (IDialogFacade) ServiceFacadeRegistry
66                             .getInstance().getService(IDialogFacade.class);
67                     facade.openMessage(result.getLocation());
68                 } catch (ServiceNotFoundException e) {
69                     e.printStackTrace();
70                 }
71
72             }
73         });
74
75         add(getPopupMenu());
76         addMouseListener(new MyMouseListener());
77
78     }
79
80     public void addAll(List JavaDoc<ISearchResult> result) {
81         Iterator JavaDoc<ISearchResult> it = result.iterator();
82         while (it.hasNext()) {
83             listModel.addElement(it.next());
84         }
85     }
86
87     public void add(ISearchResult result) {
88         listModel.addElement(result);
89     }
90
91     public void clear() {
92         listModel.clear();
93     }
94
95     class MyListCellRenderer extends JPanel JavaDoc implements ListCellRenderer JavaDoc {
96
97         private JPanel JavaDoc centerPanel;
98
99         private JPanel JavaDoc topPanel;
100
101         private Border JavaDoc lineBorder = new HeaderSeparatorBorder(new Color JavaDoc(230,
102                 230, 230));
103
104         private JLabel JavaDoc statusLabel = new JLabel JavaDoc();
105
106         private JLabel JavaDoc fromLabel = new JLabel JavaDoc();
107
108         private JLabel JavaDoc dateLabel = new JLabel JavaDoc();
109
110         private JLabel JavaDoc subjectLabel = new JLabel JavaDoc();
111
112         private JLabel JavaDoc flagLabel = new JLabel JavaDoc();
113
114         private ImageIcon JavaDoc flagIcon = MailImageLoader.getSmallIcon("flag.png");
115
116         MyListCellRenderer() {
117             setLayout(new BorderLayout JavaDoc());
118
119             topPanel = new JPanel JavaDoc();
120             topPanel.setLayout(new BorderLayout JavaDoc());
121             topPanel.add(fromLabel, BorderLayout.CENTER);
122             topPanel.add(dateLabel, BorderLayout.EAST);
123
124             centerPanel = new JPanel JavaDoc();
125             centerPanel.setLayout(new BorderLayout JavaDoc());
126             centerPanel.add(topPanel, BorderLayout.NORTH);
127             centerPanel.add(subjectLabel, BorderLayout.CENTER);
128
129             add(statusLabel, BorderLayout.WEST);
130             add(centerPanel, BorderLayout.CENTER);
131             add(flagLabel, BorderLayout.EAST);
132
133             setBorder(BorderFactory.createCompoundBorder(lineBorder,
134                     BorderFactory.createEmptyBorder(2, 2, 2, 2)));
135
136             statusLabel.setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 8));
137             flagLabel.setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4));
138
139             topPanel.setOpaque(false);
140             centerPanel.setOpaque(false);
141             setOpaque(true);
142
143         }
144
145         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value,
146                 int index, boolean isSelected, boolean cellHasFocus) {
147
148             if (isSelected) {
149                 setBackground(list.getSelectionBackground());
150                 setForeground(list.getSelectionForeground());
151             } else {
152                 setBackground(list.getBackground());
153                 setForeground(list.getForeground());
154             }
155
156             MailSearchResult result = (MailSearchResult) value;
157
158             statusLabel.setIcon(result.getStatusIcon());
159             subjectLabel.setText(result.getTitle());
160             fromLabel.setText(result.getFrom().getShortAddress());
161             dateLabel.setText(result.getStringDate());
162
163             if (result.isFlagged())
164                 flagLabel.setIcon(flagIcon);
165             else
166                 flagLabel.setIcon(new EmptyIcon());
167
168             return this;
169         }
170
171     }
172
173     class HeaderSeparatorBorder extends AbstractBorder JavaDoc {
174
175         protected Color JavaDoc color;
176
177         public HeaderSeparatorBorder(Color JavaDoc color) {
178             super();
179
180             this.color = color;
181         }
182
183         /**
184          * Paints the border for the specified component with the specified
185          * position and size.
186          *
187          * @param c
188          * the component for which this border is being painted
189          * @param g
190          * the paint graphics
191          * @param x
192          * the x position of the painted border
193          * @param y
194          * the y position of the painted border
195          * @param width
196          * the width of the painted border
197          * @param height
198          * the height of the painted border
199          */

200         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
201                 int width, int height) {
202             Color JavaDoc oldColor = g.getColor();
203             g.setColor(color);
204             g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);
205
206             g.setColor(oldColor);
207         }
208
209         /**
210          * Returns the insets of the border.
211          *
212          * @param c
213          * the component for which this border insets value applies
214          */

215         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
216             return new Insets JavaDoc(0, 0, 1, 0);
217         }
218
219         /**
220          * Reinitialize the insets parameter with this Border's current Insets.
221          *
222          * @param c
223          * the component for which this border insets value applies
224          * @param insets
225          * the object to be reinitialized
226          */

227         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
228             insets.left = insets.top = insets.right = insets.bottom = 1;
229             return insets;
230         }
231
232     }
233
234     private JPopupMenu JavaDoc getPopupMenu() {
235         if (contextMenu != null)
236             return contextMenu;
237
238         contextMenu = new JPopupMenu JavaDoc();
239
240         JMenuItem JavaDoc item = new JMenuItem JavaDoc("Open..");
241         item.addActionListener(new ActionListener JavaDoc() {
242             public void actionPerformed(ActionEvent JavaDoc event) {
243                 ISearchResult result = (ISearchResult) getSelectedValue();
244
245                 try {
246                     IDialogFacade facade = (IDialogFacade) ServiceFacadeRegistry
247                             .getInstance().getService(IDialogFacade.class);
248                     facade.openMessage(result.getLocation());
249                 } catch (ServiceNotFoundException e) {
250                     e.printStackTrace();
251                 }
252             }
253         });
254         contextMenu.add(item);
255
256         return contextMenu;
257     }
258
259     class MyMouseListener extends MouseAdapter JavaDoc {
260
261         @Override JavaDoc
262         public void mouseClicked(MouseEvent JavaDoc e) {
263             handleEvent(e);
264         }
265
266         @Override JavaDoc
267         public void mousePressed(MouseEvent JavaDoc e) {
268             handlePopupEvent(e);
269         }
270
271         @Override JavaDoc
272         public void mouseReleased(MouseEvent JavaDoc e) {
273             handlePopupEvent(e);
274         }
275
276         /**
277          * @param e
278          */

279         private void handlePopupEvent(MouseEvent JavaDoc e) {
280             Point JavaDoc p = e.getPoint();
281             if (e.isPopupTrigger()) {
282                 // check if a single entry is selected
283
if (getSelectedIndices().length <= 1) {
284                     // select new item
285
int index = locationToIndex(p);
286                     setSelectedIndex(index);
287                 }
288                 // show context menu
289
getPopupMenu().show(e.getComponent(), p.x, p.y);
290             }
291         }
292
293         /**
294          * @param e
295          */

296         private void handleEvent(MouseEvent JavaDoc e) {
297         }
298     }
299
300 }
301
Popular Tags