comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/fixation/FixMultiPeriodPanel.java @ 2883:c3feb721035c

Added ui provider for multiple date range input in fix analysis. flys-client/trunk@4555 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 30 May 2012 12:02:56 +0000
parents
children 88bca9f735c0
comparison
equal deleted inserted replaced
2882:63d1e5428a1c 2883:c3feb721035c
1 package de.intevation.flys.client.client.ui.fixation;
2
3 import java.util.List;
4 import java.util.ArrayList;
5 import java.util.Date;
6
7 import com.google.gwt.core.client.GWT;
8 import com.google.gwt.i18n.client.DateTimeFormat;
9 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
10
11 import com.smartgwt.client.util.SC;
12
13 import com.smartgwt.client.data.Record;
14
15 import com.smartgwt.client.widgets.Canvas;
16 import com.smartgwt.client.widgets.Label;
17 import com.smartgwt.client.widgets.Button;
18 import com.smartgwt.client.widgets.events.ClickEvent;
19 import com.smartgwt.client.widgets.events.ClickHandler;
20
21 import com.smartgwt.client.widgets.grid.ListGrid;
22 import com.smartgwt.client.widgets.grid.ListGridField;
23 import com.smartgwt.client.widgets.grid.ListGridRecord;
24
25 import com.smartgwt.client.widgets.layout.HLayout;
26 import com.smartgwt.client.widgets.layout.VLayout;
27
28 import com.smartgwt.client.widgets.form.DynamicForm;
29 import com.smartgwt.client.widgets.form.fields.DateRangeItem;
30 import com.smartgwt.client.widgets.form.fields.TextAreaItem;
31 import com.smartgwt.client.types.ListGridFieldType;
32
33 import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
34 import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
35
36 import de.intevation.flys.client.client.FLYSConstants;
37
38 import de.intevation.flys.client.shared.model.Data;
39 import de.intevation.flys.client.shared.model.DataItem;
40 import de.intevation.flys.client.shared.model.DataList;
41 import de.intevation.flys.client.shared.model.DefaultData;
42 import de.intevation.flys.client.shared.model.DefaultDataItem;
43
44 /**
45 * This UIProvider creates a panel for location or distance input.
46 *
47 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
48 */
49 public class FixMultiPeriodPanel
50 extends FixPeriodPanel
51 {
52
53 protected ListGrid elements;
54
55 protected String values;
56
57 public FixMultiPeriodPanel() {
58 this("", "");
59 }
60
61 public FixMultiPeriodPanel(String startName, String endName) {
62 super(startName, endName);
63 }
64
65 public Canvas createWidget(DataList data) {
66 HLayout input = new HLayout();
67 VLayout root = new VLayout();
68 VLayout grid = new VLayout();
69 VLayout layout = (VLayout) super.createWidget(data);
70 Button add = new Button("Add");
71 elements = new ListGrid();
72
73 add.addClickHandler(new ClickHandler() {
74 public void onClick(ClickEvent ce) {
75 Date f = inputPanel.getFromDate();
76 Date t = inputPanel.getToDate();
77 if (f == null || t == null) {
78 return;
79 }
80 DateRangeRecord drr = new DateRangeRecord(f, t);
81 elements.addData(drr);
82 }
83 });
84 layout.addMember(add);
85
86 Label sel = new Label("Selected");
87 sel.setHeight(25);
88 elements.setWidth(185);
89 elements.setHeight(120);
90 elements.setShowHeaderContextMenu(false);
91 elements.setCanReorderFields(false);
92 elements.setCanSort(false);
93 elements.setCanEdit(false);
94 ListGridField from = new ListGridField("from", "From");
95 ListGridField to = new ListGridField("to", "To");
96 from.setWidth(70);
97 to.setWidth(70);
98
99 final ListGridField removeField =
100 new ListGridField("_removeRecord", "Remove Record"){{
101 setType(ListGridFieldType.ICON);
102 setIcon(GWT.getHostPageBaseURL() + MSG.removeFeature());
103 setCanEdit(false);
104 setCanFilter(false);
105 setCanSort(false);
106 setCanGroupBy(false);
107 setCanFreeze(false);
108 setWidth(25);
109 }};
110
111 elements.addRecordClickHandler(new RecordClickHandler() {
112 public void onRecordClick(final RecordClickEvent event) {
113 // Just handle remove-clicks
114 if(!event.getField().getName().equals(removeField.getName())) {
115 return;
116 }
117 event.getViewer().removeData(event.getRecord());
118 }
119 });
120
121 elements.setFields(from, to, removeField);
122
123 grid.addMember(sel);
124 grid.addMember(elements);
125 input.addMember(layout);
126 input.addMember(grid);
127 root.addMember(input);
128
129 return root;
130 }
131
132 @Override
133 public Canvas createOld(DataList dataList) {
134 HLayout layout = new HLayout();
135 layout.setWidth("400px");
136 VLayout vLayout = new VLayout();
137 vLayout.setWidth(130);
138 Label label = new Label(dataList.getLabel());
139 label.setWidth("200px");
140 label.setHeight(25);
141
142 List<Data> items = dataList.getAll();
143 Data str = getData(items, "ana_data");
144 DataItem[] strItems = str.getItems();
145
146 String[] pairs = strItems[0].getLabel().split(";");
147 for (int i = 0; i < pairs.length; i++) {
148 String[] vals = pairs[i].split(",");
149 try {
150 long f = Long.valueOf(vals[0]).longValue();
151 long t = Long.valueOf(vals[1]).longValue();
152 Date from = new Date(f);
153 Date to = new Date(t);
154 String fromString =
155 DateTimeFormat.getMediumDateFormat().format(from);
156 String toString =
157 DateTimeFormat.getMediumDateFormat().format(to);
158
159 Label dateLabel = new Label(fromString + " - " + toString);
160 dateLabel.setHeight(20);
161 vLayout.addMember(dateLabel);
162 }
163 catch(NumberFormatException nfe) {
164 }
165 }
166 Canvas back = getBackButton(dataList.getState());
167 layout.addMember(label);
168 layout.addMember(vLayout);
169 layout.addMember(back);
170
171 return layout;
172 }
173
174
175 /**
176 * This method returns the selected data.
177 *
178 * @return the selected/inserted data.
179 */
180 public Data[] getData() {
181 List<Data> data = new ArrayList<Data>();
182
183 boolean valid = saveDateValues();
184 if(valid) {
185 DataItem item = new DefaultDataItem("ana_data", null, this.values);
186 data.add(new DefaultData(
187 "ana_data",
188 null,
189 null,
190 new DataItem[] { item }));
191 }
192 return (Data[]) data.toArray(new Data[data.size()]);
193 }
194
195
196
197 public void setFilter(FixFilter filter) {
198 this.fixFilter = filter;
199 }
200
201
202 public FixFilter getFilter() {
203 return this.fixFilter;
204 }
205
206 protected boolean saveDateValues() {
207 ListGridRecord[] lgr = elements.getRecords();
208 if (lgr.length == 0) {
209 return false;
210 }
211 String data = "";
212 for (int i = 0; i < lgr.length; i++) {
213 DateRangeRecord drr = (DateRangeRecord) lgr[i];
214 data += drr.getFrom() + "," + drr.getTo();
215 data += ";";
216 }
217 GWT.log(data);
218 values = data;
219 return true;
220 }
221
222
223 protected static class DateRangeRecord extends ListGridRecord {
224 protected Date from;
225 protected Date to;
226
227 protected final static String FROM_FIELD = "from";
228 protected final static String TO_FIELD = "to";
229
230 public DateRangeRecord (Date from, Date to) {
231 setFrom(from);
232 setTo(to);
233 }
234
235 public void setFrom(Date from) {
236 this.from = from;
237 setAttribute(
238 FROM_FIELD,
239 DateTimeFormat.getMediumDateFormat().format(from));
240 }
241
242
243 public void setTo(Date to) {
244 this.to = to;
245 setAttribute(
246 TO_FIELD,
247 DateTimeFormat.getMediumDateFormat().format(to));
248 }
249
250
251 public long getFrom() {
252 return this.from.getTime();
253 }
254
255
256 public long getTo() {
257 return this.to.getTime();
258 }
259 }
260 }

http://dive4elements.wald.intevation.org