001: package org.columba.calendar.ui.box;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Component;
006: import java.awt.Font;
007: import java.awt.Graphics;
008: import java.awt.Insets;
009: import java.text.DateFormat;
010: import java.util.Iterator;
011: import java.util.List;
012:
013: import javax.swing.BorderFactory;
014: import javax.swing.DefaultListModel;
015: import javax.swing.JLabel;
016: import javax.swing.JList;
017: import javax.swing.JPanel;
018: import javax.swing.JTextField;
019: import javax.swing.ListCellRenderer;
020: import javax.swing.ListModel;
021: import javax.swing.border.AbstractBorder;
022: import javax.swing.border.Border;
023:
024: import org.columba.calendar.model.api.IEventInfo;
025: import org.columba.calendar.resourceloader.IconKeys;
026: import org.columba.calendar.resourceloader.ResourceLoader;
027: import org.jdesktop.swingx.JXList;
028: import org.jdesktop.swingx.decorator.Highlighter;
029: import org.jdesktop.swingx.decorator.HighlighterPipeline;
030: import org.jdesktop.swingx.decorator.RolloverHighlighter;
031:
032: public class CalendarList extends JXList {
033:
034: private DefaultListModel listModel;
035:
036: public CalendarList() {
037: super ();
038:
039: setCellRenderer(new MyListCellRenderer());
040:
041: setBorder(null);
042: setHighlighters(new HighlighterPipeline(
043: new Highlighter[] { new RolloverHighlighter(new Color(
044: 248, 248, 248), Color.white) }));
045: setRolloverEnabled(true);
046:
047: }
048:
049: public void addAll(List<IEventInfo> list) {
050: Iterator<IEventInfo> it = list.iterator();
051: while (it.hasNext()) {
052: addElement(it.next());
053: }
054: }
055:
056: public void add(IEventInfo result) {
057: addElement(result);
058: }
059:
060: /**
061: * ********************** filtering
062: * *********************************************
063: */
064:
065: /**
066: * Associates filtering document listener to text component.
067: */
068:
069: public void installJTextField(JTextField input) {
070: if (input != null) {
071: FilteringModel model = (FilteringModel) getModel();
072: input.getDocument().addDocumentListener(model);
073: }
074: }
075:
076: /**
077: * Disassociates filtering document listener from text component.
078: */
079:
080: public void uninstallJTextField(JTextField input) {
081: if (input != null) {
082: FilteringModel model = (FilteringModel) getModel();
083: input.getDocument().removeDocumentListener(model);
084: }
085: }
086:
087: /**
088: * Doesn't let model change to non-filtering variety
089: */
090:
091: public void setModel(ListModel model) {
092: if (!(model instanceof FilteringModel)) {
093: throw new IllegalArgumentException();
094: } else {
095: super .setModel(model);
096: }
097: }
098:
099: /**
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 implements ListCellRenderer {
107:
108: private JLabel iconLabel = new JLabel();
109:
110: private JLabel titleLabel = new JLabel();
111:
112: private JLabel descriptionLabel = new JLabel();
113:
114: private JPanel centerPanel;
115:
116: private JLabel startDateLabel = new JLabel();
117:
118: private Border lineBorder = new HeaderSeparatorBorder(
119: new Color(230, 230, 230));
120:
121: private DateFormat df = DateFormat.getDateTimeInstance();
122:
123: MyListCellRenderer() {
124: setLayout(new BorderLayout());
125:
126: centerPanel = new JPanel();
127: centerPanel.setLayout(new BorderLayout());
128:
129: JPanel titlePanel = new JPanel();
130: titlePanel.setLayout(new BorderLayout());
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,
137: 0, 2));
138: add(iconLabel, BorderLayout.WEST);
139: add(centerPanel, BorderLayout.CENTER);
140:
141: descriptionLabel.setFont(descriptionLabel.getFont()
142: .deriveFont(Font.ITALIC));
143:
144: setBorder(BorderFactory.createCompoundBorder(lineBorder,
145: BorderFactory.createEmptyBorder(2, 2, 2, 2)));
146: iconLabel.setBorder(BorderFactory.createEmptyBorder(2, 4,
147: 2, 8));
148:
149: centerPanel.setOpaque(false);
150: titlePanel.setOpaque(false);
151: setOpaque(true);
152:
153: }
154:
155: public Component getListCellRendererComponent(JList list,
156: Object value, int index, boolean isSelected,
157: boolean cellHasFocus) {
158:
159: if (isSelected) {
160: setBackground(list.getSelectionBackground());
161: setForeground(list.getSelectionForeground());
162: } else {
163: setBackground(list.getBackground());
164: setForeground(list.getForeground());
165: }
166:
167: IEventInfo result = (IEventInfo) value;
168:
169: titleLabel.setText(result.getEvent().getSummary());
170: iconLabel.setIcon(ResourceLoader
171: .getSmallIcon(IconKeys.NEW_APPOINTMENT));
172: if (result.getEvent().getLocation() != null
173: && result.getEvent().getLocation().length() > 0)
174: descriptionLabel.setText(result.getEvent()
175: .getLocation());
176: else
177: descriptionLabel.setText("no Location specified");
178:
179: startDateLabel.setText(df.format(result.getEvent()
180: .getDtStart().getTime()));
181: return this ;
182: }
183:
184: }
185:
186: class HeaderSeparatorBorder extends AbstractBorder {
187:
188: protected Color color;
189:
190: public HeaderSeparatorBorder(Color color) {
191: super ();
192:
193: this .color = color;
194: }
195:
196: /**
197: * Paints the border for the specified component with the specified
198: * position and size.
199: *
200: * @param c
201: * the component for which this border is being painted
202: * @param g
203: * the paint graphics
204: * @param x
205: * the x position of the painted border
206: * @param y
207: * the y position of the painted border
208: * @param width
209: * the width of the painted border
210: * @param height
211: * the height of the painted border
212: */
213: public void paintBorder(Component c, Graphics g, int x, int y,
214: int width, int height) {
215: Color oldColor = g.getColor();
216: g.setColor(color);
217: g
218: .drawLine(x, y + height - 1, x + width - 1, y
219: + height - 1);
220:
221: g.setColor(oldColor);
222: }
223:
224: /**
225: * Returns the insets of the border.
226: *
227: * @param c
228: * the component for which this border insets value applies
229: */
230: public Insets getBorderInsets(Component c) {
231: return new Insets(0, 0, 1, 0);
232: }
233:
234: /**
235: * Reinitialize the insets parameter with this Border's current Insets.
236: *
237: * @param c
238: * the component for which this border insets value applies
239: * @param insets
240: * the object to be reinitialized
241: */
242: public Insets getBorderInsets(Component c, Insets insets) {
243: insets.left = insets.top = insets.right = insets.bottom = 1;
244: return insets;
245: }
246:
247: }
248:
249: }
|