9063
|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde |
|
2 * Software engineering by Intevation GmbH |
|
3 * |
|
4 * This file is Free Software under the GNU AGPL (>=v3) |
|
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the |
|
6 * documentation coming with Dive4Elements River for details. |
|
7 */ |
|
8 |
|
9 package org.dive4elements.river.client.client.ui.sinfo; |
|
10 |
|
11 import java.util.ArrayList; |
|
12 import java.util.List; |
|
13 |
|
14 import org.dive4elements.river.client.client.services.SedimentLoadInfoService; |
|
15 import org.dive4elements.river.client.client.services.SedimentLoadInfoServiceAsync; |
|
16 import org.dive4elements.river.client.client.ui.AbstractUIProvider; |
9076
|
17 import org.dive4elements.river.client.client.ui.FromToTableHelperPanel; |
|
18 import org.dive4elements.river.client.client.ui.FromToTableHelperPanel.IColumnClickHandler; |
9063
|
19 import org.dive4elements.river.client.shared.model.Data; |
|
20 import org.dive4elements.river.client.shared.model.DataItem; |
|
21 import org.dive4elements.river.client.shared.model.DataList; |
|
22 import org.dive4elements.river.client.shared.model.DefaultData; |
|
23 import org.dive4elements.river.client.shared.model.DefaultDataItem; |
|
24 |
|
25 import com.google.gwt.core.client.GWT; |
|
26 import com.smartgwt.client.data.Record; |
|
27 import com.smartgwt.client.types.ListGridFieldType; |
|
28 import com.smartgwt.client.widgets.Button; |
|
29 import com.smartgwt.client.widgets.Canvas; |
|
30 import com.smartgwt.client.widgets.Label; |
|
31 import com.smartgwt.client.widgets.events.ClickEvent; |
|
32 import com.smartgwt.client.widgets.events.ClickHandler; |
|
33 import com.smartgwt.client.widgets.form.DynamicForm; |
|
34 import com.smartgwt.client.widgets.form.fields.TextItem; |
|
35 import com.smartgwt.client.widgets.form.validator.IsIntegerValidator; |
|
36 import com.smartgwt.client.widgets.grid.ListGrid; |
|
37 import com.smartgwt.client.widgets.grid.ListGridField; |
|
38 import com.smartgwt.client.widgets.grid.ListGridRecord; |
|
39 import com.smartgwt.client.widgets.grid.events.RecordClickEvent; |
|
40 import com.smartgwt.client.widgets.grid.events.RecordClickHandler; |
|
41 import com.smartgwt.client.widgets.layout.HLayout; |
|
42 import com.smartgwt.client.widgets.layout.VLayout; |
|
43 |
9074
|
44 // FIXME: copy of SedLoadEpochPanel |
9063
|
45 public class CollisionLoadEpochPanel extends AbstractUIProvider { |
|
46 protected SedimentLoadInfoServiceAsync sedLoadInfoService = GWT.create(SedimentLoadInfoService.class); |
|
47 |
|
48 protected ListGrid elements; |
|
49 private TextItem start; |
|
50 private TextItem end; |
|
51 |
9078
|
52 private final String getDatakey() { |
|
53 return "epochs"; |
|
54 } |
|
55 |
9076
|
56 protected List<String> validYears = new ArrayList<String>(); |
9063
|
57 |
|
58 public Canvas createWidget(final DataList data) { |
|
59 final HLayout input = new HLayout(); |
|
60 final VLayout root = new VLayout(); |
|
61 final VLayout grid = new VLayout(); |
|
62 final VLayout intFields = new VLayout(); |
|
63 final Button add = new Button(this.MSG.add_date()); |
|
64 this.elements = new ListGrid(); |
|
65 |
|
66 final Label title = new Label(data.get(0).getDescription()); |
|
67 title.setHeight("25px"); |
|
68 |
|
69 final DynamicForm form = new DynamicForm(); |
|
70 form.setNumCols(4); |
|
71 this.start = new TextItem(this.MSG.from()); |
|
72 this.start.setWidth(60); |
|
73 this.start.setValidators(new IsIntegerValidator()); |
|
74 this.end = new TextItem(this.MSG.to()); |
|
75 this.end.setWidth(60); |
|
76 this.end.setValidators(new IsIntegerValidator()); |
|
77 form.setFields(this.start, this.end); |
|
78 add.addClickHandler(new ClickHandler() { |
|
79 @Override |
|
80 public void onClick(final ClickEvent ce) { |
|
81 final String v1 = CollisionLoadEpochPanel.this.start.getValueAsString(); |
|
82 final String v2 = CollisionLoadEpochPanel.this.end.getValueAsString(); |
|
83 if (v1 == null || v2 == null) { |
|
84 return; |
|
85 } |
|
86 if (!isValidEpoch(v1, v2)) { |
|
87 return; |
|
88 } |
|
89 final ListGridRecord r = new ListGridRecord(); |
|
90 r.setAttribute("from", v1); |
|
91 r.setAttribute("to", v2); |
|
92 CollisionLoadEpochPanel.this.elements.addData(r); |
|
93 } |
|
94 }); |
|
95 |
|
96 final Label sel = new Label(this.MSG.select()); |
|
97 sel.setHeight(25); |
|
98 this.elements.setWidth(185); |
|
99 this.elements.setHeight(120); |
|
100 this.elements.setShowHeaderContextMenu(false); |
|
101 this.elements.setCanReorderFields(false); |
|
102 this.elements.setCanSort(false); |
|
103 this.elements.setCanEdit(false); |
|
104 final ListGridField from = new ListGridField("from", this.MSG.from()); |
|
105 final ListGridField to = new ListGridField("to", this.MSG.to()); |
|
106 from.setWidth(70); |
|
107 to.setWidth(70); |
|
108 |
|
109 final ListGridField removeField = new ListGridField("_removeRecord", "Remove Record") { |
|
110 { |
|
111 setType(ListGridFieldType.ICON); |
|
112 setIcon(GWT.getHostPageBaseURL() + CollisionLoadEpochPanel.this.MSG.removeFeature()); |
|
113 setCanEdit(false); |
|
114 setCanFilter(false); |
|
115 setCanSort(false); |
|
116 setCanGroupBy(false); |
|
117 setCanFreeze(false); |
|
118 setWidth(25); |
|
119 } |
|
120 }; |
|
121 |
|
122 this.elements.addRecordClickHandler(new RecordClickHandler() { |
|
123 @Override |
|
124 public void onRecordClick(final RecordClickEvent event) { |
|
125 // Just handle remove-clicks |
|
126 if (!event.getField().getName().equals(removeField.getName())) { |
|
127 return; |
|
128 } |
|
129 event.getViewer().removeData(event.getRecord()); |
|
130 } |
|
131 }); |
|
132 |
|
133 this.elements.setFields(from, to, removeField); |
|
134 |
|
135 intFields.addMember(form); |
|
136 intFields.addMember(add); |
|
137 grid.addMember(sel); |
|
138 grid.addMember(this.elements); |
|
139 input.addMember(intFields); |
|
140 input.addMember(grid); |
|
141 root.addMember(title); |
|
142 root.addMember(input); |
|
143 |
|
144 return root; |
|
145 } |
|
146 |
|
147 @Override |
|
148 public Canvas createOld(final DataList dataList) { |
|
149 final HLayout layout = new HLayout(); |
|
150 layout.setWidth("400px"); |
|
151 final VLayout vLayout = new VLayout(); |
|
152 vLayout.setWidth(130); |
|
153 final Label label = new Label(dataList.getLabel()); |
|
154 label.setWidth("200px"); |
|
155 label.setHeight(25); |
|
156 |
|
157 final List<Data> items = dataList.getAll(); |
9078
|
158 final Data str = getData(items, getDatakey()); |
9063
|
159 final DataItem[] strItems = str.getItems(); |
|
160 |
|
161 final String[] pairs = strItems[0].getLabel().split(";"); |
|
162 for (final String pair : pairs) { |
|
163 final String[] vals = pair.split(","); |
|
164 final Label dateLabel = new Label(vals[0] + " - " + vals[1]); |
|
165 dateLabel.setHeight(20); |
|
166 vLayout.addMember(dateLabel); |
|
167 } |
|
168 final Canvas back = getBackButton(dataList.getState()); |
|
169 layout.addMember(label); |
|
170 layout.addMember(vLayout); |
|
171 layout.addMember(back); |
|
172 |
|
173 return layout; |
|
174 } |
|
175 |
|
176 @Override |
|
177 public Canvas create(final DataList data) { |
|
178 final VLayout layout = new VLayout(); |
|
179 final Canvas submit = getNextButton(); |
|
180 final Canvas widget = createWidget(data); |
|
181 |
|
182 layout.addMember(widget); |
|
183 layout.addMember(submit); |
|
184 |
9076
|
185 final IColumnClickHandler fromHandler = createHandler(this.start); |
|
186 final IColumnClickHandler toHandler = createHandler(this.end); |
9063
|
187 |
9078
|
188 final FromToTableHelperPanel helper = new FromToTableHelperPanel(data, getDatakey(), this.MSG, fromHandler, toHandler); |
9076
|
189 this.validYears = helper.getKeycolEntries(); |
|
190 final Canvas table = helper.getTable(); |
|
191 |
|
192 this.helperContainer.addMember(table); |
9063
|
193 return layout; |
|
194 } |
|
195 |
9076
|
196 private IColumnClickHandler createHandler(final TextItem field) { |
|
197 final IColumnClickHandler handler = new IColumnClickHandler() { |
9063
|
198 |
|
199 @Override |
9076
|
200 public void columnClicked(final String value) { |
|
201 field.setValue(value); |
9063
|
202 } |
9076
|
203 }; |
9063
|
204 |
9076
|
205 return handler; |
9063
|
206 } |
|
207 |
|
208 @Override |
|
209 protected Data[] getData() { |
|
210 final List<Data> data = new ArrayList<Data>(); |
|
211 |
|
212 final ListGridRecord[] lgr = this.elements.getRecords(); |
|
213 if (lgr.length == 0) { |
|
214 return new Data[0]; |
|
215 } |
|
216 String d = ""; |
|
217 for (final ListGridRecord element : lgr) { |
|
218 final Record r = element; |
|
219 d += r.getAttribute("from") + "," + r.getAttribute("to"); |
|
220 d += ";"; |
|
221 } |
|
222 |
9078
|
223 final DataItem item = new DefaultDataItem(getDatakey(), null, d); |
|
224 data.add(new DefaultData(getDatakey(), null, null, new DataItem[] { item })); |
9063
|
225 return data.toArray(new Data[data.size()]); |
|
226 } |
|
227 |
|
228 /* |
|
229 * Validate the epoch input. We do this here and not in an overridden |
|
230 * validate method as we want to validate before an epoch is added |
|
231 * to the list of epochs. |
|
232 */ |
|
233 protected boolean isValidEpoch(final String y1, final String y2) { |
|
234 // First check that both are integer |
|
235 int iY1; |
|
236 int iY2; |
|
237 final List<String> errors = new ArrayList<String>(); |
|
238 try { |
|
239 iY1 = Integer.parseInt(y1); |
|
240 } |
|
241 catch (final NumberFormatException e) { |
9078
|
242 errors.add(this.MSG.wrongFormat() + ": " + y1); |
9063
|
243 } |
|
244 try { |
|
245 iY2 = Integer.parseInt(y2); |
|
246 } |
|
247 catch (final NumberFormatException e) { |
9078
|
248 errors.add(this.MSG.wrongFormat() + ": " + y2); |
9063
|
249 } |
|
250 if (!errors.isEmpty()) { |
|
251 showErrors(errors); |
|
252 return false; |
|
253 } |
|
254 boolean startIsGood = false; |
|
255 boolean endIsGood = false; |
|
256 for (final String validYear : this.validYears) { |
|
257 if (startIsGood || y1.equals(validYear)) { |
|
258 startIsGood = true; |
|
259 } |
|
260 if (endIsGood || y2.equals(validYear)) { |
|
261 endIsGood = true; |
|
262 } |
|
263 if (startIsGood && endIsGood) { |
|
264 break; |
|
265 } |
|
266 /* |
|
267 * alternative check if data lies in between |
|
268 * int aYear = Integer.parseInt(validYear); |
|
269 * if (aYear >= iY1 && aYear <= iY2) { |
|
270 * isGood = true; |
|
271 * break; |
|
272 * } |
|
273 */ |
|
274 } |
|
275 if (!startIsGood) { |
9078
|
276 String tmp = this.MSG.no_data_for_year(); |
9063
|
277 tmp = tmp.replace("$1", y1); |
|
278 errors.add(tmp); |
|
279 } |
|
280 if (!endIsGood) { |
9078
|
281 String tmp = this.MSG.no_data_for_year(); |
9063
|
282 tmp = tmp.replace("$1", y2); |
|
283 errors.add(tmp); |
|
284 } |
|
285 if (!errors.isEmpty()) { |
|
286 showErrors(errors); |
|
287 return false; |
|
288 } |
|
289 return true; |
|
290 } |
|
291 } |