1 19 20 package org.lucane.applications.todolist.gui; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Font ; 25 import java.awt.event.ActionListener ; 26 import java.awt.event.MouseAdapter ; 27 import java.awt.event.MouseEvent ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.Comparator ; 31 import java.util.HashMap ; 32 33 import javax.swing.DefaultListSelectionModel ; 34 import javax.swing.JLabel ; 35 import javax.swing.JTable ; 36 import javax.swing.event.ListSelectionListener ; 37 import javax.swing.table.AbstractTableModel ; 38 import javax.swing.table.DefaultTableCellRenderer ; 39 import javax.swing.table.TableColumnModel ; 40 41 import org.lucane.applications.todolist.Todolist; 42 import org.lucane.client.Client; 43 44 public class TodolistTable extends JTable { 45 public TodolistTable() { 46 super(); 47 48 setModel(new TodolistTableModel()); 49 TodolistRenderer tr = new TodolistRenderer(); 50 setDefaultRenderer(Todolist.class, tr); 51 setDefaultRenderer(String .class, tr); 52 setSelectionModel(new DefaultListSelectionModel ()); 53 54 getTableHeader().addMouseListener(new MouseAdapter () { 55 public void mouseClicked(MouseEvent e) { 56 TableColumnModel columnModel = getColumnModel(); 57 int col = columnModel.getColumnIndexAtX(e.getX()); 58 if(e.getClickCount()==1 && col>=0) { 59 ((TodolistTableModel)getModel()).sort(col); 60 } 61 } 62 }); 63 64 } 65 66 public void addListSelectionListener(ListSelectionListener lsl) { 67 getSelectionModel().addListSelectionListener(lsl); 68 } 69 70 public Object getSelectedTodolist() { 71 int selectedIndex = getSelectionModel().getMinSelectionIndex(); 72 if (selectedIndex>=0) 73 return ((TodolistTableModel)getModel()).getValueAt(selectedIndex); 74 return null; 75 } 76 77 public void add(Object obj) { 78 ((TodolistTableModel)getModel()).add(obj); 79 } 80 81 public void remove(Object obj) { 82 ((TodolistTableModel)getModel()).remove(obj); 83 } 84 85 public void clear() { 86 ((TodolistTableModel)getModel()).clear(); 87 } 88 89 } 90 91 class TodolistTableModel extends AbstractTableModel { 92 93 private static String [] columnsNames = {"Name"}; 94 private static final int[] COLUMNS_SORT = {TodolistsSorter.NAME, TodolistsSorter.PRIORITY, TodolistsSorter.COMPLETED}; 95 private ArrayList lists; 96 private Comparator comparator; 97 98 private int sortedCol = -1; 99 private int sortDirection = TodolistsSorter.ASC; 100 101 public TodolistTableModel() { 102 lists = new ArrayList (); 103 comparator = new TodolistsSorter(TodolistsSorter.NAME, TodolistsSorter.ASC); 104 } 105 106 public static void setColumnsNames(String [] names) { 107 columnsNames = names; 108 } 109 110 public void setComparator(Comparator comparator) { 111 this.comparator = comparator; 112 } 113 114 public void sort(int columnIndex) { 115 if (columnIndex==sortedCol) { 116 sortDirection = sortDirection == TodolistsSorter.ASC 117 ? TodolistsSorter.DESC 118 : TodolistsSorter.ASC; 119 } else { 120 sortedCol = columnIndex; 121 sortDirection = TodolistsSorter.ASC; 122 } 123 124 comparator = new TodolistsSorter(COLUMNS_SORT[sortedCol], sortDirection); 125 Collections.sort(lists, comparator); 126 fireTableDataChanged(); 127 } 128 129 public void add(Object obj) { 130 lists.add(obj); 131 Collections.sort(lists, comparator); 132 fireTableDataChanged(); 133 } 134 135 public void remove(Object obj) { 136 lists.remove(obj); 137 fireTableDataChanged(); 138 } 139 140 public Class getColumnClass(int col) { 141 return Todolist.class; 142 } 143 144 public String getColumnName(int col) { 145 return columnsNames[col]; 146 } 147 148 public boolean isCellEditable(int row, int col) { 149 return false; 150 } 151 152 public void setValueAt(Object value, int row, int col) { 153 super.setValueAt(value, row, col); 154 } 155 156 public int getColumnCount() { 157 return 1; 158 } 159 160 public int getRowCount() { 161 return lists.size(); 162 } 163 164 public Object getValueAt(int row, int col) { 165 return lists.get(row); 166 } 167 168 public ArrayList getValues() { 169 return lists; 170 } 171 172 public Object getValueAt(int row) { 173 try { 174 return lists.get(row); 175 } catch (IndexOutOfBoundsException e) { 176 return null; 177 } 178 } 179 180 public void clear() { 181 lists.clear(); 182 fireTableRowsDeleted(0, 0); 183 } 184 } 185 186 class TodolistRenderer extends DefaultTableCellRenderer 187 { 188 private static HashMap filtersNames = null; 189 190 public Component getTableCellRendererComponent(JTable table, Object value, 191 boolean isSelected, boolean hasFocus, int row, int column) 192 { 193 Component c = super.getTableCellRendererComponent(table, value, isSelected, 194 hasFocus, row, column); 195 196 if(value instanceof Todolist) { 197 String user = Client.getInstance().getMyInfos().getName(); 198 Todolist list = (Todolist)value; 199 JLabel lbl = (JLabel ) c; 200 lbl.setText(list.getName()); 201 202 if(list.getUserName().equals(user)) 203 lbl.setForeground(Color.BLACK); 204 else if(list.isReadOnly()) 205 lbl.setForeground(Color.GRAY); 206 else 207 lbl.setForeground(Color.BLUE); 208 209 return lbl; 210 } else if(value instanceof String ) { 211 if (filtersNames != null) { 212 Object newLabel = filtersNames.get((String )value); 213 if (newLabel != null) { 214 ((JLabel )c).setText((String )newLabel); 215 ((JLabel )c).setFont(((JLabel )c).getFont().deriveFont(Font.BOLD)); 216 } 217 } 218 } 219 return c; 220 } 221 222 public static void setFiltersNames(HashMap names) { 223 filtersNames = names; 224 } 225 } 226 227 class TodolistsSorter implements Comparator { 228 229 public static final int ASC = 1; 230 public static final int DESC = -1; 231 232 public static final int NAME = 0; 233 public static final int PRIORITY = 1; 234 public static final int COMPLETED = 2; 235 236 private int sortBy=0; 237 private int direction = 0; 238 239 public TodolistsSorter(int sortBy, int direction) { 240 this.sortBy = sortBy; 241 this.direction = direction; 242 } 243 244 public int compare(Object o1, Object o2) { 245 if (!(o1 instanceof Todolist) && !(o2 instanceof Todolist)) 246 return 0; 247 if (!(o1 instanceof Todolist)) 248 return -1; 249 if (!(o2 instanceof Todolist)) 250 return 1; 251 Todolist tl1 = (Todolist) o1; 252 Todolist tl2 = (Todolist) o2; 253 switch (sortBy) { 254 case NAME : 255 return direction*(tl1.getName().compareTo(tl2.getName())); 256 } 257 return 0; 258 } 259 } 260 | Popular Tags |