comparison flys-client/src/main/java/org/dive4elements/river/client/client/ui/ProjectList.java @ 5834:f507086aa94b

Repaired internal references.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:31:32 +0200
parents flys-client/src/main/java/de/intevation/flys/client/client/ui/ProjectList.java@3d01658d9c9c
children 821a02bbfb4e
comparison
equal deleted inserted replaced
5833:a2bdc0f524e8 5834:f507086aa94b
1 package de.intevation.flys.client.client.ui;
2
3 import com.google.gwt.core.client.GWT;
4 import com.google.gwt.i18n.client.DateTimeFormat;
5 import com.google.gwt.user.client.Timer;
6 import com.google.gwt.user.client.rpc.AsyncCallback;
7
8 import com.smartgwt.client.types.Alignment;
9 import com.smartgwt.client.types.Autofit;
10 import com.smartgwt.client.types.ListGridEditEvent;
11 import com.smartgwt.client.types.ListGridFieldType;
12 import com.smartgwt.client.types.Overflow;
13 import com.smartgwt.client.types.SelectionStyle;
14 import com.smartgwt.client.types.SortArrow;
15 import com.smartgwt.client.types.SortDirection;
16 import com.smartgwt.client.types.VerticalAlignment;
17 import com.smartgwt.client.util.BooleanCallback;
18 import com.smartgwt.client.util.SC;
19 import com.smartgwt.client.widgets.Canvas;
20 import com.smartgwt.client.widgets.IconButton;
21 import com.smartgwt.client.widgets.Label;
22 import com.smartgwt.client.widgets.events.ClickEvent;
23 import com.smartgwt.client.widgets.events.VisibilityChangedEvent;
24 import com.smartgwt.client.widgets.events.VisibilityChangedHandler;
25 import com.smartgwt.client.widgets.grid.CellFormatter;
26 import com.smartgwt.client.widgets.grid.HoverCustomizer;
27 import com.smartgwt.client.widgets.grid.ListGrid;
28 import com.smartgwt.client.widgets.grid.ListGridField;
29 import com.smartgwt.client.widgets.grid.ListGridRecord;
30 import com.smartgwt.client.widgets.grid.events.CellClickEvent;
31 import com.smartgwt.client.widgets.grid.events.CellClickHandler;
32 import com.smartgwt.client.widgets.grid.events.CellDoubleClickEvent;
33 import com.smartgwt.client.widgets.grid.events.CellDoubleClickHandler;
34 import com.smartgwt.client.widgets.grid.events.EditCompleteEvent;
35 import com.smartgwt.client.widgets.grid.events.EditCompleteHandler;
36 import com.smartgwt.client.widgets.grid.events.HeaderDoubleClickEvent;
37 import com.smartgwt.client.widgets.grid.events.HeaderDoubleClickHandler;
38 import com.smartgwt.client.widgets.grid.events.RowContextClickEvent;
39 import com.smartgwt.client.widgets.grid.events.RowContextClickHandler;
40 import com.smartgwt.client.widgets.layout.HLayout;
41 import com.smartgwt.client.widgets.layout.VLayout;
42 import com.smartgwt.client.widgets.menu.Menu;
43 import com.smartgwt.client.widgets.menu.MenuItem;
44 import com.smartgwt.client.widgets.menu.MenuItemSeparator;
45 import com.smartgwt.client.widgets.menu.events.ClickHandler;
46 import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
47
48 import de.intevation.flys.client.client.Config;
49 import de.intevation.flys.client.client.FLYS;
50 import de.intevation.flys.client.client.FLYSConstants;
51 import de.intevation.flys.client.client.event.CollectionChangeEvent;
52 import de.intevation.flys.client.client.event.CollectionChangeHandler;
53 import de.intevation.flys.client.client.event.FilterHandler;
54 import de.intevation.flys.client.client.event.RangeFilterEvent;
55 import de.intevation.flys.client.client.event.StringFilterEvent;
56 import de.intevation.flys.client.client.services.AddArtifactService;
57 import de.intevation.flys.client.client.services.AddArtifactServiceAsync;
58 import de.intevation.flys.client.client.services.ArtifactService;
59 import de.intevation.flys.client.client.services.ArtifactServiceAsync;
60 import de.intevation.flys.client.client.services.CreateCollectionService;
61 import de.intevation.flys.client.client.services.CreateCollectionServiceAsync;
62 import de.intevation.flys.client.client.services.DeleteCollectionService;
63 import de.intevation.flys.client.client.services.DeleteCollectionServiceAsync;
64 import de.intevation.flys.client.client.services.DescribeCollectionService;
65 import de.intevation.flys.client.client.services.DescribeCollectionServiceAsync;
66 import de.intevation.flys.client.client.services.GetArtifactService;
67 import de.intevation.flys.client.client.services.GetArtifactServiceAsync;
68 import de.intevation.flys.client.client.services.SetCollectionNameService;
69 import de.intevation.flys.client.client.services.SetCollectionNameServiceAsync;
70 import de.intevation.flys.client.client.services.SetCollectionTTLService;
71 import de.intevation.flys.client.client.services.SetCollectionTTLServiceAsync;
72 import de.intevation.flys.client.client.services.UserCollectionsService;
73 import de.intevation.flys.client.client.services.UserCollectionsServiceAsync;
74 import de.intevation.flys.client.shared.model.Artifact;
75 import de.intevation.flys.client.shared.model.Collection;
76 import de.intevation.flys.client.shared.model.CollectionItem;
77 import de.intevation.flys.client.shared.model.CollectionRecord;
78 import de.intevation.flys.client.shared.model.Recommendation;
79 import de.intevation.flys.client.shared.model.User;
80
81 import java.util.ArrayList;
82 import java.util.Date;
83 import java.util.List;
84 import java.util.Map;
85 import java.util.MissingResourceException;
86
87
88 /**
89 * The project list shows a list of projects of a specific user.
90 *
91 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
92 */
93 public class ProjectList
94 extends VLayout
95 implements CollectionChangeHandler, EditCompleteHandler, FilterHandler,
96 VisibilityChangedHandler
97 {
98 /** Interval to refresh the user's projects.*/
99 public static final int UPDATE_INTERVAL = 30000;
100
101 /** Min Interval to refresh the user's projects.*/
102 public static final int MIN_UPDATE_INTERVAL = 5000;
103
104 /** The initial width of this panel.*/
105 public static final int MIN_WIDTH = 300;
106
107 /** The max length for new project names.*/
108 public static final int MAX_NAME_LENGTH = 50;
109
110 public static final String COLUMN_DATE_WIDTH = "100px";
111
112 public static final String COLUMN_TITLE_WIDTH = "*";
113
114 public static final String COLUMN_FAVORITE_WIDTH = "75px";
115
116 /** The interface that provides i18n messages. */
117 private final FLYSConstants messages = GWT.create(FLYSConstants.class);
118
119 /** The UserService used to retrieve information about the current user. */
120 protected UserCollectionsServiceAsync userCollectionsService =
121 GWT.create(UserCollectionsService.class);
122
123 /** The service used to set the name of a project.*/
124 protected SetCollectionNameServiceAsync nameService =
125 GWT.create(SetCollectionNameService.class);
126
127 /** The service used to set the name of a project.*/
128 protected SetCollectionTTLServiceAsync ttlService =
129 GWT.create(SetCollectionTTLService.class);
130
131 /** The service used to set the name of a project.*/
132 protected DeleteCollectionServiceAsync deleteService =
133 GWT.create(DeleteCollectionService.class);
134
135 /** The DescribeCollectionService used to update the existing collection. */
136 protected DescribeCollectionServiceAsync describeCollectionService =
137 GWT.create(DescribeCollectionService.class);
138
139 /** The ArtifactService used to communicate with the Artifact server. */
140 protected ArtifactServiceAsync createArtifactService =
141 GWT.create(ArtifactService.class);
142
143 /** The ArtifactService used to communicate with the Artifact server. */
144 protected CreateCollectionServiceAsync createCollectionService =
145 GWT.create(CreateCollectionService.class);
146
147 /** The AddArtifactService used to add an artifact to a collection. */
148 protected AddArtifactServiceAsync addArtifactService =
149 GWT.create(AddArtifactService.class);
150
151 /** The GetArtifactService used to open an existing collection. */
152 protected GetArtifactServiceAsync getArtifactService =
153 GWT.create(GetArtifactService.class);
154
155 /** A pointer to the FLYS instance.*/
156 protected FLYS flys;
157
158 /** The user whose projects should be displayed.*/
159 protected User user;
160
161 /** The grid that contains the project rows.*/
162 protected ListGrid grid;
163
164 /** All user collections.*/
165 protected List<Collection> collections;
166
167 /** The collection to clone*/
168 protected Collection cloneCollection;
169
170 /** The message class that provides i18n strings.*/
171 protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
172
173 private String filter;
174
175
176 /**
177 * The default constructor that creates a new ProjectList for a specific
178 * user.
179 *
180 * @param user The user.
181 */
182 public ProjectList(FLYS flys, User user) {
183 super();
184 this.flys = flys;
185 this.user = user;
186
187 collections = new ArrayList<Collection>();
188 grid = new ListGrid();
189 initGrid();
190 init();
191 initTimer();
192
193 grid.addEditCompleteHandler(this);
194
195 addVisibilityChangedHandler(this);
196
197 updateUserCollections();
198 }
199
200
201 protected void initGrid() {
202 grid.setWidth100();
203 grid.setAutoFitData(Autofit.VERTICAL);
204 grid.setAutoFitMaxWidth(500);
205 grid.setEmptyMessage(messages.no_projects());
206 grid.setLoadingDataMessage(messages.load_projects());
207 grid.setCanEdit(false);
208 grid.setEditEvent(ListGridEditEvent.NONE);
209 grid.setShowHeaderContextMenu(false);
210 grid.setShowSortArrow(SortArrow.NONE);
211 grid.setSortDirection(SortDirection.DESCENDING);
212 grid.setSortField(0);
213 grid.setSelectionType(SelectionStyle.SINGLE);
214 grid.setCanReorderFields(false);
215 grid.setLeaveScrollbarGap(false);
216 grid.setBorder("0px");
217
218 ListGridField date = buildDateField();
219 ListGridField name = buildNameField();
220 ListGridField fav = buildFavoriteField();
221
222 grid.setFields(date, name, fav);
223
224 grid.addHeaderDoubleClickHandler(new HeaderDoubleClickHandler() {
225 @Override
226 public void onHeaderDoubleClick(HeaderDoubleClickEvent event) {
227 // Cancel the event.
228 return;
229 }
230 });
231
232 // Add a handler to set / unset the favorite state of a project.
233 grid.addCellClickHandler(new CellClickHandler() {
234 @Override
235 public void onCellClick(CellClickEvent event) {
236 if (event.getColNum() != 2) {
237 return;
238 }
239
240 CollectionRecord r = (CollectionRecord) event.getRecord();
241 Collection c = r.getCollection();
242
243 c.setTTL(c.getTTL() == 0 ? -1 : 0);
244 updateCollectionTTL(c);
245 }
246 });
247
248 // Add a handler to open a project.
249 grid.addCellDoubleClickHandler(new CellDoubleClickHandler() {
250 @Override
251 public void onCellDoubleClick(CellDoubleClickEvent e) {
252 CollectionRecord record = (CollectionRecord) e.getRecord();
253 String uuid = record != null
254 ? record.getCollection().identifier()
255 : "";
256 getFlys().openProject(uuid);
257 }
258 });
259
260 // Add a handler to open a context menu.
261 grid.addRowContextClickHandler(new RowContextClickHandler() {
262 @Override
263 public void onRowContextClick(RowContextClickEvent event) {
264 CollectionRecord record = (CollectionRecord) event.getRecord();
265
266 Menu menu = createContextMenu(record);
267 grid.setContextMenu(menu);
268 menu.showContextMenu();
269
270 event.cancel();
271 }
272 });
273 }
274
275
276 /**
277 * Initializes a repeating timer that updates the user's collections. The
278 * interval is specified by the constant <i>UPDATE_INTERVAL</i>.
279 */
280 protected void initTimer() {
281 Config config = Config.getInstance();
282 int interval = config.getProjectListUpdateInterval();
283
284 interval = interval > MIN_UPDATE_INTERVAL ? interval : UPDATE_INTERVAL;
285
286 GWT.log("Update project list every " + interval + " milliseconds.");
287
288 Timer t = new Timer() {
289 @Override
290 public void run() {
291 updateUserCollections();
292 }
293 };
294
295 t.scheduleRepeating(interval);
296 }
297
298
299 public FLYS getFlys() {
300 return flys;
301 }
302
303
304 /**
305 * Creates a new context menu that interacts with a CollectionRecord.
306 *
307 * @param record The selected record.
308 *
309 * @return the context menu with operations that interact with
310 * <i>record</i>.
311 */
312 protected Menu createContextMenu(final CollectionRecord record) {
313 Menu menu = new Menu();
314
315 MenuItem open = new MenuItem(messages.open_project());
316 open.addClickHandler(new ClickHandler() {
317 @Override
318 public void onClick(MenuItemClickEvent evt) {
319 getFlys().openProject(record.getCollection().identifier());
320 }
321 });
322
323 MenuItem del = new MenuItem(messages.delete_project());
324 del.addClickHandler(new ClickHandler() {
325 @Override
326 public void onClick(MenuItemClickEvent evt) {
327 SC.ask(messages.really_delete(), new BooleanCallback() {
328 @Override
329 public void execute(Boolean value) {
330 if (value) {
331 deleteCollection(record.getCollection());
332 }
333 }
334 });
335 }
336 });
337
338 MenuItem rename = new MenuItem(messages.rename_project());
339 rename.addClickHandler(new ClickHandler() {
340 @Override
341 public void onClick(MenuItemClickEvent evt) {
342 int row = grid.getRecordIndex(record);
343 grid.startEditing(row, 1, false);
344 }
345 });
346
347 MenuItem clone = new MenuItem(messages.clone_project());
348 clone.addClickHandler(new ClickHandler() {
349 @Override
350 public void onClick(MenuItemClickEvent evt) {
351 cloneProject(record.getCollection());
352 }
353 });
354
355 menu.addItem(open);
356 menu.addItem(rename);
357 menu.addItem(clone);
358 menu.addItem(new MenuItemSeparator());
359 menu.addItem(del);
360
361 return menu;
362 }
363
364
365 /**
366 * The init() method handles the layout stuff for this widget.
367 */
368 protected void init() {
369 setWidth(MIN_WIDTH);
370 setMinWidth(MIN_WIDTH);
371 setHeight100();
372 setShowResizeBar(true);
373 setShowEdges(false);
374 setLayoutMargin(0);
375 setLayoutAlign(VerticalAlignment.TOP);
376 setOverflow(Overflow.AUTO);
377
378 Label title = new Label(messages.projects());
379 title.setHeight("20");
380 title.setMargin(5);
381 title.setWidth100();
382 title.setStyleName("projectHeader");
383
384 HLayout buttonWrapper = new HLayout();
385
386 IconButton addButton = new IconButton("");
387 addButton.setIcon(messages.projectListAdd());
388 addButton.setTooltip(messages.new_project());
389 addButton.setWidth("30px");
390
391 IconButton closeButton = new IconButton("");
392 closeButton.setIcon(messages.projectListMin());
393 closeButton.setTooltip(messages.projectlist_close());
394 closeButton.setWidth("30px");
395
396 buttonWrapper.addMember(addButton);
397 buttonWrapper.addMember(closeButton);
398 buttonWrapper.setAlign(Alignment.RIGHT);
399 buttonWrapper.setAutoWidth();
400
401 HLayout titleWrapper = new HLayout();
402 titleWrapper.setStyleName("bgBlueMid");
403 titleWrapper.setWidth100();
404 titleWrapper.setHeight("20px");
405 titleWrapper.addMember(title);
406 titleWrapper.addMember(buttonWrapper);
407
408 Canvas gridWrapper = new Canvas();
409 gridWrapper.setPadding(0);
410 titleWrapper.setWidth100();
411 gridWrapper.addChild(grid);
412
413 TableFilter filterpanel = new TableFilter();
414 filterpanel.setHeight("30px");
415 filterpanel.addFilterHandler(this);
416 filterpanel.setBorder("1px solid gray");
417
418 addMember(titleWrapper);
419 addMember(gridWrapper);
420 addMember(filterpanel);
421
422 addButton.addClickHandler(
423 new com.smartgwt.client.widgets.events.ClickHandler() {
424
425 @Override
426 public void onClick(ClickEvent ev) {
427 flys.newProject();
428 }
429 });
430
431 closeButton.addClickHandler(
432 new com.smartgwt.client.widgets.events.ClickHandler() {
433
434 @Override
435 public void onClick(ClickEvent ev) {
436 hide();
437 }
438 });
439 }
440
441
442 @Override
443 public void onFilterCriteriaChanged(StringFilterEvent event) {
444 String search = event.getFilter();
445 // Filter the records.
446 setFilter(search);
447 updateGrid();
448 }
449
450
451 @Override
452 public void onFilterCriteriaChanged(RangeFilterEvent event) {
453 //Empty. No Ranges to filter.
454 }
455
456
457 /** On collection change, update list (probably name change or similar). */
458 @Override
459 public void onCollectionChange(CollectionChangeEvent event) {
460 if (event.getOldValue() == null) {
461 updateUserCollections();
462 }
463 }
464
465
466 @Override
467 public void onEditComplete(EditCompleteEvent event) {
468 if (event.getColNum() != 1) {
469 return;
470 }
471
472 int row = event.getRowNum();
473
474 CollectionRecord r = (CollectionRecord) grid.getRecord(row);
475 Collection c = r.getCollection();
476
477 Map<?, ?> newValues = event.getNewValues();
478 String name = (String) newValues.get("name");
479
480 int maxLength = getMaxNameLength();
481 int length = name != null ? name.length() : 0;
482
483 if (length <= 0 || length > maxLength) {
484 String msg = messages.project_name_too_long();
485 msg = msg.replace("$LEN", String.valueOf(maxLength));
486 SC.warn(msg);
487
488 ListGridRecord[] rs = grid.getRecords();
489 rs[row] = (ListGridRecord) event.getOldRecord();
490 grid.setRecords(rs);
491
492 return;
493 }
494
495 updateCollectionName(c, name);
496 }
497
498
499 /**
500 * Set the name of the collection <i>c</i> to a new value. If the update
501 * process succeeded, the project list is refreshed.
502 *
503 * @param c The Collection with a new name.
504 * @param name Name to set on the collection
505 */
506 private void updateCollectionName(final Collection c, String name) {
507 if (c == null) {
508 return;
509 }
510
511 c.setName(name);
512
513 GWT.log("Update Collection name: " + c.identifier());
514 GWT.log("=> New name = " + c.getName());
515
516 nameService.setName(c, new AsyncCallback<Void>(){
517 @Override
518 public void onFailure(Throwable caught) {
519 String msg = caught.getMessage();
520
521 try {
522 SC.warn(messages.getString(msg));
523 }
524 catch (MissingResourceException mre) {
525 SC.warn(msg);
526 }
527 }
528
529 @Override
530 public void onSuccess(Void v) {
531 updateUserCollections();
532 if(flys.getWorkspace().hasView(c.identifier())) {
533 flys.getWorkspace().updateTitle(c.identifier(), c.getName());
534 }
535 }
536 });
537 }
538
539
540 /**
541 * Set the ttl of the collection <i>c</i> to a new value. If the update
542 * process succeeded, the project list is refreshed.
543 *
544 * @param c The Collection with a new ttl.
545 */
546 protected void updateCollectionTTL(Collection c) {
547 if (c == null) {
548 return;
549 }
550
551 GWT.log("Update Collection TTL: " + c.identifier());
552 GWT.log("=> New ttl = " + c.getTTL());
553
554 ttlService.setTTL(c, new AsyncCallback<Void>() {
555 @Override
556 public void onFailure(Throwable caught) {
557 String msg = caught.getMessage();
558
559 try {
560 SC.warn(messages.getString(msg));
561 }
562 catch (MissingResourceException mre) {
563 SC.warn(msg);
564 }
565 }
566
567 @Override
568 public void onSuccess(Void v) {
569 updateUserCollections();
570 }
571 });
572 }
573
574
575 /**
576 * Delete the collection <i>c</i>.
577 *
578 * @param c The Collection that should be deleted.
579 */
580 public void deleteCollection(final Collection c) {
581 if (c == null) {
582 return;
583 }
584
585 GWT.log("Delete Collection: " + c.identifier());
586
587 deleteService.delete(c, new AsyncCallback<Void>(){
588 @Override
589 public void onFailure(Throwable caught) {
590 String msg = caught.getMessage();
591
592 try {
593 SC.warn(messages.getString(msg));
594 }
595 catch (MissingResourceException mre) {
596 SC.warn(msg);
597 }
598 }
599
600 @Override
601 public void onSuccess(Void v) {
602 flys.getWorkspace().destroyProject(c.identifier());
603 updateUserCollections();
604 }
605 });
606 }
607
608
609 public void updateUserCollections() {
610 GWT.log("==> ProjectList updates user collections!");
611
612 Config config = Config.getInstance();
613 String locale = config.getLocale();
614
615 userCollectionsService.getUserCollections(locale, user.identifier(),
616 new AsyncCallback<Collection[]>() {
617 @Override
618 public void onFailure(Throwable caught) {
619 String msg = caught.getMessage();
620
621 try {
622 SC.warn(messages.getString(msg));
623 }
624 catch (MissingResourceException mre) {
625 SC.warn(msg);
626 }
627 }
628
629 @Override
630 public void onSuccess(Collection[] collections) {
631 int num = collections != null ? collections.length : 0;
632
633 GWT.log("Received " + num + " user collections.");
634
635 updateGridDataSource(collections);
636 }
637 }
638 );
639 }
640
641
642 /**
643 * Delete all entries in the ListGrid.
644 */
645 private void clearGrid() {
646 ListGridRecord[] records = grid.getRecords();
647
648 for (ListGridRecord record: records) {
649 grid.removeData(record);
650 }
651 }
652
653
654 /**
655 * Update the collections data source.
656 *
657 * First removes all collections to avoid duplicates, then add new entries.
658 *
659 * @param c Collections to set to the data source.
660 */
661 private void updateGridDataSource(Collection[] c) {
662 collections.clear();
663 if(c == null) {
664 clearGrid();
665 return;
666 }
667 for (Collection coll : c) {
668 this.collections.add(coll);
669 }
670 updateGrid();
671 }
672
673
674 /**
675 * Updates the ListGrid.
676 */
677 private void updateGrid() {
678 clearGrid();
679
680 if (collections == null || collections.isEmpty()) {
681 return;
682 }
683
684 for (Collection col: collections) {
685 String name;
686
687 name = col.getDisplayName().toLowerCase();
688
689 // Add a collection to the filtered list if the search string
690 // matches.
691 if (filter == null || filter.isEmpty() ||
692 name.contains(filter.toLowerCase())) {
693 grid.addData(new CollectionRecord(col));
694 }
695 }
696 }
697
698 public int getMaxNameLength() {
699 return MAX_NAME_LENGTH;
700 }
701
702
703 /**
704 * Builds the field in the grid that displays the creation time of a
705 * project.
706 *
707 * @return the grid field.
708 */
709 protected ListGridField buildDateField() {
710 ListGridField date = new ListGridField(
711 "creationTime", messages.projectlist_creationTime());
712
713 date.setType(ListGridFieldType.DATE);
714 date.setCanEdit(false);
715
716 date.setCellFormatter(new CellFormatter() {
717 @Override
718 public String format(Object value, ListGridRecord rec, int r, int c) {
719 if (value == null) {
720 return null;
721 }
722
723 DateTimeFormat dtf = DateTimeFormat.getFormat(
724 messages.datetime_format());
725
726 return dtf.format((Date)value);
727 }
728 });
729
730 date.setWidth(COLUMN_DATE_WIDTH);
731 date.setAlign(Alignment.CENTER);
732
733 return date;
734 }
735
736
737 /**
738 * Builds the field in the grid that displays the name of a project.
739 *
740 * @return the grid field.
741 */
742 protected ListGridField buildNameField() {
743 ListGridField name = new ListGridField(
744 "name", messages.projectlist_title());
745
746 name.setType(ListGridFieldType.TEXT);
747 name.setShowHover(true);
748 name.setHoverCustomizer(new HoverCustomizer() {
749 @Override
750 public String hoverHTML(
751 Object value,
752 ListGridRecord record,
753 int row,
754 int col)
755 {
756 CollectionRecord r = (CollectionRecord) record;
757 Collection c = r.getCollection();
758
759 String name = r.getName();
760
761 return name != null && name.length() > 0
762 ? name
763 : c.identifier();
764 }
765 });
766
767 name.setWidth(COLUMN_TITLE_WIDTH);
768 name.setAlign(Alignment.LEFT);
769
770 return name;
771 }
772
773
774 protected ListGridField buildFavoriteField() {
775 ListGridField fav = new ListGridField(
776 "ttl", messages.projectlist_favorite());
777
778 fav.setType(ListGridFieldType.IMAGE);
779 String base = GWT.getHostPageBaseURL();
780 fav.setImageURLPrefix(base + "images/");
781 fav.setImageURLSuffix(".png");
782 fav.setWidth(COLUMN_FAVORITE_WIDTH);
783 fav.setAlign(Alignment.CENTER);
784 fav.setCanEdit(false);
785
786 return fav;
787 }
788
789
790 protected void cloneProject(Collection c) {
791 Config config = Config.getInstance();
792 String locale = config.getLocale();
793
794 cloneCollection = c;
795
796 describeCollectionService.describe(c.identifier(), locale,
797 new AsyncCallback<Collection>() {
798 @Override
799 public void onFailure(Throwable caught) {
800 GWT.log("Could not DESCRIBE collection.");
801 SC.warn(messages.getString(caught.getMessage()));
802 }
803
804
805 @Override
806 public void onSuccess(Collection newCollection) {
807 GWT.log("Successfully DESCRIBED collection.");
808 String uuid = getMasterArtifact(newCollection);
809 cloneArtifact(uuid, newCollection);
810 }
811 }
812 );
813 }
814
815
816 /** Get master artifacts UUID of a collection. */
817 protected String getMasterArtifact(Collection newCollection) {
818 String uuid = newCollection.getItem(0).identifier();
819 // The master artifact uuid.
820 return uuid;
821 }
822
823
824 /** Clone artifact/create collection, using the refArtifacts factory. */
825 protected void cloneArtifact(String uuid, Artifact refArtifact,
826 final String locale) {
827 Recommendation recommendation = new Recommendation(
828 refArtifact.getName(),
829 null,
830 uuid,
831 null);
832
833 String factory = recommendation.getFactory();
834 createArtifactService.create(
835 locale, factory, recommendation,
836 new AsyncCallback<Artifact>() {
837 @Override
838 public void onFailure(Throwable caught) {
839 GWT.log("Error loading recommendations: " +
840 caught.getMessage());
841 }
842
843 @Override
844 public void onSuccess(Artifact artifact) {
845 GWT.log("Created new artifact: " + artifact.getUuid());
846 createClonedCollection(artifact);
847 }
848 }
849 );
850 }
851
852
853 /**
854 * Clone a project (collection).
855 */
856 private void cloneArtifact(final String uuid, Collection newCollection) {
857 Config config = Config.getInstance();
858 final String locale = config.getLocale();
859
860 // Find out which factory to use for cloning.
861 CollectionItem master = newCollection.getItem(0);
862 getArtifactService.getArtifact(
863 locale,
864 master.identifier(),
865 master.hash(),
866 new AsyncCallback<Artifact>() {
867 @Override
868 public void onFailure(Throwable caught) {
869 SC.warn(MSG.getString(caught.getMessage()));
870 }
871
872 @Override
873 public void onSuccess(Artifact artifact) {
874 cloneArtifact(uuid, artifact, locale);
875 }
876 });
877 }
878
879
880 /**
881 * Creates a {@link Collection} with the passed {@link Artifact}
882 * @param artifact {@link Artifact} to add to the new {@link Collection}
883 */
884 private void createClonedCollection(final Artifact artifact) {
885 Config config = Config.getInstance();
886 final String locale = config.getLocale();
887 final String ownerid = user.identifier();
888
889 createCollectionService.create(
890 locale,
891 ownerid,
892 new AsyncCallback<Collection>() {
893 @Override
894 public void onFailure(Throwable caught) {
895 GWT.log("Could not create the new collection.");
896 SC.warn(messages.getString(caught.getMessage()));
897 }
898
899 @Override
900 public void onSuccess(Collection collection) {
901 GWT.log("Successfully created a new collection.");
902 addArtifactToClonedCollection(artifact, collection);
903 }
904 }
905 );
906 }
907
908
909 private void addArtifactToClonedCollection(Artifact a, Collection c) {
910 Config config = Config.getInstance();
911 final String locale = config.getLocale();
912
913 addArtifactService.add(
914 c, a, locale,
915 new AsyncCallback<Collection>() {
916 @Override
917 public void onFailure(Throwable caught) {
918 GWT.log("An error occured while adding artifact.");
919 SC.warn(messages.getString(caught.getMessage()));
920 }
921
922 @Override
923 public void onSuccess(Collection newColl) {
924 String name = cloneCollection.getName();
925 if(name == null || name.equals("")) {
926 name = cloneCollection.identifier();
927 }
928
929 String colname = messages.copy_of() + ": " + name;
930
931 updateCollectionName(newColl, colname);
932 if(cloneCollection.getTTL() == 0) {
933 newColl.setTTL(0);
934 updateCollectionTTL(newColl);
935 }
936 }
937 }
938 );
939 }
940
941
942 @Override
943 public void onVisibilityChanged(VisibilityChangedEvent event) {
944 if (event.getIsVisible()) {
945 this.flys.hideHeaderProjectButton();
946 }
947 else {
948 this.flys.shoHeaderProjectButton();
949 }
950 }
951
952 private void setFilter(String filter) {
953 this.filter = filter;
954 }
955 }
956 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org