001: package org.columba.mail.gui.search;
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.awt.Point;
009: import java.awt.event.ActionEvent;
010: import java.awt.event.ActionListener;
011: import java.awt.event.MouseAdapter;
012: import java.awt.event.MouseEvent;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import javax.swing.BorderFactory;
017: import javax.swing.DefaultListModel;
018: import javax.swing.ImageIcon;
019: import javax.swing.JLabel;
020: import javax.swing.JList;
021: import javax.swing.JMenuItem;
022: import javax.swing.JPanel;
023: import javax.swing.JPopupMenu;
024: import javax.swing.ListCellRenderer;
025: import javax.swing.border.AbstractBorder;
026: import javax.swing.border.Border;
027:
028: import org.columba.api.exception.ServiceNotFoundException;
029: import org.columba.core.facade.ServiceFacadeRegistry;
030: import org.columba.core.gui.base.DoubleClickListener;
031: import org.columba.core.gui.base.EmptyIcon;
032: import org.columba.core.search.api.ISearchResult;
033: import org.columba.mail.facade.IDialogFacade;
034: import org.columba.mail.resourceloader.MailImageLoader;
035: import org.columba.mail.search.MailSearchResult;
036: import org.jdesktop.swingx.JXList;
037: import org.jdesktop.swingx.decorator.Highlighter;
038: import org.jdesktop.swingx.decorator.HighlighterPipeline;
039: import org.jdesktop.swingx.decorator.RolloverHighlighter;
040:
041: public class ResultList extends JXList {
042:
043: private DefaultListModel listModel;
044:
045: private JPopupMenu contextMenu;
046:
047: public ResultList() {
048: listModel = new DefaultListModel();
049: setModel(listModel);
050: setCellRenderer(new MyListCellRenderer());
051:
052: setBorder(null);
053: setHighlighters(new HighlighterPipeline(
054: new Highlighter[] { new RolloverHighlighter(new Color(
055: 248, 248, 248), Color.white) }));
056: setRolloverEnabled(true);
057:
058: addMouseListener(new DoubleClickListener() {
059:
060: @Override
061: public void doubleClick(MouseEvent event) {
062: ISearchResult result = (ISearchResult) getSelectedValue();
063:
064: try {
065: IDialogFacade facade = (IDialogFacade) ServiceFacadeRegistry
066: .getInstance().getService(
067: IDialogFacade.class);
068: facade.openMessage(result.getLocation());
069: } catch (ServiceNotFoundException e) {
070: e.printStackTrace();
071: }
072:
073: }
074: });
075:
076: add(getPopupMenu());
077: addMouseListener(new MyMouseListener());
078:
079: }
080:
081: public void addAll(List<ISearchResult> result) {
082: Iterator<ISearchResult> it = result.iterator();
083: while (it.hasNext()) {
084: listModel.addElement(it.next());
085: }
086: }
087:
088: public void add(ISearchResult result) {
089: listModel.addElement(result);
090: }
091:
092: public void clear() {
093: listModel.clear();
094: }
095:
096: class MyListCellRenderer extends JPanel implements ListCellRenderer {
097:
098: private JPanel centerPanel;
099:
100: private JPanel topPanel;
101:
102: private Border lineBorder = new HeaderSeparatorBorder(
103: new Color(230, 230, 230));
104:
105: private JLabel statusLabel = new JLabel();
106:
107: private JLabel fromLabel = new JLabel();
108:
109: private JLabel dateLabel = new JLabel();
110:
111: private JLabel subjectLabel = new JLabel();
112:
113: private JLabel flagLabel = new JLabel();
114:
115: private ImageIcon flagIcon = MailImageLoader
116: .getSmallIcon("flag.png");
117:
118: MyListCellRenderer() {
119: setLayout(new BorderLayout());
120:
121: topPanel = new JPanel();
122: topPanel.setLayout(new BorderLayout());
123: topPanel.add(fromLabel, BorderLayout.CENTER);
124: topPanel.add(dateLabel, BorderLayout.EAST);
125:
126: centerPanel = new JPanel();
127: centerPanel.setLayout(new BorderLayout());
128: centerPanel.add(topPanel, BorderLayout.NORTH);
129: centerPanel.add(subjectLabel, BorderLayout.CENTER);
130:
131: add(statusLabel, BorderLayout.WEST);
132: add(centerPanel, BorderLayout.CENTER);
133: add(flagLabel, BorderLayout.EAST);
134:
135: setBorder(BorderFactory.createCompoundBorder(lineBorder,
136: BorderFactory.createEmptyBorder(2, 2, 2, 2)));
137:
138: statusLabel.setBorder(BorderFactory.createEmptyBorder(2, 4,
139: 2, 8));
140: flagLabel.setBorder(BorderFactory.createEmptyBorder(2, 4,
141: 2, 4));
142:
143: topPanel.setOpaque(false);
144: centerPanel.setOpaque(false);
145: setOpaque(true);
146:
147: }
148:
149: public Component getListCellRendererComponent(JList list,
150: Object value, int index, boolean isSelected,
151: boolean cellHasFocus) {
152:
153: if (isSelected) {
154: setBackground(list.getSelectionBackground());
155: setForeground(list.getSelectionForeground());
156: } else {
157: setBackground(list.getBackground());
158: setForeground(list.getForeground());
159: }
160:
161: MailSearchResult result = (MailSearchResult) value;
162:
163: statusLabel.setIcon(result.getStatusIcon());
164: subjectLabel.setText(result.getTitle());
165: fromLabel.setText(result.getFrom().getShortAddress());
166: dateLabel.setText(result.getStringDate());
167:
168: if (result.isFlagged())
169: flagLabel.setIcon(flagIcon);
170: else
171: flagLabel.setIcon(new EmptyIcon());
172:
173: return this ;
174: }
175:
176: }
177:
178: class HeaderSeparatorBorder extends AbstractBorder {
179:
180: protected Color color;
181:
182: public HeaderSeparatorBorder(Color color) {
183: super ();
184:
185: this .color = color;
186: }
187:
188: /**
189: * Paints the border for the specified component with the specified
190: * position and size.
191: *
192: * @param c
193: * the component for which this border is being painted
194: * @param g
195: * the paint graphics
196: * @param x
197: * the x position of the painted border
198: * @param y
199: * the y position of the painted border
200: * @param width
201: * the width of the painted border
202: * @param height
203: * the height of the painted border
204: */
205: public void paintBorder(Component c, Graphics g, int x, int y,
206: int width, int height) {
207: Color oldColor = g.getColor();
208: g.setColor(color);
209: g
210: .drawLine(x, y + height - 1, x + width - 1, y
211: + height - 1);
212:
213: g.setColor(oldColor);
214: }
215:
216: /**
217: * Returns the insets of the border.
218: *
219: * @param c
220: * the component for which this border insets value applies
221: */
222: public Insets getBorderInsets(Component c) {
223: return new Insets(0, 0, 1, 0);
224: }
225:
226: /**
227: * Reinitialize the insets parameter with this Border's current Insets.
228: *
229: * @param c
230: * the component for which this border insets value applies
231: * @param insets
232: * the object to be reinitialized
233: */
234: public Insets getBorderInsets(Component c, Insets insets) {
235: insets.left = insets.top = insets.right = insets.bottom = 1;
236: return insets;
237: }
238:
239: }
240:
241: private JPopupMenu getPopupMenu() {
242: if (contextMenu != null)
243: return contextMenu;
244:
245: contextMenu = new JPopupMenu();
246:
247: JMenuItem item = new JMenuItem("Open..");
248: item.addActionListener(new ActionListener() {
249: public void actionPerformed(ActionEvent event) {
250: ISearchResult result = (ISearchResult) getSelectedValue();
251:
252: try {
253: IDialogFacade facade = (IDialogFacade) ServiceFacadeRegistry
254: .getInstance().getService(
255: IDialogFacade.class);
256: facade.openMessage(result.getLocation());
257: } catch (ServiceNotFoundException e) {
258: e.printStackTrace();
259: }
260: }
261: });
262: contextMenu.add(item);
263:
264: return contextMenu;
265: }
266:
267: class MyMouseListener extends MouseAdapter {
268:
269: @Override
270: public void mouseClicked(MouseEvent e) {
271: handleEvent(e);
272: }
273:
274: @Override
275: public void mousePressed(MouseEvent e) {
276: handlePopupEvent(e);
277: }
278:
279: @Override
280: public void mouseReleased(MouseEvent e) {
281: handlePopupEvent(e);
282: }
283:
284: /**
285: * @param e
286: */
287: private void handlePopupEvent(MouseEvent e) {
288: Point p = e.getPoint();
289: if (e.isPopupTrigger()) {
290: // check if a single entry is selected
291: if (getSelectedIndices().length <= 1) {
292: // select new item
293: int index = locationToIndex(p);
294: setSelectedIndex(index);
295: }
296: // show context menu
297: getPopupMenu().show(e.getComponent(), p.x, p.y);
298: }
299: }
300:
301: /**
302: * @param e
303: */
304: private void handleEvent(MouseEvent e) {
305: }
306: }
307:
308: }
|