1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.widgets.search; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.LinkedHashMap; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import org.kuali.student.common.assembly.data.LookupMetadata; |
23 | |
import org.kuali.student.common.ui.client.application.Application; |
24 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
25 | |
import org.kuali.student.common.ui.client.widgets.KSLabel; |
26 | |
import org.kuali.student.common.ui.client.widgets.layout.VerticalFlowPanel; |
27 | |
|
28 | |
import com.google.gwt.event.dom.client.ChangeEvent; |
29 | |
import com.google.gwt.event.dom.client.ChangeHandler; |
30 | |
import com.google.gwt.user.client.ui.Composite; |
31 | |
import com.google.gwt.user.client.ui.ListBox; |
32 | |
import com.google.gwt.user.client.ui.SimplePanel; |
33 | |
import com.google.gwt.user.client.ui.Widget; |
34 | |
|
35 | 0 | public class SwappablePanel extends Composite{ |
36 | 0 | private VerticalFlowPanel panel = new VerticalFlowPanel(); |
37 | 0 | private ListBox panelSelector = new ListBox(); |
38 | 0 | private LinkedHashMap<String, Widget> panelMap = new LinkedHashMap<String, Widget>(); |
39 | 0 | private SimplePanel content = new SimplePanel(); |
40 | |
|
41 | |
|
42 | 0 | private KSLabel searchForLabel = new KSLabel(Application.getApplicationContext().getMessage("swappablePanelSearchBy")); |
43 | 0 | private List<Callback<LookupMetadata>> lookupChangedCallbacks = new ArrayList<Callback<LookupMetadata>>(); |
44 | 0 | private LinkedHashMap<String, LookupMetadata> searchLookups = new LinkedHashMap<String, LookupMetadata>(); |
45 | |
|
46 | 0 | public SwappablePanel(LinkedHashMap<String, Widget> panelMap){ |
47 | 0 | this.panelMap = panelMap; |
48 | 0 | drawPanel(); |
49 | 0 | panelSelector.addChangeHandler(new ChangeHandler(){ |
50 | |
|
51 | |
@Override |
52 | |
public void onChange(ChangeEvent event) { |
53 | 0 | int i = panelSelector.getSelectedIndex(); |
54 | 0 | Widget p = SwappablePanel.this.panelMap.get(panelSelector.getItemText(i)); |
55 | 0 | content.setWidget(p); |
56 | 0 | if (lookupChangedCallbacks != null && |
57 | |
searchLookups != null) { |
58 | 0 | LookupMetadata selectedLookup = SwappablePanel.this.searchLookups.get( |
59 | |
panelSelector.getItemText(i)); |
60 | 0 | for (Callback<LookupMetadata> callback : lookupChangedCallbacks) { |
61 | 0 | callback.exec(selectedLookup); |
62 | |
} |
63 | |
} |
64 | 0 | } |
65 | |
}); |
66 | |
|
67 | 0 | this.initWidget(panel); |
68 | 0 | } |
69 | |
|
70 | |
private void drawPanel(){ |
71 | 0 | panel.add(searchForLabel); |
72 | 0 | panel.add(panelSelector); |
73 | 0 | panel.add(content); |
74 | |
|
75 | 0 | boolean contentSet = false; |
76 | 0 | for (String text : panelMap.keySet()) { |
77 | 0 | panelSelector.addItem(text); |
78 | 0 | if (!contentSet) { |
79 | 0 | content.setWidget(panelMap.get(text)); |
80 | 0 | contentSet = true; |
81 | |
} |
82 | |
} |
83 | 0 | } |
84 | |
|
85 | |
public void addLookupChangedCallback(Callback<LookupMetadata> callback) { |
86 | 0 | lookupChangedCallbacks.add(callback); |
87 | 0 | } |
88 | |
|
89 | |
public LinkedHashMap<String, LookupMetadata> getSearchLookups() { |
90 | 0 | return searchLookups; |
91 | |
} |
92 | |
|
93 | |
public void setSearchLookups(LinkedHashMap<String, LookupMetadata> searchLookups) { |
94 | 0 | this.searchLookups = searchLookups; |
95 | 0 | } |
96 | |
|
97 | |
} |