1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.lum.lu.ui.tools.client.widgets.itemlist; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
22 | |
|
23 | |
import com.google.gwt.event.logical.shared.ValueChangeEvent; |
24 | |
import com.google.gwt.event.logical.shared.ValueChangeHandler; |
25 | |
import com.google.gwt.event.shared.HandlerRegistration; |
26 | |
import com.google.gwt.user.client.ui.Composite; |
27 | |
import com.google.gwt.user.client.ui.FlexTable; |
28 | |
import com.google.gwt.user.client.ui.HasValue; |
29 | |
import com.google.gwt.user.client.ui.SimplePanel; |
30 | |
import com.google.gwt.user.client.ui.VerticalPanel; |
31 | |
import com.google.gwt.user.client.ui.Widget; |
32 | |
|
33 | 0 | public class ItemList<V extends ItemValue<V>> extends Composite implements HasValue<List<V>> { |
34 | |
|
35 | |
private List<V> value; |
36 | 0 | protected SimplePanel mainPanel = new SimplePanel(); |
37 | 0 | private List<ValueChangeHandler<List<V>>> valueChangeHandlers = |
38 | |
new ArrayList<ValueChangeHandler<List<V>>>(3); |
39 | 0 | private boolean editable = true; |
40 | |
|
41 | 0 | public ItemList() { |
42 | 0 | super.initWidget(mainPanel); |
43 | 0 | } |
44 | |
|
45 | |
@Override |
46 | |
public List<V> getValue() { |
47 | 0 | return value; |
48 | |
} |
49 | |
|
50 | |
@Override |
51 | |
public void setValue(List<V> value, boolean fireEvents) { |
52 | 0 | this.value = value; |
53 | 0 | redraw(); |
54 | 0 | if (fireEvents) { |
55 | 0 | callHandlers(); |
56 | |
} |
57 | 0 | } |
58 | |
|
59 | |
@Override |
60 | |
public void setValue(List<V> value) { |
61 | 0 | this.value = value; |
62 | 0 | redraw(); |
63 | 0 | callHandlers(); |
64 | 0 | } |
65 | |
|
66 | |
private void callHandlers() { |
67 | 0 | if (valueChangeHandlers != null) { |
68 | 0 | MyGwtEvent myEvent = new MyGwtEvent(value); |
69 | 0 | for (ValueChangeHandler<List<V>> handler : valueChangeHandlers) { |
70 | 0 | handler.onValueChange(myEvent); |
71 | |
} |
72 | |
} |
73 | 0 | } |
74 | |
|
75 | |
private void redraw() { |
76 | 0 | VerticalPanel itemsPanel = new VerticalPanel(); |
77 | 0 | FlexTable itemTable = new FlexTable(); |
78 | 0 | int row = 0; |
79 | 0 | mainPanel.clear(); |
80 | 0 | if (value != null) { |
81 | 0 | for (V val : value) { |
82 | 0 | val.setDeleteCallback(new Callback<V>() { |
83 | |
@Override |
84 | |
public void exec(V itemDeleted) { |
85 | 0 | value.remove(itemDeleted); |
86 | 0 | redraw(); |
87 | 0 | callHandlers(); |
88 | 0 | } |
89 | |
}); |
90 | 0 | val.setEditable(isEditable()); |
91 | 0 | List<Widget> displayWidgets = val.generateDisplayWidgets(); |
92 | 0 | if (displayWidgets != null) { |
93 | 0 | int column = 0; |
94 | 0 | for (Widget displayWidget : displayWidgets) { |
95 | 0 | itemTable.setWidget(row, column, displayWidget); |
96 | 0 | column++; |
97 | |
} |
98 | |
} |
99 | 0 | itemsPanel.add(itemTable); |
100 | 0 | row++; |
101 | 0 | } |
102 | 0 | mainPanel.setWidget(itemsPanel); |
103 | |
} |
104 | 0 | } |
105 | |
|
106 | |
@Override |
107 | |
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<List<V>> handler) { |
108 | 0 | valueChangeHandlers.add(handler); |
109 | 0 | return null; |
110 | |
} |
111 | |
|
112 | |
public class MyGwtEvent extends ValueChangeEvent<List<V>> { |
113 | 0 | public MyGwtEvent(List<V> value) { |
114 | 0 | super(value); |
115 | 0 | } |
116 | |
} |
117 | |
|
118 | |
public boolean isEditable() { |
119 | 0 | return editable; |
120 | |
} |
121 | |
|
122 | |
public void setEditable(boolean editable) { |
123 | 0 | this.editable = editable; |
124 | 0 | } |
125 | |
|
126 | |
} |