Removed all static references to the Pithos class
[pithos-web-client] / src / gr / grnet / pithos / web / client / FileList.java
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35
36 package gr.grnet.pithos.web.client;
37
38 import com.google.gwt.event.dom.client.ContextMenuEvent;
39 import com.google.gwt.event.dom.client.ContextMenuHandler;
40 import static com.google.gwt.query.client.GQuery.$;
41
42 import gr.grnet.pithos.web.client.commands.UploadFileCommand;
43 import gr.grnet.pithos.web.client.foldertree.File;
44 import gr.grnet.pithos.web.client.foldertree.Folder;
45 import gr.grnet.pithos.web.client.foldertree.FolderTreeView;
46 import gr.grnet.pithos.web.client.rest.resource.FileResource;
47 import gwtquery.plugins.draggable.client.DraggableOptions;
48 import gwtquery.plugins.draggable.client.StopDragException;
49 import gwtquery.plugins.draggable.client.DraggableOptions.DragFunction;
50 import gwtquery.plugins.draggable.client.DraggableOptions.RevertOption;
51 import gwtquery.plugins.draggable.client.events.DragContext;
52 import gwtquery.plugins.draggable.client.events.DragStartEvent;
53 import gwtquery.plugins.draggable.client.events.DragStopEvent;
54 import gwtquery.plugins.draggable.client.events.DragStartEvent.DragStartEventHandler;
55 import gwtquery.plugins.draggable.client.events.DragStopEvent.DragStopEventHandler;
56 import gwtquery.plugins.droppable.client.gwt.DragAndDropCellTable;
57 import gwtquery.plugins.droppable.client.gwt.DragAndDropColumn;
58
59 import java.util.ArrayList;
60 import java.util.Collections;
61 import java.util.Comparator;
62 import java.util.Iterator;
63 import java.util.List;
64
65 import com.google.gwt.cell.client.ImageResourceCell;
66 import com.google.gwt.cell.client.SafeHtmlCell;
67 import com.google.gwt.cell.client.TextCell;
68 import com.google.gwt.cell.client.ValueUpdater;
69 import com.google.gwt.core.client.GWT;
70 import com.google.gwt.dom.client.Style.Cursor;
71 import com.google.gwt.event.dom.client.ClickEvent;
72 import com.google.gwt.event.dom.client.ClickHandler;
73 import com.google.gwt.i18n.client.DateTimeFormat;
74 import com.google.gwt.resources.client.ClientBundle;
75 import com.google.gwt.resources.client.ImageResource;
76 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
77 import com.google.gwt.safehtml.shared.SafeHtml;
78 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
79 import com.google.gwt.user.cellview.client.CellTable;
80 import com.google.gwt.user.cellview.client.GssSimplePager;
81 import com.google.gwt.user.client.Event;
82 import com.google.gwt.user.client.ui.AbstractImagePrototype;
83 import com.google.gwt.user.client.ui.Button;
84 import com.google.gwt.user.client.ui.Composite;
85 import com.google.gwt.user.client.ui.HorizontalPanel;
86 import com.google.gwt.user.client.ui.VerticalPanel;
87 import com.google.gwt.view.client.ListDataProvider;
88 import com.google.gwt.view.client.MultiSelectionModel;
89 import com.google.gwt.view.client.ProvidesKey;
90 import com.google.gwt.view.client.SelectionChangeEvent;
91 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
92
93 /**
94  * A composite that displays the list of files in a particular folder.
95  */
96 public class FileList extends Composite {
97
98         ListDataProvider<File> provider = new ListDataProvider<File>();
99
100     /**
101        * The styles applied to the table.
102        */
103     interface TableStyle extends CellTable.Style {
104     }
105
106         interface TableResources extends DragAndDropCellTable.Resources {
107             @Source({CellTable.Style.DEFAULT_CSS, "GssCellTable.css"})
108             TableStyle cellTableStyle();
109         }
110         
111         static interface Templates extends SafeHtmlTemplates {
112             Templates INSTANCE = GWT.create(Templates.class);
113
114             @Template("<div id='dragHelper' style='border:1px solid black; background-color:#ffffff; color:black; width:150px;z-index:100'></div>")
115             SafeHtml outerHelper();
116
117         @Template("<span id='{0}'>{0}</span>")
118         public SafeHtml filenameSpan(String filename);
119
120         @Template("<a href='{0}' title='{1}' rel='lytebox[mnf]' onclick='myLytebox.start(this, false, false); return false;'>(view)</a>")
121         public SafeHtml viewLink(String link, String title);
122
123         @Template("<table><tr><td rowspan='3'>{0}</td><td style='font-size:95%;' id='{1}'>{1}</td></tr><tr><td>{2}</td></tr></table>")
124         public SafeHtml rendelContactCell(String imageHtml, String name, String fileSize);
125
126         @Template("<span id='{0}' class='{1}'>{2}</span>")
127         public SafeHtml spanWithIdAndClass(String id, String cssClass, String content);
128         }
129
130     private String showingStats = "";
131
132         private int startIndex = 0;
133
134         /**
135          * A constant that denotes the completion of an IncrementalCommand.
136          */
137         public static final boolean DONE = false;
138
139         private final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
140
141         /**
142          * Specifies that the images available for this composite will be the ones
143          * available in FileContextMenu.
144          */
145         public interface Images extends ClientBundle,FileContextMenu.Images, CellTreeView.Images {
146
147                 @Source("gr/grnet/pithos/resources/blank.gif")
148                 ImageResource blank();
149
150                 @Source("gr/grnet/pithos/resources/asc.png")
151                 ImageResource asc();
152
153                 @Source("gr/grnet/pithos/resources/desc.png")
154                 ImageResource desc();
155
156                 @Source("gr/grnet/pithos/resources/mimetypes/document_shared.png")
157                 ImageResource documentShared();
158
159                 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst.png")
160                 ImageResource wordprocessor();
161
162                 @Source("gr/grnet/pithos/resources/mimetypes/log.png")
163                 ImageResource spreadsheet();
164
165                 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr.png")
166                 ImageResource presentation();
167
168                 @Source("gr/grnet/pithos/resources/mimetypes/acroread.png")
169                 ImageResource pdf();
170
171                 @Source("gr/grnet/pithos/resources/mimetypes/image.png")
172                 ImageResource image();
173
174                 @Source("gr/grnet/pithos/resources/mimetypes/video2.png")
175                 ImageResource video();
176
177                 @Source("gr/grnet/pithos/resources/mimetypes/knotify.png")
178                 ImageResource audio();
179
180                 @Source("gr/grnet/pithos/resources/mimetypes/html.png")
181                 ImageResource html();
182
183                 @Source("gr/grnet/pithos/resources/mimetypes/txt.png")
184                 ImageResource txt();
185
186                 @Source("gr/grnet/pithos/resources/mimetypes/ark2.png")
187                 ImageResource zip();
188
189                 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst_shared.png")
190                 ImageResource wordprocessorShared();
191
192                 @Source("gr/grnet/pithos/resources/mimetypes/log_shared.png")
193                 ImageResource spreadsheetShared();
194
195                 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr_shared.png")
196                 ImageResource presentationShared();
197
198                 @Source("gr/grnet/pithos/resources/mimetypes/acroread_shared.png")
199                 ImageResource pdfShared();
200
201                 @Source("gr/grnet/pithos/resources/mimetypes/image_shared.png")
202                 ImageResource imageShared();
203
204                 @Source("gr/grnet/pithos/resources/mimetypes/video2_shared.png")
205                 ImageResource videoShared();
206
207                 @Source("gr/grnet/pithos/resources/mimetypes/knotify_shared.png")
208                 ImageResource audioShared();
209
210                 @Source("gr/grnet/pithos/resources/mimetypes/html_shared.png")
211                 ImageResource htmlShared();
212
213                 @Source("gr/grnet/pithos/resources/mimetypes/txt_shared.png")
214                 ImageResource txtShared();
215
216                 @Source("gr/grnet/pithos/resources/mimetypes/ark2_shared.png")
217                 ImageResource zipShared();
218
219         }
220         
221         /**
222          * The number of files in this folder.
223          */
224         int folderFileCount;
225
226         /**
227          * Total folder size
228          */
229         long folderTotalSize;
230
231         /**
232          * A cache of the files in the list.
233          */
234         private List<File> files;
235
236         /**
237          * The widget's image bundle.
238          */
239         private final Images images;
240         
241         private DragAndDropCellTable<File> celltable;
242
243         private final MultiSelectionModel<File> selectionModel;
244
245         private final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
246
247         SortableHeader nameHeader;
248
249         GssSimplePager pagerBottom;
250
251         GssSimplePager pagerTop;
252
253         Button uploadButtonBottom;
254
255         Button uploadButtonTop;
256
257     FolderTreeView treeView;
258
259     private Pithos app;
260
261     /**
262          * Construct the file list widget. This entails setting up the widget
263          * layout, fetching the number of files in the current folder from the
264          * server and filling the local file cache of displayed files with data from
265          * the server, as well.
266          *
267          * @param _images
268          */
269         public FileList(final Pithos _app, Images _images, FolderTreeView _treeView) {
270         app = _app;
271                 images = _images;
272         this.treeView = _treeView;
273
274         DragAndDropCellTable.Resources resources = GWT.create(TableResources.class);
275
276         ProvidesKey<File> keyProvider = new ProvidesKey<File>(){
277
278                         @Override
279                         public Object getKey(File item) {
280                                 return item.getUri();
281                         }
282                 };
283
284                 celltable = new DragAndDropCellTable<File>(Pithos.VISIBLE_FILE_COUNT, resources, keyProvider);
285         celltable.setWidth("100%");
286         celltable.setStyleName("pithos-List");
287
288                 DragAndDropColumn<File, ImageResource> status = new DragAndDropColumn<File, ImageResource>(new ImageResourceCell() {
289                     @Override
290                 public boolean handlesSelection() {
291                     return false;
292                 }
293                 })
294         {
295                  @Override
296                  public ImageResource getValue(File entity) {
297                      return getFileIcon(entity);
298                  }
299             };
300             celltable.addColumn(status,"");
301             initDragOperation(status);
302
303         final DragAndDropColumn<File,SafeHtml> nameColumn = new DragAndDropColumn<File,SafeHtml>(new SafeHtmlCell()) {
304
305                         @Override
306                         public SafeHtml getValue(File object) {
307                                 SafeHtmlBuilder sb = new SafeHtmlBuilder();
308                 sb.append(Templates.INSTANCE.filenameSpan(object.getName()));
309                                 if (object.getContentType().endsWith("png") || object.getContentType().endsWith("gif") || object.getContentType().endsWith("jpeg")) {
310                                 sb.appendHtmlConstant("&nbsp;")
311                       .append(Templates.INSTANCE.viewLink(object.getUri(), object.getOwner() + " : " + object.getPath() + object.getName()));
312                                 }
313                                 
314                                 return sb.toSafeHtml();
315                         }
316                         
317                 };
318         initDragOperation(nameColumn);
319         celltable.addColumn(nameColumn, nameHeader = new SortableHeader("Name"));
320                 allHeaders.add(nameHeader);
321                 nameHeader.setUpdater(new FileValueUpdater(nameHeader, "name"));
322
323                 celltable.redrawHeaders();
324                 
325             DragAndDropColumn<File,String> aColumn = new DragAndDropColumn<File, String>(new TextCell()) {
326                         @Override
327                         public String getValue(File object) {
328                                 return object.getOwner();
329                         }
330                 };
331         SortableHeader aheader = new SortableHeader("Owner");
332                 celltable.addColumn(aColumn, aheader);
333                 initDragOperation(aColumn);
334                 allHeaders.add(aheader);
335         aheader.setUpdater(new FileValueUpdater(aheader, "owner"));
336
337         aColumn = new DragAndDropColumn<File,String>(new TextCell()) {
338                         @Override
339                         public String getValue(File object) {
340                                 return object.getPath();
341                         }
342                 };
343         aheader = new SortableHeader("Path");
344                 celltable.addColumn(aColumn, aheader);
345                 initDragOperation(aColumn);
346                 allHeaders.add(aheader);
347                 aheader.setUpdater(new FileValueUpdater(aheader, "path"));
348
349         aColumn = new DragAndDropColumn<File,String>(new TextCell()) {
350                         @Override
351                         public String getValue(File object) {
352                         return String.valueOf(object.getVersion());
353                         }
354                 };
355         aheader = new SortableHeader("Version");
356                 celltable.addColumn(aColumn, aheader);
357                 initDragOperation(aColumn);
358                 allHeaders.add(aheader);
359                 aheader.setUpdater(new FileValueUpdater(aheader, "version"));
360
361         aColumn = new DragAndDropColumn<File,String>(new TextCell()) {
362                         @Override
363                         public String getValue(File object) {
364                                 // TODO Auto-generated method stub
365                                 return object.getSizeAsString();
366                         }
367                 };
368         aheader = new SortableHeader("Size");
369         celltable.addColumn(aColumn, aheader);
370                 initDragOperation(aColumn);
371                 allHeaders.add(aheader);
372                 aheader.setUpdater(new FileValueUpdater(aheader, "size"));
373
374         aColumn = new DragAndDropColumn<File,String>(new TextCell()) {
375                         @Override
376                         public String getValue(File object) {
377                                 return formatter.format(object.getLastModified());
378                         }
379                 };
380         aheader = new SortableHeader("Last Modified");
381                 celltable.addColumn(aColumn, aheader);
382                 allHeaders.add(aheader);
383                 aheader.setUpdater(new FileValueUpdater(aheader, "date"));
384                
385                 provider.addDataDisplay(celltable);
386
387                 celltable.addDragStopHandler(new DragStopEventHandler() {
388
389                 @Override
390                     public void onDragStop(DragStopEvent event) {
391                             GWT.log("DRAG STOPPED");
392                     }
393             });
394                 celltable.addDragStartHandler(new DragStartEventHandler() {
395
396                     public void onDragStart(DragStartEvent event) {
397                         FileResource value = event.getDraggableData();
398                         
399                         com.google.gwt.dom.client.Element helper = event.getHelper();
400                         SafeHtmlBuilder sb = new SafeHtmlBuilder();
401                         sb.appendHtmlConstant("<b>");
402                         DisplayHelper.log(value.getName());
403                         if(getSelectedFiles().size()==1)
404                                 sb.appendEscaped(value.getName());
405                         else
406                                 sb.appendEscaped(getSelectedFiles().size()+" files");
407                         sb.appendHtmlConstant("</b>");
408                         helper.setInnerHTML(sb.toSafeHtml().asString());
409                     }
410                 });
411
412                 VerticalPanel vp = new VerticalPanel();
413                 vp.setWidth("100%");
414
415                 pagerTop = new GssSimplePager(GssSimplePager.TextLocation.CENTER);
416         pagerTop.setVisible(false);
417                 pagerTop.setDisplay(celltable);
418                 uploadButtonTop = new Button("<span id='topMenu.file.upload'>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + "&nbsp;Upload</span>");
419                 uploadButtonTop.addClickHandler(new ClickHandler() {
420                         
421                         @Override
422                         public void onClick(ClickEvent event) {
423                                 new UploadFileCommand(app, null, treeView.getSelection()).execute();
424                         }
425                 });
426                 HorizontalPanel topPanel = new HorizontalPanel();
427                 topPanel.add(pagerTop);
428                 topPanel.add(uploadButtonTop);
429                 vp.add(topPanel);
430
431         vp.add(celltable);
432
433                 pagerBottom = new GssSimplePager(GssSimplePager.TextLocation.CENTER);
434         pagerBottom.setVisible(false);
435                 pagerBottom.setDisplay(celltable);
436                 uploadButtonBottom=new Button("<span id='topMenu.file.upload'>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + "&nbsp;Upload</span>");
437                 uploadButtonBottom.addClickHandler(new ClickHandler() {
438                         
439                         @Override
440                         public void onClick(ClickEvent event) {
441                                 new UploadFileCommand(app, null, treeView.getSelection()).execute();
442                         }
443                 });
444         HorizontalPanel bottomPanel = new HorizontalPanel();
445         bottomPanel.add(pagerBottom);
446                 bottomPanel.add(uploadButtonBottom);
447
448                 vp.add(bottomPanel);
449                 vp.setCellWidth(celltable, "100%");
450         vp.addHandler(new ContextMenuHandler() {
451             @Override
452             public void onContextMenu(ContextMenuEvent event) {
453                 Folder selectedFolder = treeView.getSelection();
454                 if (!selectedFolder.isTrash()) {
455                     FileContextMenu contextMenu = new FileContextMenu(app, images, selectedFolder, getSelectedFiles(), false);
456                     int x = event.getNativeEvent().getClientX();
457                     int y = event.getNativeEvent().getClientY();
458                     contextMenu.setPopupPosition(x, y);
459                     contextMenu.show();
460                 }
461             }
462         }, ContextMenuEvent.getType());
463                 initWidget(vp);
464
465                 selectionModel = new MultiSelectionModel<File>(keyProvider);
466
467                  Handler selectionHandler = new SelectionChangeEvent.Handler() {
468              @Override 
469              public void onSelectionChange(SelectionChangeEvent event) {
470                  if(getSelectedFiles().size() == 1)
471                          app.setCurrentSelection(getSelectedFiles().get(0));
472                  else
473                          app.setCurrentSelection(getSelectedFiles());
474              }
475          };
476          selectionModel.addSelectionChangeHandler(selectionHandler);
477          
478                 celltable.setSelectionModel(selectionModel, GSSSelectionEventManager.<File> createDefaultManager());
479                 celltable.setPageSize(Pithos.VISIBLE_FILE_COUNT);
480                 
481                 sinkEvents(Event.ONCONTEXTMENU);
482 //              sinkEvents(Event.ONMOUSEUP);
483 //              sinkEvents(Event.ONMOUSEDOWN);
484 //              sinkEvents(Event.ONCLICK);
485 //              sinkEvents(Event.ONKEYDOWN);
486 //              sinkEvents(Event.ONDBLCLICK);
487                 Pithos.preventIESelection();
488         }
489
490         public List<File> getSelectedFiles() {
491         return new ArrayList<File>(selectionModel.getSelectedSet());
492         }
493         
494         private void initDragOperation(DragAndDropColumn<?, ?> column) {
495         // retrieve draggableOptions on the column
496                 DraggableOptions draggableOptions = column.getDraggableOptions();
497                 // use template to construct the helper. The content of the div will be set
498                 // after
499                 draggableOptions.setHelper($(Templates.INSTANCE.outerHelper().asString()));
500                 //draggableOptions.setZIndex(100);
501                 // opacity of the helper
502                 draggableOptions.setAppendTo("body");
503                 //draggableOptions.setOpacity((float) 0.8);
504                 draggableOptions.setContainment("document");
505                 // cursor to use during the drag operation
506                 draggableOptions.setCursor(Cursor.MOVE);
507                 // set the revert option
508                 draggableOptions.setRevert(RevertOption.ON_INVALID_DROP);
509                 // prevents dragging when user click on the category drop-down list
510                 draggableOptions.setCancel("select");
511             draggableOptions.setOnBeforeDragStart(new DragFunction() {
512                         @Override
513                         public void f(DragContext context) {
514                         File value = context.getDraggableData();
515                                 if (!selectionModel.isSelected(value)) {
516                         throw new StopDragException();
517                     }
518                         }
519                 });
520     }
521         
522 //      @Override
523 //      public void onBrowserEvent(Event event) {
524 //
525 //              if (files == null || files.size() == 0) {
526 //                      if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
527 //                              contextMenu = new FileContextMenu(images, false, true);
528 //                contextMenu.show();
529 //                              event.preventDefault();
530 //                              event.cancelBubble(true);
531 //                      }
532 //                      return;
533 //              }
534 //              if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() != 0) {
535 //                      GWT.log("*****GOING TO SHOW CONTEXT MENU ****", null);
536 //                      contextMenu =  new FileContextMenu(images, false, false);
537 //                      contextMenu = contextMenu.onEvent(event);
538 //                      event.cancelBubble(true);
539 //                      event.preventDefault();
540 //              } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
541 //                      contextMenu = new FileContextMenu(images, false, true);
542 //                      contextMenu = contextMenu.onEmptyEvent(event);
543 //                      event.cancelBubble(true);
544 //                      event.preventDefault();
545 //              } else if (DOM.eventGetType(event) == Event.ONDBLCLICK)
546 //                      if (getSelectedFiles().size() == 1) {
547 //                              Pithos app = app;
548 //                              File file = getSelectedFiles().get(0);
549 //                              Window.open(file.getUri(), "_blank", "");
550 //                              event.preventDefault();
551 //                              return;
552 //                      }
553 //              super.onBrowserEvent(event);
554 //      }
555
556         /**
557          * Update the display of the file list.
558          */
559         void update(boolean sort) {
560                 int count = folderFileCount;
561                 int max = startIndex + Pithos.VISIBLE_FILE_COUNT;
562                 if (max > count)
563                         max = count;
564                 folderTotalSize = 0;
565                 
566                 for(File f : files){
567                         folderTotalSize += f.getBytes();
568                 }
569                 if (folderFileCount == 0) {
570                         showingStats = "no files";
571                 } else if (folderFileCount < Pithos.VISIBLE_FILE_COUNT) {
572                         if (folderFileCount == 1)
573                                 showingStats = "1 file";
574                         else
575                                 showingStats = folderFileCount + " files";
576                         showingStats += " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
577                 } else {
578                         showingStats = "" + (startIndex + 1) + " - " + max + " of " + count + " files" + " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
579                 }
580                 showCellTable();
581                 updateCurrentlyShowingStats();
582
583         }
584
585         /**
586          * Return the proper icon based on the MIME type of the file.
587          *
588          * @param file
589          * @return the icon
590          */
591         private ImageResource getFileIcon(File file) {
592                 String mimetype = file.getContentType();
593                 boolean shared = file.isShared();
594                 if (mimetype == null)
595                         return shared ? images.documentShared() : images.document();
596                 mimetype = mimetype.toLowerCase();
597                 if (mimetype.startsWith("application/pdf"))
598                         return shared ? images.pdfShared() : images.pdf();
599                 else if (mimetype.endsWith("excel"))
600                         return shared ? images.spreadsheetShared() : images.spreadsheet();
601                 else if (mimetype.endsWith("msword"))
602                         return shared ? images.wordprocessorShared() : images.wordprocessor();
603                 else if (mimetype.endsWith("powerpoint"))
604                         return shared ? images.presentationShared() : images.presentation();
605                 else if (mimetype.startsWith("application/zip") ||
606                                         mimetype.startsWith("application/gzip") ||
607                                         mimetype.startsWith("application/x-gzip") ||
608                                         mimetype.startsWith("application/x-tar") ||
609                                         mimetype.startsWith("application/x-gtar"))
610                         return shared ? images.zipShared() : images.zip();
611                 else if (mimetype.startsWith("text/html"))
612                         return shared ? images.htmlShared() : images.html();
613                 else if (mimetype.startsWith("text/plain"))
614                         return shared ? images.txtShared() : images.txt();
615                 else if (mimetype.startsWith("image/"))
616                         return shared ? images.imageShared() : images.image();
617                 else if (mimetype.startsWith("video/"))
618                         return shared ? images.videoShared() : images.video();
619                 else if (mimetype.startsWith("audio/"))
620                         return shared ? images.audioShared() : images.audio();
621                 return shared ? images.documentShared() : images.document();
622         }
623
624         /**
625          * Update status panel with currently showing file stats.
626          */
627         public void updateCurrentlyShowingStats() {
628                 app.getStatusPanel().updateCurrentlyShowing(showingStats);
629         }
630         
631         /**
632          * Fill the file cache with data.
633          */
634         public void setFiles(final List<File> _files) {
635                 files = new ArrayList<File>();
636         for (File fres : _files)
637                 if (!fres.isInTrash())
638                                 files.add(fres);
639                 Collections.sort(files, new Comparator<File>() {
640
641                         @Override
642                         public int compare(File arg0, File arg1) {
643                                 return arg0.getName().compareTo(arg1.getName());
644                         }
645
646                 });
647                 folderFileCount = files.size();
648                 
649                 nameHeader.setSorted(true);
650                 nameHeader.toggleReverseSort();
651                 for (SortableHeader otherHeader : allHeaders) {
652                 if (otherHeader != nameHeader) {
653                     otherHeader.setSorted(false);
654                     otherHeader.setReverseSort(true);
655                 }
656             }
657
658         if(files.size() > Pithos.VISIBLE_FILE_COUNT){
659             pagerBottom.setVisible(true);
660             pagerTop.setVisible(true);
661         }
662         else{
663             pagerTop.setVisible(false);
664             pagerBottom.setVisible(false);
665         }
666         Folder selectedItem = treeView.getSelection();
667
668         provider.setList(files);
669         selectionModel.clear();
670         }
671
672         /**
673          * Does the list contains the requested filename
674          *
675          * @param fileName
676          * @return true/false
677          */
678         public boolean contains(String fileName) {
679                 for (int i = 0; i < files.size(); i++)
680                         if (files.get(i).getName().equals(fileName))
681                                 return true;
682                 return false;
683         }
684
685         public void clearSelectedRows() {
686                 Iterator<File> it = selectionModel.getSelectedSet().iterator();
687                 while(it.hasNext()){
688                         selectionModel.setSelected(it.next(),false);
689                 }
690         }
691         
692         /**
693          *
694          */
695         public void selectAllRows() {
696                 Iterator<File> it = provider.getList().iterator();
697                 while(it.hasNext()){
698                         selectionModel.setSelected(it.next(),true);
699                 }
700         }
701
702         private void sortFiles(final String sortingProperty, final boolean sortingType){
703                 Collections.sort(files, new Comparator<File>() {
704
705             @Override
706             public int compare(File arg0, File arg1) {
707                     AbstractImagePrototype descPrototype = AbstractImagePrototype.create(images.desc());
708                     AbstractImagePrototype ascPrototype = AbstractImagePrototype.create(images.asc());
709                     if (sortingType){
710                             if (sortingProperty.equals("version")) {
711                                     return arg0.getVersion() - arg1.getVersion();
712                             } else if (sortingProperty.equals("owner")) {
713                                     return arg0.getOwner().compareTo(arg1.getOwner());
714                             } else if (sortingProperty.equals("date")) {
715                                     return arg0.getLastModified().compareTo(arg1.getLastModified());
716                             } else if (sortingProperty.equals("size")) {
717                                     return (int) (arg0.getBytes() - arg1.getBytes());
718                             } else if (sortingProperty.equals("name")) {
719                                     return arg0.getName().compareTo(arg1.getName());
720                             } else if (sortingProperty.equals("path")) {
721                                     return arg0.getUri().compareTo(arg1.getUri());
722                             } else {
723                                     return arg0.getName().compareTo(arg1.getName());
724                             }
725                     }
726                     else if (sortingProperty.equals("version")) {
727                             
728                             return arg1.getVersion() - arg0.getVersion();
729                     } else if (sortingProperty.equals("owner")) {
730                             
731                             return arg1.getOwner().compareTo(arg0.getOwner());
732                     } else if (sortingProperty.equals("date")) {
733                             
734                             return arg1.getLastModified().compareTo(arg0.getLastModified());
735                     } else if (sortingProperty.equals("size")) {
736                             return (int) (arg1.getBytes() - arg0.getBytes());
737                     } else if (sortingProperty.equals("name")) {
738                             
739                             return arg1.getName().compareTo(arg0.getName());
740                     } else if (sortingProperty.equals("path")) {
741                             
742                             return arg1.getUri().compareTo(arg0.getUri());
743                     } else {
744                             
745                             return arg1.getName().compareTo(arg0.getName());
746                     }
747             }
748
749                 });
750         }
751         
752         final class FileValueUpdater implements ValueUpdater<String>{
753                 private String property;
754                 private SortableHeader header;
755                 /**
756                  * 
757                  */
758                 public FileValueUpdater(SortableHeader header,String property) {
759                         this.property=property;
760                         this.header=header;
761                 }
762                 @Override
763                 public void update(String value) {
764                         header.setSorted(true);
765                         header.toggleReverseSort();
766
767                 for (SortableHeader otherHeader : allHeaders) {
768                   if (otherHeader != header) {
769                     otherHeader.setSorted(false);
770                     otherHeader.setReverseSort(true);
771                   }
772                 }
773                 celltable.redrawHeaders();
774                 sortFiles(property, header.getReverseSort());
775                 FileList.this.update(true);                     
776                 }
777                 
778         }
779
780         /**
781          * Shows the files in the cellTable 
782      */
783         private void showCellTable(){
784                 if(files.size()> Pithos.VISIBLE_FILE_COUNT){
785                         pagerBottom.setVisible(true);
786                         pagerTop.setVisible(true);
787                 }
788                 else{
789                         pagerTop.setVisible(false);
790                         pagerBottom.setVisible(false);
791                 }
792                 provider.setList(files);
793                 
794                 provider.refresh();
795                 
796                 //celltable.redraw();
797                 celltable.redrawHeaders();              
798         }
799
800     public void showTrash() {
801         uploadButtonBottom.setVisible(false);
802         uploadButtonTop.setVisible(false);
803     }
804
805     public void showFiles() {
806         uploadButtonBottom.setVisible(true);
807         uploadButtonTop.setVisible(true);
808     }
809 }