comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/DatacageWindow.java @ 848:9ecd58f619a4

Moved datacage into a separated widget. flys-client/trunk@2586 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 25 Aug 2011 16:03:26 +0000
parents 29966a53cfeb
children c0e13cf826d7
comparison
equal deleted inserted replaced
847:25a4bf390757 848:9ecd58f619a4
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 4
5 import com.google.gwt.user.client.rpc.AsyncCallback; 5 import com.smartgwt.client.widgets.Window;
6 6
7 import com.smartgwt.client.util.SC; 7 import de.intevation.flys.client.client.FLYSConstants;
8 8
9 import com.smartgwt.client.widgets.Window; 9 import de.intevation.flys.client.client.event.DatacageHandler;
10 import com.smartgwt.client.widgets.Button;
11
12 import com.smartgwt.client.widgets.tree.Tree;
13 import com.smartgwt.client.widgets.tree.TreeGrid;
14 import com.smartgwt.client.widgets.tree.TreeNode;
15
16 import com.smartgwt.client.types.TreeModelType;
17
18 import com.smartgwt.client.widgets.layout.Layout;
19 import com.smartgwt.client.widgets.layout.VLayout;
20
21 import com.smartgwt.client.widgets.events.ClickHandler;
22 import com.smartgwt.client.widgets.events.ClickEvent;
23
24 import com.smartgwt.client.widgets.grid.ListGridRecord;
25
26 import com.smartgwt.client.widgets.grid.events.RecordDoubleClickEvent;
27 import com.smartgwt.client.widgets.grid.events.RecordDoubleClickHandler;
28 10
29 import de.intevation.flys.client.shared.model.Artifact; 11 import de.intevation.flys.client.shared.model.Artifact;
30 import de.intevation.flys.client.shared.model.ArtifactDescription; 12 import de.intevation.flys.client.shared.model.ArtifactDescription;
31 import de.intevation.flys.client.shared.model.Data; 13 import de.intevation.flys.client.shared.model.Data;
14 import de.intevation.flys.client.shared.model.DataItem;
32 import de.intevation.flys.client.shared.model.DataList; 15 import de.intevation.flys.client.shared.model.DataList;
33 import de.intevation.flys.client.shared.model.DataItem; 16 import de.intevation.flys.client.shared.model.ToLoad;
34 import de.intevation.flys.client.shared.model.User; 17 import de.intevation.flys.client.shared.model.User;
35 import de.intevation.flys.client.shared.model.ToLoad; 18 import de.intevation.flys.client.shared.model.Recommendation;
36 19
37 import de.intevation.flys.client.client.FLYSConstants; 20 import java.util.List;
38 import de.intevation.flys.client.client.Config;
39 21
40 import de.intevation.flys.client.client.services.MetaDataService;
41 import de.intevation.flys.client.client.services.MetaDataServiceAsync;
42
43 import de.intevation.flys.client.shared.model.DataCageTree;
44 import de.intevation.flys.client.shared.model.DataCageNode;
45 import de.intevation.flys.client.shared.model.AttrList;
46
47 import java.util.ArrayList;
48 import java.util.List;
49 import java.util.Stack;
50 22
51 public class DatacageWindow 23 public class DatacageWindow
52 extends Window 24 extends Window
25 implements DatacageHandler
53 { 26 {
54 protected MetaDataServiceAsync metaDataService =
55 GWT.create(MetaDataService.class);
56
57 protected FLYSConstants messages = 27 protected FLYSConstants messages =
58 GWT.create(FLYSConstants.class); 28 GWT.create(FLYSConstants.class);
59 29
30 protected CollectionView view;
60 31
61 protected Artifact artifact; 32 public DatacageWindow(
62 protected User user; 33 Artifact artifact,
63 protected String outs; 34 User user,
64 35 String outs,
65 protected TreeGrid treeGrid; 36 CollectionView view
66 protected Tree tree; 37 ) {
67 38 this.view = view;
68 protected Layout layout; 39 setWidth(400);
69
70 protected ToLoad toLoad;
71
72
73 public DatacageWindow(Artifact artifact, User user) {
74 this(artifact, user, null);
75 }
76
77 public DatacageWindow(Artifact artifact, User user, String outs) {
78 this.artifact = artifact;
79 this.user = user;
80 this.outs = outs;
81
82 setWidth(300);
83 setHeight(500); 40 setHeight(500);
84 41
85 layout = new VLayout(); 42 DatacageWidget dw = new DatacageWidget(artifact, user, outs);
86 layout.setWidth100(); 43 dw.addDatacageHandler(this);
87 44
88 addItem(layout); 45 addItem(dw);
89 46
90 tree = new Tree(); 47 String river = findRiver(artifact);
91 tree.setModelType(TreeModelType.PARENT);
92 tree.setNameProperty("name");
93 tree.setIdField("id");
94 tree.setParentIdField("parent-id");
95
96 treeGrid = new TreeGrid();
97 treeGrid.setLoadDataOnDemand(false);
98 treeGrid.setWidth100();
99 treeGrid.setHeight100();
100 treeGrid.setShowRoot(false);
101
102 treeGrid.addRecordDoubleClickHandler(new RecordDoubleClickHandler() {
103 @Override
104 public void onRecordDoubleClick(RecordDoubleClickEvent event) {
105 doubleClickedOnTree(event);
106 }
107 });
108
109 layout.addMember(treeGrid);
110
111 // TODO: i18n + icon
112 Button plusBtn = new Button("+");
113 plusBtn.addClickHandler(new ClickHandler() {
114 @Override
115 public void onClick(ClickEvent event) {
116 plusClicked();
117 }
118 });
119
120 layout.addMember(plusBtn);
121
122 String river = findRiver();
123 // TODO: i18n 48 // TODO: i18n
124 setTitle("Datenkorb: " + river); 49 setTitle("Datenkorb: " + river);
125 setShowMinimizeButton(false); 50 setShowMinimizeButton(false);
126 setIsModal(true); 51 setIsModal(true);
127 setShowModalMask(true); 52 setShowModalMask(true);
128 setCanDragResize(true); 53 setCanDragResize(true);
129 54
130 centerInPage(); 55 centerInPage();
131
132 toLoad = new ToLoad();
133
134 triggerTreeBuilding();
135 } 56 }
136 57
137 public ToLoad getToLoad() { 58 @Override
138 return toLoad; 59 public void toLoad(ToLoad toLoad) {
60 destroy();
61 List<Recommendation> recs = toLoad.toRecommendations();
62 view.loadRecommendedArtifacts(
63 recs.toArray(new Recommendation[recs.size()]));
139 } 64 }
140 65
141 public void plusClicked() { 66 protected String findRiver(Artifact artifact) {
142 if (treeGrid == null) {
143 return;
144 }
145
146 ListGridRecord [] selection = treeGrid.getSelection();
147
148 if (selection != null) {
149 for (ListGridRecord record: selection) {
150 if (record instanceof TreeNode) {
151 collectToLoads((TreeNode)record);
152 }
153 }
154 }
155
156 if (!toLoad.isEmpty()) {
157 destroy();
158 }
159 }
160
161 protected void doubleClickedOnTree(RecordDoubleClickEvent event) {
162
163 TreeNode node = (TreeNode)event.getRecord();
164 collectToLoads(node);
165 destroy();
166 }
167
168 protected void collectToLoads(TreeNode node) {
169 Stack<TreeNode> stack = new Stack<TreeNode>();
170
171 stack.push(node);
172
173 while (!stack.isEmpty()) {
174 node = stack.pop();
175 String factory = node.getAttribute("factory");
176 if (factory != null) { // we need at least a factory
177 String artifact = node.getAttribute("artifact-id");
178 String out = node.getAttribute("out");
179 String name = node.getName();
180 String ids = node.getAttribute("ids");
181
182 toLoad.add(artifact, factory, out, name, ids);
183 }
184 TreeNode [] children = tree.getChildren(node);
185 if (children != null) {
186 for (TreeNode child: children) {
187 stack.push(child);
188 }
189 }
190 }
191 }
192
193
194 protected void triggerTreeBuilding() {
195 Config config = Config.getInstance();
196 String url = config.getServerUrl();
197 String locale = config.getLocale();
198
199 String artifactId = artifact.getUuid();
200 String userId = user != null ? user.identifier() : null;
201
202 metaDataService.getMetaData(
203 url, locale,
204 artifactId,
205 userId,
206 outs,
207 new AsyncCallback<DataCageTree>() {
208 public void onFailure(Throwable caught) {
209 GWT.log("Could not load meta data.");
210 SC.warn(caught.getMessage());
211 }
212
213 public void onSuccess(DataCageTree dcTree) {
214 GWT.log("Successfully loaded meta data.");
215 tree.setData(treeToData(dcTree));
216 treeGrid.setData(tree);
217 }
218 });
219 }
220
221 private static class IdGenerator {
222 protected int current;
223
224 public IdGenerator() {
225 }
226
227 public IdGenerator(int start) {
228 current = start;
229 }
230
231 public int next() {
232 return current++;
233 }
234 } // class IdGenerator
235
236 protected static void buildRecursive(
237 DataCageNode node,
238 int parentId,
239 IdGenerator idGenerator,
240 List<TreeNode> nodes
241 ) {
242 TreeNode tn = new TreeNode();
243 int id = idGenerator.next();
244 tn.setAttribute("parent-id", parentId);
245 tn.setAttribute("id", id);
246 // TODO: i18n
247 tn.setAttribute("name", node.getName());
248 nodes.add(tn);
249 AttrList attrs = node.getAttributes();
250 if (attrs != null) {
251 for (int i = 0, N = attrs.size(); i < N; ++i) {
252 String key = attrs.getKey(i);
253 String value = attrs.getValue(i);
254 tn.setAttribute(key, value);
255 }
256 }
257 List<DataCageNode> children = node.getChildren();
258 if (children != null) {
259 for (DataCageNode child: children) {
260 buildRecursive(child, id, idGenerator, nodes);
261 }
262 }
263 }
264
265
266 protected static TreeNode [] treeToData(DataCageTree tree) {
267 List<TreeNode> nodes = new ArrayList<TreeNode>();
268
269 DataCageNode root = tree.getRoot();
270
271 IdGenerator idGenerator = new IdGenerator();
272
273 buildRecursive(root, idGenerator.next(), idGenerator, nodes);
274
275 return nodes.toArray(new TreeNode[nodes.size()]);
276 }
277
278 protected String findRiver() {
279 67
280 ArtifactDescription adescr = artifact.getArtifactDescription(); 68 ArtifactDescription adescr = artifact.getArtifactDescription();
281 DataList [] data = adescr.getOldData(); 69 DataList [] data = adescr.getOldData();
282 70
283 if (data != null && data.length > 0) { 71 if (data != null && data.length > 0) {

http://dive4elements.wald.intevation.org