replace tree with celltree ( still work in progress)
[pithos] / src / gr / ebs / gss / client / FileList.java
1 /*
2  * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 package gr.ebs.gss.client;
20
21
22 import gr.ebs.gss.client.rest.GetCommand;
23 import gr.ebs.gss.client.rest.MultipleHeadCommand;
24 import gr.ebs.gss.client.rest.RestCommand;
25 import gr.ebs.gss.client.rest.RestException;
26 import gr.ebs.gss.client.rest.resource.FileResource;
27 import gr.ebs.gss.client.rest.resource.FolderResource;
28 import gr.ebs.gss.client.rest.resource.OtherUserResource;
29 import gr.ebs.gss.client.rest.resource.RestResource;
30 import gr.ebs.gss.client.rest.resource.SharedResource;
31 import gr.ebs.gss.client.rest.resource.TrashResource;
32 import gr.ebs.gss.client.rest.resource.UserResource;
33 import gr.ebs.gss.client.rest.resource.UserSearchResource;
34
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;
40
41 import com.google.gwt.cell.client.ImageResourceCell;
42 import com.google.gwt.cell.client.ValueUpdater;
43 import com.google.gwt.core.client.GWT;
44 import com.google.gwt.core.client.Scheduler;
45 import com.google.gwt.core.client.Scheduler.RepeatingCommand;
46 import com.google.gwt.dom.client.Element;
47 import com.google.gwt.dom.client.NativeEvent;
48 import com.google.gwt.http.client.URL;
49 import com.google.gwt.i18n.client.DateTimeFormat;
50 import com.google.gwt.resources.client.ClientBundle;
51 import com.google.gwt.resources.client.ImageResource;
52 import com.google.gwt.user.cellview.client.CellTable;
53 import com.google.gwt.user.cellview.client.Column;
54 import com.google.gwt.user.cellview.client.TextColumn;
55 import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
56 import com.google.gwt.user.client.DOM;
57 import com.google.gwt.user.client.DeferredCommand;
58 import com.google.gwt.user.client.Event;
59 import com.google.gwt.user.client.Window;
60 import com.google.gwt.user.client.ui.AbstractImagePrototype;
61 import com.google.gwt.user.client.ui.Composite;
62 import com.google.gwt.user.client.ui.Grid;
63 import com.google.gwt.user.client.ui.HTML;
64 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
65 import com.google.gwt.user.client.ui.HorizontalPanel;
66 import com.google.gwt.user.client.ui.TreeItem;
67 import com.google.gwt.user.client.ui.VerticalPanel;
68 import com.google.gwt.user.client.ui.Widget;
69 import com.google.gwt.view.client.DefaultSelectionEventManager;
70 import com.google.gwt.view.client.MultiSelectionModel;
71 import com.google.gwt.view.client.ProvidesKey;
72 import com.google.gwt.view.client.SelectionChangeEvent;
73 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
74
75 /**
76  * A composite that displays the list of files in a particular folder.
77  */
78 public class FileList extends Composite {
79
80         interface TableResources extends CellTable.Resources {
81             @Source({CellTable.Style.DEFAULT_CSS, "GssCellTable.css"})
82             TableStyle cellTableStyle();
83           }
84         
85         /**
86            * The styles applied to the table.
87            */
88           interface TableStyle extends CellTable.Style {
89           }
90
91         private String showingStats = "";
92
93         private int startIndex = 0;
94
95         /**
96          * A constant that denotes the completion of an IncrementalCommand.
97          */
98         public static final boolean DONE = false;
99
100         
101         
102         private final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
103
104         /**
105          * Specifies that the images available for this composite will be the ones
106          * available in FileContextMenu.
107          */
108         public interface Images extends ClientBundle,FileContextMenu.Images, CellTreeView.Images {
109
110                 @Source("gr/ebs/gss/resources/blank.gif")
111                 ImageResource blank();
112
113                 @Source("gr/ebs/gss/resources/asc.png")
114                 ImageResource asc();
115
116                 @Source("gr/ebs/gss/resources/desc.png")
117                 ImageResource desc();
118
119                 @Source("gr/ebs/gss/resources/mimetypes/document_shared.png")
120                 ImageResource documentShared();
121
122                 @Source("gr/ebs/gss/resources/mimetypes/kcmfontinst.png")
123                 ImageResource wordprocessor();
124
125                 @Source("gr/ebs/gss/resources/mimetypes/log.png")
126                 ImageResource spreadsheet();
127
128                 @Source("gr/ebs/gss/resources/mimetypes/kpresenter_kpr.png")
129                 ImageResource presentation();
130
131                 @Source("gr/ebs/gss/resources/mimetypes/acroread.png")
132                 ImageResource pdf();
133
134                 @Source("gr/ebs/gss/resources/mimetypes/image.png")
135                 ImageResource image();
136
137                 @Source("gr/ebs/gss/resources/mimetypes/video2.png")
138                 ImageResource video();
139
140                 @Source("gr/ebs/gss/resources/mimetypes/knotify.png")
141                 ImageResource audio();
142
143                 @Source("gr/ebs/gss/resources/mimetypes/html.png")
144                 ImageResource html();
145
146                 @Source("gr/ebs/gss/resources/mimetypes/txt.png")
147                 ImageResource txt();
148
149                 @Source("gr/ebs/gss/resources/mimetypes/ark2.png")
150                 ImageResource zip();
151
152                 @Source("gr/ebs/gss/resources/mimetypes/kcmfontinst_shared.png")
153                 ImageResource wordprocessorShared();
154
155                 @Source("gr/ebs/gss/resources/mimetypes/log_shared.png")
156                 ImageResource spreadsheetShared();
157
158                 @Source("gr/ebs/gss/resources/mimetypes/kpresenter_kpr_shared.png")
159                 ImageResource presentationShared();
160
161                 @Source("gr/ebs/gss/resources/mimetypes/acroread_shared.png")
162                 ImageResource pdfShared();
163
164                 @Source("gr/ebs/gss/resources/mimetypes/image_shared.png")
165                 ImageResource imageShared();
166
167                 @Source("gr/ebs/gss/resources/mimetypes/video2_shared.png")
168                 ImageResource videoShared();
169
170                 @Source("gr/ebs/gss/resources/mimetypes/knotify_shared.png")
171                 ImageResource audioShared();
172
173                 @Source("gr/ebs/gss/resources/mimetypes/html_shared.png")
174                 ImageResource htmlShared();
175
176                 @Source("gr/ebs/gss/resources/mimetypes/txt_shared.png")
177                 ImageResource txtShared();
178
179                 @Source("gr/ebs/gss/resources/mimetypes/ark2_shared.png")
180                 ImageResource zipShared();
181
182         }
183         
184         /**
185          * Retrieve the celltable.
186          *
187          * @return the celltable
188          */
189         public CellTable<FileResource> getCelltable() {
190                 return celltable;
191         }
192         /**
193          * A label with the number of files in this folder.
194          */
195         private HTML countLabel = new HTML();
196
197         /**
198          * The table widget with the file list.
199          */
200         //private FileTable table = new FileTable(GSS.VISIBLE_FILE_COUNT + 1, 8);
201
202         /**
203          * The navigation bar for paginating the results.
204          */
205         private HorizontalPanel navBar = new HorizontalPanel();
206
207         /**
208          * The number of files in this folder.
209          */
210         int folderFileCount;
211
212         /**
213          * Total folder size
214          */
215         long folderTotalSize;
216
217         /**
218          * A cache of the files in the list.
219          */
220         private List<FileResource> files;
221
222         /**
223          * The widget's image bundle.
224          */
225         private final Images images;
226         VerticalPanel panel;
227         
228         private FileContextMenu menuShowing;
229         private CellTable<FileResource> celltable;
230         private final MultiSelectionModel<FileResource> selectionModel;
231         private final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
232         SortableHeader nameHeader;
233         /**
234          * Construct the file list widget. This entails setting up the widget
235          * layout, fetching the number of files in the current folder from the
236          * server and filling the local file cache of displayed files with data from
237          * the server, as well.
238          *
239          * @param _images
240          */
241         public FileList(Images _images) {
242                 images = _images;
243                 CellTable.Resources resources = GWT.create(TableResources.class);
244                 
245                 
246                 // Create the 'navigation' bar at the upper-right.
247                 HorizontalPanel innerNavBar = new HorizontalPanel();
248                 innerNavBar.setStyleName("gss-ListNavBar");
249                 innerNavBar.setSpacing(8);
250                 innerNavBar.add(countLabel);
251                 navBar.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
252                 navBar.add(innerNavBar);
253                 navBar.setWidth("100%");
254                 ProvidesKey<FileResource> keyProvider = new ProvidesKey<FileResource>(){
255
256                         @Override
257                         public Object getKey(FileResource item) {
258                                 return item.getUri();
259                         }
260                         
261                 };
262                 final TextColumn<FileResource> nameColumn = new TextColumn<FileResource>() {
263
264                         @Override
265                         public String getValue(FileResource object) {
266                                 // TODO Auto-generated method stub
267                                 return object.getName();
268                         }
269                         
270                         
271                 };
272                 celltable = new CellTable<FileResource>(100,resources,keyProvider){
273                         @Override
274                         protected void onBrowserEvent2(Event event) {
275                                 /*if (DOM.eventGetType((Event) event) == Event.ONMOUSEDOWN && DOM.eventGetButton((Event) event) == NativeEvent.BUTTON_RIGHT){
276                                         fireClickEvent((Element) event.getEventTarget().cast());                                        
277                                 }*/
278                                 GWT.log("event in celltable:"+event.getType());
279                                 super.onBrowserEvent2(event);
280                         }
281                 };
282                 
283                 Column<FileResource, ImageResource> status = new Column<FileResource, ImageResource>(new ImageResourceCell()) {
284                   @Override
285                   public ImageResource getValue(FileResource entity) {
286                     return getFileIcon(entity);
287                   }
288                };
289                celltable.addColumn(status,"");
290                 
291                 
292                 celltable.addColumn(nameColumn,nameHeader = new SortableHeader("Name"));
293                 allHeaders.add(nameHeader);
294                 nameHeader.setSorted(true);
295                 nameHeader.toggleReverseSort();
296                 nameHeader.setUpdater(new FileValueUpdater(nameHeader, "name"));
297                 celltable.redrawHeaders();
298                 SortableHeader aheader;
299                 celltable.addColumn(new TextColumn<FileResource>() {
300                         @Override
301                         public String getValue(FileResource object) {
302                                 return GSS.get().findUserFullName(object.getOwner());
303                         }                       
304                 },aheader = new SortableHeader("Owner"));
305                 allHeaders.add(aheader);
306                 aheader.setUpdater(new FileValueUpdater(aheader, "owner"));
307                 celltable.addColumn(new TextColumn<FileResource>() {
308                         @Override
309                         public String getValue(FileResource object) {
310                                 // TODO Auto-generated method stub
311                                 return object.getPath();
312                         }                       
313                 },aheader = new SortableHeader("Path"));
314                 allHeaders.add(aheader);
315                 aheader.setUpdater(new FileValueUpdater(aheader, "path"));      
316                 celltable.addColumn(new TextColumn<FileResource>() {
317                         @Override
318                         public String getValue(FileResource object) {
319                                 // TODO Auto-generated method stub
320                                 return object.getVersion().toString();
321                         }                       
322                 },aheader = new SortableHeader("Version"));
323                 allHeaders.add(aheader);
324                 aheader.setUpdater(new FileValueUpdater(aheader, "version"));
325                 celltable.addColumn(new TextColumn<FileResource>() {
326                         @Override
327                         public String getValue(FileResource object) {
328                                 // TODO Auto-generated method stub
329                                 return object.getFileSizeAsString();
330                         }                       
331                 },aheader = new SortableHeader("Size"));
332                 allHeaders.add(aheader);
333                 aheader.setUpdater(new FileValueUpdater(aheader, "size"));      
334                 celltable.addColumn(new TextColumn<FileResource>() {
335                         @Override
336                         public String getValue(FileResource object) {
337                                 return formatter.format(object.getModificationDate());
338                         }                       
339                 },aheader = new SortableHeader("Last Modified"));
340                 allHeaders.add(aheader);
341                 aheader.setUpdater(new FileValueUpdater(aheader, "date"));
342                 
343                 initWidget(celltable);
344                 setStyleName("gss-List");
345                 selectionModel = new MultiSelectionModel<FileResource>();
346                 
347
348                  Handler selectionHandler = new SelectionChangeEvent.Handler() { 
349              @Override 
350              public void onSelectionChange(com.google.gwt.view.client.SelectionChangeEvent event) {
351                  if(getSelectedFiles().size()==1)
352                          GSS.get().setCurrentSelection(getSelectedFiles().get(0));
353                  else
354                          GSS.get().setCurrentSelection(getSelectedFiles());
355                                 //contextMenu.setFiles(getSelectedFiles());
356              }
357          };
358          selectionModel.addSelectionChangeHandler(selectionHandler);
359          
360                 celltable.setSelectionModel(selectionModel,GSSSelectionEventManager.<FileResource>createDefaultManager());
361                 celltable.setPageSize(GSS.VISIBLE_FILE_COUNT);
362                 celltable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
363                 Scheduler.get().scheduleIncremental(new RepeatingCommand() {
364
365                         @Override
366                         public boolean execute() {
367                                 return fetchRootFolder();
368                         }
369                 });
370                 sinkEvents(Event.ONCONTEXTMENU);
371                 sinkEvents(Event.ONMOUSEUP);
372                 sinkEvents(Event.ONMOUSEDOWN);
373                 sinkEvents(Event.ONCLICK);
374                 sinkEvents(Event.ONKEYDOWN);
375                 sinkEvents(Event.ONDBLCLICK);
376                 GSS.preventIESelection();
377         }
378         
379         //public native void fireClickEvent(Element element) /*-{
380           //  var evObj = $doc.createEvent('MouseEvents');
381             //evObj.initEvent('click', true, true);
382             //element.dispatchEvent(evObj);
383         //}-*/;
384
385          public List<FileResource> getSelectedFiles() {
386          return new ArrayList<FileResource>(selectionModel.getSelectedSet());
387          }
388         
389         
390
391         @Override
392         public void onBrowserEvent(Event event) {
393                 
394                 if (files == null || files.size() == 0) {
395                         if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
396                                 menuShowing = new FileContextMenu(images, false, true);
397                                 menuShowing=menuShowing.onEmptyEvent(event);
398                         }
399                         return;
400                 }
401                 if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() != 0) {
402                         GWT.log("*****GOING TO SHOW CONTEXT MENU ****", null);
403                         menuShowing =  new FileContextMenu(images, false, false);
404                         menuShowing=menuShowing.onEvent(event);
405                 } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
406                         menuShowing = new FileContextMenu(images, false, true);
407                         menuShowing=menuShowing.onEmptyEvent(event);
408                 } else if (DOM.eventGetType(event) == Event.ONDBLCLICK)
409                         if (getSelectedFiles().size() == 1) {
410                                 GSS app = GSS.get();
411                                 FileResource file = getSelectedFiles().get(0);
412                                 String dateString = RestCommand.getDate();
413                                 String resource = file.getUri().substring(app.getApiPath().length() - 1, file.getUri().length());
414                                 String sig = app.getCurrentUserResource().getUsername() + " " +
415                                                 RestCommand.calculateSig("GET", dateString, resource,
416                                                 RestCommand.base64decode(app.getToken()));
417                                 Window.open(file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date=" + URL.encodeComponent(dateString), "_blank", "");
418                                 event.preventDefault();
419                                 return;
420                         }
421                 super.onBrowserEvent(event);
422         }
423
424         /**
425          * Retrieve the root folder for the current user.
426          *
427          * @return true if the retrieval was successful
428          */
429         protected boolean fetchRootFolder() {
430                 UserResource user = GSS.get().getCurrentUserResource();
431                 if (user == null)
432                         return !DONE;
433                 // Update cache and clear selection.
434                 updateFileCache(true);
435                 return DONE;
436         }
437
438         
439
440         
441         /**
442          * Make the specified row look like selected or not, according to the
443          * <code>selected</code> flag.
444          *
445          * @param row
446          * @param selected
447          */
448         void styleRow(final int row, final boolean selected) {
449                 
450         }
451
452         /**
453          * Update the display of the file list.
454          */
455         void update(boolean sort) {
456                 int count = folderFileCount;
457                 int max = startIndex + GSS.VISIBLE_FILE_COUNT;
458                 if (max > count)
459                         max = count;
460                 folderTotalSize = 0;
461                 
462                 copyListAndContinue(files);
463                 
464                 if (folderFileCount == 0) {
465                         showingStats = "no files";
466                 } else if (folderFileCount < GSS.VISIBLE_FILE_COUNT) {
467                         if (folderFileCount == 1)
468                                 showingStats = "1 file";
469                         else
470                                 showingStats = folderFileCount + " files";
471                         showingStats += " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
472                 } else {
473                         showingStats = "" + (startIndex + 1) + " - " + max + " of " + count + " files" + " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
474                 }
475                 updateCurrentlyShowingStats();
476
477         }
478
479         /**
480          * Return the proper icon based on the MIME type of the file.
481          *
482          * @param file
483          * @return the icon
484          */
485         private ImageResource getFileIcon(FileResource file) {
486                 String mimetype = file.getContentType();
487                 boolean shared = false;
488                 /*Folders folders = GSS.get().getFolders();
489                 if(folders.getCurrent() != null && folders.isOthersSharedItem(folders.getCurrent())){
490                         DnDTreeItem otherUser = (DnDTreeItem) folders.getUserOfSharedItem(folders.getCurrent());
491                         if(otherUser==null)
492                                 shared = false;
493                         else{
494                                 String uname = otherUser.getOtherUserResource().getUsername();
495                                 if(uname==null)
496                                         uname = ((DnDTreeItem)folders.getSharesItem()).getOthersResource().getUsernameOfUri(otherUser.getOtherUserResource().getUri());
497                                 if(uname != null)
498                                         shared = file.isShared();
499                         }
500                 }
501                 else*/
502                         shared = file.isShared();
503                 if (mimetype == null)
504                         return shared ? images.documentShared() : images.document();
505                 mimetype = mimetype.toLowerCase();
506                 if (mimetype.startsWith("application/pdf"))
507                         return shared ? images.pdfShared() : images.pdf();
508                 else if (mimetype.endsWith("excel"))
509                         return shared ? images.spreadsheetShared() : images.spreadsheet();
510                 else if (mimetype.endsWith("msword"))
511                         return shared ? images.wordprocessorShared() : images.wordprocessor();
512                 else if (mimetype.endsWith("powerpoint"))
513                         return shared ? images.presentationShared() : images.presentation();
514                 else if (mimetype.startsWith("application/zip") ||
515                                         mimetype.startsWith("application/gzip") ||
516                                         mimetype.startsWith("application/x-gzip") ||
517                                         mimetype.startsWith("application/x-tar") ||
518                                         mimetype.startsWith("application/x-gtar"))
519                         return shared ? images.zipShared() : images.zip();
520                 else if (mimetype.startsWith("text/html"))
521                         return shared ? images.htmlShared() : images.html();
522                 else if (mimetype.startsWith("text/plain"))
523                         return shared ? images.txtShared() : images.txt();
524                 else if (mimetype.startsWith("image/"))
525                         return shared ? images.imageShared() : images.image();
526                 else if (mimetype.startsWith("video/"))
527                         return shared ? images.videoShared() : images.video();
528                 else if (mimetype.startsWith("audio/"))
529                         return shared ? images.audioShared() : images.audio();
530                 return shared ? images.documentShared() : images.document();
531         }
532
533         /**
534          * Update status panel with currently showing file stats.
535          */
536         public void updateCurrentlyShowingStats() {
537                 GSS.get().getStatusPanel().updateCurrentlyShowing(showingStats);
538         }
539
540         /**
541          * Adjust the height of the table by adding and removing rows as necessary.
542          *
543          * @param newHeight the new height to reach
544          */
545         //void resizeTableHeight(final int newHeight) {
546                 /*GWT.log("Panel: " + newHeight + ", parent: " + table.getParent().getOffsetHeight(), null);
547                 // Fill the rest with empty slots.
548                 if (newHeight > table.getOffsetHeight())
549                         while (newHeight > table.getOffsetHeight()) {
550                                 table.resizeRows(table.getRowCount() + 1);
551                                 GWT.log("Table: " + table.getOffsetHeight() + ", rows: " + table.getRowCount(), null);
552                         }
553                 else
554                         while (newHeight < table.getOffsetHeight()) {
555                                 table.resizeRows(table.getRowCount() - 1);
556                                 GWT.log("Table: " + table.getOffsetHeight() + ", rows: " + table.getRowCount(), null);
557                         }*/
558         //}
559
560         public void updateFileCache(boolean updateSelectedFolder, final boolean clearSelection) {
561                 updateFileCache(updateSelectedFolder, clearSelection, null);
562         }
563
564         public void updateFileCache(boolean updateSelectedFolder, final boolean clearSelection, final String newFilename) {
565                 if (!updateSelectedFolder && !GSS.get().getTreeView().getSelection().equals(GSS.get().getTreeView().getTrash()))
566                         updateFileCache(clearSelection);
567                 else if (GSS.get().getTreeView().getSelection() != null) {
568                         final RestResource selectedResource = GSS.get().getTreeView().getSelection(); 
569                         if (selectedResource instanceof FolderResource) {
570                                 
571                                 update(true);
572                         }
573                 }
574                 updateFileCache(clearSelection);
575         }
576
577
578         private void updateFileCache(boolean clearSelection) {
579                 updateFileCache(clearSelection, null);
580         }
581
582         /**
583          * Update the file cache with data from the server.
584          *
585          * @param newFilename the new name of the previously selected file,
586          *                      if a rename operation has taken place
587          */
588         private void updateFileCache(boolean clearSelection, String newFilename) {
589                 if (clearSelection)
590                         clearSelectedRows();
591                 startIndex = 0;
592                 final RestResource folderItem = GSS.get().getTreeView().getSelection();
593                 // Validation.
594                 if (folderItem == null || folderItem.equals(GSS.get().getTreeView().getOthers())) {
595                         setFiles(new ArrayList<FileResource>());
596                         update(true);
597                         return;
598                 }
599                 if (folderItem instanceof FolderResource) {
600                         setFiles(((FolderResource) folderItem).getFiles());
601                         update(true);
602                 }
603                 if (folderItem instanceof SharedResource) {
604                         setFiles(((SharedResource) folderItem).getFiles());
605                         update(true);
606                 }
607                 if (folderItem instanceof TrashResource) {
608                         setFiles(((TrashResource) folderItem).getFiles());
609                         update(true);
610                 }
611         }
612
613         /**
614          * Fill the file cache with data.
615          */
616         public void setFiles(final List<FileResource> _files) {
617                 /*if (_files.size() > 0 && !GSS.get().getFolders().isTrash(GSS.get().getFolders().getCurrent())) {
618                         files = new ArrayList<FileResource>();
619                         for (FileResource fres : _files)
620                                 if (!fres.isDeleted())
621                                         files.add(fres);
622                 } else*/
623                         files = _files;
624                 Collections.sort(files, new Comparator<FileResource>() {
625
626                         @Override
627                         public int compare(FileResource arg0, FileResource arg1) {
628                                 return arg0.getName().compareTo(arg1.getName());
629                         }
630
631                 });
632                 folderFileCount = files.size();
633         }
634
635         
636
637         
638         /**
639          * Does the list contains the requested filename
640          *
641          * @param fileName
642          * @return true/false
643          */
644         public boolean contains(String fileName) {
645                 for (int i = 0; i < files.size(); i++)
646                         if (files.get(i).getName().equals(fileName))
647                                 return true;
648                 return false;
649         }
650
651         public void clearSelectedRows() {
652                 Iterator<FileResource> it = selectionModel.getSelectedSet().iterator();
653                 while(it.hasNext()){
654                         selectionModel.setSelected(it.next(),false);
655                 }
656                 
657                 /*for (int r : selectedRows) {
658                         int row = r - startIndex;
659                         styleRow(row, false);
660                         makeRowNotDraggable(row+1);
661                 }
662                 selectedRows.clear();
663                 Object sel = GSS.get().getCurrentSelection();
664                 if (sel instanceof FileResource || sel instanceof List)
665                         GSS.get().setCurrentSelection(null);
666                 if(menuShowing != null && menuShowing.isShowing()){
667                         menuShowing.hide();
668                         menuShowing=null;
669                 }*/
670         }
671
672         /**
673          *
674          */
675         public void selectAllRows() {
676                 /*clearSelectedRows();
677                 int count = folderFileCount;
678                 if (count == 0)
679                         return;
680                 int max = startIndex + GSS.VISIBLE_FILE_COUNT;
681                 if (max > count)
682                         max = count;
683                 int i = 1;
684                 for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
685                         // Don't read past the end.
686                         // if (i > folderFileCount)
687                         // break;
688                         if (startIndex + i > folderFileCount)
689                                 break;
690                         selectedRows.add(startIndex + i - 1);
691                         styleRow(i - 1, true);
692                         makeRowDraggable(i);
693                 }
694                 GSS.get().setCurrentSelection(getSelectedFiles());
695                 contextMenu.setFiles(getSelectedFiles());*/
696
697
698         }
699
700         private void makeRowDraggable(int row){
701         /*      int contextRow = getWidgetRow(contextMenu, table);
702                 if (contextRow != -1)
703                         table.setWidget(contextRow, 0, getFileIcon(files.get(contextRow - 1)).createImage());
704                 contextMenu.setWidget(new HTML(getFileIcon(files.get(row - 1)).getHTML()));
705                 table.setWidget(row, 0, contextMenu);
706                 //for(int i=1;i<table.getCellCount(row);i++)
707                         //GSS.get().getDragController().makeDraggable(table.getWidget(row, i));
708                 table.setWidget(row, 1, new DnDSimpleFocusPanel(table.getWidget(row, 1)));
709                 ((DnDSimpleFocusPanel)table.getWidget(row, 1)).setFiles(getSelectedFiles());
710                 GSS.get().getDragController().makeDraggable(table.getWidget(row, 1));*/
711         }
712         private void makeRowNotDraggable(int row){
713                 /*if(table.getWidget(row, 1) instanceof DnDSimpleFocusPanel){
714                         ((DnDSimpleFocusPanel)table.getWidget(row, 1)).setFiles(null);
715                         GSS.get().getDragController().makeNotDraggable(table.getWidget(row, 1));
716                         table.setWidget(row, 1, new DnDSimpleFocusPanel(((DnDSimpleFocusPanel)table.getWidget(row, 1)).getWidget()));
717
718                 }
719                 */
720         }
721
722         private int getWidgetRow(Widget widget, Grid grid) {
723                 /*for (int row = 0; row < grid.getRowCount(); row++)
724                         for (int col = 0; col < grid.getCellCount(row); col++) {
725                                 Widget w = table.getWidget(row, col);
726                                 if (w == widget)
727                                         return row;
728                         }*/
729                 return -1;
730         }
731         
732         
733         private void sortFiles(final String sortingProperty, final boolean sortingType){
734                 Collections.sort(files, new Comparator<FileResource>() {
735
736             @Override
737             public int compare(FileResource arg0, FileResource arg1) {
738                     AbstractImagePrototype descPrototype = AbstractImagePrototype.create(images.desc());
739                     AbstractImagePrototype ascPrototype = AbstractImagePrototype.create(images.asc());
740                     if (sortingType){
741                             if (sortingProperty.equals("version")) {
742                                     return arg0.getVersion().compareTo(arg1.getVersion());
743                             } else if (sortingProperty.equals("owner")) {
744                                     return arg0.getOwner().compareTo(arg1.getOwner());
745                             } else if (sortingProperty.equals("date")) {
746                                     return arg0.getModificationDate().compareTo(arg1.getModificationDate());
747                             } else if (sortingProperty.equals("size")) {
748                                     return arg0.getContentLength().compareTo(arg1.getContentLength());
749                             } else if (sortingProperty.equals("name")) {
750                                     return arg0.getName().compareTo(arg1.getName());
751                             } else if (sortingProperty.equals("path")) {
752                                     return arg0.getUri().compareTo(arg1.getUri());
753                             } else {
754                                     return arg0.getName().compareTo(arg1.getName());
755                             }
756                     }
757                     else if (sortingProperty.equals("version")) {
758                             
759                             return arg1.getVersion().compareTo(arg0.getVersion());
760                     } else if (sortingProperty.equals("owner")) {
761                             
762                             return arg1.getOwner().compareTo(arg0.getOwner());
763                     } else if (sortingProperty.equals("date")) {
764                             
765                             return arg1.getModificationDate().compareTo(arg0.getModificationDate());
766                     } else if (sortingProperty.equals("size")) {
767                             
768                             return arg1.getContentLength().compareTo(arg0.getContentLength());
769                     } else if (sortingProperty.equals("name")) {
770                             
771                             return arg1.getName().compareTo(arg0.getName());
772                     } else if (sortingProperty.equals("path")) {
773                             
774                             return arg1.getUri().compareTo(arg0.getUri());
775                     } else {
776                             
777                             return arg1.getName().compareTo(arg0.getName());
778                     }
779             }
780
781                 });
782         }
783         
784         final class FileValueUpdater implements ValueUpdater<String>{
785                 private String property;
786                 private SortableHeader header;
787                 /**
788                  * 
789                  */
790                 public FileValueUpdater(SortableHeader header,String property) {
791                         this.property=property;
792                         this.header=header;
793                 }
794                 @Override
795                 public void update(String value) {
796                         header.setSorted(true);
797                         header.toggleReverseSort();
798
799                 for (SortableHeader otherHeader : allHeaders) {
800                   if (otherHeader != header) {
801                     otherHeader.setSorted(false);
802                     otherHeader.setReverseSort(true);
803                   }
804                 }
805                 celltable.redrawHeaders();
806                 sortFiles(property, header.getReverseSort());
807                 FileList.this.update(true);                     
808                 }
809                 
810         }
811         /**
812          * Creates a new ArrayList<FileResources> from the given files ArrayList 
813          * in order that the input files remain untouched 
814          * and continues to find user's full names of each FileResource element
815          * in the new ArrayList
816          *    
817          * @param filesInput
818          */
819         private void copyListAndContinue(List<FileResource> filesInput){
820                 List<FileResource> copiedFiles = new ArrayList<FileResource>();         
821                 for(FileResource file : filesInput) {
822                         copiedFiles.add(file);
823                 }
824                 handleFullNames(copiedFiles);
825         }
826         
827         /**
828          * Examines whether or not the user's full name exists in the 
829          * userFullNameMap in the GSS.java for every element of the input list.
830          * If the user's full name does not exist in the map then a command is being made.  
831          * 
832          * @param filesInput
833          */
834         private void handleFullNames(List<FileResource> filesInput){            
835                 if(filesInput.size() == 0){
836                         showCellTable();
837                         return;
838                 }               
839
840                 if(GSS.get().findUserFullName(filesInput.get(0).getOwner()) == null){
841                         findFullNameAndUpdate(filesInput);              
842                         return;
843                 }
844                                 
845                 if(filesInput.size() >= 1){
846                         filesInput.remove(filesInput.get(0));
847                         if(filesInput.isEmpty()){
848                                 showCellTable();                                
849                         }else{
850                                 handleFullNames(filesInput);
851                         }
852                 }               
853         }
854         
855         /**
856          * Makes a command to search for full name from a given username. 
857          * Only after the completion of the command the celltable is shown
858          * or the search for the next full name continues.
859          *  
860          * @param filesInput
861          */
862         private void findFullNameAndUpdate(final List<FileResource> filesInput){                
863                 String aUserName = filesInput.get(0).getOwner();
864                 String path = GSS.get().getApiPath() + "users/" + aUserName; 
865
866                 GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
867                         @Override
868                         public void onComplete() {
869                                 final UserSearchResource result = getResult();
870                                 for (UserResource user : result.getUsers()){
871                                         String username = user.getUsername();
872                                         String userFullName = user.getName();
873                                         GSS.get().putUserToMap(username, userFullName);
874                                         if(filesInput.size() >= 1){
875                                                 filesInput.remove(filesInput.get(0));
876                                                 if(filesInput.isEmpty()){
877                                                         showCellTable();
878                                                 }else{
879                                                         handleFullNames(filesInput);
880                                                 }                                                                                               
881                                         }                                                                       
882                                 }
883                         }
884                         @Override
885                         public void onError(Throwable t) {
886                                 GWT.log("", t);
887                                 GSS.get().displayError("Unable to fetch user's full name from the given username " + filesInput.get(0).getOwner());
888                                 if(filesInput.size() >= 1){
889                                         filesInput.remove(filesInput.get(0));
890                                         handleFullNames(filesInput);                                    
891                                 }
892                         }
893                 };
894                 DeferredCommand.addCommand(gg);
895         
896         }
897         /**
898          * Shows the files in the cellTable 
899          */
900
901         private void showCellTable(){
902                 celltable.setRowCount(files.size());
903                 celltable.setRowData(0,files);
904                 celltable.redrawHeaders();              
905         }
906
907         
908 }