2 * Copyright 2011-2013 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.i18n.client.DateTimeFormat;
58 import com.google.gwt.resources.client.ImageResource;
59 import com.google.gwt.resources.client.ImageResource.ImageOptions;
60 import com.google.gwt.resources.client.ImageResource.RepeatStyle;
61 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
62 import com.google.gwt.safehtml.shared.SafeHtml;
63 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
64 import com.google.gwt.user.cellview.client.CellTable;
65 import com.google.gwt.user.cellview.client.Column;
66 import com.google.gwt.user.client.Command;
67 import com.google.gwt.user.client.DOM;
68 import com.google.gwt.user.client.Event;
69 import com.google.gwt.user.client.Window;
70 import com.google.gwt.user.client.ui.Composite;
71 import com.google.gwt.user.client.ui.VerticalPanel;
72 import com.google.gwt.view.client.ListDataProvider;
73 import com.google.gwt.view.client.MultiSelectionModel;
74 import com.google.gwt.view.client.ProvidesKey;
75 import com.google.gwt.view.client.SelectionChangeEvent;
78 * A composite that displays the list of files in a particular folder.
80 public class FileList extends Composite {
82 ListDataProvider<File> provider = new ListDataProvider<File>();
85 * The styles applied to the table.
87 interface TableStyle extends CellTable.Style {
88 String cellTableFirstColumnShared();
91 interface TableResources extends CellTable.Resources {
93 @Source({CellTable.Style.DEFAULT_CSS, "PithosCellTable.css"})
94 TableStyle cellTableStyle();
97 @ImageOptions(repeatStyle = RepeatStyle.None)
98 ImageResource cellTableSharedIcon();
101 static interface Templates extends SafeHtmlTemplates {
102 Templates INSTANCE = GWT.create(Templates.class);
104 @Template("<div id='dragHelper' style='border:1px solid black; background-color:#ffffff; color:black; width:150px;z-index:100'></div>")
105 SafeHtml outerHelper();
107 @Template("<span id='{0}'>{0}</span>")
108 public SafeHtml filenameSpan(String filename);
110 @Template("<a href='{0}' title='{1}' rel='lytebox[mnf]' onclick='myLytebox.start(this, false, false); return false;'>(view)</a>")
111 public SafeHtml viewLink(String link, String title);
113 @Template("<table><tr><td rowspan='3'>{0}</td><td style='font-size:95%;' id='{1}'>{1}</td></tr><tr><td>{2}</td></tr></table>")
114 public SafeHtml rendelContactCell(String imageHtml, String name, String fileSize);
116 @Template("<span id='{0}' class='{1}'>{2}</span>")
117 public SafeHtml spanWithIdAndClass(String id, String cssClass, String content);
120 protected final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
123 * Specifies that the images available for this composite will be the ones
124 * available in FileContextMenu.
126 public interface Images extends FolderTreeView.Images {
128 @Source("gr/grnet/pithos/resources/blank.gif")
129 ImageResource blank();
131 @Source("gr/grnet/pithos/resources/asc.png")
134 @Source("gr/grnet/pithos/resources/desc.png")
135 ImageResource desc();
137 @Source("gr/grnet/pithos/resources/mimetypes/document_shared.png")
138 ImageResource documentShared();
140 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst.png")
141 ImageResource wordprocessor();
143 @Source("gr/grnet/pithos/resources/mimetypes/log.png")
144 ImageResource spreadsheet();
146 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr.png")
147 ImageResource presentation();
149 @Source("gr/grnet/pithos/resources/mimetypes/acroread.png")
152 @Source("gr/grnet/pithos/resources/mimetypes/image.png")
153 ImageResource image();
155 @Source("gr/grnet/pithos/resources/mimetypes/video2.png")
156 ImageResource video();
158 @Source("gr/grnet/pithos/resources/mimetypes/knotify.png")
159 ImageResource audio();
161 @Source("gr/grnet/pithos/resources/mimetypes/html.png")
162 ImageResource html();
164 @Source("gr/grnet/pithos/resources/mimetypes/txt.png")
167 @Source("gr/grnet/pithos/resources/mimetypes/ark2.png")
170 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst_shared.png")
171 ImageResource wordprocessorShared();
173 @Source("gr/grnet/pithos/resources/mimetypes/log_shared.png")
174 ImageResource spreadsheetShared();
176 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr_shared.png")
177 ImageResource presentationShared();
179 @Source("gr/grnet/pithos/resources/mimetypes/acroread_shared.png")
180 ImageResource pdfShared();
182 @Source("gr/grnet/pithos/resources/mimetypes/image_shared.png")
183 ImageResource imageShared();
185 @Source("gr/grnet/pithos/resources/mimetypes/video2_shared.png")
186 ImageResource videoShared();
188 @Source("gr/grnet/pithos/resources/mimetypes/knotify_shared.png")
189 ImageResource audioShared();
191 @Source("gr/grnet/pithos/resources/mimetypes/html_shared.png")
192 ImageResource htmlShared();
194 @Source("gr/grnet/pithos/resources/mimetypes/txt_shared.png")
195 ImageResource txtShared();
197 @Source("gr/grnet/pithos/resources/mimetypes/ark2_shared.png")
198 ImageResource zipShared();
203 * The number of files in this folder.
210 long folderTotalSize;
213 * A cache of the files in the list.
215 private List<File> files;
218 * The widget's image bundle.
220 protected final Images images;
222 protected CellTable<File> celltable;
224 private final MultiSelectionModel<File> selectionModel;
226 Column<File, String> pathColumn;
228 protected final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
230 SortableHeader nameHeader;
232 SortableHeader pathHeader;
234 protected Pithos app;
237 * Construct the file list widget. This entails setting up the widget
238 * layout, fetching the number of files in the current folder from the
239 * server and filling the local file cache of displayed files with data from
240 * the server, as well.
244 public FileList(final Pithos _app, Images _images) {
248 final CellTable.Resources resources = GWT.create(TableResources.class);
250 ProvidesKey<File> keyProvider = new ProvidesKey<File>(){
253 public Object getKey(File item) {
254 return item.getUri();
258 celltable = new CellTable<File>(10, resources, keyProvider);
259 celltable.setWidth("100%");
260 celltable.setStyleName("pithos-List");
262 Column<File, ImageResource> status = new Column<File, ImageResource>(new ImageResourceCell() {
264 public boolean handlesSelection() {
270 public ImageResource getValue(File entity) {
271 return getFileIcon(entity);
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);
281 celltable.addColumn(status,"");
283 final Column<File,SafeHtml> nameColumn = new Column<File,SafeHtml>(new SafeHtmlCell()) {
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(" ")
291 .append(Templates.INSTANCE.viewLink(Pithos.getStorageAPIURL() + object.getOwnerID() + object.getUri(), object.getName()));
294 return sb.toSafeHtml();
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);
304 celltable.redrawHeaders();
306 pathColumn = new Column<File, String>(new TextCell()) {
309 public String getValue(File f) {
311 if (!app.getSelectedTree().equals(app.mysharedTreeView)) {
312 path = f.getParent().getPrefix();
313 if (path.length() == 0)
318 if (path.lastIndexOf("/") != -1)
319 path = path.substring(0, path.lastIndexOf("/"));
326 pathHeader = new SortableHeader("Path", "path");
327 celltable.addColumn(pathColumn, pathHeader);
328 allHeaders.add(pathHeader);
329 pathHeader.setUpdater(new FileValueUpdater(pathHeader));
331 Column<File,String> aColumn = new Column<File,String>(new TextCell()) {
333 public String getValue(File object) {
334 return object.getSizeAsString();
337 SortableHeader aheader = new SortableHeader("Size", "size");
338 celltable.addColumn(aColumn, aheader);
339 allHeaders.add(aheader);
340 aheader.setUpdater(new FileValueUpdater(aheader));
342 aColumn = new Column<File,String>(new TextCell()) {
344 public String getValue(File object) {
345 return object.getLastModified() != null ? formatter.format(object.getLastModified()) : "";
348 aheader = new SortableHeader("Last Modified", "date");
349 celltable.addColumn(aColumn, aheader);
350 allHeaders.add(aheader);
351 aheader.setUpdater(new FileValueUpdater(aheader));
353 provider.addDataDisplay(celltable);
355 VerticalPanel vp = new VerticalPanel();
357 vp.addStyleName("pithos-FileListContainer");
361 vp.setCellWidth(celltable, "100%");
362 vp.addHandler(new ContextMenuHandler() {
364 public void onContextMenu(final ContextMenuEvent event) {
365 final TreeView tree = app.getSelectedTree();
367 final int x = event.getNativeEvent().getClientX();
368 final int y = event.getNativeEvent().getClientY();
369 final Folder selectedFolder = app.getSelection();
370 app.scheduleFolderHeadCommand(selectedFolder, new Command() {
373 public void execute() {
374 final List<File> selectedFiles = getSelectedFiles();
375 Iterator<File> iter = selectedFiles.iterator();
376 iterateFilesHeadCommand(iter, new Command() {
379 public void execute() {
380 FileContextMenu contextMenu = new FileContextMenu(app, images, tree, selectedFolder, selectedFiles);
381 contextMenu.setPopupPosition(x, y);
389 }, ContextMenuEvent.getType());
392 selectionModel = new MultiSelectionModel<File>(keyProvider);
393 selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
396 public void onSelectionChange(SelectionChangeEvent event) {
397 app.showRelevantToolbarButtons();
401 celltable.setSelectionModel(selectionModel, PithosSelectionEventManager.<File> createDefaultManager());
403 sinkEvents(Event.ONCONTEXTMENU);
404 sinkEvents(Event.ONDBLCLICK);
405 Pithos.preventIESelection();
408 public List<File> getSelectedFiles() {
409 return new ArrayList<File>(selectionModel.getSelectedSet());
413 public void onBrowserEvent(Event event) {
415 if (DOM.eventGetType(event) == Event.ONDBLCLICK)
416 if (getSelectedFiles().size() == 1) {
417 File file = getSelectedFiles().get(0);
418 Window.open(Pithos.getStorageAPIURL() + file.getOwnerID() + file.getUri(), "_blank", "");
419 event.preventDefault();
422 super.onBrowserEvent(event);
426 * Update the display of the file list.
433 * Return the proper icon based on the MIME type of the file.
438 protected ImageResource getFileIcon(File file) {
439 String mimetype = file.getContentType();
440 boolean published = file.isPublished();
441 if (mimetype == null)
442 return published ? images.documentShared() : images.document();
443 mimetype = mimetype.toLowerCase();
444 if (mimetype.startsWith("application/pdf"))
445 return published ? images.pdfShared() : images.pdf();
446 else if (mimetype.endsWith("excel"))
447 return published ? images.spreadsheetShared() : images.spreadsheet();
448 else if (mimetype.endsWith("msword"))
449 return published ? images.wordprocessorShared() : images.wordprocessor();
450 else if (mimetype.endsWith("powerpoint"))
451 return published ? images.presentationShared() : images.presentation();
452 else if (mimetype.startsWith("application/zip") ||
453 mimetype.startsWith("application/gzip") ||
454 mimetype.startsWith("application/x-gzip") ||
455 mimetype.startsWith("application/x-tar") ||
456 mimetype.startsWith("application/x-gtar"))
457 return published ? images.zipShared() : images.zip();
458 else if (mimetype.startsWith("text/html"))
459 return published ? images.htmlShared() : images.html();
460 else if (mimetype.startsWith("text/plain"))
461 return published ? images.txtShared() : images.txt();
462 else if (mimetype.startsWith("image/"))
463 return published ? images.imageShared() : images.image();
464 else if (mimetype.startsWith("video/"))
465 return published ? images.videoShared() : images.video();
466 else if (mimetype.startsWith("audio/"))
467 return published ? images.audioShared() : images.audio();
468 return published ? images.documentShared() : images.document();
472 * Fill the file cache with data.
474 public void setFiles(final List<File> _files) {
475 if (!app.getSelectedTree().equals(app.mysharedTreeView)) {
476 if (celltable.getColumnIndex(pathColumn) != -1)
477 celltable.removeColumn(pathColumn);
480 if (celltable.getColumnIndex(pathColumn) == -1)
481 celltable.insertColumn(2, pathColumn, pathHeader);
483 files = new ArrayList<File>();
484 for (File fres : _files) {
488 folderFileCount = files.size();
490 for (SortableHeader header : allHeaders) {
491 if (header.isSorted())
492 sortFiles(header.getProperty(), header.getReverseSort());
495 List<File> previousSelection = getSelectedFiles(); //Keep the previous selection
497 provider.getList().clear();
498 provider.setList(files);
499 selectionModel.clear();
500 for (File f : files) {
501 if (previousSelection.contains(f))
502 selectionModel.setSelected(f, true);
505 app.showFolderStatistics(folderFileCount);
506 celltable.setPageSize(folderFileCount);
510 * Does the list contains the requested filename
515 public boolean contains(String fileName) {
516 for (int i = 0; i < files.size(); i++)
517 if (files.get(i).getName().equals(fileName))
522 public void clearSelectedRows() {
523 Iterator<File> it = selectionModel.getSelectedSet().iterator();
525 selectionModel.setSelected(it.next(),false);
532 public void selectAllRows() {
533 Iterator<File> it = provider.getList().iterator();
535 selectionModel.setSelected(it.next(),true);
539 protected void sortFiles(final String sortingProperty, final boolean sortingType){
540 Collections.sort(files, new Comparator<File>() {
543 public int compare(File arg0, File arg1) {
545 if (sortingProperty.equals("version")) {
546 return arg0.getVersion() - arg1.getVersion();
547 } else if (sortingProperty.equals("owner")) {
548 return arg0.getOwnerID().compareTo(arg1.getOwnerID());
549 } else if (sortingProperty.equals("date")) {
550 if (arg0.getLastModified() != null && arg1.getLastModified() != null)
551 return arg0.getLastModified().compareTo(arg1.getLastModified());
553 } else if (sortingProperty.equals("size")) {
554 return (int) (arg0.getBytes() - arg1.getBytes());
555 } else if (sortingProperty.equals("name")) {
556 return arg0.getName().compareTo(arg1.getName());
557 } else if (sortingProperty.equals("path")) {
558 return arg0.getUri().compareTo(arg1.getUri());
560 return arg0.getName().compareTo(arg1.getName());
563 else if (sortingProperty.equals("version")) {
565 return arg1.getVersion() - arg0.getVersion();
566 } else if (sortingProperty.equals("owner")) {
568 return arg1.getOwnerID().compareTo(arg0.getOwnerID());
569 } else if (sortingProperty.equals("date")) {
571 return arg1.getLastModified().compareTo(arg0.getLastModified());
572 } else if (sortingProperty.equals("size")) {
573 return (int) (arg1.getBytes() - arg0.getBytes());
574 } else if (sortingProperty.equals("name")) {
576 return arg1.getName().compareTo(arg0.getName());
577 } else if (sortingProperty.equals("path")) {
579 return arg1.getUri().compareTo(arg0.getUri());
582 return arg1.getName().compareTo(arg0.getName());
589 final class FileValueUpdater implements ValueUpdater<String>{
590 private SortableHeader header;
594 public FileValueUpdater(SortableHeader header) {
598 public void update(String value) {
599 header.setSorted(true);
600 header.toggleReverseSort();
602 for (SortableHeader otherHeader : allHeaders) {
603 if (otherHeader != header) {
604 otherHeader.setSorted(false);
605 otherHeader.setReverseSort(true);
608 celltable.redrawHeaders();
609 sortFiles(header.getProperty(), header.getReverseSort());
610 FileList.this.update();
616 * Shows the files in the cellTable
618 private void showCellTable(){
619 provider.setList(files);
623 //celltable.redraw();
624 celltable.redrawHeaders();
627 void iterateFilesHeadCommand(final Iterator<File> iter, final Command callback) {
628 if (iter.hasNext()) {
629 File f = iter.next();
630 app.scheduleFileHeadCommand(f, new Command() {
633 public void execute() {
634 iterateFilesHeadCommand(iter, callback);
638 else if (callback != null)
642 public void selectByUrl(List<String> selectedUrls) {
643 Set<File> previous = selectionModel.getSelectedSet();
644 for (File f : previous)
645 selectionModel.setSelected(f, false);
648 boolean scrolled = false;
649 for (File f : files) {
650 if (selectedUrls.contains(Pithos.getStorageAPIURL() + f.getOwnerID() + f.getUri())) {
651 selectionModel.setSelected(f, true);
653 celltable.getRowElement(i).getCells().getItem(0).scrollIntoView();