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