1 package org.columba.mail.gui.search; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Color ; 5 import java.awt.Component ; 6 import java.awt.Graphics ; 7 import java.awt.Insets ; 8 import java.awt.Point ; 9 import java.awt.event.ActionEvent ; 10 import java.awt.event.ActionListener ; 11 import java.awt.event.MouseAdapter ; 12 import java.awt.event.MouseEvent ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 import javax.swing.BorderFactory ; 17 import javax.swing.DefaultListModel ; 18 import javax.swing.ImageIcon ; 19 import javax.swing.JLabel ; 20 import javax.swing.JList ; 21 import javax.swing.JMenuItem ; 22 import javax.swing.JPanel ; 23 import javax.swing.JPopupMenu ; 24 import javax.swing.ListCellRenderer ; 25 import javax.swing.border.AbstractBorder ; 26 import javax.swing.border.Border ; 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 listModel; 44 45 private JPopupMenu contextMenu; 46 47 public ResultList() { 48 listModel = new DefaultListModel (); 49 setModel(listModel); 50 setCellRenderer(new MyListCellRenderer()); 51 52 setBorder(null); 53 setHighlighters(new HighlighterPipeline( 54 new Highlighter[] { new RolloverHighlighter(new Color (248, 248, 55 248), Color.white) })); 56 setRolloverEnabled(true); 57 58 addMouseListener(new DoubleClickListener() { 59 60 @Override 61 public void doubleClick(MouseEvent 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 <ISearchResult> result) { 81 Iterator <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 implements ListCellRenderer { 96 97 private JPanel centerPanel; 98 99 private JPanel topPanel; 100 101 private Border lineBorder = new HeaderSeparatorBorder(new Color (230, 102 230, 230)); 103 104 private JLabel statusLabel = new JLabel (); 105 106 private JLabel fromLabel = new JLabel (); 107 108 private JLabel dateLabel = new JLabel (); 109 110 private JLabel subjectLabel = new JLabel (); 111 112 private JLabel flagLabel = new JLabel (); 113 114 private ImageIcon flagIcon = MailImageLoader.getSmallIcon("flag.png"); 115 116 MyListCellRenderer() { 117 setLayout(new BorderLayout ()); 118 119 topPanel = new JPanel (); 120 topPanel.setLayout(new BorderLayout ()); 121 topPanel.add(fromLabel, BorderLayout.CENTER); 122 topPanel.add(dateLabel, BorderLayout.EAST); 123 124 centerPanel = new JPanel (); 125 centerPanel.setLayout(new BorderLayout ()); 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 getListCellRendererComponent(JList list, Object 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 { 174 175 protected Color color; 176 177 public HeaderSeparatorBorder(Color color) { 178 super(); 179 180 this.color = color; 181 } 182 183 200 public void paintBorder(Component c, Graphics g, int x, int y, 201 int width, int height) { 202 Color 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 215 public Insets getBorderInsets(Component c) { 216 return new Insets (0, 0, 1, 0); 217 } 218 219 227 public Insets getBorderInsets(Component c, Insets insets) { 228 insets.left = insets.top = insets.right = insets.bottom = 1; 229 return insets; 230 } 231 232 } 233 234 private JPopupMenu getPopupMenu() { 235 if (contextMenu != null) 236 return contextMenu; 237 238 contextMenu = new JPopupMenu (); 239 240 JMenuItem item = new JMenuItem ("Open.."); 241 item.addActionListener(new ActionListener () { 242 public void actionPerformed(ActionEvent 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 { 260 261 @Override 262 public void mouseClicked(MouseEvent e) { 263 handleEvent(e); 264 } 265 266 @Override 267 public void mousePressed(MouseEvent e) { 268 handlePopupEvent(e); 269 } 270 271 @Override 272 public void mouseReleased(MouseEvent e) { 273 handlePopupEvent(e); 274 } 275 276 279 private void handlePopupEvent(MouseEvent e) { 280 Point p = e.getPoint(); 281 if (e.isPopupTrigger()) { 282 if (getSelectedIndices().length <= 1) { 284 int index = locationToIndex(p); 286 setSelectedIndex(index); 287 } 288 getPopupMenu().show(e.getComponent(), p.x, p.y); 290 } 291 } 292 293 296 private void handleEvent(MouseEvent e) { 297 } 298 } 299 300 } 301 | Popular Tags |