2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
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.
36 package gr.grnet.pithos.web.client;
38 import com.google.gwt.event.dom.client.ContextMenuEvent;
39 import com.google.gwt.event.dom.client.ContextMenuHandler;
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;
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;
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;
82 * A composite that displays the list of files in a particular folder.
84 public class FileList extends Composite {
86 ListDataProvider<File> provider = new ListDataProvider<File>();
89 * The styles applied to the table.
91 interface TableStyle extends CellTable.Style {
94 interface TableResources extends CellTable.Resources {
95 @Source({CellTable.Style.DEFAULT_CSS, "GssCellTable.css"})
96 TableStyle cellTableStyle();
99 static interface Templates extends SafeHtmlTemplates {
100 Templates INSTANCE = GWT.create(Templates.class);
102 @Template("<div id='dragHelper' style='border:1px solid black; background-color:#ffffff; color:black; width:150px;z-index:100'></div>")
103 SafeHtml outerHelper();
105 @Template("<span id='{0}'>{0}</span>")
106 public SafeHtml filenameSpan(String filename);
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);
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);
114 @Template("<span id='{0}' class='{1}'>{2}</span>")
115 public SafeHtml spanWithIdAndClass(String id, String cssClass, String content);
118 private String showingStats = "";
120 private int startIndex = 0;
123 * A constant that denotes the completion of an IncrementalCommand.
125 public static final boolean DONE = false;
127 private final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
130 * Specifies that the images available for this composite will be the ones
131 * available in FileContextMenu.
133 public interface Images extends ClientBundle,FileContextMenu.Images, FolderTreeView.Images {
135 @Source("gr/grnet/pithos/resources/blank.gif")
136 ImageResource blank();
138 @Source("gr/grnet/pithos/resources/asc.png")
141 @Source("gr/grnet/pithos/resources/desc.png")
142 ImageResource desc();
144 @Source("gr/grnet/pithos/resources/mimetypes/document_shared.png")
145 ImageResource documentShared();
147 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst.png")
148 ImageResource wordprocessor();
150 @Source("gr/grnet/pithos/resources/mimetypes/log.png")
151 ImageResource spreadsheet();
153 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr.png")
154 ImageResource presentation();
156 @Source("gr/grnet/pithos/resources/mimetypes/acroread.png")
159 @Source("gr/grnet/pithos/resources/mimetypes/image.png")
160 ImageResource image();
162 @Source("gr/grnet/pithos/resources/mimetypes/video2.png")
163 ImageResource video();
165 @Source("gr/grnet/pithos/resources/mimetypes/knotify.png")
166 ImageResource audio();
168 @Source("gr/grnet/pithos/resources/mimetypes/html.png")
169 ImageResource html();
171 @Source("gr/grnet/pithos/resources/mimetypes/txt.png")
174 @Source("gr/grnet/pithos/resources/mimetypes/ark2.png")
177 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst_shared.png")
178 ImageResource wordprocessorShared();
180 @Source("gr/grnet/pithos/resources/mimetypes/log_shared.png")
181 ImageResource spreadsheetShared();
183 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr_shared.png")
184 ImageResource presentationShared();
186 @Source("gr/grnet/pithos/resources/mimetypes/acroread_shared.png")
187 ImageResource pdfShared();
189 @Source("gr/grnet/pithos/resources/mimetypes/image_shared.png")
190 ImageResource imageShared();
192 @Source("gr/grnet/pithos/resources/mimetypes/video2_shared.png")
193 ImageResource videoShared();
195 @Source("gr/grnet/pithos/resources/mimetypes/knotify_shared.png")
196 ImageResource audioShared();
198 @Source("gr/grnet/pithos/resources/mimetypes/html_shared.png")
199 ImageResource htmlShared();
201 @Source("gr/grnet/pithos/resources/mimetypes/txt_shared.png")
202 ImageResource txtShared();
204 @Source("gr/grnet/pithos/resources/mimetypes/ark2_shared.png")
205 ImageResource zipShared();
210 * The number of files in this folder.
217 long folderTotalSize;
220 * A cache of the files in the list.
222 private List<File> files;
225 * The widget's image bundle.
227 private final Images images;
229 private CellTable<File> celltable;
231 private final MultiSelectionModel<File> selectionModel;
233 private final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
235 SortableHeader nameHeader;
237 GssSimplePager pagerBottom;
239 GssSimplePager pagerTop;
241 Button uploadButtonBottom;
243 Button uploadButtonTop;
245 FolderTreeView treeView;
250 * Construct the file list widget. This entails setting up the widget
251 * layout, fetching the number of files in the current folder from the
252 * server and filling the local file cache of displayed files with data from
253 * the server, as well.
257 public FileList(final Pithos _app, Images _images, FolderTreeView _treeView) {
260 this.treeView = _treeView;
262 CellTable.Resources resources = GWT.create(TableResources.class);
264 ProvidesKey<File> keyProvider = new ProvidesKey<File>(){
267 public Object getKey(File item) {
268 return item.getUri();
272 celltable = new CellTable<File>(Pithos.VISIBLE_FILE_COUNT, resources, keyProvider);
273 celltable.setWidth("100%");
274 celltable.setStyleName("pithos-List");
276 Column<File, ImageResource> status = new Column<File, ImageResource>(new ImageResourceCell() {
278 public boolean handlesSelection() {
284 public ImageResource getValue(File entity) {
285 return getFileIcon(entity);
288 celltable.addColumn(status,"");
290 final Column<File,SafeHtml> nameColumn = new Column<File,SafeHtml>(new SafeHtmlCell()) {
293 public SafeHtml getValue(File object) {
294 SafeHtmlBuilder sb = new SafeHtmlBuilder();
295 sb.append(Templates.INSTANCE.filenameSpan(object.getName()));
296 if (object.getContentType().endsWith("png") || object.getContentType().endsWith("gif") || object.getContentType().endsWith("jpeg")) {
297 sb.appendHtmlConstant(" ")
298 .append(Templates.INSTANCE.viewLink(object.getUri(), object.getOwner() + " : " + object.getPath() + object.getName()));
301 return sb.toSafeHtml();
305 celltable.addColumn(nameColumn, nameHeader = new SortableHeader("Name"));
306 allHeaders.add(nameHeader);
307 nameHeader.setUpdater(new FileValueUpdater(nameHeader, "name"));
309 celltable.redrawHeaders();
311 Column<File,String> aColumn = new Column<File, String>(new TextCell()) {
313 public String getValue(File object) {
314 return object.getOwner();
317 SortableHeader aheader = new SortableHeader("Owner");
318 celltable.addColumn(aColumn, aheader);
319 allHeaders.add(aheader);
320 aheader.setUpdater(new FileValueUpdater(aheader, "owner"));
322 aColumn = new Column<File,String>(new TextCell()) {
324 public String getValue(File object) {
325 return object.getPath();
328 aheader = new SortableHeader("Path");
329 celltable.addColumn(aColumn, aheader);
330 allHeaders.add(aheader);
331 aheader.setUpdater(new FileValueUpdater(aheader, "path"));
333 aColumn = new Column<File,String>(new TextCell()) {
335 public String getValue(File object) {
336 return String.valueOf(object.getVersion());
339 aheader = new SortableHeader("Version");
340 celltable.addColumn(aColumn, aheader);
341 allHeaders.add(aheader);
342 aheader.setUpdater(new FileValueUpdater(aheader, "version"));
344 aColumn = new Column<File,String>(new TextCell()) {
346 public String getValue(File object) {
347 // TODO Auto-generated method stub
348 return object.getSizeAsString();
351 aheader = new SortableHeader("Size");
352 celltable.addColumn(aColumn, aheader);
353 allHeaders.add(aheader);
354 aheader.setUpdater(new FileValueUpdater(aheader, "size"));
356 aColumn = new Column<File,String>(new TextCell()) {
358 public String getValue(File object) {
359 return formatter.format(object.getLastModified());
362 aheader = new SortableHeader("Last Modified");
363 celltable.addColumn(aColumn, aheader);
364 allHeaders.add(aheader);
365 aheader.setUpdater(new FileValueUpdater(aheader, "date"));
367 provider.addDataDisplay(celltable);
369 VerticalPanel vp = new VerticalPanel();
372 pagerTop = new GssSimplePager(GssSimplePager.TextLocation.CENTER);
373 pagerTop.setVisible(false);
374 pagerTop.setDisplay(celltable);
375 uploadButtonTop = new Button("<span id='topMenu.file.upload'>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + " Upload</span>");
376 uploadButtonTop.addClickHandler(new ClickHandler() {
379 public void onClick(ClickEvent event) {
380 new UploadFileCommand(app, null, treeView.getSelection()).execute();
383 HorizontalPanel topPanel = new HorizontalPanel();
384 topPanel.add(pagerTop);
385 topPanel.add(uploadButtonTop);
390 pagerBottom = new GssSimplePager(GssSimplePager.TextLocation.CENTER);
391 pagerBottom.setVisible(false);
392 pagerBottom.setDisplay(celltable);
393 uploadButtonBottom=new Button("<span id='topMenu.file.upload'>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + " Upload</span>");
394 uploadButtonBottom.addClickHandler(new ClickHandler() {
397 public void onClick(ClickEvent event) {
398 new UploadFileCommand(app, null, treeView.getSelection()).execute();
401 HorizontalPanel bottomPanel = new HorizontalPanel();
402 bottomPanel.add(pagerBottom);
403 bottomPanel.add(uploadButtonBottom);
406 vp.setCellWidth(celltable, "100%");
407 vp.addHandler(new ContextMenuHandler() {
409 public void onContextMenu(ContextMenuEvent event) {
410 Folder selectedFolder = treeView.getSelection();
411 if (!selectedFolder.isTrash()) {
412 FileContextMenu contextMenu = new FileContextMenu(app, images, selectedFolder, getSelectedFiles(), false);
413 int x = event.getNativeEvent().getClientX();
414 int y = event.getNativeEvent().getClientY();
415 contextMenu.setPopupPosition(x, y);
419 }, ContextMenuEvent.getType());
422 selectionModel = new MultiSelectionModel<File>(keyProvider);
424 celltable.setSelectionModel(selectionModel, GSSSelectionEventManager.<File> createDefaultManager());
425 celltable.setPageSize(Pithos.VISIBLE_FILE_COUNT);
427 sinkEvents(Event.ONCONTEXTMENU);
428 // sinkEvents(Event.ONMOUSEUP);
429 // sinkEvents(Event.ONMOUSEDOWN);
430 // sinkEvents(Event.ONCLICK);
431 // sinkEvents(Event.ONKEYDOWN);
432 // sinkEvents(Event.ONDBLCLICK);
433 Pithos.preventIESelection();
436 public List<File> getSelectedFiles() {
437 return new ArrayList<File>(selectionModel.getSelectedSet());
441 // public void onBrowserEvent(Event event) {
443 // if (files == null || files.size() == 0) {
444 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
445 // contextMenu = new FileContextMenu(images, false, true);
446 // contextMenu.show();
447 // event.preventDefault();
448 // event.cancelBubble(true);
452 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() != 0) {
453 // GWT.log("*****GOING TO SHOW CONTEXT MENU ****", null);
454 // contextMenu = new FileContextMenu(images, false, false);
455 // contextMenu = contextMenu.onEvent(event);
456 // event.cancelBubble(true);
457 // event.preventDefault();
458 // } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
459 // contextMenu = new FileContextMenu(images, false, true);
460 // contextMenu = contextMenu.onEmptyEvent(event);
461 // event.cancelBubble(true);
462 // event.preventDefault();
463 // } else if (DOM.eventGetType(event) == Event.ONDBLCLICK)
464 // if (getSelectedFiles().size() == 1) {
466 // File file = getSelectedFiles().get(0);
467 // Window.open(file.getUri(), "_blank", "");
468 // event.preventDefault();
471 // super.onBrowserEvent(event);
475 * Update the display of the file list.
477 void update(boolean sort) {
478 int count = folderFileCount;
479 int max = startIndex + Pithos.VISIBLE_FILE_COUNT;
485 folderTotalSize += f.getBytes();
487 if (folderFileCount == 0) {
488 showingStats = "no files";
489 } else if (folderFileCount < Pithos.VISIBLE_FILE_COUNT) {
490 if (folderFileCount == 1)
491 showingStats = "1 file";
493 showingStats = folderFileCount + " files";
494 // showingStats += " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
496 // showingStats = "" + (startIndex + 1) + " - " + max + " of " + count + " files" + " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
502 * Return the proper icon based on the MIME type of the file.
507 private ImageResource getFileIcon(File file) {
508 String mimetype = file.getContentType();
509 boolean shared = file.isShared();
510 if (mimetype == null)
511 return shared ? images.documentShared() : images.document();
512 mimetype = mimetype.toLowerCase();
513 if (mimetype.startsWith("application/pdf"))
514 return shared ? images.pdfShared() : images.pdf();
515 else if (mimetype.endsWith("excel"))
516 return shared ? images.spreadsheetShared() : images.spreadsheet();
517 else if (mimetype.endsWith("msword"))
518 return shared ? images.wordprocessorShared() : images.wordprocessor();
519 else if (mimetype.endsWith("powerpoint"))
520 return shared ? images.presentationShared() : images.presentation();
521 else if (mimetype.startsWith("application/zip") ||
522 mimetype.startsWith("application/gzip") ||
523 mimetype.startsWith("application/x-gzip") ||
524 mimetype.startsWith("application/x-tar") ||
525 mimetype.startsWith("application/x-gtar"))
526 return shared ? images.zipShared() : images.zip();
527 else if (mimetype.startsWith("text/html"))
528 return shared ? images.htmlShared() : images.html();
529 else if (mimetype.startsWith("text/plain"))
530 return shared ? images.txtShared() : images.txt();
531 else if (mimetype.startsWith("image/"))
532 return shared ? images.imageShared() : images.image();
533 else if (mimetype.startsWith("video/"))
534 return shared ? images.videoShared() : images.video();
535 else if (mimetype.startsWith("audio/"))
536 return shared ? images.audioShared() : images.audio();
537 return shared ? images.documentShared() : images.document();
541 * Fill the file cache with data.
543 public void setFiles(final List<File> _files) {
544 files = new ArrayList<File>();
545 for (File fres : _files)
546 if (!fres.isInTrash())
548 Collections.sort(files, new Comparator<File>() {
551 public int compare(File arg0, File arg1) {
552 return arg0.getName().compareTo(arg1.getName());
556 folderFileCount = files.size();
558 nameHeader.setSorted(true);
559 nameHeader.toggleReverseSort();
560 for (SortableHeader otherHeader : allHeaders) {
561 if (otherHeader != nameHeader) {
562 otherHeader.setSorted(false);
563 otherHeader.setReverseSort(true);
567 if(files.size() > Pithos.VISIBLE_FILE_COUNT){
568 pagerBottom.setVisible(true);
569 pagerTop.setVisible(true);
572 pagerTop.setVisible(false);
573 pagerBottom.setVisible(false);
575 Folder selectedItem = treeView.getSelection();
577 provider.setList(files);
578 selectionModel.clear();
582 * Does the list contains the requested filename
587 public boolean contains(String fileName) {
588 for (int i = 0; i < files.size(); i++)
589 if (files.get(i).getName().equals(fileName))
594 public void clearSelectedRows() {
595 Iterator<File> it = selectionModel.getSelectedSet().iterator();
597 selectionModel.setSelected(it.next(),false);
604 public void selectAllRows() {
605 Iterator<File> it = provider.getList().iterator();
607 selectionModel.setSelected(it.next(),true);
611 private void sortFiles(final String sortingProperty, final boolean sortingType){
612 Collections.sort(files, new Comparator<File>() {
615 public int compare(File arg0, File arg1) {
616 AbstractImagePrototype descPrototype = AbstractImagePrototype.create(images.desc());
617 AbstractImagePrototype ascPrototype = AbstractImagePrototype.create(images.asc());
619 if (sortingProperty.equals("version")) {
620 return arg0.getVersion() - arg1.getVersion();
621 } else if (sortingProperty.equals("owner")) {
622 return arg0.getOwner().compareTo(arg1.getOwner());
623 } else if (sortingProperty.equals("date")) {
624 return arg0.getLastModified().compareTo(arg1.getLastModified());
625 } else if (sortingProperty.equals("size")) {
626 return (int) (arg0.getBytes() - arg1.getBytes());
627 } else if (sortingProperty.equals("name")) {
628 return arg0.getName().compareTo(arg1.getName());
629 } else if (sortingProperty.equals("path")) {
630 return arg0.getUri().compareTo(arg1.getUri());
632 return arg0.getName().compareTo(arg1.getName());
635 else if (sortingProperty.equals("version")) {
637 return arg1.getVersion() - arg0.getVersion();
638 } else if (sortingProperty.equals("owner")) {
640 return arg1.getOwner().compareTo(arg0.getOwner());
641 } else if (sortingProperty.equals("date")) {
643 return arg1.getLastModified().compareTo(arg0.getLastModified());
644 } else if (sortingProperty.equals("size")) {
645 return (int) (arg1.getBytes() - arg0.getBytes());
646 } else if (sortingProperty.equals("name")) {
648 return arg1.getName().compareTo(arg0.getName());
649 } else if (sortingProperty.equals("path")) {
651 return arg1.getUri().compareTo(arg0.getUri());
654 return arg1.getName().compareTo(arg0.getName());
661 final class FileValueUpdater implements ValueUpdater<String>{
662 private String property;
663 private SortableHeader header;
667 public FileValueUpdater(SortableHeader header,String property) {
668 this.property=property;
672 public void update(String value) {
673 header.setSorted(true);
674 header.toggleReverseSort();
676 for (SortableHeader otherHeader : allHeaders) {
677 if (otherHeader != header) {
678 otherHeader.setSorted(false);
679 otherHeader.setReverseSort(true);
682 celltable.redrawHeaders();
683 sortFiles(property, header.getReverseSort());
684 FileList.this.update(true);
690 * Shows the files in the cellTable
692 private void showCellTable(){
693 if(files.size()> Pithos.VISIBLE_FILE_COUNT){
694 pagerBottom.setVisible(true);
695 pagerTop.setVisible(true);
698 pagerTop.setVisible(false);
699 pagerBottom.setVisible(false);
701 provider.setList(files);
705 //celltable.redraw();
706 celltable.redrawHeaders();
709 public void showTrash() {
710 uploadButtonBottom.setVisible(false);
711 uploadButtonTop.setVisible(false);
714 public void showFiles() {
715 uploadButtonBottom.setVisible(true);
716 uploadButtonTop.setVisible(true);