KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > ui > search > SearchResultList


1 package org.columba.calendar.ui.search;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Color JavaDoc;
5 import java.awt.Component JavaDoc;
6 import java.awt.Font JavaDoc;
7 import java.awt.Graphics JavaDoc;
8 import java.awt.Insets JavaDoc;
9 import java.awt.Point JavaDoc;
10 import java.awt.event.ActionEvent JavaDoc;
11 import java.awt.event.ActionListener JavaDoc;
12 import java.awt.event.MouseAdapter JavaDoc;
13 import java.awt.event.MouseEvent JavaDoc;
14 import java.net.URI JavaDoc;
15 import java.text.DateFormat JavaDoc;
16 import java.util.Calendar JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import javax.swing.BorderFactory JavaDoc;
21 import javax.swing.DefaultListModel JavaDoc;
22 import javax.swing.JLabel JavaDoc;
23 import javax.swing.JList JavaDoc;
24 import javax.swing.JMenuItem JavaDoc;
25 import javax.swing.JPanel JavaDoc;
26 import javax.swing.JPopupMenu JavaDoc;
27 import javax.swing.ListCellRenderer JavaDoc;
28 import javax.swing.border.AbstractBorder JavaDoc;
29 import javax.swing.border.Border JavaDoc;
30
31 import org.columba.calendar.facade.DialogFacade;
32 import org.columba.calendar.model.api.IComponent;
33 import org.columba.calendar.model.api.IEvent;
34 import org.columba.calendar.model.api.ITodo;
35 import org.columba.calendar.resourceloader.IconKeys;
36 import org.columba.calendar.resourceloader.ResourceLoader;
37 import org.columba.calendar.search.CalendarSearchResult;
38 import org.columba.core.gui.base.DoubleClickListener;
39 import org.columba.core.search.api.ISearchResult;
40 import org.jdesktop.swingx.JXList;
41 import org.jdesktop.swingx.decorator.Highlighter;
42 import org.jdesktop.swingx.decorator.HighlighterPipeline;
43 import org.jdesktop.swingx.decorator.RolloverHighlighter;
44
45 public class SearchResultList extends JXList {
46
47     private DefaultListModel JavaDoc listModel;
48
49     private JPopupMenu JavaDoc contextMenu;
50
51     public SearchResultList() {
52         super();
53
54         listModel = new DefaultListModel JavaDoc();
55         setModel(listModel);
56         setCellRenderer(new MyListCellRenderer());
57
58         setBorder(null);
59         setHighlighters(new HighlighterPipeline(
60                 new Highlighter[] { new RolloverHighlighter(new Color JavaDoc(248, 248,
61                         248), Color.white) }));
62         setRolloverEnabled(true);
63
64         addMouseListener(new DoubleClickListener() {
65             @Override JavaDoc
66             public void doubleClick(MouseEvent JavaDoc event) {
67                 ISearchResult result = (ISearchResult) getSelectedValue();
68                 URI JavaDoc uri = result.getLocation();
69                 new DialogFacade().openEventEditorDialog(uri);
70             }
71         });
72
73         add(getPopupMenu());
74         addMouseListener(new MyMouseListener());
75     }
76
77     private JPopupMenu JavaDoc getPopupMenu() {
78         if (contextMenu != null)
79             return contextMenu;
80
81         contextMenu = new JPopupMenu JavaDoc();
82
83         JMenuItem JavaDoc item = new JMenuItem JavaDoc("Open..");
84         item.addActionListener(new ActionListener JavaDoc() {
85             public void actionPerformed(ActionEvent JavaDoc event) {
86                 ISearchResult result = (ISearchResult) getSelectedValue();
87                 URI JavaDoc uri = result.getLocation();
88                 new DialogFacade().openEventEditorDialog(uri);
89             }
90         });
91
92         contextMenu.add(item);
93         return contextMenu;
94     }
95
96     public void addAll(List JavaDoc<ISearchResult> result) {
97         Iterator JavaDoc<ISearchResult> it = result.iterator();
98         while (it.hasNext()) {
99             listModel.addElement(it.next());
100         }
101     }
102
103     public void add(ISearchResult result) {
104         listModel.addElement(result);
105     }
106
107     public void clear() {
108         listModel.clear();
109     }
110
111     class MyListCellRenderer extends JPanel JavaDoc implements ListCellRenderer JavaDoc {
112
113         private JLabel JavaDoc iconLabel = new JLabel JavaDoc();
114
115         private JLabel JavaDoc titleLabel = new JLabel JavaDoc();
116
117         private JLabel JavaDoc startDateLabel = new JLabel JavaDoc();
118
119         private JLabel JavaDoc descriptionLabel = new JLabel JavaDoc();
120
121         private JPanel JavaDoc centerPanel;
122
123         private Border JavaDoc lineBorder = new HeaderSeparatorBorder(new Color JavaDoc(230,
124                 230, 230));
125
126         private DateFormat JavaDoc df = DateFormat.getDateTimeInstance();
127
128         MyListCellRenderer() {
129             setLayout(new BorderLayout JavaDoc());
130
131             centerPanel = new JPanel JavaDoc();
132             centerPanel.setLayout(new BorderLayout JavaDoc());
133
134             JPanel JavaDoc titlePanel = new JPanel JavaDoc();
135             titlePanel.setLayout(new BorderLayout JavaDoc());
136             titlePanel.add(titleLabel, BorderLayout.WEST);
137             titlePanel.add(startDateLabel, BorderLayout.EAST);
138
139             centerPanel.add(titlePanel, BorderLayout.NORTH);
140             centerPanel.add(descriptionLabel, BorderLayout.CENTER);
141             centerPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 2));
142             add(iconLabel, BorderLayout.WEST);
143             add(centerPanel, BorderLayout.CENTER);
144
145             descriptionLabel.setFont(descriptionLabel.getFont().deriveFont(
146                     Font.ITALIC));
147
148             setBorder(BorderFactory.createCompoundBorder(lineBorder,
149                     BorderFactory.createEmptyBorder(2, 2, 2, 2)));
150             iconLabel.setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 8));
151
152             titlePanel.setOpaque(false);
153             centerPanel.setOpaque(false);
154             setOpaque(true);
155
156         }
157
158         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value,
159                 int index, boolean isSelected, boolean cellHasFocus) {
160
161             if (isSelected) {
162                 setBackground(list.getSelectionBackground());
163                 setForeground(list.getSelectionForeground());
164
165             } else {
166                 setBackground(list.getBackground());
167                 setForeground(list.getForeground());
168
169             }
170
171             CalendarSearchResult result = (CalendarSearchResult) value;
172
173             titleLabel.setText(result.getTitle());
174             iconLabel.setIcon(ResourceLoader
175                     .getSmallIcon(IconKeys.NEW_APPOINTMENT));
176             if (result.getDescription() != null
177                     && result.getDescription().length() > 0)
178                 descriptionLabel.setText(result.getDescription());
179             else
180                 descriptionLabel.setText("no Description specified");
181
182             IComponent c = result.getModel();
183             if (c.getType().equals(IComponent.TYPE.EVENT)) {
184                 IEvent event = (IEvent) c;
185                 Calendar JavaDoc startDate = event.getDtStart();
186
187                 startDateLabel.setText(df.format(startDate.getTime()));
188             } else if (c.getType().equals(IComponent.TYPE.TODO)) {
189                 ITodo todo = (ITodo) c;
190                 Calendar JavaDoc startDate = todo.getDtStart();
191                 startDateLabel.setText(df.format(startDate.getTime()));
192             } else
193                 throw new IllegalArgumentException JavaDoc(
194                         "unsupported component type " + c.getType());
195
196             return this;
197         }
198
199     }
200
201     class MyMouseListener extends MouseAdapter JavaDoc {
202
203         @Override JavaDoc
204         public void mouseClicked(MouseEvent JavaDoc e) {
205             handleEvent(e);
206         }
207
208         @Override JavaDoc
209         public void mousePressed(MouseEvent JavaDoc e) {
210             handlePopupEvent(e);
211         }
212
213         @Override JavaDoc
214         public void mouseReleased(MouseEvent JavaDoc e) {
215             handlePopupEvent(e);
216         }
217
218         /**
219          * @param e
220          */

221         private void handlePopupEvent(MouseEvent JavaDoc e) {
222             Point JavaDoc p = e.getPoint();
223             if (e.isPopupTrigger()) {
224                 // check if a single entry is selected
225
if (getSelectedIndices().length <= 1) {
226                     // select new item
227
int index = locationToIndex(p);
228                     setSelectedIndex(index);
229                 }
230                 // show context menu
231
getPopupMenu().show(e.getComponent(), p.x, p.y);
232             }
233         }
234
235         /**
236          * @param e
237          */

238         private void handleEvent(MouseEvent JavaDoc e) {
239         }
240     }
241
242     class HeaderSeparatorBorder extends AbstractBorder JavaDoc {
243
244         protected Color JavaDoc color;
245
246         public HeaderSeparatorBorder(Color JavaDoc color) {
247             super();
248
249             this.color = color;
250         }
251
252         /**
253          * Paints the border for the specified component with the specified
254          * position and size.
255          *
256          * @param c
257          * the component for which this border is being painted
258          * @param g
259          * the paint graphics
260          * @param x
261          * the x position of the painted border
262          * @param y
263          * the y position of the painted border
264          * @param width
265          * the width of the painted border
266          * @param height
267          * the height of the painted border
268          */

269         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
270                 int width, int height) {
271             Color JavaDoc oldColor = g.getColor();
272             g.setColor(color);
273             g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);
274
275             g.setColor(oldColor);
276         }
277
278         /**
279          * Returns the insets of the border.
280          *
281          * @param c
282          * the component for which this border insets value applies
283          */

284         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
285             return new Insets JavaDoc(0, 0, 1, 0);
286         }
287
288         /**
289          * Reinitialize the insets parameter with this Border's current Insets.
290          *
291          * @param c
292          * the component for which this border insets value applies
293          * @param insets
294          * the object to be reinitialized
295          */

296         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
297             insets.left = insets.top = insets.right = insets.bottom = 1;
298             return insets;
299         }
300
301     }
302 }
303
Popular Tags