KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > tagging > TagList


1 package org.columba.core.gui.tagging;
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.Graphics2D JavaDoc;
8 import java.awt.Insets JavaDoc;
9 import java.awt.Point JavaDoc;
10 import java.awt.event.MouseAdapter JavaDoc;
11 import java.awt.event.MouseEvent JavaDoc;
12 import java.awt.image.BufferedImage JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 import javax.swing.BorderFactory JavaDoc;
17 import javax.swing.DefaultListModel JavaDoc;
18 import javax.swing.Icon JavaDoc;
19 import javax.swing.ImageIcon JavaDoc;
20 import javax.swing.JLabel JavaDoc;
21 import javax.swing.JList 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.core.tagging.TagManager;
29 import org.columba.core.tagging.api.ITag;
30 import org.columba.core.tagging.api.ITagEvent;
31 import org.columba.core.tagging.api.ITagListener;
32 import org.jdesktop.swingx.JXList;
33 import org.jdesktop.swingx.decorator.Highlighter;
34 import org.jdesktop.swingx.decorator.HighlighterPipeline;
35 import org.jdesktop.swingx.decorator.RolloverHighlighter;
36
37 public class TagList extends JXList {
38
39     private DefaultListModel JavaDoc listModel;
40
41     private JPopupMenu JavaDoc popup;
42     
43     public TagList() {
44         super();
45
46         // fill list model with tags
47
listModel = new DefaultListModel JavaDoc();
48         Iterator JavaDoc<ITag> it = TagManager.getInstance().getAllTags();
49         while (it.hasNext()) {
50             ITag tag = it.next();
51             listModel.addElement(tag);
52         }
53         setModel(listModel);
54
55         // replace with "MyListCellRenderer" for simple one-line renderer
56
// replace with "MyComplexListCellRenderer" for an additional description line
57
setCellRenderer(new MyListCellRenderer());
58
59         setBorder(null);
60         setHighlighters(new HighlighterPipeline(
61                 new Highlighter[] { new RolloverHighlighter(new Color JavaDoc(248, 248,
62                         248), Color.white) }));
63         setRolloverEnabled(true);
64         
65         // popup menu
66
addMouseListener(new MyMouseListener());
67         
68         // update tag list if mode changes
69
TagManager.getInstance().addTagListener(new MyTagListener());
70     }
71
72     public void setPopupMenu(JPopupMenu JavaDoc popup) {
73         this.popup = popup;
74     }
75     
76     class MyMouseListener extends MouseAdapter JavaDoc {
77
78         @Override JavaDoc
79         public void mouseClicked(MouseEvent JavaDoc e) {
80             handleEvent(e);
81         }
82
83         @Override JavaDoc
84         public void mousePressed(MouseEvent JavaDoc e) {
85             handlePopupEvent(e);
86         }
87
88         @Override JavaDoc
89         public void mouseReleased(MouseEvent JavaDoc e) {
90             handlePopupEvent(e);
91         }
92
93         /**
94          * @param e
95          */

96         private void handlePopupEvent(MouseEvent JavaDoc e) {
97             Point JavaDoc p = e.getPoint();
98             if (e.isPopupTrigger()) {
99                 // check if a single entry is selected
100
if (getSelectedIndices().length <= 1) {
101                     // select new item
102
int index = locationToIndex(p);
103                     setSelectedIndex(index);
104                 }
105                 // show context menu
106
popup.show(e.getComponent(), p.x, p.y);
107             }
108         }
109
110         /**
111          * @param e
112          */

113         private void handleEvent(MouseEvent JavaDoc e) {
114         }
115     }
116     
117     
118     public ITag getSelectedTag() {
119         return (ITag) getSelectedValue();
120     }
121
122     public Iterator JavaDoc<ITag> getSelectedTags() {
123         Object JavaDoc[] values = getSelectedValues();
124         Vector JavaDoc<ITag> v = new Vector JavaDoc<ITag>();
125
126         for (Object JavaDoc o : values) {
127             v.add((ITag) o);
128         }
129
130         return v.iterator();
131     }
132
133     class MyListCellRenderer extends JPanel JavaDoc implements ListCellRenderer JavaDoc {
134
135         private Border JavaDoc lineBorder = new HeaderSeparatorBorder(new Color JavaDoc(230,
136                 230, 230));
137
138         private JLabel JavaDoc nameLabel = new JLabel JavaDoc();
139
140         MyListCellRenderer() {
141             setLayout(new BorderLayout JavaDoc());
142
143             add(nameLabel, BorderLayout.WEST);
144
145             setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
146
147             setOpaque(true);
148
149         }
150
151         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value,
152                 int index, boolean isSelected, boolean cellHasFocus) {
153
154             if (isSelected) {
155                 setBackground(list.getSelectionBackground());
156                 setForeground(list.getSelectionForeground());
157             } else {
158                 setBackground(list.getBackground());
159                 setForeground(list.getForeground());
160             }
161
162             ITag result = (ITag) value;
163
164             nameLabel.setText(result.getName());
165             if (result.getColor() != null)
166                 nameLabel.setIcon(createIcon(result.getColor()));
167             else
168                 nameLabel.setIcon(createIcon(list.getBackground()));
169
170             setToolTipText(result.getDescription());
171             return this;
172         }
173
174     }
175
176     class MyComplexListCellRenderer extends JPanel JavaDoc implements ListCellRenderer JavaDoc {
177
178         private Border JavaDoc lineBorder = new HeaderSeparatorBorder(new Color JavaDoc(230,
179                 230, 230));
180
181         private JLabel JavaDoc nameLabel = new JLabel JavaDoc();
182
183         private JLabel JavaDoc descriptionLabel = new JLabel JavaDoc();
184
185         private JLabel JavaDoc iconLabel = new JLabel JavaDoc();
186
187         private JPanel JavaDoc centerPanel;
188
189         MyComplexListCellRenderer() {
190
191             descriptionLabel.setForeground(new Color JavaDoc(100, 100, 100));
192             // descriptionLabel.setFont(descriptionLabel.getFont().deriveFont(Font.ITALIC));
193

194             setLayout(new BorderLayout JavaDoc());
195
196             centerPanel = new JPanel JavaDoc();
197             centerPanel.setLayout(new BorderLayout JavaDoc());
198
199             centerPanel.add(nameLabel, BorderLayout.NORTH);
200             centerPanel.add(descriptionLabel, BorderLayout.CENTER);
201             add(iconLabel, BorderLayout.WEST);
202             add(centerPanel, BorderLayout.CENTER);
203
204             setBorder(BorderFactory.createCompoundBorder(lineBorder,
205                     BorderFactory.createEmptyBorder(2, 2, 2, 2)));
206             iconLabel.setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 8));
207
208             centerPanel.setOpaque(false);
209             setOpaque(true);
210
211         }
212
213         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value,
214                 int index, boolean isSelected, boolean cellHasFocus) {
215
216             if (isSelected) {
217                 setBackground(list.getSelectionBackground());
218                 setForeground(list.getSelectionForeground());
219             } else {
220                 setBackground(list.getBackground());
221                 setForeground(list.getForeground());
222             }
223
224             ITag result = (ITag) value;
225
226             nameLabel.setText(result.getName());
227
228             if (result.getColor() != null)
229                 iconLabel.setIcon(createIcon(result.getColor()));
230             else
231                 iconLabel.setIcon(createIcon(list.getBackground()));
232
233             if ( result.getDescription() != null)
234                 descriptionLabel.setText(result.getDescription());
235             else
236                 descriptionLabel.setText("");
237             
238             setToolTipText(result.getDescription());
239             return this;
240         }
241
242     }
243
244     class HeaderSeparatorBorder extends AbstractBorder JavaDoc {
245
246         protected Color JavaDoc color;
247
248         public HeaderSeparatorBorder(Color JavaDoc color) {
249             super();
250
251             this.color = color;
252         }
253
254         /**
255          * Paints the border for the specified component with the specified
256          * position and size.
257          *
258          * @param c
259          * the component for which this border is being painted
260          * @param g
261          * the paint graphics
262          * @param x
263          * the x position of the painted border
264          * @param y
265          * the y position of the painted border
266          * @param width
267          * the width of the painted border
268          * @param height
269          * the height of the painted border
270          */

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

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

298         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
299             insets.left = insets.top = insets.right = insets.bottom = 1;
300             return insets;
301         }
302
303     }
304
305     class MyTagListener implements ITagListener {
306         MyTagListener() {
307         }
308
309         public void tagChanged(ITagEvent event) {
310             String JavaDoc tagId = event.getId();
311             updateList();
312         }
313
314         public void tagAdded(ITagEvent event) {
315             String JavaDoc tagId = event.getId();
316             updateList();
317         }
318
319         public void tagDeleted(ITagEvent event) {
320             String JavaDoc tagId = event.getId();
321             updateList();
322         }
323
324         // real stupid recreation of whole list model
325
// -> replace with id-based listmodel update
326
private void updateList() {
327             listModel = new DefaultListModel JavaDoc();
328             Iterator JavaDoc<ITag> it = TagManager.getInstance().getAllTags();
329             while (it.hasNext()) {
330                 ITag tag = it.next();
331                 listModel.addElement(tag);
332             }
333             int index = getSelectedIndex();
334             clearSelection();
335             setModel(listModel);
336             if (index != -1)
337                 setSelectedIndex(index);
338         }
339     }
340
341     private Icon JavaDoc createIcon(Color JavaDoc color) {
342         int width = 16;
343         int height = 16;
344         BufferedImage JavaDoc image = new BufferedImage JavaDoc(width, height,
345                 BufferedImage.TYPE_INT_ARGB);
346
347         Graphics2D JavaDoc graphics = (Graphics2D JavaDoc) image.getGraphics();
348         graphics.setColor(darker(color));
349         graphics.drawRect(1, 1, width - 3, height - 3);
350         graphics.setColor(color);
351         graphics.fillRect(2, 2, width - 4, height - 4);
352         graphics.dispose();
353
354         return new ImageIcon JavaDoc(image);
355     }
356
357     private final static double FACTOR = 0.90;
358
359     private Color JavaDoc darker(Color JavaDoc c) {
360         return new Color JavaDoc(Math.max((int) (c.getRed() * FACTOR), 0), Math.max(
361                 (int) (c.getGreen() * FACTOR), 0), Math.max(
362                 (int) (c.getBlue() * FACTOR), 0));
363     }
364
365 }
366
Popular Tags