1 19 package org.lucane.applications.calendar.gui; 20 21 import java.awt.Dimension ; 22 import java.awt.event.MouseEvent ; 23 import java.awt.event.MouseListener ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.Comparator ; 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 37 { 38 private CalendarPlugin plugin; 39 40 public AttendeeTable(CalendarPlugin plugin, String name, ArrayList 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 name) 56 { 57 this(plugin, name, new ArrayList ()); 58 } 59 60 public void setAttendees(ArrayList 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 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 e) {} 90 public void mouseExited(MouseEvent e) {} 91 public void mousePressed(MouseEvent e) {} 92 public void mouseReleased(MouseEvent e) {} 93 public void mouseClicked(MouseEvent 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 (780, 550)); 100 viewer.show(); 101 } 102 } 103 } 104 105 class AttendeeTableModel extends AbstractTableModel 106 { 107 private ArrayList attendees; 108 109 private ImageIcon acceptedIcon; 110 private ImageIcon refusedIcon; 111 private ImageIcon unknownIcon; 112 113 public AttendeeTableModel(Plugin plugin, ArrayList 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 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 geAttendeeList() 152 { 153 return attendees; 154 } 155 156 public Object 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 getColumnClass(int c) 179 { 180 return (c==0 ? ImageIcon.class : String .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 195 { 196 public int compare(Object o1, Object 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 |