1 13 package com.tonbeller.wcf.selection; 14 15 import java.util.Collection ; 16 import java.util.Collections ; 17 import java.util.Comparator ; 18 import java.util.HashSet ; 19 import java.util.Set ; 20 import java.util.TreeSet ; 21 22 import com.tonbeller.wcf.controller.RequestContext; 23 24 29 public class DefaultSelectionModel implements SelectionModel { 30 31 Set selection = new HashSet (); 32 33 int mode = MULTIPLE_SELECTION; 34 35 SelectableFilter selectableFilter = null; 36 37 SelectionChangeSupport changeSupport; 38 39 public DefaultSelectionModel() { 40 changeSupport = new SelectionChangeSupport(this); 41 } 42 43 public DefaultSelectionModel(int mode) { 44 this.mode = mode; 45 changeSupport = new SelectionChangeSupport(this); 46 } 47 48 public void setComparator(Comparator comp) { 49 selection = new TreeSet (comp); 50 } 51 52 public void add(Object obj) { 53 selection.add(obj); 54 } 55 56 public void remove(Object obj) { 57 selection.remove(obj); 58 } 59 60 public void clear() { 61 selection.clear(); 62 } 63 64 public boolean contains(Object obj) { 65 return selection.contains(obj); 66 } 67 68 public int getMode() { 69 return mode; 70 } 71 72 public Set getSelection() { 73 return Collections.unmodifiableSet(selection); 74 } 75 76 public void setMode(int mode) { 77 this.mode = mode; 78 } 79 80 public void setSelection(Collection newSelection) { 81 selection.clear(); 82 selection.addAll(newSelection); 83 } 84 85 public void addAll(Collection c) { 86 this.selection.addAll(c); 87 } 88 89 98 public boolean isSelectable(Object item) { 99 if (item instanceof Unselectable) 100 return false; 101 if (selectableFilter != null) 102 return selectableFilter.isSelectable(item); 103 return true; 104 } 105 106 public Object getSingleSelection() { 107 if (selection.isEmpty()) 108 return null; 109 if (selection.size() == 1) 110 return selection.iterator().next(); 111 throw new IllegalStateException ("getSingleSelection: selection contains " + selection.size() 112 + " elements"); 113 } 114 115 public void setSingleSelection(Object selectedObject) { 116 selection.clear(); 117 selection.add(selectedObject); 118 } 119 120 126 public SelectableFilter getSelectableFilter() { 127 return selectableFilter; 128 } 129 130 139 public void setSelectableFilter(SelectableFilter filter) { 140 selectableFilter = filter; 141 } 142 143 public void fireSelectionChanged(RequestContext context) { 144 changeSupport.fireSelectionChanged(context); 145 } 146 147 public void addSelectionListener(SelectionChangeListener l) { 148 changeSupport.addSelectionListener(l); 149 } 150 151 public void removeSelectionListener(SelectionChangeListener l) { 152 changeSupport.removeSelectionListener(l); 153 } 154 155 public boolean isEmpty() { 156 return selection.isEmpty(); 157 } 158 159 } | Popular Tags |