2 * Copyright (c) 2011 Greek Research and Technology Network
5 package gr.grnet.pithos.web.client;
7 import static com.google.gwt.query.client.GQuery.$;
9 import gr.grnet.pithos.web.client.commands.UploadFileCommand;
10 import gr.grnet.pithos.web.client.rest.GetCommand;
11 import gr.grnet.pithos.web.client.rest.RestCommand;
12 import gr.grnet.pithos.web.client.rest.resource.FileResource;
13 import gr.grnet.pithos.web.client.rest.resource.OtherUserResource;
14 import gr.grnet.pithos.web.client.rest.resource.OthersFolderResource;
15 import gr.grnet.pithos.web.client.rest.resource.OthersResource;
16 import gr.grnet.pithos.web.client.rest.resource.RestResource;
17 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
18 import gr.grnet.pithos.web.client.rest.resource.SharedResource;
19 import gr.grnet.pithos.web.client.rest.resource.TrashFolderResource;
20 import gr.grnet.pithos.web.client.rest.resource.TrashResource;
21 import gr.grnet.pithos.web.client.rest.resource.UserResource;
22 import gr.grnet.pithos.web.client.rest.resource.UserSearchResource;
23 import gwtquery.plugins.draggable.client.DraggableOptions;
24 import gwtquery.plugins.draggable.client.StopDragException;
25 import gwtquery.plugins.draggable.client.DraggableOptions.DragFunction;
26 import gwtquery.plugins.draggable.client.DraggableOptions.RevertOption;
27 import gwtquery.plugins.draggable.client.events.DragContext;
28 import gwtquery.plugins.draggable.client.events.DragStartEvent;
29 import gwtquery.plugins.draggable.client.events.DragStopEvent;
30 import gwtquery.plugins.draggable.client.events.DragStartEvent.DragStartEventHandler;
31 import gwtquery.plugins.draggable.client.events.DragStopEvent.DragStopEventHandler;
32 import gwtquery.plugins.droppable.client.gwt.DragAndDropCellTable;
33 import gwtquery.plugins.droppable.client.gwt.DragAndDropColumn;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.Comparator;
38 import java.util.Iterator;
39 import java.util.List;
41 import com.google.gwt.cell.client.AbstractCell;
42 import com.google.gwt.cell.client.ImageResourceCell;
43 import com.google.gwt.cell.client.SafeHtmlCell;
44 import com.google.gwt.cell.client.TextCell;
45 import com.google.gwt.cell.client.ValueUpdater;
46 import com.google.gwt.core.client.GWT;
47 import com.google.gwt.core.client.Scheduler;
48 import com.google.gwt.core.client.Scheduler.RepeatingCommand;
49 import com.google.gwt.dom.client.Style.Cursor;
50 import com.google.gwt.event.dom.client.ClickEvent;
51 import com.google.gwt.event.dom.client.ClickHandler;
52 import com.google.gwt.http.client.URL;
53 import com.google.gwt.i18n.client.DateTimeFormat;
54 import com.google.gwt.resources.client.ClientBundle;
55 import com.google.gwt.resources.client.ImageResource;
56 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
57 import com.google.gwt.safehtml.shared.SafeHtml;
58 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
59 import com.google.gwt.user.cellview.client.CellTable;
60 import com.google.gwt.user.cellview.client.GssSimplePager;
61 import com.google.gwt.user.client.DOM;
62 import com.google.gwt.user.client.DeferredCommand;
63 import com.google.gwt.user.client.Event;
64 import com.google.gwt.user.client.Window;
65 import com.google.gwt.user.client.ui.AbstractImagePrototype;
66 import com.google.gwt.user.client.ui.Button;
67 import com.google.gwt.user.client.ui.Composite;
68 import com.google.gwt.user.client.ui.HorizontalPanel;
69 import com.google.gwt.user.client.ui.VerticalPanel;
70 import com.google.gwt.view.client.ListDataProvider;
71 import com.google.gwt.view.client.MultiSelectionModel;
72 import com.google.gwt.view.client.ProvidesKey;
73 import com.google.gwt.view.client.SelectionChangeEvent;
74 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
77 * A composite that displays the list of files in a particular folder.
79 public class FileList extends Composite {
80 ListDataProvider<FileResource> provider = new ListDataProvider<FileResource>();
81 interface TableResources extends DragAndDropCellTable.Resources {
82 @Source({CellTable.Style.DEFAULT_CSS, "GssCellTable.css"})
83 TableStyle cellTableStyle();
86 static interface Templates extends SafeHtmlTemplates {
87 Templates INSTANCE = GWT.create(Templates.class);
89 @Template("<div id='dragHelper' style='border:1px solid black; background-color:#ffffff; color:black; width:150px;z-index:100'></div>")
90 SafeHtml outerHelper();
92 @Template("<span id='{0}'>{0}</span>")
93 public SafeHtml filenameSpan(String filename);
95 @Template("<a href='{0}' title='{1}' rel='lytebox[mnf]' onclick='myLytebox.start(this, false, false); return false;'>(view)</a>")
96 public SafeHtml viewLink(String link, String title);
98 @Template("<table><tr><td rowspan='3'>{0}</td><td style='font-size:95%;' id='{1}'>{1}</td></tr><tr><td>{2}</td></tr></table>")
99 public SafeHtml rendelContactCell(String imageHtml, String name, String fileSize);
101 @Template("<span id='{0}' class='{1}'>{2}</span>")
102 public SafeHtml spanWithIdAndClass(String id, String cssClass, String content);
107 * The styles applied to the table.
109 interface TableStyle extends CellTable.Style {
112 private String showingStats = "";
114 private int startIndex = 0;
117 * A constant that denotes the completion of an IncrementalCommand.
119 public static final boolean DONE = false;
123 private final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
126 * Specifies that the images available for this composite will be the ones
127 * available in FileContextMenu.
129 public interface Images extends ClientBundle,FileContextMenu.Images, CellTreeView.Images {
131 @Source("gr/grnet/pithos/resources/blank.gif")
132 ImageResource blank();
134 @Source("gr/grnet/pithos/resources/asc.png")
137 @Source("gr/grnet/pithos/resources/desc.png")
138 ImageResource desc();
140 @Source("gr/grnet/pithos/resources/mimetypes/document_shared.png")
141 ImageResource documentShared();
143 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst.png")
144 ImageResource wordprocessor();
146 @Source("gr/grnet/pithos/resources/mimetypes/log.png")
147 ImageResource spreadsheet();
149 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr.png")
150 ImageResource presentation();
152 @Source("gr/grnet/pithos/resources/mimetypes/acroread.png")
155 @Source("gr/grnet/pithos/resources/mimetypes/image.png")
156 ImageResource image();
158 @Source("gr/grnet/pithos/resources/mimetypes/video2.png")
159 ImageResource video();
161 @Source("gr/grnet/pithos/resources/mimetypes/knotify.png")
162 ImageResource audio();
164 @Source("gr/grnet/pithos/resources/mimetypes/html.png")
165 ImageResource html();
167 @Source("gr/grnet/pithos/resources/mimetypes/txt.png")
170 @Source("gr/grnet/pithos/resources/mimetypes/ark2.png")
173 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst_shared.png")
174 ImageResource wordprocessorShared();
176 @Source("gr/grnet/pithos/resources/mimetypes/log_shared.png")
177 ImageResource spreadsheetShared();
179 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr_shared.png")
180 ImageResource presentationShared();
182 @Source("gr/grnet/pithos/resources/mimetypes/acroread_shared.png")
183 ImageResource pdfShared();
185 @Source("gr/grnet/pithos/resources/mimetypes/image_shared.png")
186 ImageResource imageShared();
188 @Source("gr/grnet/pithos/resources/mimetypes/video2_shared.png")
189 ImageResource videoShared();
191 @Source("gr/grnet/pithos/resources/mimetypes/knotify_shared.png")
192 ImageResource audioShared();
194 @Source("gr/grnet/pithos/resources/mimetypes/html_shared.png")
195 ImageResource htmlShared();
197 @Source("gr/grnet/pithos/resources/mimetypes/txt_shared.png")
198 ImageResource txtShared();
200 @Source("gr/grnet/pithos/resources/mimetypes/ark2_shared.png")
201 ImageResource zipShared();
205 DragStopEventHandler dragStop = new DragStopEventHandler() {
208 public void onDragStop(DragStopEvent event) {
209 GWT.log("DRAG STOPPED");
214 private static class ContactCell extends AbstractCell<gr.grnet.pithos.web.client.rest.resource.FileResource> {
217 * The html of the image used for contacts.
220 private final String imageHtml;
222 public ContactCell(ImageResource image) {
223 this.imageHtml = AbstractImagePrototype.create(image).getHTML();
231 public void render(Context context, FileResource value, SafeHtmlBuilder sb) {
232 // Value can be null, so do a null check..
237 sb.append(Templates.INSTANCE.rendelContactCell(imageHtml, value.getName(), value.getFileSizeAsString()));
243 * Retrieve the celltable.
245 * @return the celltable
247 public DragAndDropCellTable<FileResource> getCelltable() {
253 * The number of files in this folder.
260 long folderTotalSize;
263 * A cache of the files in the list.
265 private List<FileResource> files;
268 * The widget's image bundle.
270 private final Images images;
272 private FileContextMenu menuShowing;
273 private DragAndDropCellTable<FileResource> celltable;
274 private final MultiSelectionModel<FileResource> selectionModel;
275 private final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
276 SortableHeader nameHeader;
277 GssSimplePager pagerBottom;
278 GssSimplePager pagerTop;
279 Button uploadButtonBottom;
280 Button uploadButtonTop;
282 * Construct the file list widget. This entails setting up the widget
283 * layout, fetching the number of files in the current folder from the
284 * server and filling the local file cache of displayed files with data from
285 * the server, as well.
289 public FileList(Images _images) {
291 DragAndDropCellTable.Resources resources = GWT.create(TableResources.class);
292 ProvidesKey<FileResource> keyProvider = new ProvidesKey<FileResource>(){
295 public Object getKey(FileResource item) {
296 return item.getUri();
300 celltable = new DragAndDropCellTable<FileResource>(GSS.VISIBLE_FILE_COUNT,resources,keyProvider);
302 DragAndDropColumn<FileResource, ImageResource> status = new DragAndDropColumn<FileResource, ImageResource>(new ImageResourceCell(){
304 public boolean handlesSelection() {
309 public ImageResource getValue(FileResource entity) {
310 return getFileIcon(entity);
314 celltable.addColumn(status,"");
316 initDragOperation(status);
317 final DragAndDropColumn<FileResource,SafeHtml> nameColumn = new DragAndDropColumn<FileResource,SafeHtml>(new SafeHtmlCell()) {
320 public SafeHtml getValue(FileResource object) {
321 SafeHtmlBuilder sb = new SafeHtmlBuilder();
322 sb.append(Templates.INSTANCE.filenameSpan(object.getName()));
323 if (object.getContentType().endsWith("png") || object.getContentType().endsWith("gif") || object.getContentType().endsWith("jpeg") ){
324 sb.appendHtmlConstant(" ").append(Templates.INSTANCE.viewLink(GSS.get().getTopPanel().getFileMenu().getDownloadURL(object), object.getOwner() + " : " + object.getPath() + object.getName()));
327 return sb.toSafeHtml();
331 initDragOperation(nameColumn);
332 celltable.addColumn(nameColumn,nameHeader = new SortableHeader("Name"));
333 allHeaders.add(nameHeader);
334 //nameHeader.setSorted(true);
335 //nameHeader.toggleReverseSort();
336 nameHeader.setUpdater(new FileValueUpdater(nameHeader, "name"));
337 celltable.redrawHeaders();
342 SortableHeader aheader;
343 DragAndDropColumn<FileResource,String> aColumn;
344 celltable.addColumn(aColumn=new DragAndDropColumn<FileResource,String>(new TextCell()) {
346 public String getValue(FileResource object) {
347 return GSS.get().findUserFullName(object.getOwner());
349 },aheader = new SortableHeader("Owner"));
350 initDragOperation(aColumn);
351 allHeaders.add(aheader);
352 aheader.setUpdater(new FileValueUpdater(aheader, "owner"));
353 celltable.addColumn(aColumn=new DragAndDropColumn<FileResource,String>(new TextCell()) {
355 public String getValue(FileResource object) {
356 // TODO Auto-generated method stub
357 return object.getPath();
359 },aheader = new SortableHeader("Path"));
360 initDragOperation(aColumn);
361 allHeaders.add(aheader);
363 aheader.setUpdater(new FileValueUpdater(aheader, "path"));
364 celltable.addColumn(aColumn=new DragAndDropColumn<FileResource,String>(new TextCell()) {
366 public String getValue(FileResource object) {
367 if(object.isVersioned())
368 return object.getVersion().toString();
371 },aheader = new SortableHeader("Version"));
372 initDragOperation(aColumn);
373 allHeaders.add(aheader);
374 aheader.setUpdater(new FileValueUpdater(aheader, "version"));
375 celltable.addColumn(aColumn=new DragAndDropColumn<FileResource,String>(new TextCell()) {
377 public String getValue(FileResource object) {
378 // TODO Auto-generated method stub
379 return object.getFileSizeAsString();
381 },aheader = new SortableHeader("Size"));
382 initDragOperation(aColumn);
383 allHeaders.add(aheader);
384 aheader.setUpdater(new FileValueUpdater(aheader, "size"));
385 celltable.addColumn(aColumn=new DragAndDropColumn<FileResource,String>(new TextCell()) {
387 public String getValue(FileResource object) {
388 return formatter.format(object.getModificationDate());
390 },aheader = new SortableHeader("Last Modified"));
391 allHeaders.add(aheader);
392 aheader.setUpdater(new FileValueUpdater(aheader, "date"));
395 provider.addDataDisplay(celltable);
396 celltable.addDragStopHandler(dragStop);
397 celltable.addDragStartHandler(new DragStartEventHandler() {
399 public void onDragStart(DragStartEvent event) {
400 FileResource value = event.getDraggableData();
402 com.google.gwt.dom.client.Element helper = event.getHelper();
403 SafeHtmlBuilder sb = new SafeHtmlBuilder();
404 sb.appendHtmlConstant("<b>");
405 DisplayHelper.log(value.getName());
406 if(getSelectedFiles().size()==1)
407 sb.appendEscaped(value.getName());
409 sb.appendEscaped(getSelectedFiles().size()+" files");
410 sb.appendHtmlConstant("</b>");
411 helper.setInnerHTML(sb.toSafeHtml().asString());
421 VerticalPanel vp = new VerticalPanel();
423 pagerTop = new GssSimplePager(GssSimplePager.TextLocation.CENTER);
424 pagerTop.setDisplay(celltable);
425 uploadButtonTop=new Button("<span id='topMenu.file.upload'>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + " Upload</span>");
426 uploadButtonTop.addClickHandler(new ClickHandler() {
429 public void onClick(ClickEvent event) {
430 new UploadFileCommand(null).execute();
433 HorizontalPanel topPanel = new HorizontalPanel();
434 topPanel.add(pagerTop);
435 topPanel.add(uploadButtonTop);
437 celltable.setWidth("100%");
439 pagerBottom = new GssSimplePager(GssSimplePager.TextLocation.CENTER);
440 pagerBottom.setDisplay(celltable);
441 HorizontalPanel bottomPanel = new HorizontalPanel();
442 bottomPanel.add(pagerBottom);
443 uploadButtonBottom=new Button("<span id='topMenu.file.upload'>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + " Upload</span>");
444 uploadButtonBottom.addClickHandler(new ClickHandler() {
447 public void onClick(ClickEvent event) {
448 new UploadFileCommand(null).execute();
451 bottomPanel.add(uploadButtonBottom);
453 vp.setCellWidth(celltable, "100%");
456 pagerBottom.setVisible(false);
457 pagerTop.setVisible(false);
459 celltable.setStyleName("pithos-List");
460 selectionModel = new MultiSelectionModel<FileResource>(keyProvider);
463 Handler selectionHandler = new SelectionChangeEvent.Handler() {
465 public void onSelectionChange(com.google.gwt.view.client.SelectionChangeEvent event) {
466 if(getSelectedFiles().size()==1)
467 GSS.get().setCurrentSelection(getSelectedFiles().get(0));
469 GSS.get().setCurrentSelection(getSelectedFiles());
472 selectionModel.addSelectionChangeHandler(selectionHandler);
474 celltable.setSelectionModel(selectionModel,GSSSelectionEventManager.<FileResource>createDefaultManager());
475 celltable.setPageSize(GSS.VISIBLE_FILE_COUNT);
477 // Scheduler.get().scheduleIncremental(new RepeatingCommand() {
480 // public boolean execute() {
481 // return fetchRootFolder();
484 sinkEvents(Event.ONCONTEXTMENU);
485 sinkEvents(Event.ONMOUSEUP);
486 sinkEvents(Event.ONMOUSEDOWN);
487 sinkEvents(Event.ONCLICK);
488 sinkEvents(Event.ONKEYDOWN);
489 sinkEvents(Event.ONDBLCLICK);
490 GSS.preventIESelection();
495 public List<FileResource> getSelectedFiles() {
496 return new ArrayList<FileResource>(selectionModel.getSelectedSet());
499 private void initDragOperation(DragAndDropColumn<?, ?> column) {
501 // retrieve draggableOptions on the column
502 DraggableOptions draggableOptions = column.getDraggableOptions();
503 // use template to construct the helper. The content of the div will be set
505 draggableOptions.setHelper($(Templates.INSTANCE.outerHelper().asString()));
506 //draggableOptions.setZIndex(100);
507 // opacity of the helper
508 draggableOptions.setAppendTo("body");
509 //draggableOptions.setOpacity((float) 0.8);
510 draggableOptions.setContainment("document");
511 // cursor to use during the drag operation
512 draggableOptions.setCursor(Cursor.MOVE);
513 // set the revert option
514 draggableOptions.setRevert(RevertOption.ON_INVALID_DROP);
515 // prevents dragging when user click on the category drop-down list
516 draggableOptions.setCancel("select");
519 draggableOptions.setOnBeforeDragStart(new DragFunction() {
522 public void f(DragContext context) {
523 FileResource value = context.getDraggableData();
524 if(!selectionModel.isSelected(value)){
525 throw new StopDragException();
532 public void showContextMenu(Event event){
533 menuShowing = new FileContextMenu(images, false, true);
534 menuShowing=menuShowing.onEmptyEvent(event);
537 public void onBrowserEvent(Event event) {
539 if (files == null || files.size() == 0) {
540 if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
541 menuShowing = new FileContextMenu(images, false, true);
542 menuShowing=menuShowing.onEmptyEvent(event);
543 event.preventDefault();
544 event.cancelBubble(true);
548 if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() != 0) {
549 GWT.log("*****GOING TO SHOW CONTEXT MENU ****", null);
550 menuShowing = new FileContextMenu(images, false, false);
551 menuShowing=menuShowing.onEvent(event);
552 event.cancelBubble(true);
553 event.preventDefault();
554 } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
555 menuShowing = new FileContextMenu(images, false, true);
556 menuShowing=menuShowing.onEmptyEvent(event);
557 event.cancelBubble(true);
558 event.preventDefault();
559 } else if (DOM.eventGetType(event) == Event.ONDBLCLICK)
560 if (getSelectedFiles().size() == 1) {
562 FileResource file = getSelectedFiles().get(0);
563 String dateString = RestCommand.getDate();
564 String resource = file.getUri().substring(app.getApiPath().length() - 1, file.getUri().length());
565 String sig = app.getCurrentUserResource().getUsername() + " " +
566 RestCommand.calculateSig("GET", dateString, resource,
567 RestCommand.base64decode(app.getToken()));
568 Window.open(file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date=" + URL.encodeComponent(dateString), "_blank", "");
569 event.preventDefault();
572 super.onBrowserEvent(event);
576 * Retrieve the root folder for the current user.
578 * @return true if the retrieval was successful
580 protected boolean fetchRootFolder() {
581 UserResource user = GSS.get().getCurrentUserResource();
584 // Update cache and clear selection.
585 updateFileCache(true);
591 * Update the display of the file list.
593 void update(boolean sort) {
594 int count = folderFileCount;
595 int max = startIndex + GSS.VISIBLE_FILE_COUNT;
600 copyListAndContinue(files);
601 for(FileResource f : files){
602 folderTotalSize += f.getContentLength();
604 if (folderFileCount == 0) {
605 showingStats = "no files";
606 } else if (folderFileCount < GSS.VISIBLE_FILE_COUNT) {
607 if (folderFileCount == 1)
608 showingStats = "1 file";
610 showingStats = folderFileCount + " files";
611 showingStats += " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
613 showingStats = "" + (startIndex + 1) + " - " + max + " of " + count + " files" + " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
616 updateCurrentlyShowingStats();
621 * Return the proper icon based on the MIME type of the file.
626 private ImageResource getFileIcon(FileResource file) {
627 String mimetype = file.getContentType();
628 boolean shared = false;
629 if(GSS.get().getTreeView().getSelection()!=null && (GSS.get().getTreeView().getSelection() instanceof OtherUserResource || GSS.get().getTreeView().getSelection() instanceof OthersFolderResource)){
630 OtherUserResource otherUser = null;
631 if(GSS.get().getTreeView().getSelection() instanceof OtherUserResource)
632 otherUser = (OtherUserResource) GSS.get().getTreeView().getSelection();
633 else if (GSS.get().getTreeView().getSelection() instanceof OthersFolderResource){
634 otherUser = GSS.get().getTreeView().getOtherUserResourceOfOtherFolder((OthersFolderResource) GSS.get().getTreeView().getSelection());
639 String uname = otherUser.getUsername();
641 uname = GSS.get().getTreeView().getOthers().getUsernameOfUri(otherUser.getUri());
643 shared = file.isShared();
647 shared = file.isShared();
648 if (mimetype == null)
649 return shared ? images.documentShared() : images.document();
650 mimetype = mimetype.toLowerCase();
651 if (mimetype.startsWith("application/pdf"))
652 return shared ? images.pdfShared() : images.pdf();
653 else if (mimetype.endsWith("excel"))
654 return shared ? images.spreadsheetShared() : images.spreadsheet();
655 else if (mimetype.endsWith("msword"))
656 return shared ? images.wordprocessorShared() : images.wordprocessor();
657 else if (mimetype.endsWith("powerpoint"))
658 return shared ? images.presentationShared() : images.presentation();
659 else if (mimetype.startsWith("application/zip") ||
660 mimetype.startsWith("application/gzip") ||
661 mimetype.startsWith("application/x-gzip") ||
662 mimetype.startsWith("application/x-tar") ||
663 mimetype.startsWith("application/x-gtar"))
664 return shared ? images.zipShared() : images.zip();
665 else if (mimetype.startsWith("text/html"))
666 return shared ? images.htmlShared() : images.html();
667 else if (mimetype.startsWith("text/plain"))
668 return shared ? images.txtShared() : images.txt();
669 else if (mimetype.startsWith("image/"))
670 return shared ? images.imageShared() : images.image();
671 else if (mimetype.startsWith("video/"))
672 return shared ? images.videoShared() : images.video();
673 else if (mimetype.startsWith("audio/"))
674 return shared ? images.audioShared() : images.audio();
675 return shared ? images.documentShared() : images.document();
679 * Update status panel with currently showing file stats.
681 public void updateCurrentlyShowingStats() {
682 GSS.get().getStatusPanel().updateCurrentlyShowing(showingStats);
685 public void updateFileCache(boolean clearSelection){
690 final RestResource folderItem = GSS.get().getTreeView().getSelection();
692 if (folderItem == null || folderItem.equals(GSS.get().getTreeView().getOthers())) {
693 setFiles(new ArrayList<FileResource>());
697 else if (folderItem instanceof RestResourceWrapper) {
698 setFiles(((RestResourceWrapper) folderItem).getResource().getFiles());
701 else if (folderItem instanceof SharedResource) {
702 setFiles(((SharedResource) folderItem).getFiles());
705 else if (folderItem instanceof OtherUserResource) {
706 setFiles(((OtherUserResource) folderItem).getFiles());
709 else if (folderItem instanceof TrashResource) {
710 setFiles(((TrashResource) folderItem).getFiles());
717 * Fill the file cache with data.
719 public void setFiles(final List<FileResource> _files) {
720 if (_files.size() > 0 && ! (GSS.get().getTreeView().getSelection() instanceof TrashResource)) {
721 files = new ArrayList<FileResource>();
722 for (FileResource fres : _files)
723 if (!fres.isDeleted())
728 Collections.sort(files, new Comparator<FileResource>() {
731 public int compare(FileResource arg0, FileResource arg1) {
732 return arg0.getName().compareTo(arg1.getName());
736 folderFileCount = files.size();
738 nameHeader.setSorted(true);
739 nameHeader.toggleReverseSort();
740 for (SortableHeader otherHeader : allHeaders) {
741 if (otherHeader != nameHeader) {
742 otherHeader.setSorted(false);
743 otherHeader.setReverseSort(true);
753 * Does the list contains the requested filename
758 public boolean contains(String fileName) {
759 for (int i = 0; i < files.size(); i++)
760 if (files.get(i).getName().equals(fileName))
765 public void clearSelectedRows() {
766 Iterator<FileResource> it = selectionModel.getSelectedSet().iterator();
768 selectionModel.setSelected(it.next(),false);
776 public void selectAllRows() {
777 Iterator<FileResource> it = provider.getList().iterator();
779 selectionModel.setSelected(it.next(),true);
786 private void sortFiles(final String sortingProperty, final boolean sortingType){
787 Collections.sort(files, new Comparator<FileResource>() {
790 public int compare(FileResource arg0, FileResource arg1) {
791 AbstractImagePrototype descPrototype = AbstractImagePrototype.create(images.desc());
792 AbstractImagePrototype ascPrototype = AbstractImagePrototype.create(images.asc());
794 if (sortingProperty.equals("version")) {
795 return arg0.getVersion().compareTo(arg1.getVersion());
796 } else if (sortingProperty.equals("owner")) {
797 return arg0.getOwner().compareTo(arg1.getOwner());
798 } else if (sortingProperty.equals("date")) {
799 return arg0.getModificationDate().compareTo(arg1.getModificationDate());
800 } else if (sortingProperty.equals("size")) {
801 return arg0.getContentLength().compareTo(arg1.getContentLength());
802 } else if (sortingProperty.equals("name")) {
803 return arg0.getName().compareTo(arg1.getName());
804 } else if (sortingProperty.equals("path")) {
805 return arg0.getUri().compareTo(arg1.getUri());
807 return arg0.getName().compareTo(arg1.getName());
810 else if (sortingProperty.equals("version")) {
812 return arg1.getVersion().compareTo(arg0.getVersion());
813 } else if (sortingProperty.equals("owner")) {
815 return arg1.getOwner().compareTo(arg0.getOwner());
816 } else if (sortingProperty.equals("date")) {
818 return arg1.getModificationDate().compareTo(arg0.getModificationDate());
819 } else if (sortingProperty.equals("size")) {
821 return arg1.getContentLength().compareTo(arg0.getContentLength());
822 } else if (sortingProperty.equals("name")) {
824 return arg1.getName().compareTo(arg0.getName());
825 } else if (sortingProperty.equals("path")) {
827 return arg1.getUri().compareTo(arg0.getUri());
830 return arg1.getName().compareTo(arg0.getName());
837 final class FileValueUpdater implements ValueUpdater<String>{
838 private String property;
839 private SortableHeader header;
843 public FileValueUpdater(SortableHeader header,String property) {
844 this.property=property;
848 public void update(String value) {
849 header.setSorted(true);
850 header.toggleReverseSort();
852 for (SortableHeader otherHeader : allHeaders) {
853 if (otherHeader != header) {
854 otherHeader.setSorted(false);
855 otherHeader.setReverseSort(true);
858 celltable.redrawHeaders();
859 sortFiles(property, header.getReverseSort());
860 FileList.this.update(true);
865 * Creates a new ArrayList<FileResources> from the given files ArrayList
866 * in order that the input files remain untouched
867 * and continues to find user's full names of each FileResource element
868 * in the new ArrayList
872 private void copyListAndContinue(List<FileResource> filesInput){
873 List<FileResource> copiedFiles = new ArrayList<FileResource>();
874 for(FileResource file : filesInput) {
875 copiedFiles.add(file);
877 handleFullNames(copiedFiles);
881 * Examines whether or not the user's full name exists in the
882 * userFullNameMap in the GSS.java for every element of the input list.
883 * If the user's full name does not exist in the map then a command is being made.
887 private void handleFullNames(List<FileResource> filesInput){
888 if(filesInput.size() == 0){
893 if(GSS.get().findUserFullName(filesInput.get(0).getOwner()) == null){
894 findFullNameAndUpdate(filesInput);
898 if(filesInput.size() >= 1){
899 filesInput.remove(filesInput.get(0));
900 if(filesInput.isEmpty()){
903 handleFullNames(filesInput);
909 * Makes a command to search for full name from a given username.
910 * Only after the completion of the command the celltable is shown
911 * or the search for the next full name continues.
915 private void findFullNameAndUpdate(final List<FileResource> filesInput){
916 String aUserName = filesInput.get(0).getOwner();
917 String path = GSS.get().getApiPath() + "users/" + aUserName;
919 GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
921 public void onComplete() {
922 final UserSearchResource result = getResult();
923 for (UserResource user : result.getUsers()){
924 String username = user.getUsername();
925 String userFullName = user.getName();
926 GSS.get().putUserToMap(username, userFullName);
927 if(filesInput.size() >= 1){
928 filesInput.remove(filesInput.get(0));
929 if(filesInput.isEmpty()){
932 handleFullNames(filesInput);
938 public void onError(Throwable t) {
940 GSS.get().displayError("Unable to fetch user's full name from the given username " + filesInput.get(0).getOwner());
941 if(filesInput.size() >= 1){
942 filesInput.remove(filesInput.get(0));
943 handleFullNames(filesInput);
947 DeferredCommand.addCommand(gg);
951 * Shows the files in the cellTable
954 private void showCellTable(){
955 if(files.size()>GSS.VISIBLE_FILE_COUNT){
956 pagerBottom.setVisible(true);
957 pagerTop.setVisible(true);
960 pagerTop.setVisible(false);
961 pagerBottom.setVisible(false);
963 RestResource selectedItem = GSS.get().getTreeView().getSelection();
964 boolean uploadVisible = !(selectedItem != null && (selectedItem instanceof TrashResource || selectedItem instanceof TrashFolderResource || selectedItem instanceof SharedResource || selectedItem instanceof OthersResource || selectedItem instanceof OtherUserResource));
965 uploadButtonBottom.setVisible(uploadVisible&&files.size()>=GSS.VISIBLE_FILE_COUNT);
966 uploadButtonTop.setVisible(uploadVisible&&files.size()>=GSS.VISIBLE_FILE_COUNT);
967 provider.setList(files);
971 //celltable.redraw();
972 celltable.redrawHeaders();