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