KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > selection > DefaultSelectionModel


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.selection;
14
15 import java.util.Collection JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Set JavaDoc;
20 import java.util.TreeSet JavaDoc;
21
22 import com.tonbeller.wcf.controller.RequestContext;
23
24 /**
25  * default SelectionModel for Tree and Table
26  *
27  * @author av
28  */

29 public class DefaultSelectionModel implements SelectionModel {
30
31   Set JavaDoc selection = new HashSet JavaDoc();
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 JavaDoc comp) {
49     selection = new TreeSet JavaDoc(comp);
50   }
51
52   public void add(Object JavaDoc obj) {
53     selection.add(obj);
54   }
55
56   public void remove(Object JavaDoc obj) {
57     selection.remove(obj);
58   }
59
60   public void clear() {
61     selection.clear();
62   }
63
64   public boolean contains(Object JavaDoc obj) {
65     return selection.contains(obj);
66   }
67
68   public int getMode() {
69     return mode;
70   }
71
72   public Set JavaDoc 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 JavaDoc newSelection) {
81     selection.clear();
82     selection.addAll(newSelection);
83   }
84
85   public void addAll(Collection JavaDoc c) {
86     this.selection.addAll(c);
87   }
88
89   /**
90    * return false if item implements the tagging Unselectable interface
91    * true if no SelectableFilter is set or if the SelectableFilter accepts the item.
92    *
93    * @see SelectableFilter
94    * @see #setSelectableFilter
95    * @see #getSelectableFilter
96    * @see Unselectable
97    */

98   public boolean isSelectable(Object JavaDoc 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 JavaDoc getSingleSelection() {
107     if (selection.isEmpty())
108       return null;
109     if (selection.size() == 1)
110       return selection.iterator().next();
111     throw new IllegalStateException JavaDoc("getSingleSelection: selection contains " + selection.size()
112         + " elements");
113   }
114
115   public void setSingleSelection(Object JavaDoc selectedObject) {
116     selection.clear();
117     selection.add(selectedObject);
118   }
119
120   /**
121    * returns the current SelectableFilter
122    *
123    * @see SelectableFilter
124    * @see #isSelectable
125    */

126   public SelectableFilter getSelectableFilter() {
127     return selectableFilter;
128   }
129
130   /**
131    * sets the current SelectableFilter.
132    *
133    * @param filter
134    * the new filter or null to accept all nodes
135    * @see SelectableFilter
136    * @see #isSelectable
137    *
138    */

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