2 * Copyright 2011-2012 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 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;
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;
49 import com.google.gwt.cell.client.Cell.Context;
50 import com.google.gwt.cell.client.ImageResourceCell;
51 import com.google.gwt.cell.client.SafeHtmlCell;
52 import com.google.gwt.cell.client.TextCell;
53 import com.google.gwt.cell.client.ValueUpdater;
54 import com.google.gwt.core.client.GWT;
55 import com.google.gwt.event.dom.client.ContextMenuEvent;
56 import com.google.gwt.event.dom.client.ContextMenuHandler;
57 import com.google.gwt.http.client.URL;
58 import com.google.gwt.i18n.client.DateTimeFormat;
59 import com.google.gwt.resources.client.ImageResource;
60 import com.google.gwt.resources.client.ClientBundle.Source;
61 import com.google.gwt.resources.client.ImageResource.ImageOptions;
62 import com.google.gwt.resources.client.ImageResource.RepeatStyle;
63 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
64 import com.google.gwt.safehtml.shared.SafeHtml;
65 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
66 import com.google.gwt.user.cellview.client.CellTable;
67 import com.google.gwt.user.cellview.client.Column;
68 import com.google.gwt.user.client.Command;
69 import com.google.gwt.user.client.DOM;
70 import com.google.gwt.user.client.Event;
71 import com.google.gwt.user.client.Window;
72 import com.google.gwt.user.client.ui.Composite;
73 import com.google.gwt.user.client.ui.VerticalPanel;
74 import com.google.gwt.view.client.ListDataProvider;
75 import com.google.gwt.view.client.MultiSelectionModel;
76 import com.google.gwt.view.client.ProvidesKey;
77 import com.google.gwt.view.client.SelectionChangeEvent;
80 * A composite that displays the list of files in a particular folder.
82 public class FileList extends Composite {
84 ListDataProvider<File> provider = new ListDataProvider<File>();
87 * The styles applied to the table.
89 interface TableStyle extends CellTable.Style {
90 String cellTableFirstColumnShared();
93 interface TableResources extends CellTable.Resources {
95 @Source({CellTable.Style.DEFAULT_CSS, "PithosCellTable.css"})
96 TableStyle cellTableStyle();
99 @ImageOptions(repeatStyle = RepeatStyle.None)
100 ImageResource cellTableSharedIcon();
103 static interface Templates extends SafeHtmlTemplates {
104 Templates INSTANCE = GWT.create(Templates.class);
106 @Template("<div id='dragHelper' style='border:1px solid black; background-color:#ffffff; color:black; width:150px;z-index:100'></div>")
107 SafeHtml outerHelper();
109 @Template("<span id='{0}'>{0}</span>")
110 public SafeHtml filenameSpan(String filename);
112 @Template("<a href='{0}' title='{1}' rel='lytebox[mnf]' onclick='myLytebox.start(this, false, false); return false;'>(view)</a>")
113 public SafeHtml viewLink(String link, String title);
115 @Template("<table><tr><td rowspan='3'>{0}</td><td style='font-size:95%;' id='{1}'>{1}</td></tr><tr><td>{2}</td></tr></table>")
116 public SafeHtml rendelContactCell(String imageHtml, String name, String fileSize);
118 @Template("<span id='{0}' class='{1}'>{2}</span>")
119 public SafeHtml spanWithIdAndClass(String id, String cssClass, String content);
122 protected final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
125 * Specifies that the images available for this composite will be the ones
126 * available in FileContextMenu.
128 public interface Images extends FolderTreeView.Images {
130 @Source("gr/grnet/pithos/resources/blank.gif")
131 ImageResource blank();
133 @Source("gr/grnet/pithos/resources/asc.png")
136 @Source("gr/grnet/pithos/resources/desc.png")
137 ImageResource desc();
139 @Source("gr/grnet/pithos/resources/mimetypes/document_shared.png")
140 ImageResource documentShared();
142 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst.png")
143 ImageResource wordprocessor();
145 @Source("gr/grnet/pithos/resources/mimetypes/log.png")
146 ImageResource spreadsheet();
148 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr.png")
149 ImageResource presentation();
151 @Source("gr/grnet/pithos/resources/mimetypes/acroread.png")
154 @Source("gr/grnet/pithos/resources/mimetypes/image.png")
155 ImageResource image();
157 @Source("gr/grnet/pithos/resources/mimetypes/video2.png")
158 ImageResource video();
160 @Source("gr/grnet/pithos/resources/mimetypes/knotify.png")
161 ImageResource audio();
163 @Source("gr/grnet/pithos/resources/mimetypes/html.png")
164 ImageResource html();
166 @Source("gr/grnet/pithos/resources/mimetypes/txt.png")
169 @Source("gr/grnet/pithos/resources/mimetypes/ark2.png")
172 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst_shared.png")
173 ImageResource wordprocessorShared();
175 @Source("gr/grnet/pithos/resources/mimetypes/log_shared.png")
176 ImageResource spreadsheetShared();
178 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr_shared.png")
179 ImageResource presentationShared();
181 @Source("gr/grnet/pithos/resources/mimetypes/acroread_shared.png")
182 ImageResource pdfShared();
184 @Source("gr/grnet/pithos/resources/mimetypes/image_shared.png")
185 ImageResource imageShared();
187 @Source("gr/grnet/pithos/resources/mimetypes/video2_shared.png")
188 ImageResource videoShared();
190 @Source("gr/grnet/pithos/resources/mimetypes/knotify_shared.png")
191 ImageResource audioShared();
193 @Source("gr/grnet/pithos/resources/mimetypes/html_shared.png")
194 ImageResource htmlShared();
196 @Source("gr/grnet/pithos/resources/mimetypes/txt_shared.png")
197 ImageResource txtShared();
199 @Source("gr/grnet/pithos/resources/mimetypes/ark2_shared.png")
200 ImageResource zipShared();
205 * The number of files in this folder.
212 long folderTotalSize;
215 * A cache of the files in the list.
217 private List<File> files;
220 * The widget's image bundle.
222 protected final Images images;
224 protected CellTable<File> celltable;
226 private final MultiSelectionModel<File> selectionModel;
228 Column<File, String> pathColumn;
230 protected final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
232 SortableHeader nameHeader;
234 SortableHeader pathHeader;
236 protected Pithos app;
239 * Construct the file list widget. This entails setting up the widget
240 * layout, fetching the number of files in the current folder from the
241 * server and filling the local file cache of displayed files with data from
242 * the server, as well.
246 public FileList(final Pithos _app, Images _images) {
250 final CellTable.Resources resources = GWT.create(TableResources.class);
252 ProvidesKey<File> keyProvider = new ProvidesKey<File>(){
255 public Object getKey(File item) {
256 return item.getUri();
260 celltable = new CellTable<File>(10, resources, keyProvider);
261 celltable.setWidth("100%");
262 celltable.setStyleName("pithos-List");
264 Column<File, ImageResource> status = new Column<File, ImageResource>(new ImageResourceCell() {
266 public boolean handlesSelection() {
272 public ImageResource getValue(File entity) {
273 return getFileIcon(entity);
277 public String getCellStyleNames(Context context, File object) {
278 if (!object.getPermissions().isEmpty() && !object.isPublished())
279 return ((TableStyle) resources.cellTableStyle()).cellTableFirstColumnShared();
280 return super.getCellStyleNames(context, object);
283 celltable.addColumn(status,"");
285 final Column<File,SafeHtml> nameColumn = new Column<File,SafeHtml>(new SafeHtmlCell()) {
288 public SafeHtml getValue(File object) {
289 SafeHtmlBuilder sb = new SafeHtmlBuilder();
290 sb.append(Templates.INSTANCE.filenameSpan(object.getName()));
291 if (object.getContentType() != null && (object.getContentType().endsWith("png") || object.getContentType().endsWith("gif") || object.getContentType().endsWith("jpeg"))) {
292 sb.appendHtmlConstant(" ")
293 .append(Templates.INSTANCE.viewLink(app.getApiPath() + object.getOwner() + object.getUri(), object.getName()));
296 return sb.toSafeHtml();
300 celltable.addColumn(nameColumn, nameHeader = new SortableHeader("Name", "name"));
301 allHeaders.add(nameHeader);
302 nameHeader.setUpdater(new FileValueUpdater(nameHeader));
303 nameHeader.setSorted(true);
304 nameHeader.setReverseSort(true);
306 celltable.redrawHeaders();
308 pathColumn = new Column<File, String>(new TextCell()) {
311 public String getValue(File f) {
313 if (!app.getSelectedTree().equals(app.mysharedTreeView)) {
314 path = f.getParent().getPrefix();
315 if (path.length() == 0)
320 if (path.lastIndexOf("/") != -1)
321 path = path.substring(0, path.lastIndexOf("/"));
328 pathHeader = new SortableHeader("Path", "path");
329 celltable.addColumn(pathColumn, pathHeader);
330 allHeaders.add(pathHeader);
331 pathHeader.setUpdater(new FileValueUpdater(pathHeader));
333 Column<File,String> aColumn = new Column<File,String>(new TextCell()) {
335 public String getValue(File object) {
336 return object.getSizeAsString();
339 SortableHeader aheader = new SortableHeader("Size", "size");
340 celltable.addColumn(aColumn, aheader);
341 allHeaders.add(aheader);
342 aheader.setUpdater(new FileValueUpdater(aheader));
344 aColumn = new Column<File,String>(new TextCell()) {
346 public String getValue(File object) {
347 return object.getLastModified() != null ? formatter.format(object.getLastModified()) : "";
350 aheader = new SortableHeader("Last Modified", "date");
351 celltable.addColumn(aColumn, aheader);
352 allHeaders.add(aheader);
353 aheader.setUpdater(new FileValueUpdater(aheader));
355 provider.addDataDisplay(celltable);
357 VerticalPanel vp = new VerticalPanel();
359 vp.addStyleName("pithos-FileListContainer");
363 vp.setCellWidth(celltable, "100%");
364 vp.addHandler(new ContextMenuHandler() {
366 public void onContextMenu(final ContextMenuEvent event) {
367 final TreeView tree = app.getSelectedTree();
369 final int x = event.getNativeEvent().getClientX();
370 final int y = event.getNativeEvent().getClientY();
371 final Folder selectedFolder = app.getSelection();
372 app.scheduleFolderHeadCommand(selectedFolder, new Command() {
375 public void execute() {
376 final List<File> selectedFiles = getSelectedFiles();
377 Iterator<File> iter = selectedFiles.iterator();
378 iterateFilesHeadCommand(iter, new Command() {
381 public void execute() {
382 FileContextMenu contextMenu = new FileContextMenu(app, images, tree, selectedFolder, selectedFiles);
383 contextMenu.setPopupPosition(x, y);
391 }, ContextMenuEvent.getType());
394 selectionModel = new MultiSelectionModel<File>(keyProvider);
395 selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
398 public void onSelectionChange(SelectionChangeEvent event) {
399 app.showRelevantToolbarButtons();
403 celltable.setSelectionModel(selectionModel, PithosSelectionEventManager.<File> createDefaultManager());
404 // celltable.setPageSize(Pithos.VISIBLE_FILE_COUNT);
406 sinkEvents(Event.ONCONTEXTMENU);
407 // sinkEvents(Event.ONMOUSEUP);
408 // sinkEvents(Event.ONMOUSEDOWN);
409 // sinkEvents(Event.ONCLICK);
410 // sinkEvents(Event.ONKEYDOWN);
411 sinkEvents(Event.ONDBLCLICK);
412 Pithos.preventIESelection();
415 public List<File> getSelectedFiles() {
416 return new ArrayList<File>(selectionModel.getSelectedSet());
420 public void onBrowserEvent(Event event) {
422 // if (files == null || files.size() == 0) {
423 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
424 // contextMenu = new FileContextMenu(images, false, true);
425 // contextMenu.show();
426 // event.preventDefault();
427 // event.cancelBubble(true);
431 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() != 0) {
432 // GWT.log("*****GOING TO SHOW CONTEXT MENU ****", null);
433 // contextMenu = new FileContextMenu(images, false, false);
434 // contextMenu = contextMenu.onEvent(event);
435 // event.cancelBubble(true);
436 // event.preventDefault();
437 // } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
438 // contextMenu = new FileContextMenu(images, false, true);
439 // contextMenu = contextMenu.onEmptyEvent(event);
440 // event.cancelBubble(true);
441 // event.preventDefault();
443 if (DOM.eventGetType(event) == Event.ONDBLCLICK)
444 if (getSelectedFiles().size() == 1) {
445 File file = getSelectedFiles().get(0);
446 Window.open(app.getApiPath() + file.getOwner() + file.getUri(), "_blank", "");
447 event.preventDefault();
450 super.onBrowserEvent(event);
454 * Update the display of the file list.
461 * Return the proper icon based on the MIME type of the file.
466 protected ImageResource getFileIcon(File file) {
467 String mimetype = file.getContentType();
468 boolean published = file.isPublished();
469 if (mimetype == null)
470 return published ? images.documentShared() : images.document();
471 mimetype = mimetype.toLowerCase();
472 if (mimetype.startsWith("application/pdf"))
473 return published ? images.pdfShared() : images.pdf();
474 else if (mimetype.endsWith("excel"))
475 return published ? images.spreadsheetShared() : images.spreadsheet();
476 else if (mimetype.endsWith("msword"))
477 return published ? images.wordprocessorShared() : images.wordprocessor();
478 else if (mimetype.endsWith("powerpoint"))
479 return published ? images.presentationShared() : images.presentation();
480 else if (mimetype.startsWith("application/zip") ||
481 mimetype.startsWith("application/gzip") ||
482 mimetype.startsWith("application/x-gzip") ||
483 mimetype.startsWith("application/x-tar") ||
484 mimetype.startsWith("application/x-gtar"))
485 return published ? images.zipShared() : images.zip();
486 else if (mimetype.startsWith("text/html"))
487 return published ? images.htmlShared() : images.html();
488 else if (mimetype.startsWith("text/plain"))
489 return published ? images.txtShared() : images.txt();
490 else if (mimetype.startsWith("image/"))
491 return published ? images.imageShared() : images.image();
492 else if (mimetype.startsWith("video/"))
493 return published ? images.videoShared() : images.video();
494 else if (mimetype.startsWith("audio/"))
495 return published ? images.audioShared() : images.audio();
496 return published ? images.documentShared() : images.document();
500 * Fill the file cache with data.
502 public void setFiles(final List<File> _files) {
503 if (!app.getSelectedTree().equals(app.mysharedTreeView)) {
504 if (celltable.getColumnIndex(pathColumn) != -1)
505 celltable.removeColumn(pathColumn);
508 if (celltable.getColumnIndex(pathColumn) == -1)
509 celltable.insertColumn(2, pathColumn, pathHeader);
511 files = new ArrayList<File>();
512 for (File fres : _files) {
516 folderFileCount = files.size();
518 for (SortableHeader header : allHeaders) {
519 if (header.isSorted())
520 sortFiles(header.getProperty(), header.getReverseSort());
523 List<File> previousSelection = getSelectedFiles(); //Keep the previous selection
525 provider.getList().clear();
526 provider.setList(files);
527 selectionModel.clear();
528 for (File f : files) {
529 if (previousSelection.contains(f))
530 selectionModel.setSelected(f, true);
533 app.showFolderStatistics(folderFileCount);
534 celltable.setPageSize(folderFileCount);
538 * Does the list contains the requested filename
543 public boolean contains(String fileName) {
544 for (int i = 0; i < files.size(); i++)
545 if (files.get(i).getName().equals(fileName))
550 public void clearSelectedRows() {
551 Iterator<File> it = selectionModel.getSelectedSet().iterator();
553 selectionModel.setSelected(it.next(),false);
560 public void selectAllRows() {
561 Iterator<File> it = provider.getList().iterator();
563 selectionModel.setSelected(it.next(),true);
567 protected void sortFiles(final String sortingProperty, final boolean sortingType){
568 Collections.sort(files, new Comparator<File>() {
571 public int compare(File arg0, File arg1) {
573 if (sortingProperty.equals("version")) {
574 return arg0.getVersion() - arg1.getVersion();
575 } else if (sortingProperty.equals("owner")) {
576 return arg0.getOwner().compareTo(arg1.getOwner());
577 } else if (sortingProperty.equals("date")) {
578 if (arg0.getLastModified() != null && arg1.getLastModified() != null)
579 return arg0.getLastModified().compareTo(arg1.getLastModified());
581 } else if (sortingProperty.equals("size")) {
582 return (int) (arg0.getBytes() - arg1.getBytes());
583 } else if (sortingProperty.equals("name")) {
584 return arg0.getName().compareTo(arg1.getName());
585 } else if (sortingProperty.equals("path")) {
586 return arg0.getUri().compareTo(arg1.getUri());
588 return arg0.getName().compareTo(arg1.getName());
591 else if (sortingProperty.equals("version")) {
593 return arg1.getVersion() - arg0.getVersion();
594 } else if (sortingProperty.equals("owner")) {
596 return arg1.getOwner().compareTo(arg0.getOwner());
597 } else if (sortingProperty.equals("date")) {
599 return arg1.getLastModified().compareTo(arg0.getLastModified());
600 } else if (sortingProperty.equals("size")) {
601 return (int) (arg1.getBytes() - arg0.getBytes());
602 } else if (sortingProperty.equals("name")) {
604 return arg1.getName().compareTo(arg0.getName());
605 } else if (sortingProperty.equals("path")) {
607 return arg1.getUri().compareTo(arg0.getUri());
610 return arg1.getName().compareTo(arg0.getName());
617 final class FileValueUpdater implements ValueUpdater<String>{
618 private SortableHeader header;
622 public FileValueUpdater(SortableHeader header) {
626 public void update(String value) {
627 header.setSorted(true);
628 header.toggleReverseSort();
630 for (SortableHeader otherHeader : allHeaders) {
631 if (otherHeader != header) {
632 otherHeader.setSorted(false);
633 otherHeader.setReverseSort(true);
636 celltable.redrawHeaders();
637 sortFiles(header.getProperty(), header.getReverseSort());
638 FileList.this.update();
644 * Shows the files in the cellTable
646 private void showCellTable(){
647 provider.setList(files);
651 //celltable.redraw();
652 celltable.redrawHeaders();
655 void iterateFilesHeadCommand(final Iterator<File> iter, final Command callback) {
656 if (iter.hasNext()) {
657 File f = iter.next();
658 app.scheduleFileHeadCommand(f, new Command() {
661 public void execute() {
662 iterateFilesHeadCommand(iter, callback);
666 else if (callback != null)
670 public void selectByUrl(List<String> selectedUrls) {
671 Set<File> previous = selectionModel.getSelectedSet();
672 for (File f : previous)
673 selectionModel.setSelected(f, false);
676 boolean scrolled = false;
677 for (File f : files) {
678 if (selectedUrls.contains(app.getApiPath() + f.getOwner() + f.getUri())) {
679 selectionModel.setSelected(f, true);
681 celltable.getRowElement(i).getCells().getItem(0).scrollIntoView();