f9b4d21e1b767a94cae73a1bfcc823ecf4231202
[pithos-web-client] / src / gr / grnet / pithos / web / client / FileList.java
1 /*
2  * Copyright 2011-2012 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 gr.grnet.pithos.web.client.foldertree.File;
39 import gr.grnet.pithos.web.client.foldertree.Folder;
40 import gr.grnet.pithos.web.client.foldertree.FolderTreeView;
41
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.Comparator;
45 import java.util.Iterator;
46 import java.util.List;
47
48 import com.google.gwt.cell.client.ImageResourceCell;
49 import com.google.gwt.cell.client.SafeHtmlCell;
50 import com.google.gwt.cell.client.TextCell;
51 import com.google.gwt.cell.client.ValueUpdater;
52 import com.google.gwt.core.client.GWT;
53 import com.google.gwt.event.dom.client.ContextMenuEvent;
54 import com.google.gwt.event.dom.client.ContextMenuHandler;
55 import com.google.gwt.i18n.client.DateTimeFormat;
56 import com.google.gwt.resources.client.ImageResource;
57 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
58 import com.google.gwt.safehtml.shared.SafeHtml;
59 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
60 import com.google.gwt.user.cellview.client.CellTable;
61 import com.google.gwt.user.cellview.client.Column;
62 import com.google.gwt.user.client.DOM;
63 import com.google.gwt.user.client.Event;
64 import com.google.gwt.user.client.Window;
65 import com.google.gwt.user.client.ui.Composite;
66 import com.google.gwt.user.client.ui.VerticalPanel;
67 import com.google.gwt.view.client.ListDataProvider;
68 import com.google.gwt.view.client.MultiSelectionModel;
69 import com.google.gwt.view.client.ProvidesKey;
70 import com.google.gwt.view.client.SelectionChangeEvent;
71
72 /**
73  * A composite that displays the list of files in a particular folder.
74  */
75 public class FileList extends Composite {
76
77         ListDataProvider<File> provider = new ListDataProvider<File>();
78
79     /**
80        * The styles applied to the table.
81        */
82     interface TableStyle extends CellTable.Style {
83     }
84
85         interface TableResources extends CellTable.Resources {
86             @Override
87                 @Source({CellTable.Style.DEFAULT_CSS, "PithosCellTable.css"})
88             TableStyle cellTableStyle();
89         }
90         
91         static interface Templates extends SafeHtmlTemplates {
92             Templates INSTANCE = GWT.create(Templates.class);
93
94             @Template("<div id='dragHelper' style='border:1px solid black; background-color:#ffffff; color:black; width:150px;z-index:100'></div>")
95             SafeHtml outerHelper();
96
97         @Template("<span id='{0}'>{0}</span>")
98         public SafeHtml filenameSpan(String filename);
99
100         @Template("<a href='{0}' title='{1}' rel='lytebox[mnf]' onclick='myLytebox.start(this, false, false); return false;'>(view)</a>")
101         public SafeHtml viewLink(String link, String title);
102
103         @Template("<table><tr><td rowspan='3'>{0}</td><td style='font-size:95%;' id='{1}'>{1}</td></tr><tr><td>{2}</td></tr></table>")
104         public SafeHtml rendelContactCell(String imageHtml, String name, String fileSize);
105
106         @Template("<span id='{0}' class='{1}'>{2}</span>")
107         public SafeHtml spanWithIdAndClass(String id, String cssClass, String content);
108         }
109
110         protected final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
111
112         /**
113          * Specifies that the images available for this composite will be the ones
114          * available in FileContextMenu.
115          */
116         public interface Images extends FolderTreeView.Images {
117
118                 @Source("gr/grnet/pithos/resources/blank.gif")
119                 ImageResource blank();
120
121                 @Source("gr/grnet/pithos/resources/asc.png")
122                 ImageResource asc();
123
124                 @Source("gr/grnet/pithos/resources/desc.png")
125                 ImageResource desc();
126
127                 @Source("gr/grnet/pithos/resources/mimetypes/document_shared.png")
128                 ImageResource documentShared();
129
130                 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst.png")
131                 ImageResource wordprocessor();
132
133                 @Source("gr/grnet/pithos/resources/mimetypes/log.png")
134                 ImageResource spreadsheet();
135
136                 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr.png")
137                 ImageResource presentation();
138
139                 @Source("gr/grnet/pithos/resources/mimetypes/acroread.png")
140                 ImageResource pdf();
141
142                 @Source("gr/grnet/pithos/resources/mimetypes/image.png")
143                 ImageResource image();
144
145                 @Source("gr/grnet/pithos/resources/mimetypes/video2.png")
146                 ImageResource video();
147
148                 @Source("gr/grnet/pithos/resources/mimetypes/knotify.png")
149                 ImageResource audio();
150
151                 @Source("gr/grnet/pithos/resources/mimetypes/html.png")
152                 ImageResource html();
153
154                 @Source("gr/grnet/pithos/resources/mimetypes/txt.png")
155                 ImageResource txt();
156
157                 @Source("gr/grnet/pithos/resources/mimetypes/ark2.png")
158                 ImageResource zip();
159
160                 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst_shared.png")
161                 ImageResource wordprocessorShared();
162
163                 @Source("gr/grnet/pithos/resources/mimetypes/log_shared.png")
164                 ImageResource spreadsheetShared();
165
166                 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr_shared.png")
167                 ImageResource presentationShared();
168
169                 @Source("gr/grnet/pithos/resources/mimetypes/acroread_shared.png")
170                 ImageResource pdfShared();
171
172                 @Source("gr/grnet/pithos/resources/mimetypes/image_shared.png")
173                 ImageResource imageShared();
174
175                 @Source("gr/grnet/pithos/resources/mimetypes/video2_shared.png")
176                 ImageResource videoShared();
177
178                 @Source("gr/grnet/pithos/resources/mimetypes/knotify_shared.png")
179                 ImageResource audioShared();
180
181                 @Source("gr/grnet/pithos/resources/mimetypes/html_shared.png")
182                 ImageResource htmlShared();
183
184                 @Source("gr/grnet/pithos/resources/mimetypes/txt_shared.png")
185                 ImageResource txtShared();
186
187                 @Source("gr/grnet/pithos/resources/mimetypes/ark2_shared.png")
188                 ImageResource zipShared();
189
190         }
191         
192         /**
193          * The number of files in this folder.
194          */
195         int folderFileCount;
196
197         /**
198          * Total folder size
199          */
200         long folderTotalSize;
201
202         /**
203          * A cache of the files in the list.
204          */
205         private List<File> files;
206
207         /**
208          * The widget's image bundle.
209          */
210         protected final Images images;
211         
212         protected CellTable<File> celltable;
213
214         private final MultiSelectionModel<File> selectionModel;
215
216         protected final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
217
218         SortableHeader nameHeader;
219
220     FolderTreeView treeView;
221
222     protected Pithos app;
223
224     /**
225          * Construct the file list widget. This entails setting up the widget
226          * layout, fetching the number of files in the current folder from the
227          * server and filling the local file cache of displayed files with data from
228          * the server, as well.
229          *
230          * @param _images
231          */
232         public FileList(final Pithos _app, Images _images, FolderTreeView _treeView) {
233         app = _app;
234                 images = _images;
235         this.treeView = _treeView;
236
237         CellTable.Resources resources = GWT.create(TableResources.class);
238
239         ProvidesKey<File> keyProvider = new ProvidesKey<File>(){
240
241                         @Override
242                         public Object getKey(File item) {
243                                 return item.getUri();
244                         }
245                 };
246
247                 celltable = new CellTable<File>(10, resources, keyProvider);
248         celltable.setWidth("100%");
249         celltable.setStyleName("pithos-List");
250
251                 Column<File, ImageResource> status = new Column<File, ImageResource>(new ImageResourceCell() {
252                     @Override
253                 public boolean handlesSelection() {
254                     return false;
255                 }
256                 })
257         {
258                  @Override
259                  public ImageResource getValue(File entity) {
260                      return getFileIcon(entity);
261                  }
262             };
263             celltable.addColumn(status,"");
264
265         final Column<File,SafeHtml> nameColumn = new Column<File,SafeHtml>(new SafeHtmlCell()) {
266
267                         @Override
268                         public SafeHtml getValue(File object) {
269                                 SafeHtmlBuilder sb = new SafeHtmlBuilder();
270                 sb.append(Templates.INSTANCE.filenameSpan(object.getName()));
271                                 if (object.getContentType() != null && (object.getContentType().endsWith("png") || object.getContentType().endsWith("gif") || object.getContentType().endsWith("jpeg"))) {
272                                 sb.appendHtmlConstant("&nbsp;")
273                       .append(Templates.INSTANCE.viewLink(app.getApiPath() + object.getOwner() + object.getUri() + "?X-Auth-Token=" + app.getToken(), object.getName()));
274                                 }
275                                 
276                                 return sb.toSafeHtml();
277                         }
278                         
279                 };
280         celltable.addColumn(nameColumn, nameHeader = new SortableHeader("Name"));
281                 allHeaders.add(nameHeader);
282                 nameHeader.setUpdater(new FileValueUpdater(nameHeader, "name"));
283
284                 celltable.redrawHeaders();
285                 
286         Column<File,String> aColumn = new Column<File,String>(new TextCell()) {
287                         @Override
288                         public String getValue(File object) {
289                                 // TODO Auto-generated method stub
290                                 return object.getSizeAsString();
291                         }
292                 };
293         SortableHeader aheader = new SortableHeader("Size");
294         celltable.addColumn(aColumn, aheader);
295                 allHeaders.add(aheader);
296                 aheader.setUpdater(new FileValueUpdater(aheader, "size"));
297
298         aColumn = new Column<File,String>(new TextCell()) {
299                         @Override
300                         public String getValue(File object) {
301                                 return object.getLastModified() != null ? formatter.format(object.getLastModified()) : "";
302                         }
303                 };
304         aheader = new SortableHeader("Last Modified");
305                 celltable.addColumn(aColumn, aheader);
306                 allHeaders.add(aheader);
307                 aheader.setUpdater(new FileValueUpdater(aheader, "date"));
308                
309                 provider.addDataDisplay(celltable);
310
311                 VerticalPanel vp = new VerticalPanel();
312                 vp.setWidth("100%");
313                 vp.addStyleName("pithos-FileListContainer");
314
315         vp.add(celltable);
316
317                 vp.setCellWidth(celltable, "100%");
318         vp.addHandler(new ContextMenuHandler() {
319             @Override
320             public void onContextMenu(ContextMenuEvent event) {
321                 TreeView tree = app.getSelectedTree();
322                 if (tree != null && (tree.equals(app.getFolderTreeView()) || tree.equals(app.getOtherSharedTreeView()))) {
323                         Folder selectedFolder = app.getSelection();
324                         FileContextMenu contextMenu = new FileContextMenu(app, images, tree, selectedFolder, getSelectedFiles());
325                         int x = event.getNativeEvent().getClientX();
326                         int y = event.getNativeEvent().getClientY();
327                         contextMenu.setPopupPosition(x, y);
328                         contextMenu.show();
329                 }
330             }
331         }, ContextMenuEvent.getType());
332                 initWidget(vp);
333
334                 selectionModel = new MultiSelectionModel<File>(keyProvider);
335                 selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
336                         
337                         @Override
338                         public void onSelectionChange(SelectionChangeEvent event) {
339                                 app.showRelevantToolbarButtons();
340                         }
341                 });
342                 
343                 celltable.setSelectionModel(selectionModel, PithosSelectionEventManager.<File> createDefaultManager());
344 //              celltable.setPageSize(Pithos.VISIBLE_FILE_COUNT);
345                 
346                 sinkEvents(Event.ONCONTEXTMENU);
347 //              sinkEvents(Event.ONMOUSEUP);
348 //              sinkEvents(Event.ONMOUSEDOWN);
349 //              sinkEvents(Event.ONCLICK);
350 //              sinkEvents(Event.ONKEYDOWN);
351                 sinkEvents(Event.ONDBLCLICK);
352                 Pithos.preventIESelection();
353         }
354
355         public List<File> getSelectedFiles() {
356         return new ArrayList<File>(selectionModel.getSelectedSet());
357         }
358         
359         @Override
360         public void onBrowserEvent(Event event) {
361
362 //              if (files == null || files.size() == 0) {
363 //                      if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
364 //                              contextMenu = new FileContextMenu(images, false, true);
365 //                contextMenu.show();
366 //                              event.preventDefault();
367 //                              event.cancelBubble(true);
368 //                      }
369 //                      return;
370 //              }
371 //              if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() != 0) {
372 //                      GWT.log("*****GOING TO SHOW CONTEXT MENU ****", null);
373 //                      contextMenu =  new FileContextMenu(images, false, false);
374 //                      contextMenu = contextMenu.onEvent(event);
375 //                      event.cancelBubble(true);
376 //                      event.preventDefault();
377 //              } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
378 //                      contextMenu = new FileContextMenu(images, false, true);
379 //                      contextMenu = contextMenu.onEmptyEvent(event);
380 //                      event.cancelBubble(true);
381 //                      event.preventDefault();
382 //              } else 
383                 if (DOM.eventGetType(event) == Event.ONDBLCLICK)
384                         if (getSelectedFiles().size() == 1) {
385                                 File file = getSelectedFiles().get(0);
386                                 Window.open(app.getApiPath() + file.getOwner() + file.getUri() + "?X-Auth-Token=" + app.getToken(), "_blank", "");
387                                 event.preventDefault();
388                                 return;
389                         }
390                 super.onBrowserEvent(event);
391         }
392
393         /**
394          * Update the display of the file list.
395          */
396         void update() {
397                 showCellTable();
398         }
399
400         /**
401          * Return the proper icon based on the MIME type of the file.
402          *
403          * @param file
404          * @return the icon
405          */
406         protected ImageResource getFileIcon(File file) {
407                 String mimetype = file.getContentType();
408                 boolean shared = file.isShared();
409                 if (mimetype == null)
410                         return shared ? images.documentShared() : images.document();
411                 mimetype = mimetype.toLowerCase();
412                 if (mimetype.startsWith("application/pdf"))
413                         return shared ? images.pdfShared() : images.pdf();
414                 else if (mimetype.endsWith("excel"))
415                         return shared ? images.spreadsheetShared() : images.spreadsheet();
416                 else if (mimetype.endsWith("msword"))
417                         return shared ? images.wordprocessorShared() : images.wordprocessor();
418                 else if (mimetype.endsWith("powerpoint"))
419                         return shared ? images.presentationShared() : images.presentation();
420                 else if (mimetype.startsWith("application/zip") ||
421                                         mimetype.startsWith("application/gzip") ||
422                                         mimetype.startsWith("application/x-gzip") ||
423                                         mimetype.startsWith("application/x-tar") ||
424                                         mimetype.startsWith("application/x-gtar"))
425                         return shared ? images.zipShared() : images.zip();
426                 else if (mimetype.startsWith("text/html"))
427                         return shared ? images.htmlShared() : images.html();
428                 else if (mimetype.startsWith("text/plain"))
429                         return shared ? images.txtShared() : images.txt();
430                 else if (mimetype.startsWith("image/"))
431                         return shared ? images.imageShared() : images.image();
432                 else if (mimetype.startsWith("video/"))
433                         return shared ? images.videoShared() : images.video();
434                 else if (mimetype.startsWith("audio/"))
435                         return shared ? images.audioShared() : images.audio();
436                 return shared ? images.documentShared() : images.document();
437         }
438
439         /**
440          * Fill the file cache with data.
441          */
442         public void setFiles(final List<File> _files) {
443                 files = new ArrayList<File>();
444         for (File fres : _files)
445                         files.add(fres);
446                 Collections.sort(files, new Comparator<File>() {
447
448                         @Override
449                         public int compare(File arg0, File arg1) {
450                                 return arg0.getName().compareTo(arg1.getName());
451                         }
452
453                 });
454                 folderFileCount = files.size();
455                 
456                 nameHeader.setSorted(true);
457                 nameHeader.toggleReverseSort();
458                 for (SortableHeader otherHeader : allHeaders) {
459                 if (otherHeader != nameHeader) {
460                     otherHeader.setSorted(false);
461                     otherHeader.setReverseSort(true);
462                 }
463             }
464
465         provider.getList().clear();
466         provider.setList(files);
467         selectionModel.clear();
468         app.showFolderStatistics(folderFileCount);
469         celltable.setPageSize(folderFileCount);
470         }
471
472         /**
473          * Does the list contains the requested filename
474          *
475          * @param fileName
476          * @return true/false
477          */
478         public boolean contains(String fileName) {
479                 for (int i = 0; i < files.size(); i++)
480                         if (files.get(i).getName().equals(fileName))
481                                 return true;
482                 return false;
483         }
484
485         public void clearSelectedRows() {
486                 Iterator<File> it = selectionModel.getSelectedSet().iterator();
487                 while(it.hasNext()){
488                         selectionModel.setSelected(it.next(),false);
489                 }
490         }
491         
492         /**
493          *
494          */
495         public void selectAllRows() {
496                 Iterator<File> it = provider.getList().iterator();
497                 while(it.hasNext()){
498                         selectionModel.setSelected(it.next(),true);
499                 }
500         }
501
502         protected void sortFiles(final String sortingProperty, final boolean sortingType){
503                 Collections.sort(files, new Comparator<File>() {
504
505             @Override
506             public int compare(File arg0, File arg1) {
507                     if (sortingType){
508                             if (sortingProperty.equals("version")) {
509                                     return arg0.getVersion() - arg1.getVersion();
510                             } else if (sortingProperty.equals("owner")) {
511                                     return arg0.getOwner().compareTo(arg1.getOwner());
512                             } else if (sortingProperty.equals("date")) {
513                                         if (arg0.getLastModified() != null && arg1.getLastModified() != null)
514                                                 return arg0.getLastModified().compareTo(arg1.getLastModified());
515                                         return 0;
516                             } else if (sortingProperty.equals("size")) {
517                                     return (int) (arg0.getBytes() - arg1.getBytes());
518                             } else if (sortingProperty.equals("name")) {
519                                     return arg0.getName().compareTo(arg1.getName());
520                             } else if (sortingProperty.equals("path")) {
521                                     return arg0.getUri().compareTo(arg1.getUri());
522                             } else {
523                                     return arg0.getName().compareTo(arg1.getName());
524                             }
525                     }
526                     else if (sortingProperty.equals("version")) {
527                             
528                             return arg1.getVersion() - arg0.getVersion();
529                     } else if (sortingProperty.equals("owner")) {
530                             
531                             return arg1.getOwner().compareTo(arg0.getOwner());
532                     } else if (sortingProperty.equals("date")) {
533                             
534                             return arg1.getLastModified().compareTo(arg0.getLastModified());
535                     } else if (sortingProperty.equals("size")) {
536                             return (int) (arg1.getBytes() - arg0.getBytes());
537                     } else if (sortingProperty.equals("name")) {
538                             
539                             return arg1.getName().compareTo(arg0.getName());
540                     } else if (sortingProperty.equals("path")) {
541                             
542                             return arg1.getUri().compareTo(arg0.getUri());
543                     } else {
544                             
545                             return arg1.getName().compareTo(arg0.getName());
546                     }
547             }
548
549                 });
550         }
551         
552         final class FileValueUpdater implements ValueUpdater<String>{
553                 private String property;
554                 private SortableHeader header;
555                 /**
556                  * 
557                  */
558                 public FileValueUpdater(SortableHeader header,String property) {
559                         this.property=property;
560                         this.header=header;
561                 }
562                 @Override
563                 public void update(String value) {
564                         header.setSorted(true);
565                         header.toggleReverseSort();
566
567                 for (SortableHeader otherHeader : allHeaders) {
568                   if (otherHeader != header) {
569                     otherHeader.setSorted(false);
570                     otherHeader.setReverseSort(true);
571                   }
572                 }
573                 celltable.redrawHeaders();
574                 sortFiles(property, header.getReverseSort());
575                 FileList.this.update();                 
576                 }
577                 
578         }
579
580         /**
581          * Shows the files in the cellTable 
582      */
583         private void showCellTable(){
584                 provider.setList(files);
585                 
586                 provider.refresh();
587                 
588                 //celltable.redraw();
589                 celltable.redrawHeaders();              
590         }
591 }