2 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
6 * GSS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
19 package gr.ebs.gss.client;
21 import static com.google.gwt.query.client.GQuery.$;
22 import gr.ebs.gss.client.rest.GetCommand;
23 import gr.ebs.gss.client.rest.RestCommand;
24 import gr.ebs.gss.client.rest.resource.FileResource;
25 import gr.ebs.gss.client.rest.resource.RestResource;
26 import gr.ebs.gss.client.rest.resource.RestResourceWrapper;
27 import gr.ebs.gss.client.rest.resource.SharedResource;
28 import gr.ebs.gss.client.rest.resource.TrashResource;
29 import gr.ebs.gss.client.rest.resource.UserResource;
30 import gr.ebs.gss.client.rest.resource.UserSearchResource;
31 import gwtquery.plugins.draggable.client.DraggableOptions;
32 import gwtquery.plugins.draggable.client.StopDragException;
33 import gwtquery.plugins.draggable.client.DraggableOptions.DragFunction;
34 import gwtquery.plugins.draggable.client.DraggableOptions.RevertOption;
35 import gwtquery.plugins.draggable.client.events.DragContext;
36 import gwtquery.plugins.draggable.client.events.DragStartEvent;
37 import gwtquery.plugins.draggable.client.events.DragStopEvent;
38 import gwtquery.plugins.draggable.client.events.DragStartEvent.DragStartEventHandler;
39 import gwtquery.plugins.draggable.client.events.DragStopEvent.DragStopEventHandler;
40 import gwtquery.plugins.droppable.client.gwt.DragAndDropCellTable;
41 import gwtquery.plugins.droppable.client.gwt.DragAndDropColumn;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.Comparator;
46 import java.util.Iterator;
47 import java.util.List;
49 import com.google.gwt.cell.client.AbstractCell;
50 import com.google.gwt.cell.client.ImageResourceCell;
51 import com.google.gwt.cell.client.SafeHtmlCell;
52 import com.google.gwt.cell.client.TextCell;
53 import com.google.gwt.cell.client.ValueUpdater;
54 import com.google.gwt.core.client.GWT;
55 import com.google.gwt.core.client.Scheduler;
56 import com.google.gwt.core.client.Scheduler.RepeatingCommand;
57 import com.google.gwt.dom.client.Style.Cursor;
58 import com.google.gwt.http.client.URL;
59 import com.google.gwt.i18n.client.DateTimeFormat;
60 import com.google.gwt.resources.client.ClientBundle;
61 import com.google.gwt.resources.client.ImageResource;
62 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
63 import com.google.gwt.safehtml.shared.SafeHtml;
64 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
65 import com.google.gwt.safehtml.shared.SafeHtmlUtils;
66 import com.google.gwt.user.cellview.client.CellTable;
67 import com.google.gwt.user.cellview.client.Column;
68 import com.google.gwt.user.cellview.client.TextColumn;
69 import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
70 import com.google.gwt.user.client.DOM;
71 import com.google.gwt.user.client.DeferredCommand;
72 import com.google.gwt.user.client.Event;
73 import com.google.gwt.user.client.Window;
74 import com.google.gwt.user.client.ui.AbstractImagePrototype;
75 import com.google.gwt.user.client.ui.Composite;
76 import com.google.gwt.user.client.ui.HTML;
77 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
78 import com.google.gwt.user.client.ui.HorizontalPanel;
79 import com.google.gwt.user.client.ui.VerticalPanel;
80 import com.google.gwt.view.client.MultiSelectionModel;
81 import com.google.gwt.view.client.ProvidesKey;
82 import com.google.gwt.view.client.SelectionChangeEvent;
83 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
86 * A composite that displays the list of files in a particular folder.
88 public class FileList extends Composite {
90 interface TableResources extends DragAndDropCellTable.Resources {
91 @Source({CellTable.Style.DEFAULT_CSS, "GssCellTable.css"})
92 TableStyle cellTableStyle();
95 static interface Templates extends SafeHtmlTemplates {
96 Templates INSTANCE = GWT.create(Templates.class);
98 @Template("<div id='dragHelper' style='border:1px solid black; background-color:#ffffff; color:black; width:150px;z-index:100'></div>")
99 SafeHtml outerHelper();
104 * The styles applied to the table.
106 interface TableStyle extends CellTable.Style {
109 private String showingStats = "";
111 private int startIndex = 0;
114 * A constant that denotes the completion of an IncrementalCommand.
116 public static final boolean DONE = false;
120 private final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
123 * Specifies that the images available for this composite will be the ones
124 * available in FileContextMenu.
126 public interface Images extends ClientBundle,FileContextMenu.Images, CellTreeView.Images {
128 @Source("gr/ebs/gss/resources/blank.gif")
129 ImageResource blank();
131 @Source("gr/ebs/gss/resources/asc.png")
134 @Source("gr/ebs/gss/resources/desc.png")
135 ImageResource desc();
137 @Source("gr/ebs/gss/resources/mimetypes/document_shared.png")
138 ImageResource documentShared();
140 @Source("gr/ebs/gss/resources/mimetypes/kcmfontinst.png")
141 ImageResource wordprocessor();
143 @Source("gr/ebs/gss/resources/mimetypes/log.png")
144 ImageResource spreadsheet();
146 @Source("gr/ebs/gss/resources/mimetypes/kpresenter_kpr.png")
147 ImageResource presentation();
149 @Source("gr/ebs/gss/resources/mimetypes/acroread.png")
152 @Source("gr/ebs/gss/resources/mimetypes/image.png")
153 ImageResource image();
155 @Source("gr/ebs/gss/resources/mimetypes/video2.png")
156 ImageResource video();
158 @Source("gr/ebs/gss/resources/mimetypes/knotify.png")
159 ImageResource audio();
161 @Source("gr/ebs/gss/resources/mimetypes/html.png")
162 ImageResource html();
164 @Source("gr/ebs/gss/resources/mimetypes/txt.png")
167 @Source("gr/ebs/gss/resources/mimetypes/ark2.png")
170 @Source("gr/ebs/gss/resources/mimetypes/kcmfontinst_shared.png")
171 ImageResource wordprocessorShared();
173 @Source("gr/ebs/gss/resources/mimetypes/log_shared.png")
174 ImageResource spreadsheetShared();
176 @Source("gr/ebs/gss/resources/mimetypes/kpresenter_kpr_shared.png")
177 ImageResource presentationShared();
179 @Source("gr/ebs/gss/resources/mimetypes/acroread_shared.png")
180 ImageResource pdfShared();
182 @Source("gr/ebs/gss/resources/mimetypes/image_shared.png")
183 ImageResource imageShared();
185 @Source("gr/ebs/gss/resources/mimetypes/video2_shared.png")
186 ImageResource videoShared();
188 @Source("gr/ebs/gss/resources/mimetypes/knotify_shared.png")
189 ImageResource audioShared();
191 @Source("gr/ebs/gss/resources/mimetypes/html_shared.png")
192 ImageResource htmlShared();
194 @Source("gr/ebs/gss/resources/mimetypes/txt_shared.png")
195 ImageResource txtShared();
197 @Source("gr/ebs/gss/resources/mimetypes/ark2_shared.png")
198 ImageResource zipShared();
202 DragStopEventHandler dragStop = new DragStopEventHandler() {
205 public void onDragStop(DragStopEvent event) {
206 GWT.log("DRAG STOPPED");
211 private static class ContactCell extends AbstractCell<gr.ebs.gss.client.rest.resource.FileResource> {
214 * The html of the image used for contacts.
217 private final String imageHtml;
219 public ContactCell(ImageResource image) {
220 this.imageHtml = AbstractImagePrototype.create(image).getHTML();
228 public void render(Context context, FileResource value, SafeHtmlBuilder sb) {
229 // Value can be null, so do a null check..
234 sb.appendHtmlConstant("<table>");
236 // Add the contact image.
237 sb.appendHtmlConstant("<tr><td rowspan='3'>");
238 sb.appendHtmlConstant(imageHtml);
239 sb.appendHtmlConstant("</td>");
241 // Add the name and address.
242 DisplayHelper.log("value.getName()");
243 sb.appendHtmlConstant("<td style='font-size:95%;' id='"+value.getName()+"'>");
244 sb.appendEscaped(value.getName());
245 sb.appendHtmlConstant("</td></tr><tr><td>");
246 sb.appendEscaped(value.getFileSizeAsString());
247 sb.appendHtmlConstant("</td></tr></table>");
253 * Retrieve the celltable.
255 * @return the celltable
257 public DragAndDropCellTable<FileResource> getCelltable() {
261 * A label with the number of files in this folder.
263 private HTML countLabel = new HTML();
266 * The table widget with the file list.
268 //private FileTable table = new FileTable(GSS.VISIBLE_FILE_COUNT + 1, 8);
271 * The navigation bar for paginating the results.
273 private HorizontalPanel navBar = new HorizontalPanel();
276 * The number of files in this folder.
283 long folderTotalSize;
286 * A cache of the files in the list.
288 private List<FileResource> files;
291 * The widget's image bundle.
293 private final Images images;
296 private FileContextMenu menuShowing;
297 private DragAndDropCellTable<FileResource> celltable;
298 private final MultiSelectionModel<FileResource> selectionModel;
299 private final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
300 SortableHeader nameHeader;
302 * Construct the file list widget. This entails setting up the widget
303 * layout, fetching the number of files in the current folder from the
304 * server and filling the local file cache of displayed files with data from
305 * the server, as well.
309 public FileList(Images _images) {
311 DragAndDropCellTable.Resources resources = GWT.create(TableResources.class);
314 // Create the 'navigation' bar at the upper-right.
315 HorizontalPanel innerNavBar = new HorizontalPanel();
316 innerNavBar.setStyleName("gss-ListNavBar");
317 innerNavBar.setSpacing(8);
318 innerNavBar.add(countLabel);
319 navBar.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
320 navBar.add(innerNavBar);
321 navBar.setWidth("100%");
322 ProvidesKey<FileResource> keyProvider = new ProvidesKey<FileResource>(){
325 public Object getKey(FileResource item) {
326 return item.getUri();
330 final DragAndDropColumn<FileResource,SafeHtml> nameColumn = new DragAndDropColumn<FileResource,SafeHtml>(new SafeHtmlCell()) {
334 public SafeHtml getValue(FileResource object) {
335 SafeHtmlBuilder sb = new SafeHtmlBuilder();
336 if (object.getContentType().endsWith("png") || object.getContentType().endsWith("gif") || object.getContentType().endsWith("jpeg") ){
337 sb.appendHtmlConstant("<span id='fileList."+ object.getName() +"'>");
338 sb.appendEscaped(object.getName());
339 sb.appendHtmlConstant("</span>");
340 sb.appendHtmlConstant(" <a href='" +
341 GSS.get().getTopPanel().getFileMenu().getDownloadURL(object) +
342 "' title='" + object.getOwner() + " : " + object.getPath() + object.getName() +
343 "' rel='lytebox[mnf]' " +
344 "onclick='myLytebox.start(this, false, false); return false;'>" +
350 sb.appendHtmlConstant("<span id='fileList."+ object.getName() +"'>");
351 sb.appendEscaped(object.getName());
352 sb.appendHtmlConstant("</span>");
354 return sb.toSafeHtml();
359 initDragOperation(nameColumn);
360 celltable = new DragAndDropCellTable<FileResource>(100,resources,keyProvider){
362 protected void onBrowserEvent2(Event event) {
363 /*if (DOM.eventGetType((Event) event) == Event.ONMOUSEDOWN && DOM.eventGetButton((Event) event) == NativeEvent.BUTTON_RIGHT){
364 fireClickEvent((Element) event.getEventTarget().cast());
366 super.onBrowserEvent2(event);
370 celltable.addDragStopHandler(dragStop);
371 celltable.addDragStartHandler(new DragStartEventHandler() {
373 public void onDragStart(DragStartEvent event) {
374 FileResource value = event.getDraggableData();
375 com.google.gwt.dom.client.Element helper = event.getHelper();
376 SafeHtmlBuilder sb = new SafeHtmlBuilder();
377 sb.appendHtmlConstant("<b>");
378 DisplayHelper.log(value.getName());
379 sb.appendEscaped(value.getName());
380 sb.appendHtmlConstant("</b>");
381 helper.setInnerHTML(sb.toSafeHtml().asString());
385 Column<FileResource, ImageResource> status = new Column<FileResource, ImageResource>(new ImageResourceCell()) {
387 public ImageResource getValue(FileResource entity) {
388 return getFileIcon(entity);
391 celltable.addColumn(status,"");
394 celltable.addColumn(nameColumn,nameHeader = new SortableHeader("Name"));
395 allHeaders.add(nameHeader);
396 nameHeader.setSorted(true);
397 nameHeader.toggleReverseSort();
398 nameHeader.setUpdater(new FileValueUpdater(nameHeader, "name"));
399 celltable.redrawHeaders();
400 SortableHeader aheader;
401 celltable.addColumn(new TextColumn<FileResource>() {
403 public String getValue(FileResource object) {
404 return GSS.get().findUserFullName(object.getOwner());
406 },aheader = new SortableHeader("Owner"));
407 allHeaders.add(aheader);
408 aheader.setUpdater(new FileValueUpdater(aheader, "owner"));
409 celltable.addColumn(new TextColumn<FileResource>() {
411 public String getValue(FileResource object) {
412 // TODO Auto-generated method stub
413 return object.getPath();
415 },aheader = new SortableHeader("Path"));
416 allHeaders.add(aheader);
417 aheader.setUpdater(new FileValueUpdater(aheader, "path"));
418 celltable.addColumn(new TextColumn<FileResource>() {
420 public String getValue(FileResource object) {
421 // TODO Auto-generated method stub
422 return object.getVersion().toString();
424 },aheader = new SortableHeader("Version"));
425 allHeaders.add(aheader);
426 aheader.setUpdater(new FileValueUpdater(aheader, "version"));
427 celltable.addColumn(new TextColumn<FileResource>() {
429 public String getValue(FileResource object) {
430 // TODO Auto-generated method stub
431 return object.getFileSizeAsString();
433 },aheader = new SortableHeader("Size"));
434 allHeaders.add(aheader);
435 aheader.setUpdater(new FileValueUpdater(aheader, "size"));
436 celltable.addColumn(new TextColumn<FileResource>() {
438 public String getValue(FileResource object) {
439 return formatter.format(object.getModificationDate());
441 },aheader = new SortableHeader("Last Modified"));
442 allHeaders.add(aheader);
443 aheader.setUpdater(new FileValueUpdater(aheader, "date"));
445 initWidget(celltable);
446 setStyleName("gss-List");
447 selectionModel = new MultiSelectionModel<FileResource>();
450 Handler selectionHandler = new SelectionChangeEvent.Handler() {
452 public void onSelectionChange(com.google.gwt.view.client.SelectionChangeEvent event) {
453 if(getSelectedFiles().size()==1)
454 GSS.get().setCurrentSelection(getSelectedFiles().get(0));
456 GSS.get().setCurrentSelection(getSelectedFiles());
457 //contextMenu.setFiles(getSelectedFiles());
460 selectionModel.addSelectionChangeHandler(selectionHandler);
462 celltable.setSelectionModel(selectionModel,GSSSelectionEventManager.<FileResource>createDefaultManager());
463 celltable.setPageSize(GSS.VISIBLE_FILE_COUNT);
464 celltable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
465 Scheduler.get().scheduleIncremental(new RepeatingCommand() {
468 public boolean execute() {
469 return fetchRootFolder();
472 sinkEvents(Event.ONCONTEXTMENU);
473 sinkEvents(Event.ONMOUSEUP);
474 sinkEvents(Event.ONMOUSEDOWN);
475 sinkEvents(Event.ONCLICK);
476 sinkEvents(Event.ONKEYDOWN);
477 sinkEvents(Event.ONDBLCLICK);
478 GSS.preventIESelection();
481 //public native void fireClickEvent(Element element) /*-{
482 // var evObj = $doc.createEvent('MouseEvents');
483 //evObj.initEvent('click', true, true);
484 //element.dispatchEvent(evObj);
487 public List<FileResource> getSelectedFiles() {
488 return new ArrayList<FileResource>(selectionModel.getSelectedSet());
491 private void initDragOperation(DragAndDropColumn<?, ?> column) {
493 // retrieve draggableOptions on the column
494 DraggableOptions draggableOptions = column.getDraggableOptions();
495 // use template to construct the helper. The content of the div will be set
497 draggableOptions.setHelper($(Templates.INSTANCE.outerHelper().asString()));
498 //draggableOptions.setZIndex(100);
499 // opacity of the helper
500 draggableOptions.setAppendTo("body");
501 //draggableOptions.setOpacity((float) 0.8);
502 draggableOptions.setContainment("document");
503 // cursor to use during the drag operation
504 draggableOptions.setCursor(Cursor.MOVE);
505 // set the revert option
506 draggableOptions.setRevert(RevertOption.ON_INVALID_DROP);
507 // prevents dragging when user click on the category drop-down list
508 draggableOptions.setCancel("select");
510 draggableOptions.setOnBeforeDragStart(new DragFunction() {
513 public void f(DragContext context) {
514 FileResource value = context.getDraggableData();
515 if(!selectionModel.isSelected(value)){
516 throw new StopDragException();
523 public void showContextMenu(Event event){
524 menuShowing = new FileContextMenu(images, false, true);
525 menuShowing=menuShowing.onEmptyEvent(event);
528 public void onBrowserEvent(Event event) {
530 if (files == null || files.size() == 0) {
531 if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
532 menuShowing = new FileContextMenu(images, false, true);
533 menuShowing=menuShowing.onEmptyEvent(event);
534 event.preventDefault();
535 event.cancelBubble(true);
539 if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() != 0) {
540 GWT.log("*****GOING TO SHOW CONTEXT MENU ****", null);
541 menuShowing = new FileContextMenu(images, false, false);
542 menuShowing=menuShowing.onEvent(event);
543 event.cancelBubble(true);
544 event.preventDefault();
545 } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
546 menuShowing = new FileContextMenu(images, false, true);
547 menuShowing=menuShowing.onEmptyEvent(event);
548 event.cancelBubble(true);
549 event.preventDefault();
550 } else if (DOM.eventGetType(event) == Event.ONDBLCLICK)
551 if (getSelectedFiles().size() == 1) {
553 FileResource file = getSelectedFiles().get(0);
554 String dateString = RestCommand.getDate();
555 String resource = file.getUri().substring(app.getApiPath().length() - 1, file.getUri().length());
556 String sig = app.getCurrentUserResource().getUsername() + " " +
557 RestCommand.calculateSig("GET", dateString, resource,
558 RestCommand.base64decode(app.getToken()));
559 Window.open(file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date=" + URL.encodeComponent(dateString), "_blank", "");
560 event.preventDefault();
563 super.onBrowserEvent(event);
567 * Retrieve the root folder for the current user.
569 * @return true if the retrieval was successful
571 protected boolean fetchRootFolder() {
572 UserResource user = GSS.get().getCurrentUserResource();
575 // Update cache and clear selection.
576 updateFileCache(true);
582 * Update the display of the file list.
584 void update(boolean sort) {
585 int count = folderFileCount;
586 int max = startIndex + GSS.VISIBLE_FILE_COUNT;
591 copyListAndContinue(files);
592 for(FileResource f : files){
593 folderTotalSize += f.getContentLength();
595 if (folderFileCount == 0) {
596 showingStats = "no files";
597 } else if (folderFileCount < GSS.VISIBLE_FILE_COUNT) {
598 if (folderFileCount == 1)
599 showingStats = "1 file";
601 showingStats = folderFileCount + " files";
602 showingStats += " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
604 showingStats = "" + (startIndex + 1) + " - " + max + " of " + count + " files" + " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
606 updateCurrentlyShowingStats();
611 * Return the proper icon based on the MIME type of the file.
616 private ImageResource getFileIcon(FileResource file) {
617 String mimetype = file.getContentType();
618 boolean shared = false;
619 //TODO: FETCH USER OF OTHER FOLDER ITEM
620 //if(GSS.get().getTreeView().getSelection()!=null && (GSS.get().getTreeView().getSelection() instanceof OtherUserResource || GSS.get().getTreeView().getSelection() instanceof OthersFolderResource))
621 /*Folders folders = GSS.get().getFolders();
622 if(folders.getCurrent() != null && folders.isOthersSharedItem(folders.getCurrent())){
623 DnDTreeItem otherUser = (DnDTreeItem) folders.getUserOfSharedItem(folders.getCurrent());
627 String uname = otherUser.getOtherUserResource().getUsername();
629 uname = ((DnDTreeItem)folders.getSharesItem()).getOthersResource().getUsernameOfUri(otherUser.getOtherUserResource().getUri());
631 shared = file.isShared();
635 shared = file.isShared();
636 if (mimetype == null)
637 return shared ? images.documentShared() : images.document();
638 mimetype = mimetype.toLowerCase();
639 if (mimetype.startsWith("application/pdf"))
640 return shared ? images.pdfShared() : images.pdf();
641 else if (mimetype.endsWith("excel"))
642 return shared ? images.spreadsheetShared() : images.spreadsheet();
643 else if (mimetype.endsWith("msword"))
644 return shared ? images.wordprocessorShared() : images.wordprocessor();
645 else if (mimetype.endsWith("powerpoint"))
646 return shared ? images.presentationShared() : images.presentation();
647 else if (mimetype.startsWith("application/zip") ||
648 mimetype.startsWith("application/gzip") ||
649 mimetype.startsWith("application/x-gzip") ||
650 mimetype.startsWith("application/x-tar") ||
651 mimetype.startsWith("application/x-gtar"))
652 return shared ? images.zipShared() : images.zip();
653 else if (mimetype.startsWith("text/html"))
654 return shared ? images.htmlShared() : images.html();
655 else if (mimetype.startsWith("text/plain"))
656 return shared ? images.txtShared() : images.txt();
657 else if (mimetype.startsWith("image/"))
658 return shared ? images.imageShared() : images.image();
659 else if (mimetype.startsWith("video/"))
660 return shared ? images.videoShared() : images.video();
661 else if (mimetype.startsWith("audio/"))
662 return shared ? images.audioShared() : images.audio();
663 return shared ? images.documentShared() : images.document();
667 * Update status panel with currently showing file stats.
669 public void updateCurrentlyShowingStats() {
670 GSS.get().getStatusPanel().updateCurrentlyShowing(showingStats);
673 public void updateFileCache(boolean updateSelectedFolder, final boolean clearSelection) {
674 updateFileCache(updateSelectedFolder, clearSelection, null);
677 public void updateFileCache(boolean updateSelectedFolder, final boolean clearSelection, final String newFilename) {
678 if (!updateSelectedFolder && !GSS.get().getTreeView().getSelection().equals(GSS.get().getTreeView().getTrash()))
679 updateFileCache(clearSelection);
680 else if (GSS.get().getTreeView().getSelection() != null) {
683 updateFileCache(clearSelection);
687 private void updateFileCache(boolean clearSelection) {
688 updateFileCache(clearSelection, null);
692 * Update the file cache with data from the server.
694 * @param newFilename the new name of the previously selected file,
695 * if a rename operation has taken place
697 private void updateFileCache(boolean clearSelection, String newFilename) {
701 final RestResource folderItem = GSS.get().getTreeView().getSelection();
703 if (folderItem == null || folderItem.equals(GSS.get().getTreeView().getOthers())) {
704 setFiles(new ArrayList<FileResource>());
708 if (folderItem instanceof RestResourceWrapper) {
709 setFiles(((RestResourceWrapper) folderItem).getResource().getFiles());
712 if (folderItem instanceof SharedResource) {
713 setFiles(((SharedResource) folderItem).getFiles());
716 if (folderItem instanceof TrashResource) {
717 setFiles(((TrashResource) folderItem).getFiles());
723 * Fill the file cache with data.
725 public void setFiles(final List<FileResource> _files) {
726 if (_files.size() > 0 && ! (GSS.get().getTreeView().getSelection() instanceof TrashResource)) {
727 files = new ArrayList<FileResource>();
728 for (FileResource fres : _files)
729 if (!fres.isDeleted())
734 Collections.sort(files, new Comparator<FileResource>() {
737 public int compare(FileResource arg0, FileResource arg1) {
738 return arg0.getName().compareTo(arg1.getName());
742 folderFileCount = files.size();
749 * Does the list contains the requested filename
754 public boolean contains(String fileName) {
755 for (int i = 0; i < files.size(); i++)
756 if (files.get(i).getName().equals(fileName))
761 public void clearSelectedRows() {
762 Iterator<FileResource> it = selectionModel.getSelectedSet().iterator();
764 selectionModel.setSelected(it.next(),false);
771 public void selectAllRows() {
772 Iterator<FileResource> it = selectionModel.getSelectedSet().iterator();
774 selectionModel.setSelected(it.next(),true);
781 private void sortFiles(final String sortingProperty, final boolean sortingType){
782 Collections.sort(files, new Comparator<FileResource>() {
785 public int compare(FileResource arg0, FileResource arg1) {
786 AbstractImagePrototype descPrototype = AbstractImagePrototype.create(images.desc());
787 AbstractImagePrototype ascPrototype = AbstractImagePrototype.create(images.asc());
789 if (sortingProperty.equals("version")) {
790 return arg0.getVersion().compareTo(arg1.getVersion());
791 } else if (sortingProperty.equals("owner")) {
792 return arg0.getOwner().compareTo(arg1.getOwner());
793 } else if (sortingProperty.equals("date")) {
794 return arg0.getModificationDate().compareTo(arg1.getModificationDate());
795 } else if (sortingProperty.equals("size")) {
796 return arg0.getContentLength().compareTo(arg1.getContentLength());
797 } else if (sortingProperty.equals("name")) {
798 return arg0.getName().compareTo(arg1.getName());
799 } else if (sortingProperty.equals("path")) {
800 return arg0.getUri().compareTo(arg1.getUri());
802 return arg0.getName().compareTo(arg1.getName());
805 else if (sortingProperty.equals("version")) {
807 return arg1.getVersion().compareTo(arg0.getVersion());
808 } else if (sortingProperty.equals("owner")) {
810 return arg1.getOwner().compareTo(arg0.getOwner());
811 } else if (sortingProperty.equals("date")) {
813 return arg1.getModificationDate().compareTo(arg0.getModificationDate());
814 } else if (sortingProperty.equals("size")) {
816 return arg1.getContentLength().compareTo(arg0.getContentLength());
817 } else if (sortingProperty.equals("name")) {
819 return arg1.getName().compareTo(arg0.getName());
820 } else if (sortingProperty.equals("path")) {
822 return arg1.getUri().compareTo(arg0.getUri());
825 return arg1.getName().compareTo(arg0.getName());
832 final class FileValueUpdater implements ValueUpdater<String>{
833 private String property;
834 private SortableHeader header;
838 public FileValueUpdater(SortableHeader header,String property) {
839 this.property=property;
843 public void update(String value) {
844 header.setSorted(true);
845 header.toggleReverseSort();
847 for (SortableHeader otherHeader : allHeaders) {
848 if (otherHeader != header) {
849 otherHeader.setSorted(false);
850 otherHeader.setReverseSort(true);
853 celltable.redrawHeaders();
854 sortFiles(property, header.getReverseSort());
855 FileList.this.update(true);
860 * Creates a new ArrayList<FileResources> from the given files ArrayList
861 * in order that the input files remain untouched
862 * and continues to find user's full names of each FileResource element
863 * in the new ArrayList
867 private void copyListAndContinue(List<FileResource> filesInput){
868 List<FileResource> copiedFiles = new ArrayList<FileResource>();
869 for(FileResource file : filesInput) {
870 copiedFiles.add(file);
872 handleFullNames(copiedFiles);
876 * Examines whether or not the user's full name exists in the
877 * userFullNameMap in the GSS.java for every element of the input list.
878 * If the user's full name does not exist in the map then a command is being made.
882 private void handleFullNames(List<FileResource> filesInput){
883 if(filesInput.size() == 0){
888 if(GSS.get().findUserFullName(filesInput.get(0).getOwner()) == null){
889 findFullNameAndUpdate(filesInput);
893 if(filesInput.size() >= 1){
894 filesInput.remove(filesInput.get(0));
895 if(filesInput.isEmpty()){
898 handleFullNames(filesInput);
904 * Makes a command to search for full name from a given username.
905 * Only after the completion of the command the celltable is shown
906 * or the search for the next full name continues.
910 private void findFullNameAndUpdate(final List<FileResource> filesInput){
911 String aUserName = filesInput.get(0).getOwner();
912 String path = GSS.get().getApiPath() + "users/" + aUserName;
914 GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
916 public void onComplete() {
917 final UserSearchResource result = getResult();
918 for (UserResource user : result.getUsers()){
919 String username = user.getUsername();
920 String userFullName = user.getName();
921 GSS.get().putUserToMap(username, userFullName);
922 if(filesInput.size() >= 1){
923 filesInput.remove(filesInput.get(0));
924 if(filesInput.isEmpty()){
927 handleFullNames(filesInput);
933 public void onError(Throwable t) {
935 GSS.get().displayError("Unable to fetch user's full name from the given username " + filesInput.get(0).getOwner());
936 if(filesInput.size() >= 1){
937 filesInput.remove(filesInput.get(0));
938 handleFullNames(filesInput);
942 DeferredCommand.addCommand(gg);
946 * Shows the files in the cellTable
949 private void showCellTable(){
950 celltable.setRowCount(files.size());
951 celltable.setRowData(0,files);
952 celltable.redrawHeaders();