1 19 20 package org.netbeans.modules.tasklist.usertasks; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Comparator ; 25 import java.util.Date ; 26 import java.util.List ; 27 import java.util.logging.Level ; 28 import javax.swing.tree.TreeNode ; 29 30 import javax.swing.tree.TreePath ; 31 32 import org.netbeans.modules.tasklist.core.filter.Filter; 33 import org.netbeans.modules.tasklist.usertasks.treetable.AdvancedTreeTableNode; 34 import org.netbeans.modules.tasklist.usertasks.treetable.BooleanComparator; 35 import org.netbeans.modules.tasklist.usertasks.treetable.DefaultMutableTreeTableNode; 36 import org.netbeans.modules.tasklist.usertasks.treetable.DefaultTreeTableModel; 37 import org.netbeans.modules.tasklist.usertasks.treetable.FilterIntf; 38 import org.netbeans.modules.tasklist.usertasks.treetable.NotComparator; 39 import org.netbeans.modules.tasklist.core.table.SortingModel; 40 import org.netbeans.modules.tasklist.usertasks.treetable.StringIgnoreCaseComparator; 41 import org.netbeans.modules.tasklist.usertasks.util.UTUtils; 42 import org.openide.text.Line; 43 import org.netbeans.modules.tasklist.usertasks.model.UserTask; 44 import org.netbeans.modules.tasklist.usertasks.model.UserTaskList; 45 46 51 public class UserTasksTreeTableModel extends DefaultTreeTableModel { 52 public static final int SUMMARY = 0; 53 public static final int PRIORITY = 1; 54 public static final int DONE = 2; 55 public static final int PERCENT_COMPLETE = 3; 56 public static final int EFFORT = 4; 57 public static final int REMAINING_EFFORT = 5; 58 public static final int SPENT_TIME = 6; 59 public static final int DETAILS = 7; 60 public static final int FILE_BASE_NAME = 8; 61 public static final int LINE_NUMBER = 9; 62 public static final int CATEGORY = 10; 63 public static final int CREATED = 11; 64 public static final int LAST_EDITED = 12; 65 public static final int COMPLETED_DATE = 13; 66 public static final int DUE_DATE = 14; 67 public static final int OWNER = 15; 68 public static final int START = 16; 69 public static final int SPENT_TIME_TODAY = 17; 70 71 74 private static class DueDateComparator implements Comparator <Date > { 75 public int compare(Date o1, Date o2) { 76 if (o1 == null && o2 == null) 77 return 0; 78 if (o1 == null) 79 return 1; 80 if (o2 == null) 81 return -1; 82 return o1.compareTo(o2); 83 } 84 } 85 86 90 private static class TextComparator implements Comparator <String > { 91 public int compare(String f1, String f2) { 92 boolean empty1 = f1 == null || f1.length() == 0; 93 boolean empty2 = f2 == null || f2.length() == 0; 94 95 if (empty1 && empty2) 96 return 0; 97 if (empty1) 98 return 1; 99 if (empty2) 100 return -1; 101 return f1.compareToIgnoreCase(f2); 102 } 103 } 104 105 108 public static class LinesComparator implements Comparator { 109 public int compare(Object o1, Object o2) { 110 Line f1 = (Line) o1; 111 Line f2 = (Line) o2; 112 if (f1 == null && f2 == null) 113 return 0; 114 if (f1 == null) 115 return -1; 116 if (f2 == null) 117 return 1; 118 return f1.getLineNumber() - f2.getLineNumber(); 119 } 120 } 121 122 125 private static class EffortComparator implements Comparator { 126 public int compare(Object o1, Object o2) { 127 if (o1 == null && o2 == null) 128 return 0; 129 if (o1 == null) 130 return -1; 131 if (o2 == null) 132 return 1; 133 UserTask ut1 = (UserTask) o1; 134 UserTask ut2 = (UserTask) o2; 135 return ut1.getEffort() - ut2.getEffort(); 136 } 137 } 138 139 142 private static class CategoryComparator implements Comparator { 143 public int compare(Object o1, Object o2) { 144 if (o1 == null && o2 == null) 145 return 0; 146 if (o1 == null) 147 return -1; 148 if (o2 == null) 149 return 1; 150 UserTask ut1 = (UserTask) o1; 151 UserTask ut2 = (UserTask) o2; 152 String cat1 = ut1.getCategory(); 153 String cat2 = ut2.getCategory(); 154 if (cat1.length() == 0 && cat2.length() == 0) 155 return 0; 156 if (cat1.length() == 0) 157 return 1; 158 if (cat2.length() == 0) 159 return -1; 160 return cat1.compareToIgnoreCase(cat2); 161 } 162 } 163 164 167 private static class OwnerComparator implements Comparator { 168 public int compare(Object o1, Object o2) { 169 if (o1 == null && o2 == null) 170 return 0; 171 if (o1 == null) 172 return -1; 173 if (o2 == null) 174 return 1; 175 UserTask ut1 = (UserTask) o1; 176 UserTask ut2 = (UserTask) o2; 177 String owner1 = ut1.getOwner(); 178 String owner2 = ut2.getOwner(); 179 if (owner1.length() == 0 && owner2.length() == 0) 180 return 0; 181 if (owner1.length() == 0) 182 return 1; 183 if (owner2.length() == 0) 184 return -1; 185 return owner1.compareToIgnoreCase(owner2); 186 } 187 } 188 189 190 public static final String [] COLUMN_PROPERTIES = { 191 "summary", "priority", "done", "percentComplete", "effort", "remainingEffort", "spentTime", "details", "file", "line", "category", "created", "edited", "completedDate", "due", "owner", "start", "spentTimeToday" }; 210 211 private static final Comparator [] COMPARATORS = { 212 new StringIgnoreCaseComparator(), 213 new PriorityComparator(), 214 new NotComparator(new BooleanComparator()), 215 new NotComparator(SortingModel.DEFAULT_COMPARATOR), 216 new NotComparator(new EffortComparator()), 217 new NotComparator(SortingModel.DEFAULT_COMPARATOR), 218 new NotComparator(SortingModel.DEFAULT_COMPARATOR), 219 new TextComparator(), 220 new TextComparator(), 221 SortingModel.DEFAULT_COMPARATOR, 222 new CategoryComparator(), 223 new NotComparator(SortingModel.DEFAULT_COMPARATOR), 224 new NotComparator(SortingModel.DEFAULT_COMPARATOR), 225 new NotComparator(SortingModel.DEFAULT_COMPARATOR), 226 new DueDateComparator(), 227 new OwnerComparator(), 228 new NotComparator(SortingModel.DEFAULT_COMPARATOR), 229 new NotComparator(SortingModel.DEFAULT_COMPARATOR) 230 }; 231 232 private static final Class [] COLUMN_CLASS = { 233 null, 234 Integer .class, 235 Boolean .class, 236 Integer .class, 237 UserTask.class, 238 Integer .class, 239 Integer .class, 240 String .class, 241 String .class, 242 Integer .class, 243 UserTask.class, 244 String .class, 245 Long .class, 246 Long .class, 247 Long .class, 248 String .class, 249 Date .class, 250 Integer .class 251 }; 252 253 private static final String [] COLUMNS = { 254 org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnSummary"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnPriority"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnDone"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnPercentComplete"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnEffort"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnRemEffort"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnSpentTime"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnDetails"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnFile"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnLine"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnCategory"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnCreated"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnEdited"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnCompletedDate"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnDue"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnOwner"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnStart"), org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnSpentTimeToday") }; 273 274 private UserTaskList utl; 275 276 283 public UserTasksTreeTableModel(UserTaskList utl, SortingModel sm, 284 final Filter filter) { 285 super(null, COLUMNS); 286 this.utl = utl; 287 for (int i = 0; i < COMPARATORS.length; i++) { 288 sm.setColumnComparator(i, COMPARATORS[i]); 289 } 290 291 FilterIntf fi = null; 292 if (filter != null) { 293 fi = new FilterIntf() { 294 public boolean accept(Object obj) { 295 if (obj instanceof UserTask) { 296 return filter.accept((UserTask) obj); 297 } else { 298 return true; 299 } 300 } 301 }; 302 } 303 this.root = new UserTaskListTreeTableNode(fi, this, utl, null); 304 } 306 307 311 public void destroy() { 312 ((AdvancedTreeTableNode) root).destroy(); 313 } 314 315 320 public UserTaskList getUserTaskList() { 321 return utl; 322 } 323 324 330 public TreePath findPathTo(UserTask ut) { 331 List <UserTask> path = new ArrayList <UserTask>(); 332 while (ut != null) { 333 path.add(0, ut); 334 ut = ut.getParent(); 335 } 336 List <TreeNode > retp = new ArrayList <TreeNode >(); 337 retp.add((TreeNode ) getRoot()); 338 339 DefaultMutableTreeTableNode n = (DefaultMutableTreeTableNode) getRoot(); 340 for(int i = 0; i < path.size(); i++) { 341 Object obj = path.get(i); 342 boolean found = false; 343 for (int j = 0; j < n.getChildCount(); j++) { 344 if (((DefaultMutableTreeTableNode) n.getChildAt(j)). 345 getUserObject() == obj) { 346 found = true; 347 retp.add(n); 348 break; 349 } 350 } 351 if (!found) 352 return null; 353 } 354 355 return new TreePath (retp.toArray()); 356 } 357 358 public Class getColumnClass(int column) { 359 if (column == 0) 360 return super.getColumnClass(0); 361 return COLUMN_CLASS[column]; 362 } 363 364 367 public void sort(SortingModel sm) { 368 final int sortedColumn = sm.getSortedColumn(); 369 if (sortedColumn == -1) { 370 ((UserTaskListTreeTableNode) getRoot()).setComparator(null); 371 return; 372 } 373 374 @SuppressWarnings ("unchecked") 375 Comparator <Object > c = COMPARATORS[sortedColumn]; 376 if (c == null) 377 return; 378 379 final Comparator <Object > c2; 380 if (!sm.isSortOrderDescending()) 381 c2 = new NotComparator<Object >(c); 382 else 383 c2 = c; 384 385 Comparator <AdvancedTreeTableNode> comparator = 386 new Comparator <AdvancedTreeTableNode>() { 387 public int compare(AdvancedTreeTableNode obj1, 388 AdvancedTreeTableNode obj2) { 389 UserTaskTreeTableNode n1 = (UserTaskTreeTableNode) obj1; 390 UserTaskTreeTableNode n2 = (UserTaskTreeTableNode) obj2; 391 return c2.compare( 392 n1.getValueAt(sortedColumn), n2.getValueAt(sortedColumn)); 393 } 394 }; 395 396 ((UserTaskListTreeTableNode) getRoot()).setComparator(comparator); 397 } 398 399 public void fireTreeNodesChanged(Object source, Object [] path, 400 int[] childIndices, Object [] children) { 401 super.fireTreeNodesChanged(source, path, childIndices, children); 402 } 403 404 public void fireTreeNodesInserted(Object source, Object [] path, 405 int[] childIndices, Object [] children) { 406 super.fireTreeNodesInserted(source, path, childIndices, children); 407 } 408 409 public void fireTreeNodesRemoved(Object source, Object [] path, 410 int[] childIndices, Object [] children) { 411 super.fireTreeNodesRemoved(source, path, childIndices, children); 412 } 413 414 public void fireTreeStructureChanged(Object source, Object [] path) { 415 super.fireTreeStructureChanged(source, path); 416 } 417 418 public void fireTreeStructureChanged(Object source, Object [] path, 419 int[] childIndices, Object [] children) { 420 super.fireTreeStructureChanged(source, path, childIndices, children); 421 } 422 } 423 | Popular Tags |