1 | |
package org.kuali.student.common.ui.client.configurable.mvc.sections; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.HashMap; |
5 | |
import java.util.Iterator; |
6 | |
import java.util.List; |
7 | |
|
8 | |
import org.kuali.student.common.ui.client.application.Application; |
9 | |
import org.kuali.student.common.ui.client.widgets.dialog.ConfirmationDialog; |
10 | |
import org.kuali.student.common.ui.client.widgets.field.layout.layouts.VerticalFieldLayout; |
11 | |
import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract; |
12 | |
import org.kuali.student.common.ui.client.widgets.list.SelectionChangeEvent; |
13 | |
import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler; |
14 | |
|
15 | |
import com.google.gwt.event.dom.client.ClickEvent; |
16 | |
import com.google.gwt.event.dom.client.ClickHandler; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | 0 | public class SwapSection extends BaseSection implements HasSectionDeletion{ |
26 | |
|
27 | 0 | private HashMap<String, Section> swapSectionMap = new HashMap<String, Section>(); |
28 | |
private KSSelectItemWidgetAbstract selectableWidget; |
29 | 0 | private List<Section> deleted = new ArrayList<Section>(); |
30 | |
private ConfirmationDialog dialog; |
31 | 0 | private boolean showConfirmation = true; |
32 | 0 | private List<String> lastSelection = new ArrayList<String>(); |
33 | |
private List<String> deletionParentKeys; |
34 | |
private SwapEventHandler swapEventHandler; |
35 | 0 | protected List<String> prevSelection = new ArrayList<String>(); |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public SwapSection(KSSelectItemWidgetAbstract selectableWidget){ |
44 | 0 | this.init(selectableWidget); |
45 | 0 | } |
46 | |
|
47 | 0 | public SwapSection(KSSelectItemWidgetAbstract selectableWidget, ConfirmationDialog dialog){ |
48 | 0 | this.dialog = dialog; |
49 | 0 | this.init(selectableWidget); |
50 | 0 | } |
51 | |
|
52 | |
private void init(KSSelectItemWidgetAbstract selectableWidget){ |
53 | 0 | this.selectableWidget = selectableWidget; |
54 | |
|
55 | 0 | if(dialog == null){ |
56 | 0 | dialog = |
57 | |
new ConfirmationDialog(Application.getApplicationContext().getMessage("fieldDeletionTitle"), |
58 | |
Application.getApplicationContext().getMessage("fieldDeletionConfirmMessage")); |
59 | |
} |
60 | 0 | dialog.getConfirmButton().addClickHandler(new ClickHandler(){ |
61 | |
|
62 | |
@Override |
63 | |
public void onClick(ClickEvent event) { |
64 | 0 | handleUserSelection(); |
65 | 0 | dialog.hide(); |
66 | 0 | } |
67 | |
}); |
68 | |
|
69 | 0 | dialog.getCancelButton().addClickHandler(new ClickHandler() { |
70 | |
|
71 | |
@Override |
72 | |
public void onClick(ClickEvent event) { |
73 | 0 | for (int i = 0; i < prevSelection.size(); i++) { |
74 | 0 | SwapSection.this.selectableWidget.selectItem(prevSelection.get(i)); |
75 | |
} |
76 | 0 | } |
77 | |
}); |
78 | |
|
79 | 0 | selectableWidget.addSelectionChangeHandler(new SelectionChangeHandler(){ |
80 | |
|
81 | |
@Override |
82 | |
public void onSelectionChange(SelectionChangeEvent event) { |
83 | 0 | if(event.isUserInitiated() && showConfirmation){ |
84 | 0 | if(SwapSection.this.selectableWidget.getSelectedItems().size() < lastSelection.size()){ |
85 | 0 | dialog.show(); |
86 | |
} |
87 | 0 | else if(!SwapSection.this.selectableWidget.getSelectedItems().containsAll(lastSelection)){ |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | 0 | dialog.show(); |
99 | |
} |
100 | |
else{ |
101 | 0 | handleUserSelection(); |
102 | |
} |
103 | |
} |
104 | 0 | else if(event.isUserInitiated()){ |
105 | 0 | handleUserSelection(); |
106 | |
} |
107 | |
else{ |
108 | 0 | handleSelection(); |
109 | |
} |
110 | 0 | prevSelection.clear(); |
111 | 0 | prevSelection.addAll(lastSelection); |
112 | 0 | lastSelection.clear(); |
113 | 0 | lastSelection.addAll(SwapSection.this.selectableWidget.getSelectedItems()); |
114 | 0 | } |
115 | |
}); |
116 | 0 | layout = new VerticalFieldLayout(); |
117 | 0 | this.add(layout); |
118 | 0 | } |
119 | |
|
120 | |
private void handleUserSelection(){ |
121 | 0 | List<String> selected = SwapSection.this.selectableWidget.getSelectedItems(); |
122 | 0 | for(int i = 0; i < selected.size(); i++){ |
123 | 0 | String key = selected.get(i); |
124 | 0 | showSwappableSection(key); |
125 | |
} |
126 | |
|
127 | 0 | Iterator<String> it = swapSectionMap.keySet().iterator(); |
128 | 0 | while(it.hasNext()){ |
129 | 0 | String key = it.next(); |
130 | 0 | if(!selected.contains(key)){ |
131 | 0 | removeSwappableSection(key); |
132 | |
} |
133 | 0 | } |
134 | |
|
135 | |
|
136 | |
|
137 | 0 | } |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
private void handleSelection(){ |
145 | 0 | List<String> selected = SwapSection.this.selectableWidget.getSelectedItems(); |
146 | 0 | for(int i = 0; i < selected.size(); i++){ |
147 | 0 | String key = selected.get(i); |
148 | 0 | showSwappableSection(key); |
149 | |
} |
150 | |
|
151 | 0 | Iterator<String> it = swapSectionMap.keySet().iterator(); |
152 | 0 | while(it.hasNext()){ |
153 | 0 | String key = it.next(); |
154 | 0 | if(!selected.contains(key)){ |
155 | 0 | removeSwappableSection(key); |
156 | |
} |
157 | 0 | } |
158 | 0 | } |
159 | |
|
160 | |
private void showSwappableSection(String key){ |
161 | 0 | Section section = swapSectionMap.get(key); |
162 | 0 | if(section != null){ |
163 | 0 | if(deleted.contains(section)){ |
164 | 0 | deleted.remove(section); |
165 | |
} |
166 | 0 | if(!section.getLayout().isVisible()){ |
167 | 0 | section.enableValidation(true); |
168 | 0 | section.getLayout().setVisible(true); |
169 | |
} |
170 | |
} |
171 | 0 | if (swapEventHandler != null){ |
172 | 0 | swapEventHandler.onShowSwappableSection(key, section); |
173 | |
} |
174 | 0 | } |
175 | |
|
176 | |
private void removeSwappableSection(String key){ |
177 | 0 | Section section = swapSectionMap.get(key); |
178 | 0 | if(section != null){ |
179 | 0 | if(!deleted.contains(section)){ |
180 | 0 | deleted.add(section); |
181 | |
} |
182 | 0 | if(section.getLayout().isVisible()){ |
183 | 0 | section.enableValidation(false); |
184 | 0 | section.getLayout().setVisible(false); |
185 | |
} |
186 | |
|
187 | |
} |
188 | 0 | if (swapEventHandler != null){ |
189 | 0 | swapEventHandler.onRemoveSwappableSection(key, section); |
190 | |
} |
191 | 0 | } |
192 | |
|
193 | |
public void enableConfirmation(boolean enable){ |
194 | 0 | showConfirmation = enable; |
195 | 0 | } |
196 | |
|
197 | |
public String addSection(Section section, String swapKey){ |
198 | 0 | swapSectionMap.put(swapKey, section); |
199 | 0 | String key = layout.addLayout(section.getLayout()); |
200 | 0 | section.getLayout().setVisible(false); |
201 | 0 | if(selectableWidget.getSelectedItems().contains(swapKey)){ |
202 | 0 | handleSelection(); |
203 | |
} |
204 | 0 | sections.add(section); |
205 | 0 | return key; |
206 | |
} |
207 | |
|
208 | |
public String addSection(String key, Section section, String swapKey){ |
209 | 0 | swapSectionMap.put(swapKey, section); |
210 | 0 | section.getLayout().setKey(key); |
211 | 0 | String rkey = layout.addLayout(section.getLayout()); |
212 | 0 | section.getLayout().setVisible(false); |
213 | 0 | if(selectableWidget.getSelectedItems().contains(swapKey)){ |
214 | 0 | handleSelection(); |
215 | |
} |
216 | 0 | sections.add(section); |
217 | 0 | return rkey; |
218 | |
} |
219 | |
|
220 | |
|
221 | |
@Override |
222 | |
public String addSection(Section section) { |
223 | 0 | throw new UnsupportedOperationException("Sections can be added to swappable section only through " + |
224 | |
"the addSection(Section section, String swapKey) method"); |
225 | |
|
226 | |
} |
227 | |
|
228 | |
@Override |
229 | |
public String addSection(String key, Section section) { |
230 | 0 | throw new UnsupportedOperationException("Sections can be added to swappable section only through " + |
231 | |
"the addSection(Section section, String swapKey) method"); |
232 | |
} |
233 | |
|
234 | |
@Override |
235 | |
public List<Section> getDeletedSections() { |
236 | 0 | return deleted; |
237 | |
} |
238 | |
|
239 | |
@Override |
240 | |
public List<String> getDeletionParentKeys() { |
241 | 0 | return deletionParentKeys; |
242 | |
} |
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
@Override |
251 | |
public void setDeletionParentKey(List<String> deletionParentKeys) { |
252 | 0 | this.deletionParentKeys = deletionParentKeys; |
253 | 0 | } |
254 | |
|
255 | |
public SwapEventHandler getSwapEventHandler() { |
256 | 0 | return swapEventHandler; |
257 | |
} |
258 | |
|
259 | |
public void setSwapEventHandler(SwapEventHandler swapEventHandler) { |
260 | 0 | this.swapEventHandler = swapEventHandler; |
261 | 0 | } |
262 | |
|
263 | |
} |