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