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());
405 sinkEvents(Event.ONCONTEXTMENU);
406 sinkEvents(Event.ONDBLCLICK);
407 Pithos.preventIESelection();
410 public List<File> getSelectedFiles() {
411 return new ArrayList<File>(selectionModel.getSelectedSet());
415 public void onBrowserEvent(Event event) {
417 if (DOM.eventGetType(event) == Event.ONDBLCLICK)
418 if (getSelectedFiles().size() == 1) {
419 File file = getSelectedFiles().get(0);
420 Window.open(app.getApiPath() + file.getOwner() + file.getUri(), "_blank", "");
421 event.preventDefault();
424 super.onBrowserEvent(event);
428 * Update the display of the file list.
435 * Return the proper icon based on the MIME type of the file.
440 protected ImageResource getFileIcon(File file) {
441 String mimetype = file.getContentType();
442 boolean published = file.isPublished();
443 if (mimetype == null)
444 return published ? images.documentShared() : images.document();
445 mimetype = mimetype.toLowerCase();
446 if (mimetype.startsWith("application/pdf"))
447 return published ? images.pdfShared() : images.pdf();
448 else if (mimetype.endsWith("excel"))
449 return published ? images.spreadsheetShared() : images.spreadsheet();
450 else if (mimetype.endsWith("msword"))
451 return published ? images.wordprocessorShared() : images.wordprocessor();
452 else if (mimetype.endsWith("powerpoint"))
453 return published ? images.presentationShared() : images.presentation();
454 else if (mimetype.startsWith("application/zip") ||
455 mimetype.startsWith("application/gzip") ||
456 mimetype.startsWith("application/x-gzip") ||
457 mimetype.startsWith("application/x-tar") ||
458 mimetype.startsWith("application/x-gtar"))
459 return published ? images.zipShared() : images.zip();
460 else if (mimetype.startsWith("text/html"))
461 return published ? images.htmlShared() : images.html();
462 else if (mimetype.startsWith("text/plain"))
463 return published ? images.txtShared() : images.txt();
464 else if (mimetype.startsWith("image/"))
465 return published ? images.imageShared() : images.image();
466 else if (mimetype.startsWith("video/"))
467 return published ? images.videoShared() : images.video();
468 else if (mimetype.startsWith("audio/"))
469 return published ? images.audioShared() : images.audio();
470 return published ? images.documentShared() : images.document();
474 * Fill the file cache with data.
476 public void setFiles(final List<File> _files) {
477 if (!app.getSelectedTree().equals(app.mysharedTreeView)) {
478 if (celltable.getColumnIndex(pathColumn) != -1)
479 celltable.removeColumn(pathColumn);
482 if (celltable.getColumnIndex(pathColumn) == -1)
483 celltable.insertColumn(2, pathColumn, pathHeader);
485 files = new ArrayList<File>();
486 for (File fres : _files) {
490 folderFileCount = files.size();
492 for (SortableHeader header : allHeaders) {
493 if (header.isSorted())
494 sortFiles(header.getProperty(), header.getReverseSort());
497 List<File> previousSelection = getSelectedFiles(); //Keep the previous selection
499 provider.getList().clear();
500 provider.setList(files);
501 selectionModel.clear();
502 for (File f : files) {
503 if (previousSelection.contains(f))
504 selectionModel.setSelected(f, true);
507 app.showFolderStatistics(folderFileCount);
508 celltable.setPageSize(folderFileCount);
512 * Does the list contains the requested filename
517 public boolean contains(String fileName) {
518 for (int i = 0; i < files.size(); i++)
519 if (files.get(i).getName().equals(fileName))
524 public void clearSelectedRows() {
525 Iterator<File> it = selectionModel.getSelectedSet().iterator();
527 selectionModel.setSelected(it.next(),false);
534 public void selectAllRows() {
535 Iterator<File> it = provider.getList().iterator();
537 selectionModel.setSelected(it.next(),true);
541 protected void sortFiles(final String sortingProperty, final boolean sortingType){
542 Collections.sort(files, new Comparator<File>() {
545 public int compare(File arg0, File arg1) {
547 if (sortingProperty.equals("version")) {
548 return arg0.getVersion() - arg1.getVersion();
549 } else if (sortingProperty.equals("owner")) {
550 return arg0.getOwner().compareTo(arg1.getOwner());
551 } else if (sortingProperty.equals("date")) {
552 if (arg0.getLastModified() != null && arg1.getLastModified() != null)
553 return arg0.getLastModified().compareTo(arg1.getLastModified());
555 } else if (sortingProperty.equals("size")) {
556 return (int) (arg0.getBytes() - arg1.getBytes());
557 } else if (sortingProperty.equals("name")) {
558 return arg0.getName().compareTo(arg1.getName());
559 } else if (sortingProperty.equals("path")) {
560 return arg0.getUri().compareTo(arg1.getUri());
562 return arg0.getName().compareTo(arg1.getName());
565 else if (sortingProperty.equals("version")) {
567 return arg1.getVersion() - arg0.getVersion();
568 } else if (sortingProperty.equals("owner")) {
570 return arg1.getOwner().compareTo(arg0.getOwner());
571 } else if (sortingProperty.equals("date")) {
573 return arg1.getLastModified().compareTo(arg0.getLastModified());
574 } else if (sortingProperty.equals("size")) {
575 return (int) (arg1.getBytes() - arg0.getBytes());
576 } else if (sortingProperty.equals("name")) {
578 return arg1.getName().compareTo(arg0.getName());
579 } else if (sortingProperty.equals("path")) {
581 return arg1.getUri().compareTo(arg0.getUri());
584 return arg1.getName().compareTo(arg0.getName());
591 final class FileValueUpdater implements ValueUpdater<String>{
592 private SortableHeader header;
596 public FileValueUpdater(SortableHeader header) {
600 public void update(String value) {
601 header.setSorted(true);
602 header.toggleReverseSort();
604 for (SortableHeader otherHeader : allHeaders) {
605 if (otherHeader != header) {
606 otherHeader.setSorted(false);
607 otherHeader.setReverseSort(true);
610 celltable.redrawHeaders();
611 sortFiles(header.getProperty(), header.getReverseSort());
612 FileList.this.update();
618 * Shows the files in the cellTable
620 private void showCellTable(){
621 provider.setList(files);
625 //celltable.redraw();
626 celltable.redrawHeaders();
629 void iterateFilesHeadCommand(final Iterator<File> iter, final Command callback) {
630 if (iter.hasNext()) {
631 File f = iter.next();
632 app.scheduleFileHeadCommand(f, new Command() {
635 public void execute() {
636 iterateFilesHeadCommand(iter, callback);
640 else if (callback != null)
644 public void selectByUrl(List<String> selectedUrls) {
645 Set<File> previous = selectionModel.getSelectedSet();
646 for (File f : previous)
647 selectionModel.setSelected(f, false);
650 boolean scrolled = false;
651 for (File f : files) {
652 if (selectedUrls.contains(app.getApiPath() + f.getOwner() + f.getUri())) {
653 selectionModel.setSelected(f, true);
655 celltable.getRowElement(i).getCells().getItem(0).scrollIntoView();