1 19 20 package org.lucane.applications.todolist.gui; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.event.MouseAdapter ; 25 import java.awt.event.MouseEvent ; 26 import java.text.DateFormat ; 27 import java.util.Date ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.Comparator ; 31 32 import javax.swing.DefaultListSelectionModel ; 33 import javax.swing.JTable ; 34 import javax.swing.JTextArea ; 35 import javax.swing.event.ListSelectionListener ; 36 import javax.swing.table.AbstractTableModel ; 37 import javax.swing.table.DefaultTableCellRenderer ; 38 import javax.swing.table.TableColumnModel ; 39 40 import org.lucane.applications.todolist.TodolistItem; 41 42 public class TodolistItemTable extends JTable { 43 public TodolistItemTable() { 44 super(); 45 46 setDefaultRenderer(String .class, new ColoredCellRenderer()); 47 48 setModel(new TodolistItemTableModel()); 49 setSelectionModel(new DefaultListSelectionModel ()); 50 51 for (int i=1; i<=6; i++) { 52 getColumnModel().getColumn(i).setMinWidth(70); 53 getColumnModel().getColumn(i).setMaxWidth(70); 54 getColumnModel().getColumn(i).setResizable(false); 55 } 56 setRowHeight(getRowHeight()*2); 57 getTableHeader().addMouseListener(new MouseAdapter () { 58 public void mouseClicked(MouseEvent e) { 59 TableColumnModel columnModel = getColumnModel(); 60 int col = columnModel.getColumnIndexAtX(e.getX()); 61 if(e.getClickCount()==1 && col>=0) { 62 ((TodolistItemTableModel)getModel()).sort(col); 63 } 64 } 65 }); 66 } 67 68 public static void setColumnsNames(String [] names) { 69 TodolistItemTableModel.setColumnsNames(names); 70 } 71 72 public void addListSelectionListener(ListSelectionListener lsl) { 73 getSelectionModel().addListSelectionListener(lsl); 74 } 75 76 public TodolistItem getSelectedTodolistItem() { 77 int selectedIndex = getSelectionModel().getMinSelectionIndex(); 78 if (selectedIndex>=0) 79 return (TodolistItem)((TodolistItemTableModel)getModel()).getValueAt(selectedIndex); 80 return null; 81 } 82 83 public void add(TodolistItem tli) { 84 ((TodolistItemTableModel)getModel()).add(tli); 85 } 86 87 public void remove(TodolistItem tli) { 88 ((TodolistItemTableModel)getModel()).remove(tli); 89 } 90 91 public void clear() { 92 ((TodolistItemTableModel)getModel()).clear(); 93 } 94 95 } 96 97 class TodolistItemTableModel extends AbstractTableModel { 98 99 private static String [] columnsNames = {"Name", "Priority", "Complete", "Estimated start", "Estimated end", "real start", "Real end"}; 100 private static final int[] COLUMNS_SORT = {TodolistItemsSorter.NAME, TodolistItemsSorter.PRIORITY, TodolistItemsSorter.COMPLETED, TodolistItemsSorter.ESTIMATED_START, TodolistItemsSorter.ESTIMATED_END, TodolistItemsSorter.REAL_START, TodolistItemsSorter.REAL_END}; 101 private ArrayList items; 102 private Comparator comparator; 103 104 private int sortedCol = -1; 105 private int sortDirection = TodolistItemsSorter.ASC; 106 107 public TodolistItemTableModel() { 108 items = new ArrayList (); 109 comparator = new TodolistItemsSorter(TodolistItemsSorter.NAME, TodolistItemsSorter.ASC); 110 } 111 112 public static void setColumnsNames(String [] names) { 113 columnsNames = names; 114 } 115 116 public void setComparator(Comparator comparator) { 117 this.comparator = comparator; 118 } 119 120 public void sort(int columnIndex) { 121 if (columnIndex==sortedCol) { 122 sortDirection = sortDirection == TodolistItemsSorter.ASC 123 ? TodolistItemsSorter.DESC 124 : TodolistItemsSorter.ASC; 125 } else { 126 sortedCol = columnIndex; 127 sortDirection = TodolistItemsSorter.ASC; 128 } 129 130 comparator = new TodolistItemsSorter(COLUMNS_SORT[sortedCol], sortDirection); 131 Collections.sort(items, comparator); 132 fireTableDataChanged(); 133 } 134 135 public void add(TodolistItem item) { 136 items.add(item); 137 Collections.sort(items, comparator); 138 fireTableDataChanged(); 139 } 140 141 public void remove(TodolistItem item) { 142 items.remove(item); 143 fireTableDataChanged(); 144 } 145 146 public Class getColumnClass(int col) { 147 return String .class; 148 } 149 150 public String getColumnName(int col) { 151 return columnsNames[col]; 152 } 153 154 public boolean isCellEditable(int row, int col) { 155 return false; 156 } 157 158 public void setValueAt(Object value, int row, int col) { 159 super.setValueAt(value, row, col); 160 } 161 162 public int getColumnCount() { 163 return columnsNames.length; 164 } 165 166 public int getRowCount() { 167 return items.size(); 168 } 169 170 public Object getValueAt(int row, int col) { 171 TodolistItem item = (TodolistItem)items.get(row); 172 173 DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT); 174 DateFormat df2 = DateFormat.getTimeInstance(DateFormat.SHORT); 175 176 switch (col) { 177 case 0 : 178 return item.getName(); 179 case 1 : 180 return TodolistItem.getPriorityLabels()[item.getPriority()]; 181 case 2 : 182 return TodolistItem.getCompleteLabels()[item.isCompleted()?1:0]; 183 case 3 : 184 if (item.getEstimatedStartDate()==null) return ""; 185 return "<HTML>"+df1.format(item.getEstimatedStartDate())+"<BR>"+df2.format(item.getEstimatedStartDate())+"</HTML>"; 186 case 4 : 187 if (item.getEstimatedEndDate()==null) return ""; 188 return "<HTML>"+df1.format(item.getEstimatedEndDate())+"<BR>"+df2.format(item.getEstimatedEndDate())+"</HTML>"; 189 case 5 : 190 if (item.getStartDate()==null) return ""; 191 return "<HTML>"+df1.format(item.getStartDate())+"<BR>"+df2.format(item.getStartDate())+"</HTML>"; 192 case 6 : 193 if (item.getEndDate()==null) return ""; 194 return "<HTML>"+df1.format(item.getEndDate())+"<BR>"+df2.format(item.getEndDate())+"</HTML>"; 195 } 196 return ""; 197 } 198 199 public Object getValueAt(int row) { 200 try { 201 return ((TodolistItem)items.get(row)); 202 } catch (IndexOutOfBoundsException e) { 203 return null; 204 } 205 } 206 207 public void clear() { 208 items.clear(); 209 fireTableRowsDeleted(0, 0); 210 } 211 } 212 213 class TodolistItemsSorter implements Comparator { 214 215 public static final int ASC = 1; 216 public static final int DESC = -1; 217 218 public static final int NAME = 0; 219 public static final int PRIORITY = 1; 220 public static final int COMPLETED = 2; 221 public static final int ESTIMATED_START = 3; 222 public static final int ESTIMATED_END = 4; 223 public static final int REAL_START = 5; 224 public static final int REAL_END = 6; 225 226 private int sortBy = NAME; 227 private int direction = ASC; 228 229 public TodolistItemsSorter(int sortBy, int direction) { 230 this.sortBy = sortBy; 231 this.direction = direction; 232 } 233 234 public int compare(Object o1, Object o2) { 235 TodolistItem tli1 = (TodolistItem) o1; 236 TodolistItem tli2 = (TodolistItem) o2; 237 int res = 0; 238 switch (sortBy) { 239 case NAME : 240 res = direction*(tli1.getName().compareTo(tli2.getName())); 241 if (res == 0) res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0)); 242 if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority()); 243 if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate()); 244 if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate()); 245 if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate()); 246 if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate()); 247 break; 248 case PRIORITY : 249 res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0)); 250 if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority()); 251 if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName())); 252 if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate()); 253 if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate()); 254 if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate()); 255 if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate()); 256 break; 257 case COMPLETED : 258 res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0)); 259 if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName())); 260 if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority()); 261 if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate()); 262 if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate()); 263 if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate()); 264 if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate()); 265 break; 266 case REAL_START : 267 if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate()); 268 if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName())); 269 if (res == 0) res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0)); 270 if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority()); 271 if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate()); 272 if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate()); 273 if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate()); 274 break; 275 case REAL_END : 276 if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate()); 277 if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName())); 278 if (res == 0) res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0)); 279 if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority()); 280 if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate()); 281 if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate()); 282 if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate()); 283 break; 284 case ESTIMATED_START : 285 if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate()); 286 if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName())); 287 if (res == 0) res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0)); 288 if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority()); 289 if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate()); 290 if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate()); 291 if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate()); 292 break; 293 case ESTIMATED_END : 294 if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate()); 295 if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName())); 296 if (res == 0) res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0)); 297 if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority()); 298 if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate()); 299 if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate()); 300 if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate()); 301 break; 302 } 303 return res; 304 } 305 private static int compareDates (Date d1, Date d2) { 306 if (d1 == null && d2 == null) return 0; 307 if (d1 == null) return 1; 308 if (d2 == null) return -1; 309 return d1.compareTo(d2); 310 } 311 } 312 313 class ColoredCellRenderer extends DefaultTableCellRenderer { 314 315 public Component getTableCellRendererComponent(JTable table, Object value, 316 boolean isSelected, boolean hasFocus, int row, int column) { 317 318 Component cmp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 319 if (column>=3) { 320 Component cmp2 = new JTextArea (value.toString()); 321 } 322 if (!isSelected) { 323 TodolistItem tli = (TodolistItem) ((TodolistItemTableModel)table.getModel()).getValueAt(row); 324 if (!tli.isCompleted()) { 325 switch (tli.getPriority()) { 326 case TodolistItem.PRIORITY_LOW : 327 cmp.setBackground(new Color (223, 255, 223)); 328 break; 329 case TodolistItem.PRIORITY_MEDIUM : 330 cmp.setBackground(new Color (255, 255, 223)); 331 break; 332 case TodolistItem.PRIORITY_HIGH : 333 cmp.setBackground(new Color (255, 223, 223)); 334 break; 335 default : 336 cmp.setBackground(Color.white); 337 break; 338 } 339 } else { 340 cmp.setBackground(new Color (223, 223, 223)); 341 } 342 343 359 360 return cmp; 361 } else { 362 return cmp; 363 } 364 } 365 } 366 | Popular Tags |