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 com.google.gwt.cell.client.Cell.Context;
39 import com.google.gwt.cell.client.ImageResourceCell;
40 import com.google.gwt.cell.client.SafeHtmlCell;
41 import com.google.gwt.cell.client.TextCell;
42 import com.google.gwt.cell.client.ValueUpdater;
43 import com.google.gwt.core.client.GWT;
44 import com.google.gwt.event.dom.client.ContextMenuEvent;
45 import com.google.gwt.event.dom.client.ContextMenuHandler;
46 import com.google.gwt.i18n.client.DateTimeFormat;
47 import com.google.gwt.resources.client.ImageResource;
48 import com.google.gwt.resources.client.ImageResource.ImageOptions;
49 import com.google.gwt.resources.client.ImageResource.RepeatStyle;
50 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
51 import com.google.gwt.safehtml.shared.SafeHtml;
52 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
53 import com.google.gwt.user.cellview.client.CellTable;
54 import com.google.gwt.user.cellview.client.Column;
55 import com.google.gwt.user.client.Command;
56 import com.google.gwt.user.client.DOM;
57 import com.google.gwt.user.client.Event;
58 import com.google.gwt.user.client.Window;
59 import com.google.gwt.user.client.ui.Composite;
60 import com.google.gwt.user.client.ui.VerticalPanel;
61 import com.google.gwt.view.client.ListDataProvider;
62 import com.google.gwt.view.client.MultiSelectionModel;
63 import com.google.gwt.view.client.ProvidesKey;
64 import com.google.gwt.view.client.SelectionChangeEvent;
65 import gr.grnet.pithos.web.client.foldertree.File;
66 import gr.grnet.pithos.web.client.foldertree.Folder;
67 import gr.grnet.pithos.web.client.foldertree.FolderTreeView;
72 * A composite that displays the list of files in a particular folder.
74 public class FileList extends Composite {
76 ListDataProvider<File> provider = new ListDataProvider<File>();
79 * The styles applied to the table.
81 interface TableStyle extends CellTable.Style {
82 String cellTableFirstColumnShared();
85 interface TableResources extends CellTable.Resources {
87 @Source({CellTable.Style.DEFAULT_CSS, "PithosCellTable.css"})
88 TableStyle cellTableStyle();
91 @ImageOptions(repeatStyle = RepeatStyle.None)
92 ImageResource cellTableSharedIcon();
95 static interface Templates extends SafeHtmlTemplates {
96 Templates INSTANCE = GWT.create(Templates.class);
98 @Template("<div id='dragHelper' style='border:1px solid black; background-color:#ffffff; color:black; width:150px;z-index:100'></div>")
99 SafeHtml outerHelper();
101 @Template("<span id='{0}'>{0}</span>")
102 public SafeHtml filenameSpan(String filename);
104 @Template("<a href='{0}' title='{1}' rel='lytebox[mnf]' onclick='myLytebox.start(this, false, false); return false;'>(view)</a>")
105 public SafeHtml viewLink(String link, String title);
107 @Template("<table><tr><td rowspan='3'>{0}</td><td style='font-size:95%;' id='{1}'>{1}</td></tr><tr><td>{2}</td></tr></table>")
108 public SafeHtml rendelContactCell(String imageHtml, String name, String fileSize);
110 @Template("<span id='{0}' class='{1}'>{2}</span>")
111 public SafeHtml spanWithIdAndClass(String id, String cssClass, String content);
114 protected final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
117 * Specifies that the images available for this composite will be the ones
118 * available in FileContextMenu.
120 public interface Images extends FolderTreeView.Images {
122 @Source("gr/grnet/pithos/resources/blank.gif")
123 ImageResource blank();
125 @Source("gr/grnet/pithos/resources/asc.png")
128 @Source("gr/grnet/pithos/resources/desc.png")
129 ImageResource desc();
131 @Source("gr/grnet/pithos/resources/mimetypes/document_shared.png")
132 ImageResource documentShared();
134 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst.png")
135 ImageResource wordprocessor();
137 @Source("gr/grnet/pithos/resources/mimetypes/log.png")
138 ImageResource spreadsheet();
140 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr.png")
141 ImageResource presentation();
143 @Source("gr/grnet/pithos/resources/mimetypes/acroread.png")
146 @Source("gr/grnet/pithos/resources/mimetypes/image.png")
147 ImageResource image();
149 @Source("gr/grnet/pithos/resources/mimetypes/video2.png")
150 ImageResource video();
152 @Source("gr/grnet/pithos/resources/mimetypes/knotify.png")
153 ImageResource audio();
155 @Source("gr/grnet/pithos/resources/mimetypes/html.png")
156 ImageResource html();
158 @Source("gr/grnet/pithos/resources/mimetypes/txt.png")
161 @Source("gr/grnet/pithos/resources/mimetypes/ark2.png")
164 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst_shared.png")
165 ImageResource wordprocessorShared();
167 @Source("gr/grnet/pithos/resources/mimetypes/log_shared.png")
168 ImageResource spreadsheetShared();
170 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr_shared.png")
171 ImageResource presentationShared();
173 @Source("gr/grnet/pithos/resources/mimetypes/acroread_shared.png")
174 ImageResource pdfShared();
176 @Source("gr/grnet/pithos/resources/mimetypes/image_shared.png")
177 ImageResource imageShared();
179 @Source("gr/grnet/pithos/resources/mimetypes/video2_shared.png")
180 ImageResource videoShared();
182 @Source("gr/grnet/pithos/resources/mimetypes/knotify_shared.png")
183 ImageResource audioShared();
185 @Source("gr/grnet/pithos/resources/mimetypes/html_shared.png")
186 ImageResource htmlShared();
188 @Source("gr/grnet/pithos/resources/mimetypes/txt_shared.png")
189 ImageResource txtShared();
191 @Source("gr/grnet/pithos/resources/mimetypes/ark2_shared.png")
192 ImageResource zipShared();
197 * The number of files in this folder.
204 long folderTotalSize;
207 * A cache of the files in the list.
209 private List<File> files;
212 * The widget's image bundle.
214 protected final Images images;
216 protected CellTable<File> celltable;
218 private final MultiSelectionModel<File> selectionModel;
220 Column<File, String> pathColumn;
222 protected final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
224 SortableHeader nameHeader;
226 SortableHeader pathHeader;
228 protected Pithos app;
231 * Construct the file list widget. This entails setting up the widget
232 * layout, fetching the number of files in the current folder from the
233 * server and filling the local file cache of displayed files with data from
234 * the server, as well.
238 public FileList(final Pithos _app, Images _images) {
242 final CellTable.Resources resources = GWT.create(TableResources.class);
244 ProvidesKey<File> keyProvider = new ProvidesKey<File>(){
247 public Object getKey(File item) {
248 return item.getUri();
252 celltable = new CellTable<File>(10, resources, keyProvider);
253 celltable.setWidth("100%");
254 celltable.setStyleName("pithos-List");
256 Column<File, ImageResource> status = new Column<File, ImageResource>(new ImageResourceCell() {
258 public boolean handlesSelection() {
264 public ImageResource getValue(File entity) {
265 return getFileIcon(entity);
269 public String getCellStyleNames(Context context, File object) {
270 if (!object.getPermissions().isEmpty() && !object.isPublished())
271 return ((TableStyle) resources.cellTableStyle()).cellTableFirstColumnShared();
272 return super.getCellStyleNames(context, object);
275 celltable.addColumn(status,"");
277 final Column<File,SafeHtml> nameColumn = new Column<File,SafeHtml>(new SafeHtmlCell()) {
280 public SafeHtml getValue(File object) {
281 SafeHtmlBuilder sb = new SafeHtmlBuilder();
282 sb.append(Templates.INSTANCE.filenameSpan(object.getName()));
283 if (object.getContentType() != null && (object.getContentType().endsWith("png") || object.getContentType().endsWith("gif") || object.getContentType().endsWith("jpeg"))) {
284 sb.appendHtmlConstant(" ")
285 .append(Templates.INSTANCE.viewLink(Pithos.getStorageAPIURL() + object.getOwnerID() + object.getUri(), object.getName()));
288 return sb.toSafeHtml();
292 celltable.addColumn(nameColumn, nameHeader = new SortableHeader("Name", "name"));
293 allHeaders.add(nameHeader);
294 nameHeader.setUpdater(new FileValueUpdater(nameHeader));
295 nameHeader.setSorted(true);
296 nameHeader.setReverseSort(true);
298 celltable.redrawHeaders();
300 pathColumn = new Column<File, String>(new TextCell()) {
303 public String getValue(File f) {
305 if (!app.getSelectedTree().equals(app.mysharedTreeView)) {
306 path = f.getParent().getPrefix();
307 if (path.length() == 0)
312 if (path.lastIndexOf("/") != -1)
313 path = path.substring(0, path.lastIndexOf("/"));
320 pathHeader = new SortableHeader("Path", "path");
321 celltable.addColumn(pathColumn, pathHeader);
322 allHeaders.add(pathHeader);
323 pathHeader.setUpdater(new FileValueUpdater(pathHeader));
325 Column<File,String> aColumn = new Column<File,String>(new TextCell()) {
327 public String getValue(File object) {
328 return object.getSizeAsString();
331 SortableHeader aheader = new SortableHeader("Size", "size");
332 celltable.addColumn(aColumn, aheader);
333 allHeaders.add(aheader);
334 aheader.setUpdater(new FileValueUpdater(aheader));
336 aColumn = new Column<File,String>(new TextCell()) {
338 public String getValue(File object) {
339 return object.getLastModified() != null ? formatter.format(object.getLastModified()) : "";
342 aheader = new SortableHeader("Last Modified", "date");
343 celltable.addColumn(aColumn, aheader);
344 allHeaders.add(aheader);
345 aheader.setUpdater(new FileValueUpdater(aheader));
347 provider.addDataDisplay(celltable);
349 VerticalPanel vp = new VerticalPanel();
351 vp.addStyleName("pithos-FileListContainer");
355 vp.setCellWidth(celltable, "100%");
356 vp.addHandler(new ContextMenuHandler() {
358 public void onContextMenu(final ContextMenuEvent event) {
359 final TreeView tree = app.getSelectedTree();
361 final int x = event.getNativeEvent().getClientX();
362 final int y = event.getNativeEvent().getClientY();
363 final Folder selectedFolder = app.getSelection();
364 app.scheduleFolderHeadCommand(selectedFolder, new Command() {
367 public void execute() {
368 final List<File> selectedFiles = getSelectedFiles();
369 Iterator<File> iter = selectedFiles.iterator();
370 iterateFilesHeadCommand(iter, new Command() {
373 public void execute() {
374 FileContextMenu contextMenu = new FileContextMenu(app, images, tree, selectedFolder, selectedFiles);
375 contextMenu.setPopupPosition(x, y);
383 }, ContextMenuEvent.getType());
386 selectionModel = new MultiSelectionModel<File>(keyProvider);
387 selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
390 public void onSelectionChange(SelectionChangeEvent event) {
391 app.showRelevantToolbarButtons();
395 celltable.setSelectionModel(selectionModel, PithosSelectionEventManager.<File> createDefaultManager());
397 sinkEvents(Event.ONCONTEXTMENU);
398 sinkEvents(Event.ONDBLCLICK);
399 Pithos.preventIESelection();
402 public List<File> getSelectedFiles() {
403 return new ArrayList<File>(selectionModel.getSelectedSet());
407 public void onBrowserEvent(Event event) {
409 if (DOM.eventGetType(event) == Event.ONDBLCLICK)
410 if (getSelectedFiles().size() == 1) {
411 File file = getSelectedFiles().get(0);
412 Window.open(Pithos.getStorageViewURL() + file.getOwnerID() + file.getUri(), "_blank", "");
413 event.preventDefault();
416 super.onBrowserEvent(event);
420 * Update the display of the file list.
427 * Return the proper icon based on the MIME type of the file.
432 protected ImageResource getFileIcon(File file) {
433 String mimetype = file.getContentType();
434 boolean published = file.isPublished();
435 if (mimetype == null)
436 return published ? images.documentShared() : images.document();
437 mimetype = mimetype.toLowerCase();
438 if (mimetype.startsWith("application/pdf"))
439 return published ? images.pdfShared() : images.pdf();
440 else if (mimetype.endsWith("excel"))
441 return published ? images.spreadsheetShared() : images.spreadsheet();
442 else if (mimetype.endsWith("msword"))
443 return published ? images.wordprocessorShared() : images.wordprocessor();
444 else if (mimetype.endsWith("powerpoint"))
445 return published ? images.presentationShared() : images.presentation();
446 else if (mimetype.startsWith("application/zip") ||
447 mimetype.startsWith("application/gzip") ||
448 mimetype.startsWith("application/x-gzip") ||
449 mimetype.startsWith("application/x-tar") ||
450 mimetype.startsWith("application/x-gtar"))
451 return published ? images.zipShared() : images.zip();
452 else if (mimetype.startsWith("text/html"))
453 return published ? images.htmlShared() : images.html();
454 else if (mimetype.startsWith("text/plain"))
455 return published ? images.txtShared() : images.txt();
456 else if (mimetype.startsWith("image/"))
457 return published ? images.imageShared() : images.image();
458 else if (mimetype.startsWith("video/"))
459 return published ? images.videoShared() : images.video();
460 else if (mimetype.startsWith("audio/"))
461 return published ? images.audioShared() : images.audio();
462 return published ? images.documentShared() : images.document();
466 * Fill the file cache with data.
468 public void setFiles(final List<File> _files) {
469 if (!app.getSelectedTree().equals(app.mysharedTreeView)) {
470 if (celltable.getColumnIndex(pathColumn) != -1)
471 celltable.removeColumn(pathColumn);
474 if (celltable.getColumnIndex(pathColumn) == -1)
475 celltable.insertColumn(2, pathColumn, pathHeader);
477 files = new ArrayList<File>();
478 for (File fres : _files) {
482 folderFileCount = files.size();
484 for (SortableHeader header : allHeaders) {
485 if (header.isSorted())
486 sortFiles(header.getProperty(), header.getReverseSort());
489 List<File> previousSelection = getSelectedFiles(); //Keep the previous selection
491 provider.getList().clear();
492 provider.setList(files);
493 selectionModel.clear();
494 for (File f : files) {
495 if (previousSelection.contains(f))
496 selectionModel.setSelected(f, true);
499 app.showFolderStatistics(folderFileCount);
500 celltable.setPageSize(folderFileCount);
504 * Does the list contains the requested filename
509 public boolean contains(String fileName) {
510 for (int i = 0; i < files.size(); i++)
511 if (files.get(i).getName().equals(fileName))
516 public void clearSelectedRows() {
517 Iterator<File> it = selectionModel.getSelectedSet().iterator();
519 selectionModel.setSelected(it.next(),false);
526 public void selectAllRows() {
527 Iterator<File> it = provider.getList().iterator();
529 selectionModel.setSelected(it.next(),true);
533 protected void sortFiles(final String sortingProperty, final boolean sortingType){
534 Collections.sort(files, new Comparator<File>() {
537 public int compare(File arg0, File arg1) {
539 if (sortingProperty.equals("version")) {
540 return arg0.getVersion() - arg1.getVersion();
541 } else if (sortingProperty.equals("owner")) {
542 return arg0.getOwnerID().compareTo(arg1.getOwnerID());
543 } else if (sortingProperty.equals("date")) {
544 if (arg0.getLastModified() != null && arg1.getLastModified() != null)
545 return arg0.getLastModified().compareTo(arg1.getLastModified());
547 } else if (sortingProperty.equals("size")) {
548 return (int) (arg0.getBytes() - arg1.getBytes());
549 } else if (sortingProperty.equals("name")) {
550 return arg0.getName().compareTo(arg1.getName());
551 } else if (sortingProperty.equals("path")) {
552 return arg0.getUri().compareTo(arg1.getUri());
554 return arg0.getName().compareTo(arg1.getName());
557 else if (sortingProperty.equals("version")) {
559 return arg1.getVersion() - arg0.getVersion();
560 } else if (sortingProperty.equals("owner")) {
562 return arg1.getOwnerID().compareTo(arg0.getOwnerID());
563 } else if (sortingProperty.equals("date")) {
565 return arg1.getLastModified().compareTo(arg0.getLastModified());
566 } else if (sortingProperty.equals("size")) {
567 return (int) (arg1.getBytes() - arg0.getBytes());
568 } else if (sortingProperty.equals("name")) {
570 return arg1.getName().compareTo(arg0.getName());
571 } else if (sortingProperty.equals("path")) {
573 return arg1.getUri().compareTo(arg0.getUri());
576 return arg1.getName().compareTo(arg0.getName());
583 final class FileValueUpdater implements ValueUpdater<String>{
584 private SortableHeader header;
588 public FileValueUpdater(SortableHeader header) {
592 public void update(String value) {
593 header.setSorted(true);
594 header.toggleReverseSort();
596 for (SortableHeader otherHeader : allHeaders) {
597 if (otherHeader != header) {
598 otherHeader.setSorted(false);
599 otherHeader.setReverseSort(true);
602 celltable.redrawHeaders();
603 sortFiles(header.getProperty(), header.getReverseSort());
604 FileList.this.update();
610 * Shows the files in the cellTable
612 private void showCellTable(){
613 provider.setList(files);
617 //celltable.redraw();
618 celltable.redrawHeaders();
621 void iterateFilesHeadCommand(final Iterator<File> iter, final Command callback) {
622 if (iter.hasNext()) {
623 File f = iter.next();
624 app.scheduleFileHeadCommand(f, new Command() {
627 public void execute() {
628 iterateFilesHeadCommand(iter, callback);
632 else if (callback != null)
636 public void selectByUrl(List<String> selectedUrls) {
637 Set<File> previous = selectionModel.getSelectedSet();
638 for (File f : previous)
639 selectionModel.setSelected(f, false);
642 boolean scrolled = false;
643 for (File f : files) {
644 if (selectedUrls.contains(Pithos.getStorageAPIURL() + f.getOwnerID() + f.getUri())) {
645 selectionModel.setSelected(f, true);
647 celltable.getRowElement(i).getCells().getItem(0).scrollIntoView();