KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > calendar > gui > AttendeeTable


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.calendar.gui;
20
21 import java.awt.Dimension JavaDoc;
22 import java.awt.event.MouseEvent JavaDoc;
23 import java.awt.event.MouseListener JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Comparator JavaDoc;
27
28 import javax.swing.*;
29 import javax.swing.table.*;
30
31 import org.lucane.client.Plugin;
32 import org.lucane.applications.calendar.Attendee;
33 import org.lucane.applications.calendar.CalendarPlugin;
34
35 public class AttendeeTable extends JTable
36 implements MouseListener JavaDoc
37 {
38     private CalendarPlugin plugin;
39     
40     public AttendeeTable(CalendarPlugin plugin, String JavaDoc name, ArrayList JavaDoc attendees)
41     {
42         super(new AttendeeTableModel(plugin, attendees));
43
44         getColumnModel().getColumn(0).setMinWidth(16);
45         getColumnModel().getColumn(0).setMaxWidth(20);
46         getTableHeader().getColumnModel().getColumn(0).setHeaderValue("");
47         getTableHeader().getColumnModel().getColumn(1).setHeaderValue(name);
48         setRowSelectionAllowed(true);
49         setShowVerticalLines(false);
50         
51         this.plugin = plugin;
52         this.addMouseListener(this);
53     }
54
55     public AttendeeTable(CalendarPlugin plugin, String JavaDoc name)
56     {
57         this(plugin, name, new ArrayList JavaDoc());
58     }
59     
60     public void setAttendees(ArrayList JavaDoc list)
61     {
62         AttendeeTableModel model = (AttendeeTableModel)this.getModel();
63         model.setAttendees(list);
64     }
65     
66     public void addAttendee(Attendee a)
67     {
68         AttendeeTableModel model = (AttendeeTableModel)this.getModel();
69         model.addAttendee(a);
70     }
71     
72     public Attendee getAttendeeAt(int row)
73     {
74         return ((AttendeeTableModel)this.getModel()).getAttendeeAt(row);
75     }
76     
77     public ArrayList JavaDoc getAttendeeList()
78     {
79         return ((AttendeeTableModel)this.getModel()).geAttendeeList();
80     }
81     
82     public void removeSelected()
83     {
84         int[] rows = this.getSelectedRows();
85         for(int i=rows.length-1;i>= 0;i--)
86             ((AttendeeTableModel)this.getModel()).removeAt(rows[i]);
87     }
88
89     public void mouseEntered(MouseEvent JavaDoc e) {}
90     public void mouseExited(MouseEvent JavaDoc e) {}
91     public void mousePressed(MouseEvent JavaDoc e) {}
92     public void mouseReleased(MouseEvent JavaDoc e) {}
93     public void mouseClicked(MouseEvent JavaDoc e)
94     {
95         if(e.getClickCount() == 2)
96         {
97             Attendee a = this.getAttendeeAt(this.getSelectedRow());
98             CalendarViewer viewer = new CalendarViewer(plugin, a.getUser());
99             viewer.setPreferredSize(new Dimension JavaDoc(780, 550));
100             viewer.show();
101         }
102     }
103 }
104
105 class AttendeeTableModel extends AbstractTableModel
106 {
107     private ArrayList JavaDoc attendees;
108
109     private ImageIcon acceptedIcon;
110     private ImageIcon refusedIcon;
111     private ImageIcon unknownIcon;
112     
113     public AttendeeTableModel(Plugin plugin, ArrayList JavaDoc concepts)
114     {
115         this.attendees = concepts;
116         Collections.sort(attendees, new AttendeeComparator());
117         
118         this.acceptedIcon = plugin.getImageIcon("accepted.png");
119         this.refusedIcon = plugin.getImageIcon("refused.png");
120         this.unknownIcon = plugin.getImageIcon("unknown.png");
121     }
122
123     public void setAttendees(ArrayList JavaDoc attendees)
124     {
125         this.attendees = attendees;
126         Collections.sort(attendees, new AttendeeComparator());
127         this.fireTableDataChanged();
128     }
129
130     public void addAttendee(Attendee attendee)
131     {
132         this.attendees.add(attendee);
133         Collections.sort(attendees, new AttendeeComparator());
134         this.fireTableDataChanged();
135     }
136     
137     public Attendee getAttendeeAt(int row)
138     {
139         return (Attendee)attendees.get(row);
140     }
141     
142     public void removeAt(int row)
143     {
144         if(row < 0)
145             return;
146         
147         attendees.remove(row);
148         this.fireTableDataChanged();
149     }
150     
151     public ArrayList JavaDoc geAttendeeList()
152     {
153         return attendees;
154     }
155     
156     public Object JavaDoc getValueAt(int x, int y)
157     {
158         Attendee a = (Attendee)attendees.get(x);
159         
160         if(y == 1)
161         {
162             if(a.isMandatory())
163                 return "<html><b>" + a.getUser() + "</b></html>";
164             
165             return a.getUser();
166         }
167         
168         if(a.getStatus() == Attendee.STATUS_ACCEPTED)
169             return acceptedIcon;
170         if(a.getStatus() == Attendee.STATUS_REFUSED)
171             return refusedIcon;
172         if(a.getStatus() == Attendee.STATUS_UNKNOWN)
173             return unknownIcon;
174         
175         return "";
176     }
177     
178     public Class JavaDoc getColumnClass(int c)
179     {
180         return (c==0 ? ImageIcon.class : String JavaDoc.class);
181     }
182
183     public int getColumnCount()
184     {
185         return 2;
186     }
187
188     public int getRowCount()
189     {
190         return attendees.size();
191     }
192 }
193
194 class AttendeeComparator implements Comparator JavaDoc
195 {
196     public int compare(Object JavaDoc o1, Object JavaDoc o2)
197     {
198         Attendee a1 = (Attendee)o1;
199         Attendee a2 = (Attendee)o2;
200         
201         if(a1.isMandatory() && !a2.isMandatory())
202             return -1;
203         if(!a1.isMandatory() && a2.isMandatory())
204             return 1;
205         
206         return a1.getUser().compareTo(a2.getUser());
207     }
208 }
209
Popular Tags