Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/client/FLYS.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-client/src/main/java/org/dive4elements/river/client/client/FLYS.java@821a02bbfb4e |
children | 172338b1407f |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.client.client; | |
2 | |
3 import com.google.gwt.core.client.EntryPoint; | |
4 import com.google.gwt.core.client.GWT; | |
5 import com.google.gwt.event.shared.UmbrellaException; | |
6 import com.google.gwt.user.client.rpc.AsyncCallback; | |
7 import com.google.gwt.xml.client.XMLParser; | |
8 | |
9 import com.smartgwt.client.util.SC; | |
10 import com.smartgwt.client.widgets.HTMLPane; | |
11 import com.smartgwt.client.widgets.Window; | |
12 import com.smartgwt.client.widgets.events.CloseClickEvent; | |
13 import com.smartgwt.client.widgets.events.CloseClickHandler; | |
14 import com.smartgwt.client.widgets.layout.VLayout; | |
15 | |
16 import org.dive4elements.river.client.client.event.CollectionChangeEvent; | |
17 import org.dive4elements.river.client.client.event.CollectionChangeHandler; | |
18 import org.dive4elements.river.client.client.services.ArtifactService; | |
19 import org.dive4elements.river.client.client.services.ArtifactServiceAsync; | |
20 import org.dive4elements.river.client.client.services.CreateCollectionService; | |
21 import org.dive4elements.river.client.client.services.CreateCollectionServiceAsync; | |
22 import org.dive4elements.river.client.client.services.DescribeCollectionService; | |
23 import org.dive4elements.river.client.client.services.DescribeCollectionServiceAsync; | |
24 import org.dive4elements.river.client.client.services.GetArtifactService; | |
25 import org.dive4elements.river.client.client.services.GetArtifactServiceAsync; | |
26 import org.dive4elements.river.client.client.services.RiverService; | |
27 import org.dive4elements.river.client.client.services.RiverServiceAsync; | |
28 import org.dive4elements.river.client.client.services.UserService; | |
29 import org.dive4elements.river.client.client.services.UserServiceAsync; | |
30 import org.dive4elements.river.client.client.ui.CollectionView; | |
31 import org.dive4elements.river.client.client.ui.FLYSHeader; | |
32 import org.dive4elements.river.client.client.ui.FLYSView; | |
33 import org.dive4elements.river.client.client.ui.FLYSWorkspace; | |
34 import org.dive4elements.river.client.client.ui.ProjectList; | |
35 import org.dive4elements.river.client.shared.model.Artifact; | |
36 import org.dive4elements.river.client.shared.model.Collection; | |
37 import org.dive4elements.river.client.shared.model.CollectionItem; | |
38 import org.dive4elements.river.client.shared.model.GaugeInfo; | |
39 import org.dive4elements.river.client.shared.model.River; | |
40 import org.dive4elements.river.client.shared.model.User; | |
41 | |
42 import org.dive4elements.river.client.client.ui.wq.WQAutoTabSet; | |
43 | |
44 import java.util.ArrayList; | |
45 import java.util.List; | |
46 import java.util.MissingResourceException; | |
47 import java.util.Set; | |
48 | |
49 | |
50 /** | |
51 * Entry point classes define <code>onModuleLoad()</code>. | |
52 * | |
53 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
54 */ | |
55 public class FLYS implements EntryPoint, CollectionChangeHandler { | |
56 | |
57 /** The message class that provides i18n strings.*/ | |
58 protected FLYSConstants MSG = GWT.create(FLYSConstants.class); | |
59 | |
60 /** The UserService used to retrieve information about the current user. */ | |
61 protected UserServiceAsync userService = GWT.create(UserService.class); | |
62 | |
63 /** The RiverService used to retrieve the supported rivers of the server.*/ | |
64 protected RiverServiceAsync riverService = GWT.create(RiverService.class); | |
65 | |
66 /** The ArtifactService used to communicate with the Artifact server. */ | |
67 protected ArtifactServiceAsync artifactService = | |
68 GWT.create(ArtifactService.class); | |
69 | |
70 /** The ArtifactService used to communicate with the Artifact server. */ | |
71 protected DescribeCollectionServiceAsync describeCollectionService = | |
72 GWT.create(DescribeCollectionService.class); | |
73 | |
74 /** The GetArtifactService used to open an existing collection. */ | |
75 protected GetArtifactServiceAsync getArtifactService = | |
76 GWT.create(GetArtifactService.class); | |
77 | |
78 /** The CreateCollectionServiceAsync used to create a new collection */ | |
79 protected CreateCollectionServiceAsync collectionService = | |
80 GWT.create(CreateCollectionService.class); | |
81 | |
82 /** The content window. It takes the whole space beneath the header. */ | |
83 protected FLYSView view; | |
84 | |
85 /** The project list that displays the projects of the user. */ | |
86 protected ProjectList projectList; | |
87 | |
88 /** The FLYSWorkspace. */ | |
89 protected FLYSWorkspace workspace; | |
90 | |
91 /** The user who is currently logged in. */ | |
92 protected User currentUser; | |
93 | |
94 /** The list of rivers supported by the server. */ | |
95 protected River[] rivers; | |
96 | |
97 /** This list is used to track the opened projects. */ | |
98 protected List<String> openProjects; | |
99 | |
100 private FLYSHeader header; | |
101 | |
102 | |
103 public static String getExceptionString(FLYSConstants msg, Throwable caught) { | |
104 try { | |
105 return msg.getString(caught.getMessage()); | |
106 } | |
107 catch(MissingResourceException ex) { | |
108 // There are some server error exceptions with | |
109 // varying text messages that cannot be localized | |
110 // easily. In this rare cases, use the plain | |
111 // exception message. | |
112 GWT.log("Missing resource for: " + caught.getMessage()); | |
113 return caught.getLocalizedMessage(); | |
114 } | |
115 } | |
116 | |
117 /** | |
118 * This is the entry point method. | |
119 */ | |
120 @Override | |
121 public void onModuleLoad() { | |
122 openProjects = new ArrayList<String>(); | |
123 | |
124 //GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() { | |
125 // public void onUncaughtException(Throwable e) { | |
126 // showWarning(e); | |
127 // } | |
128 //}); | |
129 | |
130 VLayout vertical = new VLayout(); | |
131 vertical.setLayoutMargin(1); | |
132 vertical.setWidth100(); | |
133 vertical.setHeight100(); | |
134 | |
135 view = new FLYSView(); | |
136 header = new FLYSHeader(this); | |
137 | |
138 vertical.addMember(header); | |
139 vertical.addMember(view); | |
140 | |
141 vertical.draw(); | |
142 | |
143 initConfiguration(); | |
144 | |
145 Config config = Config.getInstance(); | |
146 String locale = config.getLocale(); | |
147 | |
148 userService.getCurrentUser(locale, new AsyncCallback<User>() { | |
149 @Override | |
150 public void onFailure(Throwable caught) { | |
151 GWT.log("Could not find a logged in user."); | |
152 String msg = getExceptionString(MSG, caught); | |
153 SC.warn(msg); | |
154 } | |
155 | |
156 @Override | |
157 public void onSuccess(User user) { | |
158 GWT.log("Found a user. Set '"+ user.getName() + "'"); | |
159 setCurrentUser(user); | |
160 | |
161 header.setCurrentUser(user); | |
162 | |
163 projectList = new ProjectList(FLYS.this, user); | |
164 workspace = new FLYSWorkspace(FLYS.this); | |
165 view.setProjectList(projectList); | |
166 view.setFLYSWorkspace(workspace); | |
167 | |
168 readRivers(); | |
169 } | |
170 }); | |
171 } | |
172 | |
173 | |
174 public void showWarning(Throwable e) { | |
175 StringBuilder sb = new StringBuilder(); | |
176 sb.append("<tt>"); | |
177 | |
178 if (e instanceof UmbrellaException) { | |
179 UmbrellaException u = (UmbrellaException) e; | |
180 Set<Throwable> throwables = u.getCauses(); | |
181 | |
182 for (Throwable t: throwables) { | |
183 sb.append(t.getLocalizedMessage()); | |
184 sb.append("<br>"); | |
185 } | |
186 } | |
187 else { | |
188 sb.append(e.getLocalizedMessage()); | |
189 } | |
190 | |
191 sb.append("</tt>"); | |
192 | |
193 Window w = new Window(); | |
194 w.setTitle(MSG.unexpected_exception()); | |
195 w.setWidth(550); | |
196 w.setHeight(300); | |
197 w.centerInPage(); | |
198 w.setCanDragResize(true); | |
199 | |
200 HTMLPane p = new HTMLPane(); | |
201 p.setContents(sb.toString()); | |
202 | |
203 w.addItem(p); | |
204 w.show(); | |
205 } | |
206 | |
207 | |
208 /** | |
209 * This method should be called at system start. It initialzes the client | |
210 * configuration. | |
211 */ | |
212 protected void initConfiguration() { | |
213 String xml = FLYSResources.INSTANCE.initialConfiguration().getText(); | |
214 Config.getInstance(XMLParser.parse(xml)); | |
215 } | |
216 | |
217 | |
218 /** | |
219 * Returns the user that is currently logged in. | |
220 * | |
221 * @return the current user. | |
222 */ | |
223 public User getCurrentUser() { | |
224 return currentUser; | |
225 } | |
226 | |
227 | |
228 /** | |
229 * Sets the current user. | |
230 */ | |
231 public void setCurrentUser(User user) { | |
232 currentUser = user; | |
233 } | |
234 | |
235 | |
236 /** | |
237 * Returns the project list. | |
238 */ | |
239 public ProjectList getProjectList() { | |
240 return projectList; | |
241 } | |
242 | |
243 | |
244 /** | |
245 * Returns the projects workspace that contains all project windows. | |
246 * | |
247 * @return the FLYSWorkspace. | |
248 */ | |
249 public FLYSWorkspace getWorkspace() { | |
250 return workspace; | |
251 } | |
252 | |
253 | |
254 /** | |
255 * Returns a list of rivers supported by the artifact server. | |
256 * | |
257 * @return a list of rivers supported by the artifact server. | |
258 */ | |
259 public River[] getRivers() { | |
260 return rivers; | |
261 } | |
262 | |
263 | |
264 protected void readRivers() { | |
265 Config config = Config.getInstance(); | |
266 String locale = config.getLocale(); | |
267 | |
268 riverService.list(locale, new AsyncCallback<River[]>() { | |
269 @Override | |
270 public void onFailure(Throwable caught) { | |
271 GWT.log("Could not recieve a list of rivers."); | |
272 SC.warn(getExceptionString(MSG, caught)); | |
273 } | |
274 | |
275 @Override | |
276 public void onSuccess(River[] newRivers) { | |
277 GWT.log("Retrieved " + newRivers.length + " new rivers."); | |
278 rivers = newRivers; | |
279 } | |
280 }); | |
281 } | |
282 | |
283 | |
284 /** | |
285 * This method creates a new CollectionView and adds it to the workspace. | |
286 * <b>NOTE</b>The user needs to be logged in and there need to at least one | |
287 * river - otherwise a warning is displayed and no CollectionView is | |
288 * created. | |
289 */ | |
290 public void newProject() { | |
291 if (getCurrentUser() == null) { | |
292 SC.warn(MSG.error_not_logged_in()); | |
293 return; | |
294 } | |
295 | |
296 if (getRivers() == null) { | |
297 SC.warn(MSG.error_no_rivers_found()); | |
298 readRivers(); | |
299 | |
300 return; | |
301 } | |
302 | |
303 CollectionView view = new CollectionView(this); | |
304 workspace.addView("new-project", view); | |
305 | |
306 view.addCollectionChangeHandler(getProjectList()); | |
307 } | |
308 | |
309 | |
310 protected void lockProject(String uuid) { | |
311 if (isProjectLocked(uuid)) { | |
312 return; | |
313 } | |
314 | |
315 openProjects.add(uuid); | |
316 } | |
317 | |
318 | |
319 protected void unlockProject(String uuid) { | |
320 openProjects.remove(uuid); | |
321 } | |
322 | |
323 | |
324 /** Whether project uuid is currently opened. */ | |
325 protected boolean isProjectLocked(String uuid) { | |
326 return openProjects.contains(uuid); | |
327 } | |
328 | |
329 | |
330 /** Opens (or bring into foreground) project with given id. */ | |
331 public void openProject(final String collectionID) { | |
332 if (collectionID == null) { | |
333 return; | |
334 } | |
335 | |
336 if (isProjectLocked(collectionID)) { | |
337 workspace.bringUp(collectionID); | |
338 return; | |
339 } | |
340 | |
341 lockProject(collectionID); | |
342 | |
343 GWT.log("Open existing project: " + collectionID); | |
344 | |
345 Config config = Config.getInstance(); | |
346 final String locale = config.getLocale(); | |
347 | |
348 describeCollectionService.describe(collectionID, locale, | |
349 new AsyncCallback<Collection>() { | |
350 @Override | |
351 public void onFailure(Throwable caught) { | |
352 SC.warn(getExceptionString(MSG, caught)); | |
353 } | |
354 | |
355 @Override | |
356 public void onSuccess(Collection c) { | |
357 final Collection collection = c; | |
358 | |
359 if (collection.getItemLength() == 0) { | |
360 CollectionView view = new CollectionView( | |
361 FLYS.this, collection, null); | |
362 | |
363 view.addCollectionChangeHandler( | |
364 getProjectList()); | |
365 view.addCloseClickHandler( | |
366 new CloseCollectionViewHandler( | |
367 FLYS.this, collectionID)); | |
368 | |
369 workspace.addView(collectionID, view); | |
370 | |
371 return; | |
372 } | |
373 | |
374 final CollectionItem item = c.getItem(0); | |
375 | |
376 if (item == null) { | |
377 SC.warn(MSG.error_load_parameterization()); | |
378 return; | |
379 } | |
380 | |
381 getArtifactService.getArtifact( | |
382 locale, | |
383 item.identifier(), | |
384 item.hash(), | |
385 new AsyncCallback<Artifact>() { | |
386 @Override | |
387 public void onFailure(Throwable caught) { | |
388 unlockProject(collectionID); | |
389 SC.warn(getExceptionString(MSG, caught)); | |
390 } | |
391 | |
392 @Override | |
393 public void onSuccess(Artifact artifact) { | |
394 CollectionView view = new CollectionView( | |
395 FLYS.this, collection, artifact); | |
396 | |
397 view.addCollectionChangeHandler( | |
398 getProjectList()); | |
399 view.addCloseClickHandler( | |
400 new CloseCollectionViewHandler( | |
401 FLYS.this, collectionID)); | |
402 | |
403 workspace.addView(collectionID, view); | |
404 } | |
405 }); | |
406 | |
407 } | |
408 }); | |
409 } | |
410 | |
411 | |
412 public void closeProject(String uuid) { | |
413 unlockProject(uuid); | |
414 workspace.destroyProject(uuid); | |
415 } | |
416 | |
417 | |
418 /** | |
419 * Create a new Artifact. | |
420 */ | |
421 public void newArtifact(String factory) { | |
422 Config config = Config.getInstance(); | |
423 String locale = config.getLocale(); | |
424 | |
425 artifactService.create(locale, factory, null, | |
426 new AsyncCallback<Artifact>() { | |
427 @Override | |
428 public void onFailure(Throwable caught) { | |
429 GWT.log("Could not create the new artifact."); | |
430 SC.warn(getExceptionString(MSG, caught)); | |
431 } | |
432 | |
433 @Override | |
434 public void onSuccess(Artifact artifact) { | |
435 GWT.log("Successfully created a new artifact."); | |
436 } | |
437 }); | |
438 } | |
439 | |
440 | |
441 /** Opens a window with Main Values from gauge. */ | |
442 public void newGaugeMainValueTable(GaugeInfo gauge) { | |
443 Window mainValueView = new Window(); | |
444 | |
445 // Take middle to avoid issues at borders. | |
446 double km = (gauge.getKmEnd() + gauge.getKmStart())/2d; | |
447 mainValueView.addItem(new WQAutoTabSet(gauge.getRiverName(), | |
448 new double[] {km, km})); | |
449 mainValueView.setWidth(450); | |
450 mainValueView.setHeight(600); | |
451 | |
452 mainValueView.setMaximized(false); | |
453 mainValueView.centerInPage(); | |
454 mainValueView.setCanDragReposition(true); | |
455 mainValueView.setCanDragResize(true); | |
456 mainValueView.setShowMaximizeButton(true); | |
457 mainValueView.setKeepInParentRect(true); | |
458 | |
459 mainValueView.setTitle(MSG.mainvalues() + " " + gauge.getName() + " (" + gauge.getRiverName() + ")" ); | |
460 workspace.addChild(mainValueView); | |
461 } | |
462 | |
463 public void newGaugeDischargeCurve(String river, Long gaugeref) { | |
464 Config config = Config.getInstance(); | |
465 | |
466 final String locale = config.getLocale(); | |
467 final String riv = river; | |
468 final Long ref = gaugeref; | |
469 final FLYS flys = this; | |
470 | |
471 User user = getCurrentUser(); | |
472 | |
473 if (user == null) { | |
474 SC.warn(MSG.error_not_logged_in()); | |
475 return; | |
476 } | |
477 | |
478 collectionService.create(locale, user.identifier(), | |
479 new AsyncCallback<Collection>() { | |
480 @Override | |
481 public void onFailure(Throwable caught) { | |
482 GWT.log("Could not create new collection."); | |
483 SC.warn(getExceptionString(MSG, caught)); | |
484 } | |
485 | |
486 @Override | |
487 public void onSuccess(Collection collection) { | |
488 GWT.log("Successfully created a new collection."); | |
489 final Collection col = collection; | |
490 artifactService.createGaugeDischargeCurveArtifact( | |
491 col, locale, riv, ref, | |
492 new AsyncCallback<Artifact>() { | |
493 @Override | |
494 public void onFailure(Throwable caught) { | |
495 GWT.log("Could not create the new artifact."); | |
496 SC.warn(getExceptionString(MSG, caught)); | |
497 } | |
498 | |
499 @Override | |
500 public void onSuccess(Artifact artifact) { | |
501 GWT.log("Successfully created a new artifact."); | |
502 CollectionView view = new CollectionView(flys, | |
503 col, artifact); | |
504 workspace.addView(col.identifier(), view); | |
505 | |
506 view.addCollectionChangeHandler(getProjectList()); | |
507 view.addCloseClickHandler( | |
508 new CloseCollectionViewHandler( | |
509 FLYS.this, col.identifier())); | |
510 projectList.updateUserCollections(); | |
511 } | |
512 }); | |
513 } | |
514 }); | |
515 } | |
516 | |
517 public void newSQRelation(String river, int measurementStation) { | |
518 Config config = Config.getInstance(); | |
519 | |
520 final String locale = config.getLocale(); | |
521 final String riv = river; | |
522 final int mStation = measurementStation; | |
523 final FLYS flys = this; | |
524 | |
525 User user = getCurrentUser(); | |
526 | |
527 if (user == null) { | |
528 SC.warn(MSG.error_not_logged_in()); | |
529 return; | |
530 } | |
531 | |
532 collectionService.create(locale, user.identifier(), | |
533 new AsyncCallback<Collection>() { | |
534 @Override | |
535 public void onFailure(Throwable caught) { | |
536 GWT.log("Could not create new collection."); | |
537 SC.warn(getExceptionString(MSG, caught)); | |
538 } | |
539 | |
540 @Override | |
541 public void onSuccess(Collection collection) { | |
542 GWT.log("Successfully created a new collection."); | |
543 final Collection col = collection; | |
544 artifactService.createSQRelationArtifact( | |
545 col, locale, riv, mStation, | |
546 new AsyncCallback<Artifact>() { | |
547 @Override | |
548 public void onFailure(Throwable caught) { | |
549 GWT.log("Could not create the new artifact."); | |
550 SC.warn(getExceptionString(MSG, caught)); | |
551 } | |
552 | |
553 @Override | |
554 public void onSuccess(Artifact artifact) { | |
555 GWT.log("Successfully created a new artifact."); | |
556 CollectionView view = new CollectionView(flys, | |
557 col, artifact); | |
558 workspace.addView(col.identifier(), view); | |
559 | |
560 view.addCollectionChangeHandler(getProjectList()); | |
561 view.addCloseClickHandler( | |
562 new CloseCollectionViewHandler( | |
563 FLYS.this, col.identifier())); | |
564 projectList.updateUserCollections(); | |
565 } | |
566 }); | |
567 } | |
568 }); | |
569 } | |
570 | |
571 @Override | |
572 public void onCollectionChange(CollectionChangeEvent event) { | |
573 Collection oldC = event.getOldValue(); | |
574 | |
575 if (oldC == null) { | |
576 Collection newC = event.getNewValue(); | |
577 lockProject(newC.identifier()); | |
578 } | |
579 } | |
580 | |
581 | |
582 | |
583 /** | |
584 * This CloseClickHandler is used to remove lock on a specific Collection so | |
585 * that is might be opened again. | |
586 */ | |
587 public class CloseCollectionViewHandler implements CloseClickHandler { | |
588 protected FLYS flys; | |
589 protected String uuid; | |
590 | |
591 public CloseCollectionViewHandler(FLYS flys, String uuid) { | |
592 this.flys = flys; | |
593 this.uuid = uuid; | |
594 } | |
595 | |
596 @Override | |
597 public void onCloseClick(CloseClickEvent event) { | |
598 flys.closeProject(uuid); | |
599 } | |
600 } | |
601 | |
602 public boolean isProjectListVisible() { | |
603 if (this.projectList == null) { | |
604 return true; | |
605 } | |
606 return this.projectList.isVisible(); | |
607 } | |
608 | |
609 public void hideProjectList() { | |
610 if (this.projectList != null) { | |
611 this.projectList.hide(); | |
612 } | |
613 } | |
614 | |
615 public void openProjectList() { | |
616 if (this.projectList != null) { | |
617 this.projectList.show(); | |
618 } | |
619 } | |
620 | |
621 public void hideHeaderProjectButton() { | |
622 this.header.hideProjectButton(); | |
623 } | |
624 | |
625 public void shoHeaderProjectButton() { | |
626 this.header.showProjectButton(); | |
627 } | |
628 } | |
629 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |