1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.ui.client.configurable.mvc.multiplicity;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Set;
21
22 import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle;
23 import org.kuali.student.common.ui.client.mvc.Callback;
24
25 import com.google.gwt.user.client.ui.Composite;
26 import com.google.gwt.user.client.ui.FlowPanel;
27 import com.google.gwt.user.client.ui.Widget;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public abstract class MultiplicityComposite extends Composite {
43
44 protected FlowPanel mainPanel = new FlowPanel();
45 protected FlowPanel itemsPanel = new FlowPanel();
46 protected boolean loaded = false;
47 protected int itemCount = 0;
48 protected int visualItemCount = 0;
49 protected int minEmptyItems = 0;
50 protected List<MultiplicityItem> items = new ArrayList<MultiplicityItem>();
51 protected List<MultiplicityItem> removed = new ArrayList<MultiplicityItem>();
52 protected Set<Integer> itemKeys;
53 public static enum StyleType{TOP_LEVEL, SUB_LEVEL};
54 protected StyleType style;
55 protected SectionTitle titleWidget = SectionTitle.generateEmptyTitle();
56
57 public MultiplicityComposite(StyleType style){
58 this.style = style;
59 initWidget(mainPanel);
60 }
61
62 protected Callback<MultiplicityItem> removeCallback = new Callback<MultiplicityItem>(){
63
64 public void exec(MultiplicityItem itemToRemove) {
65
66 visualItemCount--;
67 itemToRemove.setDeleted(true);
68 removed.add(itemToRemove);
69 itemsPanel.remove(itemToRemove);
70 }
71 };
72
73
74
75
76
77
78
79 public MultiplicityItem addItem(){
80 itemCount++;
81 visualItemCount++;
82
83 MultiplicityItem item = getItemDecorator(style);
84 Widget itemWidget = createItem();
85
86 if (item != null){
87 item.setItemKey(new Integer(itemCount -1));
88 item.setItemWidget(itemWidget);
89 item.setRemoveCallback(removeCallback);
90 } else if (itemWidget instanceof MultiplicityItem){
91 item = (MultiplicityItem)itemWidget;
92 item.setItemKey(new Integer(itemCount -1));
93 }
94 items.add(item);
95 item.redraw();
96 itemsPanel.add(item);
97
98 return item;
99 }
100
101
102
103
104
105
106 public int getAddItemKey(){
107 return itemCount-1;
108 }
109
110 public void incrementItemKey(){
111 itemCount++;
112 }
113
114 public void onLoad() {
115 if (!loaded) {
116
117
118 mainPanel.add(itemsPanel);
119
120 Widget addWidget = generateAddWidget();
121 if (addWidget != null){
122 mainPanel.add(addWidget);
123 }
124
125 loaded = true;
126 }
127
128 if (!loaded || itemCount == 0){
129 for (int i=0; i < minEmptyItems; i++){
130 addItem();
131 }
132 }
133 }
134
135 public List<MultiplicityItem> getItems() {
136 return items;
137 }
138
139 public List<MultiplicityItem> getRemovedItems() {
140 return removed;
141 }
142
143 public void clear(){
144 itemsPanel.clear();
145 items.clear();
146 removed.clear();
147 itemCount = 0;
148 }
149
150 public void redraw(){
151 for (MultiplicityItem item:items){
152 item.redraw();
153 }
154 }
155
156
157
158
159 public void setMinEmptyItems(int minCount){
160 this.minEmptyItems = minCount;
161 }
162
163
164
165
166 public abstract MultiplicityItem getItemDecorator(StyleType style);
167
168
169
170
171
172 public abstract Widget createItem();
173
174
175
176
177
178
179 public abstract Widget generateAddWidget();
180
181 }