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