1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.widgets.list; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.Collections; |
20 | |
import java.util.Comparator; |
21 | |
import java.util.List; |
22 | |
|
23 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
24 | |
import org.kuali.student.common.ui.client.mvc.CollectionModel; |
25 | |
import org.kuali.student.common.ui.client.mvc.CollectionModelChangeEvent; |
26 | |
import org.kuali.student.common.ui.client.mvc.ModelChangeEvent; |
27 | |
import org.kuali.student.common.ui.client.mvc.ModelChangeHandler; |
28 | |
import org.kuali.student.core.dto.Idable; |
29 | |
|
30 | |
import com.google.gwt.event.shared.HandlerRegistration; |
31 | |
|
32 | 0 | public abstract class ModelListItems<T extends Idable> implements ListItems{ |
33 | 0 | protected List<Callback<T>> addCallbacks = new ArrayList<Callback<T>>(); |
34 | 0 | protected List<Callback<T>> removeCallbacks = new ArrayList<Callback<T>>(); |
35 | 0 | protected List<Callback<T>> updateCallbacks = new ArrayList<Callback<T>>(); |
36 | 0 | protected List<Callback<T>> bulkUpdateCallbacks = new ArrayList<Callback<T>>(); |
37 | |
|
38 | 0 | protected List<T> listItems = new ArrayList<T>(); |
39 | 0 | protected Comparator<T> listComparator = null; |
40 | 0 | protected HandlerRegistration reg = null; |
41 | |
|
42 | |
protected void add(T item){ |
43 | 0 | listItems.add(item); |
44 | 0 | reSort(); |
45 | 0 | } |
46 | |
|
47 | |
protected void update(T item){ |
48 | 0 | for(T i : listItems){ |
49 | 0 | if(i.getId().equals(item.getId())){ |
50 | 0 | listItems.remove(i); |
51 | 0 | listItems.add(item); |
52 | 0 | reSort(); |
53 | 0 | break; |
54 | |
} |
55 | |
} |
56 | 0 | } |
57 | |
|
58 | |
protected void remove(T item){ |
59 | 0 | listItems.remove(item); |
60 | 0 | } |
61 | |
|
62 | |
protected void reSort(){ |
63 | 0 | if(listComparator != null){ |
64 | 0 | Collections.sort(listItems, listComparator); |
65 | |
} |
66 | 0 | } |
67 | |
|
68 | |
public void setComparator(Comparator<T> comparator){ |
69 | 0 | listComparator = comparator; |
70 | 0 | reSort(); |
71 | 0 | for (Callback<T> c : bulkUpdateCallbacks) { |
72 | 0 | c.exec(null); |
73 | |
} |
74 | 0 | } |
75 | |
|
76 | |
public void setModel(CollectionModel<T> model){ |
77 | 0 | if(reg != null){ |
78 | 0 | reg.removeHandler(); |
79 | |
} |
80 | 0 | reg = model.addModelChangeHandler(new ModelChangeHandler() { |
81 | |
public void onModelChange(ModelChangeEvent evt) { |
82 | 0 | CollectionModelChangeEvent<T> event = (CollectionModelChangeEvent<T>) evt; |
83 | 0 | switch (event.getAction()) { |
84 | |
case ADD: |
85 | 0 | ModelListItems.this.add(event.getValue()); |
86 | 0 | for (Callback<T> c : addCallbacks) { |
87 | 0 | c.exec(event.getValue()); |
88 | |
} |
89 | 0 | break; |
90 | |
case UPDATE: |
91 | 0 | ModelListItems.this.update(event.getValue()); |
92 | 0 | for (Callback<T> c : updateCallbacks) { |
93 | 0 | c.exec(event.getValue()); |
94 | |
} |
95 | 0 | break; |
96 | |
case REMOVE: |
97 | 0 | ModelListItems.this.remove(event.getValue()); |
98 | 0 | for (Callback<T> c : removeCallbacks) { |
99 | 0 | c.exec(event.getValue()); |
100 | |
} |
101 | |
break; |
102 | |
} |
103 | 0 | } |
104 | |
}); |
105 | 0 | listItems.clear(); |
106 | 0 | listItems.addAll(model.getValues()); |
107 | 0 | reSort(); |
108 | 0 | } |
109 | |
|
110 | |
|
111 | |
public void addOnAddCallback(Callback<T> callback) { |
112 | 0 | addCallbacks.add(callback); |
113 | 0 | } |
114 | |
|
115 | |
|
116 | |
public void addOnRemoveCallback(Callback<T> callback) { |
117 | 0 | removeCallbacks.add(callback); |
118 | 0 | } |
119 | |
|
120 | |
|
121 | |
public void addOnUpdateCallback(Callback<T> callback) { |
122 | 0 | updateCallbacks.add(callback); |
123 | 0 | } |
124 | |
|
125 | |
public void addOnBulkUpdateCallback(Callback<T> callback) { |
126 | 0 | bulkUpdateCallbacks.add(callback); |
127 | 0 | } |
128 | |
|
129 | |
@Override |
130 | |
public int getItemCount() { |
131 | 0 | return listItems.size(); |
132 | |
} |
133 | |
|
134 | |
@Override |
135 | |
public List<String> getItemIds() { |
136 | 0 | List<String> ids = new ArrayList<String>(); |
137 | 0 | for(T i: listItems){ |
138 | 0 | ids.add(i.getId()); |
139 | |
} |
140 | 0 | return ids; |
141 | |
} |
142 | |
} |