1 13 package com.tonbeller.wcf.selection; 14 15 import java.util.Collection ; 16 import java.util.Iterator ; 17 import java.util.Set ; 18 19 import com.tonbeller.wcf.controller.RequestContext; 20 21 26 public abstract class AbstractSelectionModel implements SelectionModel { 27 private int mode; 28 private SelectionChangeSupport changeSupport; 29 30 public abstract Set getSelection(); 31 public abstract void add(Object obj); 32 public abstract void remove(Object obj); 33 public abstract void clear(); 34 35 protected AbstractSelectionModel(int mode) { 36 this.mode = mode; 37 changeSupport = new SelectionChangeSupport(this); 38 } 39 40 public int getMode() { 41 return mode; 42 } 43 44 public void setMode(int mode) { 45 this.mode = mode; 46 } 47 48 49 public void addAll(Collection c) { 50 for (Iterator it = c.iterator(); it.hasNext();) 51 add(it.next()); 52 } 53 54 public boolean contains(Object obj) { 55 return getSelection().contains(obj); 56 } 57 58 public Object getSingleSelection() { 59 Set selection = getSelection(); 60 if (selection.size() == 0) 61 return null; 62 if (selection.size() == 1) 63 return selection.iterator().next(); 64 throw new IllegalStateException ("single selection contains " + selection.size() + " elements"); 65 } 66 67 public void setSingleSelection(Object obj) { 68 clear(); 69 add(obj); 70 } 71 72 public boolean isEmpty() { 73 return getSelection().isEmpty(); 74 } 75 76 80 public boolean isSelectable(Object item) { 81 if (item instanceof Unselectable) 82 return false; 83 return true; 84 } 85 86 public void fireSelectionChanged(RequestContext context) { 87 changeSupport.fireSelectionChanged(context); 88 } 89 90 public void addSelectionListener(SelectionChangeListener l) { 91 changeSupport.addSelectionListener(l); 92 } 93 94 public void removeSelectionListener(SelectionChangeListener l) { 95 changeSupport.removeSelectionListener(l); 96 } 97 98 } 99 | Popular Tags |