001: package org.columba.core.gui.search;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Component;
005: import java.awt.event.MouseEvent;
006: import java.util.Iterator;
007: import java.util.List;
008:
009: import javax.swing.BorderFactory;
010: import javax.swing.DefaultListModel;
011: import javax.swing.ImageIcon;
012: import javax.swing.JComponent;
013: import javax.swing.JLabel;
014: import javax.swing.JList;
015: import javax.swing.JPanel;
016: import javax.swing.ListCellRenderer;
017: import javax.swing.UIManager;
018:
019: import org.columba.core.gui.base.DoubleClickListener;
020: import org.columba.core.gui.search.api.IResultPanel;
021: import org.columba.core.resourceloader.IconKeys;
022: import org.columba.core.resourceloader.ImageLoader;
023: import org.columba.core.search.api.IResultEvent;
024: import org.columba.core.search.api.ISearchResult;
025: import org.jdesktop.swingx.JXHyperlink;
026: import org.jdesktop.swingx.JXList;
027: import org.jdesktop.swingx.decorator.AlternateRowHighlighter;
028: import org.jdesktop.swingx.decorator.Highlighter;
029: import org.jdesktop.swingx.decorator.HighlighterPipeline;
030:
031: public class GenericResultPanel extends JXList implements IResultPanel {
032:
033: private String providerNamespace;
034:
035: private DefaultListModel listModel;
036:
037: private String providerName;
038:
039: public GenericResultPanel(String providerName,
040: String providerNamespace) {
041: super ();
042:
043: this .providerName = providerName;
044: this .providerNamespace = providerNamespace;
045:
046: listModel = new DefaultListModel();
047: setModel(listModel);
048: setCellRenderer(new MyListCellRenderer());
049: setBorder(null);
050: setHighlighters(new HighlighterPipeline(
051: new Highlighter[] { AlternateRowHighlighter.genericGrey }));
052:
053: addMouseListener(new DoubleClickListener() {
054:
055: @Override
056: public void doubleClick(MouseEvent event) {
057: // do something with link
058: }
059: });
060: }
061:
062: public String getSearchCriteriaTechnicalName() {
063: return providerName;
064: }
065:
066: public String getProviderTechnicalName() {
067: return providerNamespace;
068: }
069:
070: public void resultArrived(IResultEvent event) {
071: if (!event.getProviderName().equals(this .providerName))
072: return;
073:
074: List<ISearchResult> result = event.getSearchResults();
075:
076: Iterator<ISearchResult> it = result.iterator();
077: while (it.hasNext()) {
078: listModel.addElement(it.next());
079: }
080:
081: // setPreferredSize(list.getPreferredSize());
082: revalidate();
083: }
084:
085: public void clearSearch(IResultEvent event) {
086: listModel.clear();
087: }
088:
089: class MyListCellRenderer extends JPanel implements ListCellRenderer {
090: // private JLabel iconLabel = new JLabel();
091:
092: private JXHyperlink titleLabel = new JXHyperlink();
093:
094: private JLabel descriptionLabel = new JLabel();
095:
096: private JPanel topPanel;
097:
098: MyListCellRenderer() {
099: setLayout(new BorderLayout());
100:
101: titleLabel.setClickedColor(UIManager
102: .getColor("Label.foreground"));
103: titleLabel.setUnclickedColor(UIManager
104: .getColor("Label.foreground"));
105: // titleLabel.setBackground(UIManager.getColor("Label.background"));
106: titleLabel.setOpaque(false);
107: titleLabel.setBorderPainted(true);
108:
109: topPanel = new JPanel();
110: topPanel.setLayout(new BorderLayout());
111:
112: // topPanel.add(iconLabel, BorderLayout.WEST);
113: topPanel.add(titleLabel, BorderLayout.CENTER);
114:
115: add(topPanel, BorderLayout.NORTH);
116: add(descriptionLabel, BorderLayout.CENTER);
117:
118: topPanel.setOpaque(true);
119: setOpaque(true);
120:
121: setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
122:
123: }
124:
125: public Component getListCellRendererComponent(JList list,
126: Object value, int index, boolean isSelected,
127: boolean cellHasFocus) {
128:
129: if (isSelected) {
130: setBackground(list.getSelectionBackground());
131: setForeground(list.getSelectionForeground());
132:
133: topPanel.setBackground(list.getSelectionBackground());
134: topPanel.setForeground(list.getSelectionForeground());
135: } else {
136: setBackground(list.getBackground());
137: setForeground(list.getForeground());
138: topPanel.setBackground(list.getBackground());
139: topPanel.setForeground(list.getForeground());
140: }
141:
142: ISearchResult result = (ISearchResult) value;
143:
144: titleLabel.setText(result.getTitle());
145:
146: ImageIcon icon = null;
147: if (providerNamespace.equals("org.columba.contact"))
148: icon = ImageLoader.getSmallIcon(IconKeys.USER);
149: else if (providerNamespace.equals("org.columba.mail"))
150: icon = ImageLoader.getSmallIcon(IconKeys.COMPUTER);
151:
152: titleLabel.setIcon(icon);
153: descriptionLabel.setText(result.getDescription());
154:
155: return this ;
156: }
157:
158: }
159:
160: public JComponent getView() {
161: return this ;
162: }
163:
164: public ImageIcon getIcon() {
165: return null;
166: }
167:
168: public String getTitle(String searchTerm) {
169: return providerName;
170: }
171:
172: public String getDescription(String searchTerm) {
173: return providerNamespace;
174: }
175:
176: public void reset(IResultEvent event) {
177: listModel.clear();
178: }
179:
180: public void finished(IResultEvent event) {
181: // TODO Auto-generated method stub
182:
183: }
184: }
|