KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > ui > box > CalendarList


1 package org.columba.calendar.ui.box;
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.text.DateFormat JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12
13 import javax.swing.BorderFactory JavaDoc;
14 import javax.swing.DefaultListModel JavaDoc;
15 import javax.swing.JLabel JavaDoc;
16 import javax.swing.JList JavaDoc;
17 import javax.swing.JPanel JavaDoc;
18 import javax.swing.JTextField JavaDoc;
19 import javax.swing.ListCellRenderer JavaDoc;
20 import javax.swing.ListModel JavaDoc;
21 import javax.swing.border.AbstractBorder JavaDoc;
22 import javax.swing.border.Border JavaDoc;
23
24 import org.columba.calendar.model.api.IEventInfo;
25 import org.columba.calendar.resourceloader.IconKeys;
26 import org.columba.calendar.resourceloader.ResourceLoader;
27 import org.jdesktop.swingx.JXList;
28 import org.jdesktop.swingx.decorator.Highlighter;
29 import org.jdesktop.swingx.decorator.HighlighterPipeline;
30 import org.jdesktop.swingx.decorator.RolloverHighlighter;
31
32 public class CalendarList extends JXList {
33
34     private DefaultListModel JavaDoc listModel;
35
36     public CalendarList() {
37         super();
38
39         setCellRenderer(new MyListCellRenderer());
40
41         setBorder(null);
42         setHighlighters(new HighlighterPipeline(
43                 new Highlighter[] { new RolloverHighlighter(new Color JavaDoc(248, 248,
44                         248), Color.white) }));
45         setRolloverEnabled(true);
46
47     }
48
49     public void addAll(List JavaDoc<IEventInfo> list) {
50         Iterator JavaDoc<IEventInfo> it = list.iterator();
51         while (it.hasNext()) {
52             addElement(it.next());
53         }
54     }
55
56     public void add(IEventInfo result) {
57         addElement(result);
58     }
59
60     /**
61      * ********************** filtering
62      * *********************************************
63      */

64
65     /**
66      * Associates filtering document listener to text component.
67      */

68
69     public void installJTextField(JTextField JavaDoc input) {
70         if (input != null) {
71             FilteringModel model = (FilteringModel) getModel();
72             input.getDocument().addDocumentListener(model);
73         }
74     }
75
76     /**
77      * Disassociates filtering document listener from text component.
78      */

79
80     public void uninstallJTextField(JTextField JavaDoc input) {
81         if (input != null) {
82             FilteringModel model = (FilteringModel) getModel();
83             input.getDocument().removeDocumentListener(model);
84         }
85     }
86
87     /**
88      * Doesn't let model change to non-filtering variety
89      */

90
91     public void setModel(ListModel JavaDoc model) {
92         if (!(model instanceof FilteringModel)) {
93             throw new IllegalArgumentException JavaDoc();
94         } else {
95             super.setModel(model);
96         }
97     }
98
99     /**
100      * Adds item to model of list
101      */

102     public void addElement(IEventInfo element) {
103         ((FilteringModel) getModel()).addElement(element);
104     }
105
106     class MyListCellRenderer extends JPanel JavaDoc implements ListCellRenderer JavaDoc {
107
108         private JLabel JavaDoc iconLabel = new JLabel JavaDoc();
109
110         private JLabel JavaDoc titleLabel = new JLabel JavaDoc();
111
112         private JLabel JavaDoc descriptionLabel = new JLabel JavaDoc();
113
114         private JPanel JavaDoc centerPanel;
115
116         private JLabel JavaDoc startDateLabel = new JLabel JavaDoc();
117
118         private Border JavaDoc lineBorder = new HeaderSeparatorBorder(new Color JavaDoc(230,
119                 230, 230));
120
121         private DateFormat JavaDoc df = DateFormat.getDateTimeInstance();
122
123         MyListCellRenderer() {
124             setLayout(new BorderLayout JavaDoc());
125
126             centerPanel = new JPanel JavaDoc();
127             centerPanel.setLayout(new BorderLayout JavaDoc());
128
129             JPanel JavaDoc titlePanel = new JPanel JavaDoc();
130             titlePanel.setLayout(new BorderLayout JavaDoc());
131             titlePanel.add(titleLabel, BorderLayout.WEST);
132             titlePanel.add(startDateLabel, BorderLayout.EAST);
133
134             centerPanel.add(titlePanel, BorderLayout.NORTH);
135             centerPanel.add(descriptionLabel, BorderLayout.CENTER);
136             centerPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 2));
137             add(iconLabel, BorderLayout.WEST);
138             add(centerPanel, BorderLayout.CENTER);
139
140             descriptionLabel.setFont(descriptionLabel.getFont().deriveFont(
141                     Font.ITALIC));
142
143             setBorder(BorderFactory.createCompoundBorder(lineBorder,
144                     BorderFactory.createEmptyBorder(2, 2, 2, 2)));
145             iconLabel.setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 8));
146
147             centerPanel.setOpaque(false);
148             titlePanel.setOpaque(false);
149             setOpaque(true);
150
151         }
152
153         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value,
154                 int index, boolean isSelected, boolean cellHasFocus) {
155
156             if (isSelected) {
157                 setBackground(list.getSelectionBackground());
158                 setForeground(list.getSelectionForeground());
159             } else {
160                 setBackground(list.getBackground());
161                 setForeground(list.getForeground());
162             }
163
164             IEventInfo result = (IEventInfo) value;
165
166             titleLabel.setText(result.getEvent().getSummary());
167             iconLabel.setIcon(ResourceLoader
168                     .getSmallIcon(IconKeys.NEW_APPOINTMENT));
169             if (result.getEvent().getLocation() != null
170                     && result.getEvent().getLocation().length() > 0)
171                 descriptionLabel.setText(result.getEvent().getLocation());
172             else
173                 descriptionLabel.setText("no Location specified");
174
175             startDateLabel.setText(df.format(result.getEvent().getDtStart().getTime()));
176             return this;
177         }
178
179     }
180
181     class HeaderSeparatorBorder extends AbstractBorder JavaDoc {
182
183         protected Color JavaDoc color;
184
185         public HeaderSeparatorBorder(Color JavaDoc color) {
186             super();
187
188             this.color = color;
189         }
190
191         /**
192          * Paints the border for the specified component with the specified
193          * position and size.
194          *
195          * @param c
196          * the component for which this border is being painted
197          * @param g
198          * the paint graphics
199          * @param x
200          * the x position of the painted border
201          * @param y
202          * the y position of the painted border
203          * @param width
204          * the width of the painted border
205          * @param height
206          * the height of the painted border
207          */

208         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
209                 int width, int height) {
210             Color JavaDoc oldColor = g.getColor();
211             g.setColor(color);
212             g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);
213
214             g.setColor(oldColor);
215         }
216
217         /**
218          * Returns the insets of the border.
219          *
220          * @param c
221          * the component for which this border insets value applies
222          */

223         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
224             return new Insets JavaDoc(0, 0, 1, 0);
225         }
226
227         /**
228          * Reinitialize the insets parameter with this Border's current Insets.
229          *
230          * @param c
231          * the component for which this border insets value applies
232          * @param insets
233          * the object to be reinitialized
234          */

235         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
236             insets.left = insets.top = insets.right = insets.bottom = 1;
237             return insets;
238         }
239
240     }
241
242 }
243
Popular Tags