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