Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/SelectProvider.java @ 4573:b87073a05f9d
flys-client: Patch to render combobox options as clickable links.
The way of passing data arguments to the links and further to
the Artifact feeding service is somewhat hacked and should be
refactored (later...).
author | Christian Lins <christian.lins@intevation.de> |
---|---|
date | Tue, 27 Nov 2012 12:50:10 +0100 |
parents | 03de5c424f95 |
children | 32a4651eef93 |
comparison
equal
deleted
inserted
replaced
4572:4bd42fe098b4 | 4573:b87073a05f9d |
---|---|
1 package de.intevation.flys.client.client.ui; | 1 package de.intevation.flys.client.client.ui; |
2 | 2 |
3 import com.google.gwt.core.client.GWT; | 3 import com.google.gwt.core.client.GWT; |
4 | |
5 import com.smartgwt.client.types.VerticalAlignment; | 4 import com.smartgwt.client.types.VerticalAlignment; |
6 import com.smartgwt.client.widgets.Canvas; | 5 import com.smartgwt.client.widgets.Canvas; |
7 import com.smartgwt.client.widgets.Label; | 6 import com.smartgwt.client.widgets.Label; |
8 import com.smartgwt.client.widgets.form.DynamicForm; | 7 import com.smartgwt.client.widgets.form.DynamicForm; |
8 import com.smartgwt.client.widgets.form.fields.FormItem; | |
9 import com.smartgwt.client.widgets.form.fields.LinkItem; | |
9 import com.smartgwt.client.widgets.form.fields.SelectItem; | 10 import com.smartgwt.client.widgets.form.fields.SelectItem; |
11 import com.smartgwt.client.widgets.form.fields.SpacerItem; | |
12 import com.smartgwt.client.widgets.form.fields.events.ClickEvent; | |
13 import com.smartgwt.client.widgets.form.fields.events.ClickHandler; | |
10 import com.smartgwt.client.widgets.layout.HLayout; | 14 import com.smartgwt.client.widgets.layout.HLayout; |
11 import com.smartgwt.client.widgets.layout.VLayout; | 15 import com.smartgwt.client.widgets.layout.VLayout; |
12 | 16 |
13 import de.intevation.flys.client.client.FLYSConstants; | 17 import de.intevation.flys.client.client.FLYSConstants; |
18 import de.intevation.flys.client.client.event.StepForwardEvent; | |
14 import de.intevation.flys.client.shared.model.Data; | 19 import de.intevation.flys.client.shared.model.Data; |
15 import de.intevation.flys.client.shared.model.DataItem; | 20 import de.intevation.flys.client.shared.model.DataItem; |
16 import de.intevation.flys.client.shared.model.DataList; | 21 import de.intevation.flys.client.shared.model.DataList; |
17 import de.intevation.flys.client.shared.model.DefaultData; | 22 import de.intevation.flys.client.shared.model.DefaultData; |
18 import de.intevation.flys.client.shared.model.DefaultDataItem; | 23 import de.intevation.flys.client.shared.model.DefaultDataItem; |
19 | 24 |
25 import java.util.ArrayList; | |
20 import java.util.Iterator; | 26 import java.util.Iterator; |
21 import java.util.LinkedHashMap; | 27 import java.util.LinkedHashMap; |
22 import java.util.Map; | 28 import java.util.Map; |
23 | 29 |
24 | 30 |
34 | 40 |
35 /** The message class that provides i18n strings.*/ | 41 /** The message class that provides i18n strings.*/ |
36 protected FLYSConstants messages = GWT.create(FLYSConstants.class); | 42 protected FLYSConstants messages = GWT.create(FLYSConstants.class); |
37 | 43 |
38 /** The combobox.*/ | 44 /** The combobox.*/ |
39 protected DynamicForm form; | 45 protected DynamicForm form = new DynamicForm(); |
46 | |
47 public static final int COMBOBOX_THRESHOLD = 20; | |
40 | 48 |
41 | 49 |
42 /** | 50 /** |
43 * This method currently returns a | 51 * This method currently returns a |
44 * {@link com.smartgwt.client.widgets.form.DynamicForm} that contains all | 52 * {@link com.smartgwt.client.widgets.form.DynamicForm} that contains all |
48 * | 56 * |
49 * @return a combobox. | 57 * @return a combobox. |
50 */ | 58 */ |
51 @Override | 59 @Override |
52 public Canvas create(DataList data) { | 60 public Canvas create(DataList data) { |
53 VLayout v = new VLayout(); | 61 if (data.size() > COMBOBOX_THRESHOLD) { |
54 v.setMembersMargin(10); | 62 VLayout v = new VLayout(); |
55 | 63 v.setMembersMargin(10); |
56 Canvas content = createWidget(data); | 64 v.addMember(createComboboxWidget(data)); |
57 Canvas button = getNextButton(); | 65 v.addMember(getNextButton()); |
58 | 66 return v; |
59 v.addMember(content); | 67 } |
60 v.addMember(button); | 68 else { |
61 | 69 return createListWidget(data); |
62 return v; | 70 } |
71 } | |
72 | |
73 | |
74 protected Canvas createWidget(DataList data) { | |
75 return create(data); | |
63 } | 76 } |
64 | 77 |
65 | 78 |
66 @Override | 79 @Override |
67 public Canvas createOld(DataList dataList) { | 80 public Canvas createOld(DataList dataList) { |
96 | 109 |
97 return layout; | 110 return layout; |
98 } | 111 } |
99 | 112 |
100 | 113 |
101 /** | 114 protected Canvas createListWidget(DataList data) { |
102 * This method creates the content of the widget. | 115 VLayout layout = new VLayout(); |
103 * | |
104 * @param data The {@link DataList} object. | |
105 * | |
106 * @return a combobox. | |
107 */ | |
108 protected Canvas createWidget(DataList data) { | |
109 GWT.log("SelectProvider - create()"); | |
110 | |
111 VLayout layout = new VLayout(); | |
112 layout.setAlign(VerticalAlignment.TOP); | 116 layout.setAlign(VerticalAlignment.TOP); |
113 layout.setHeight(25); | 117 layout.setHeight(25); |
114 | 118 layout.setWidth("100%"); |
115 LinkedHashMap<String, String> initial = new LinkedHashMap<String, String>(); | 119 |
116 | 120 VLayout formLayout = new VLayout(); |
117 form = new DynamicForm(); | 121 formLayout.setLayoutTopMargin(20); |
118 | 122 formLayout.setLayoutLeftMargin(50); |
119 int size = data.size(); | 123 |
120 | 124 ClickHandler handler = new ClickHandler() { |
121 for (int i = 0; i < size; i++) { | 125 @Override |
126 public void onClick(ClickEvent event) { | |
127 LinkItem li = (LinkItem)event.getItem(); | |
128 String attr = li.getAttribute(li.getName()); | |
129 GWT.log("li.getTarget: " + attr + " " + li.getName()); | |
130 DataItem item = new DefaultDataItem( | |
131 attr, | |
132 null, | |
133 attr); | |
134 | |
135 Data d = new DefaultData( | |
136 li.getName(), | |
137 null, | |
138 null, | |
139 new DataItem [] { item }); | |
140 | |
141 Data [] odata = getData(); | |
142 Data [] ndata = new Data[odata.length+1]; | |
143 System.arraycopy(odata, 0, ndata, 0, odata.length); | |
144 ndata[odata.length] = d; | |
145 | |
146 fireStepForwardEvent(new StepForwardEvent(ndata)); | |
147 } | |
148 }; | |
149 | |
150 for (int i = 0, size = data.size(); i < size; i++) { | |
122 Data d = data.get(i); | 151 Data d = data.get(i); |
123 | 152 |
124 Label label = new Label(d.getDescription()); | 153 Label label = new Label(d.getDescription()); |
125 label.setValign(VerticalAlignment.TOP); | 154 label.setValign(VerticalAlignment.TOP); |
126 label.setHeight(20); | 155 label.setHeight(20); |
127 label.setWidth(400); | 156 label.setWidth(400); |
128 | 157 |
158 LinkedHashMap<String, String> initial = new LinkedHashMap<String, String>(); | |
159 ArrayList<FormItem> formItems = new ArrayList<FormItem>(); | |
160 | |
161 for (DataItem item: d.getItems()) { | |
162 initial.put(item.getLabel().replace(' ', '_'), item.getStringValue()); | |
163 GWT.log("put: " + item.getLabel().replace(' ', '_') + "=" + item.getStringValue()); | |
164 | |
165 LinkItem link = new LinkItem(d.getLabel()); | |
166 link.setLinkTitle(item.getLabel()); // i18n text of the link | |
167 link.setAttribute(d.getLabel(), item.getStringValue()); // e.g. "calculation_mode":"foo" | |
168 link.setShowTitle(false); | |
169 link.setEndRow(true); | |
170 link.setWidth("350px"); | |
171 | |
172 SpacerItem space = new SpacerItem(); | |
173 space.setWidth(15); | |
174 formItems.add(space); | |
175 formItems.add(link); | |
176 | |
177 link.addClickHandler(handler); | |
178 } | |
179 | |
180 form.setFields(formItems.toArray(new FormItem[0])); | |
181 form.setValues(initial); | |
182 | |
183 layout.addMember(label); | |
184 layout.addMember(form); | |
185 } | |
186 return layout; | |
187 } | |
188 | |
189 protected Canvas createComboboxWidget(DataList data) { | |
190 GWT.log("SelectProvider.createComboboxWidget()"); | |
191 | |
192 VLayout layout = new VLayout(); | |
193 layout.setAlign(VerticalAlignment.TOP); | |
194 layout.setHeight(25); | |
195 | |
196 LinkedHashMap<String, String> initial = new LinkedHashMap<String, String>(); | |
197 | |
198 int size = data.size(); | |
199 | |
200 for (int i = 0; i < size; i++) { | |
201 Data d = data.get(i); | |
202 | |
203 Label label = new Label(d.getDescription()); | |
204 label.setValign(VerticalAlignment.TOP); | |
205 label.setHeight(20); | |
206 label.setWidth(400); | |
207 | |
129 SelectItem combobox = new SelectItem(d.getLabel()); | 208 SelectItem combobox = new SelectItem(d.getLabel()); |
130 combobox.setWidth(250); | 209 combobox.setWidth(250); |
131 | 210 |
132 LinkedHashMap<String, String> it = new LinkedHashMap<String, String>(); | 211 LinkedHashMap<String, String> it = new LinkedHashMap<String, String>(); |
133 | 212 |
140 if (defValue != null && defValue.length() > 0) { | 219 if (defValue != null && defValue.length() > 0) { |
141 initial.put(d.getLabel(), def.getStringValue()); | 220 initial.put(d.getLabel(), def.getStringValue()); |
142 defaultSet = true; | 221 defaultSet = true; |
143 } | 222 } |
144 | 223 |
145 // I was here. Me 2. | |
146 for (DataItem item: d.getItems()) { | 224 for (DataItem item: d.getItems()) { |
147 if (!defaultSet && first) { | 225 if (!defaultSet && first) { |
148 initial.put(d.getLabel(), item.getStringValue()); | 226 initial.put(d.getLabel(), item.getStringValue()); |
149 first = false; | 227 first = false; |
150 } | 228 } |
176 | 254 |
177 Data[] list = new Data[values.size()]; | 255 Data[] list = new Data[values.size()]; |
178 int i = 0; | 256 int i = 0; |
179 | 257 |
180 while (keys.hasNext()) { | 258 while (keys.hasNext()) { |
181 String fieldname = (String) keys.next(); | 259 String fieldname = ((String) keys.next()).replace('_', ' '); |
182 String selection = (String) values.get(fieldname); | 260 String selection = (String) values.get(fieldname); |
183 | 261 |
184 DataItem item = new DefaultDataItem(fieldname, null, selection); | 262 DataItem item = new DefaultDataItem(fieldname, null, selection); |
185 | 263 |
186 list[i++] = new DefaultData( | 264 list[i++] = new DefaultData( |