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;
48 import com.google.gwt.cell.client.Cell.Context;
49 import com.google.gwt.cell.client.ImageResourceCell;
50 import com.google.gwt.cell.client.SafeHtmlCell;
51 import com.google.gwt.cell.client.TextCell;
52 import com.google.gwt.cell.client.ValueUpdater;
53 import com.google.gwt.core.client.GWT;
54 import com.google.gwt.event.dom.client.ContextMenuEvent;
55 import com.google.gwt.event.dom.client.ContextMenuHandler;
56 import com.google.gwt.http.client.URL;
57 import com.google.gwt.i18n.client.DateTimeFormat;
58 import com.google.gwt.resources.client.ImageResource;
59 import com.google.gwt.resources.client.ClientBundle.Source;
60 import com.google.gwt.resources.client.ImageResource.ImageOptions;
61 import com.google.gwt.resources.client.ImageResource.RepeatStyle;
62 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
63 import com.google.gwt.safehtml.shared.SafeHtml;
64 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
65 import com.google.gwt.user.cellview.client.CellTable;
66 import com.google.gwt.user.cellview.client.Column;
67 import com.google.gwt.user.client.Command;
68 import com.google.gwt.user.client.DOM;
69 import com.google.gwt.user.client.Event;
70 import com.google.gwt.user.client.Window;
71 import com.google.gwt.user.client.ui.Composite;
72 import com.google.gwt.user.client.ui.VerticalPanel;
73 import com.google.gwt.view.client.ListDataProvider;
74 import com.google.gwt.view.client.MultiSelectionModel;
75 import com.google.gwt.view.client.ProvidesKey;
76 import com.google.gwt.view.client.SelectionChangeEvent;
79 * A composite that displays the list of files in a particular folder.
81 public class FileList extends Composite {
83 ListDataProvider<File> provider = new ListDataProvider<File>();
86 * The styles applied to the table.
88 interface TableStyle extends CellTable.Style {
89 String cellTableFirstColumnShared();
92 interface TableResources extends CellTable.Resources {
94 @Source({CellTable.Style.DEFAULT_CSS, "PithosCellTable.css"})
95 TableStyle cellTableStyle();
98 @ImageOptions(repeatStyle = RepeatStyle.None)
99 ImageResource cellTableSharedIcon();
102 static interface Templates extends SafeHtmlTemplates {
103 Templates INSTANCE = GWT.create(Templates.class);
105 @Template("<div id='dragHelper' style='border:1px solid black; background-color:#ffffff; color:black; width:150px;z-index:100'></div>")
106 SafeHtml outerHelper();
108 @Template("<span id='{0}'>{0}</span>")
109 public SafeHtml filenameSpan(String filename);
111 @Template("<a href='{0}' title='{1}' rel='lytebox[mnf]' onclick='myLytebox.start(this, false, false); return false;'>(view)</a>")
112 public SafeHtml viewLink(String link, String title);
114 @Template("<table><tr><td rowspan='3'>{0}</td><td style='font-size:95%;' id='{1}'>{1}</td></tr><tr><td>{2}</td></tr></table>")
115 public SafeHtml rendelContactCell(String imageHtml, String name, String fileSize);
117 @Template("<span id='{0}' class='{1}'>{2}</span>")
118 public SafeHtml spanWithIdAndClass(String id, String cssClass, String content);
121 protected final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
124 * Specifies that the images available for this composite will be the ones
125 * available in FileContextMenu.
127 public interface Images extends FolderTreeView.Images {
129 @Source("gr/grnet/pithos/resources/blank.gif")
130 ImageResource blank();
132 @Source("gr/grnet/pithos/resources/asc.png")
135 @Source("gr/grnet/pithos/resources/desc.png")
136 ImageResource desc();
138 @Source("gr/grnet/pithos/resources/mimetypes/document_shared.png")
139 ImageResource documentShared();
141 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst.png")
142 ImageResource wordprocessor();
144 @Source("gr/grnet/pithos/resources/mimetypes/log.png")
145 ImageResource spreadsheet();
147 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr.png")
148 ImageResource presentation();
150 @Source("gr/grnet/pithos/resources/mimetypes/acroread.png")
153 @Source("gr/grnet/pithos/resources/mimetypes/image.png")
154 ImageResource image();
156 @Source("gr/grnet/pithos/resources/mimetypes/video2.png")
157 ImageResource video();
159 @Source("gr/grnet/pithos/resources/mimetypes/knotify.png")
160 ImageResource audio();
162 @Source("gr/grnet/pithos/resources/mimetypes/html.png")
163 ImageResource html();
165 @Source("gr/grnet/pithos/resources/mimetypes/txt.png")
168 @Source("gr/grnet/pithos/resources/mimetypes/ark2.png")
171 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst_shared.png")
172 ImageResource wordprocessorShared();
174 @Source("gr/grnet/pithos/resources/mimetypes/log_shared.png")
175 ImageResource spreadsheetShared();
177 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr_shared.png")
178 ImageResource presentationShared();
180 @Source("gr/grnet/pithos/resources/mimetypes/acroread_shared.png")
181 ImageResource pdfShared();
183 @Source("gr/grnet/pithos/resources/mimetypes/image_shared.png")
184 ImageResource imageShared();
186 @Source("gr/grnet/pithos/resources/mimetypes/video2_shared.png")
187 ImageResource videoShared();
189 @Source("gr/grnet/pithos/resources/mimetypes/knotify_shared.png")
190 ImageResource audioShared();
192 @Source("gr/grnet/pithos/resources/mimetypes/html_shared.png")
193 ImageResource htmlShared();
195 @Source("gr/grnet/pithos/resources/mimetypes/txt_shared.png")
196 ImageResource txtShared();
198 @Source("gr/grnet/pithos/resources/mimetypes/ark2_shared.png")
199 ImageResource zipShared();
204 * The number of files in this folder.
211 long folderTotalSize;
214 * A cache of the files in the list.
216 private List<File> files;
219 * The widget's image bundle.
221 protected final Images images;
223 protected CellTable<File> celltable;
225 private final MultiSelectionModel<File> selectionModel;
227 Column<File, String> pathColumn;
229 protected final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
231 SortableHeader nameHeader;
233 SortableHeader pathHeader;
235 protected Pithos app;
238 * Construct the file list widget. This entails setting up the widget
239 * layout, fetching the number of files in the current folder from the
240 * server and filling the local file cache of displayed files with data from
241 * the server, as well.
245 public FileList(final Pithos _app, Images _images) {
249 final CellTable.Resources resources = GWT.create(TableResources.class);
251 ProvidesKey<File> keyProvider = new ProvidesKey<File>(){
254 public Object getKey(File item) {
255 return item.getUri();
259 celltable = new CellTable<File>(10, resources, keyProvider);
260 celltable.setWidth("100%");
261 celltable.setStyleName("pithos-List");
263 Column<File, ImageResource> status = new Column<File, ImageResource>(new ImageResourceCell() {
265 public boolean handlesSelection() {
271 public ImageResource getValue(File entity) {
272 return getFileIcon(entity);
276 public String getCellStyleNames(Context context, File object) {
277 if (!object.getPermissions().isEmpty() && !object.isPublished())
278 return ((TableStyle) resources.cellTableStyle()).cellTableFirstColumnShared();
279 return super.getCellStyleNames(context, object);
282 celltable.addColumn(status,"");
284 final Column<File,SafeHtml> nameColumn = new Column<File,SafeHtml>(new SafeHtmlCell()) {
287 public SafeHtml getValue(File object) {
288 SafeHtmlBuilder sb = new SafeHtmlBuilder();
289 sb.append(Templates.INSTANCE.filenameSpan(object.getName()));
290 if (object.getContentType() != null && (object.getContentType().endsWith("png") || object.getContentType().endsWith("gif") || object.getContentType().endsWith("jpeg"))) {
291 sb.appendHtmlConstant(" ")
292 .append(Templates.INSTANCE.viewLink(app.getApiPath() + object.getOwner() + object.getUri(), object.getName()));
295 return sb.toSafeHtml();
299 celltable.addColumn(nameColumn, nameHeader = new SortableHeader("Name", "name"));
300 allHeaders.add(nameHeader);
301 nameHeader.setUpdater(new FileValueUpdater(nameHeader));
302 nameHeader.setSorted(true);
303 nameHeader.setReverseSort(true);
305 celltable.redrawHeaders();
307 pathColumn = new Column<File, String>(new TextCell()) {
310 public String getValue(File f) {
312 if (!app.getSelectedTree().equals(app.mysharedTreeView)) {
313 path = f.getParent().getPrefix();
314 if (path.length() == 0)
319 if (path.lastIndexOf("/") != -1)
320 path = path.substring(0, path.lastIndexOf("/"));
327 pathHeader = new SortableHeader("Path", "path");
328 celltable.addColumn(pathColumn, pathHeader);
329 allHeaders.add(pathHeader);
330 pathHeader.setUpdater(new FileValueUpdater(pathHeader));
332 Column<File,String> aColumn = new Column<File,String>(new TextCell()) {
334 public String getValue(File object) {
335 return object.getSizeAsString();
338 SortableHeader aheader = new SortableHeader("Size", "size");
339 celltable.addColumn(aColumn, aheader);
340 allHeaders.add(aheader);
341 aheader.setUpdater(new FileValueUpdater(aheader));
343 aColumn = new Column<File,String>(new TextCell()) {
345 public String getValue(File object) {
346 return object.getLastModified() != null ? formatter.format(object.getLastModified()) : "";
349 aheader = new SortableHeader("Last Modified", "date");
350 celltable.addColumn(aColumn, aheader);
351 allHeaders.add(aheader);
352 aheader.setUpdater(new FileValueUpdater(aheader));
354 provider.addDataDisplay(celltable);
356 VerticalPanel vp = new VerticalPanel();
358 vp.addStyleName("pithos-FileListContainer");
362 vp.setCellWidth(celltable, "100%");
363 vp.addHandler(new ContextMenuHandler() {
365 public void onContextMenu(final ContextMenuEvent event) {
366 final TreeView tree = app.getSelectedTree();
368 final int x = event.getNativeEvent().getClientX();
369 final int y = event.getNativeEvent().getClientY();
370 final Folder selectedFolder = app.getSelection();
371 app.scheduleFolderHeadCommand(selectedFolder, new Command() {
374 public void execute() {
375 final List<File> selectedFiles = getSelectedFiles();
376 Iterator<File> iter = selectedFiles.iterator();
377 iterateFilesHeadCommand(iter, new Command() {
380 public void execute() {
381 FileContextMenu contextMenu = new FileContextMenu(app, images, tree, selectedFolder, selectedFiles);
382 contextMenu.setPopupPosition(x, y);
390 }, ContextMenuEvent.getType());
393 selectionModel = new MultiSelectionModel<File>(keyProvider);
394 selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
397 public void onSelectionChange(SelectionChangeEvent event) {
398 app.showRelevantToolbarButtons();
402 celltable.setSelectionModel(selectionModel, PithosSelectionEventManager.<File> createDefaultManager());
403 // celltable.setPageSize(Pithos.VISIBLE_FILE_COUNT);
405 sinkEvents(Event.ONCONTEXTMENU);
406 // sinkEvents(Event.ONMOUSEUP);
407 // sinkEvents(Event.ONMOUSEDOWN);
408 // sinkEvents(Event.ONCLICK);
409 // sinkEvents(Event.ONKEYDOWN);
410 sinkEvents(Event.ONDBLCLICK);
411 Pithos.preventIESelection();
414 public List<File> getSelectedFiles() {
415 return new ArrayList<File>(selectionModel.getSelectedSet());
419 public void onBrowserEvent(Event event) {
421 // if (files == null || files.size() == 0) {
422 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
423 // contextMenu = new FileContextMenu(images, false, true);
424 // contextMenu.show();
425 // event.preventDefault();
426 // event.cancelBubble(true);
430 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() != 0) {
431 // GWT.log("*****GOING TO SHOW CONTEXT MENU ****", null);
432 // contextMenu = new FileContextMenu(images, false, false);
433 // contextMenu = contextMenu.onEvent(event);
434 // event.cancelBubble(true);
435 // event.preventDefault();
436 // } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
437 // contextMenu = new FileContextMenu(images, false, true);
438 // contextMenu = contextMenu.onEmptyEvent(event);
439 // event.cancelBubble(true);
440 // event.preventDefault();
442 if (DOM.eventGetType(event) == Event.ONDBLCLICK)
443 if (getSelectedFiles().size() == 1) {
444 File file = getSelectedFiles().get(0);
445 Window.open(app.getApiPath() + file.getOwner() + file.getUri(), "_blank", "");
446 event.preventDefault();
449 super.onBrowserEvent(event);
453 * Update the display of the file list.
460 * Return the proper icon based on the MIME type of the file.
465 protected ImageResource getFileIcon(File file) {
466 String mimetype = file.getContentType();
467 boolean published = file.isPublished();
468 if (mimetype == null)
469 return published ? images.documentShared() : images.document();
470 mimetype = mimetype.toLowerCase();
471 if (mimetype.startsWith("application/pdf"))
472 return published ? images.pdfShared() : images.pdf();
473 else if (mimetype.endsWith("excel"))
474 return published ? images.spreadsheetShared() : images.spreadsheet();
475 else if (mimetype.endsWith("msword"))
476 return published ? images.wordprocessorShared() : images.wordprocessor();
477 else if (mimetype.endsWith("powerpoint"))
478 return published ? images.presentationShared() : images.presentation();
479 else if (mimetype.startsWith("application/zip") ||
480 mimetype.startsWith("application/gzip") ||
481 mimetype.startsWith("application/x-gzip") ||
482 mimetype.startsWith("application/x-tar") ||
483 mimetype.startsWith("application/x-gtar"))
484 return published ? images.zipShared() : images.zip();
485 else if (mimetype.startsWith("text/html"))
486 return published ? images.htmlShared() : images.html();
487 else if (mimetype.startsWith("text/plain"))
488 return published ? images.txtShared() : images.txt();
489 else if (mimetype.startsWith("image/"))
490 return published ? images.imageShared() : images.image();
491 else if (mimetype.startsWith("video/"))
492 return published ? images.videoShared() : images.video();
493 else if (mimetype.startsWith("audio/"))
494 return published ? images.audioShared() : images.audio();
495 return published ? images.documentShared() : images.document();
499 * Fill the file cache with data.
501 public void setFiles(final List<File> _files) {
502 if (!app.getSelectedTree().equals(app.mysharedTreeView)) {
503 if (celltable.getColumnIndex(pathColumn) != -1)
504 celltable.removeColumn(pathColumn);
507 if (celltable.getColumnIndex(pathColumn) == -1)
508 celltable.insertColumn(2, pathColumn, pathHeader);
510 files = new ArrayList<File>();
511 for (File fres : _files) {
515 folderFileCount = files.size();
517 for (SortableHeader header : allHeaders) {
518 if (header.isSorted())
519 sortFiles(header.getProperty(), header.getReverseSort());
522 List<File> previousSelection = getSelectedFiles(); //Keep the previous selection
524 provider.getList().clear();
525 provider.setList(files);
526 selectionModel.clear();
527 for (File f : files) {
528 if (previousSelection.contains(f))
529 selectionModel.setSelected(f, true);
532 app.showFolderStatistics(folderFileCount);
533 celltable.setPageSize(folderFileCount);
537 * Does the list contains the requested filename
542 public boolean contains(String fileName) {
543 for (int i = 0; i < files.size(); i++)
544 if (files.get(i).getName().equals(fileName))
549 public void clearSelectedRows() {
550 Iterator<File> it = selectionModel.getSelectedSet().iterator();
552 selectionModel.setSelected(it.next(),false);
559 public void selectAllRows() {
560 Iterator<File> it = provider.getList().iterator();
562 selectionModel.setSelected(it.next(),true);
566 protected void sortFiles(final String sortingProperty, final boolean sortingType){
567 Collections.sort(files, new Comparator<File>() {
570 public int compare(File arg0, File arg1) {
572 if (sortingProperty.equals("version")) {
573 return arg0.getVersion() - arg1.getVersion();
574 } else if (sortingProperty.equals("owner")) {
575 return arg0.getOwner().compareTo(arg1.getOwner());
576 } else if (sortingProperty.equals("date")) {
577 if (arg0.getLastModified() != null && arg1.getLastModified() != null)
578 return arg0.getLastModified().compareTo(arg1.getLastModified());
580 } else if (sortingProperty.equals("size")) {
581 return (int) (arg0.getBytes() - arg1.getBytes());
582 } else if (sortingProperty.equals("name")) {
583 return arg0.getName().compareTo(arg1.getName());
584 } else if (sortingProperty.equals("path")) {
585 return arg0.getUri().compareTo(arg1.getUri());
587 return arg0.getName().compareTo(arg1.getName());
590 else if (sortingProperty.equals("version")) {
592 return arg1.getVersion() - arg0.getVersion();
593 } else if (sortingProperty.equals("owner")) {
595 return arg1.getOwner().compareTo(arg0.getOwner());
596 } else if (sortingProperty.equals("date")) {
598 return arg1.getLastModified().compareTo(arg0.getLastModified());
599 } else if (sortingProperty.equals("size")) {
600 return (int) (arg1.getBytes() - arg0.getBytes());
601 } else if (sortingProperty.equals("name")) {
603 return arg1.getName().compareTo(arg0.getName());
604 } else if (sortingProperty.equals("path")) {
606 return arg1.getUri().compareTo(arg0.getUri());
609 return arg1.getName().compareTo(arg0.getName());
616 final class FileValueUpdater implements ValueUpdater<String>{
617 private SortableHeader header;
621 public FileValueUpdater(SortableHeader header) {
625 public void update(String value) {
626 header.setSorted(true);
627 header.toggleReverseSort();
629 for (SortableHeader otherHeader : allHeaders) {
630 if (otherHeader != header) {
631 otherHeader.setSorted(false);
632 otherHeader.setReverseSort(true);
635 celltable.redrawHeaders();
636 sortFiles(header.getProperty(), header.getReverseSort());
637 FileList.this.update();
643 * Shows the files in the cellTable
645 private void showCellTable(){
646 provider.setList(files);
650 //celltable.redraw();
651 celltable.redrawHeaders();
654 void iterateFilesHeadCommand(final Iterator<File> iter, final Command callback) {
655 if (iter.hasNext()) {
656 File f = iter.next();
657 app.scheduleFileHeadCommand(f, new Command() {
660 public void execute() {
661 iterateFilesHeadCommand(iter, callback);
665 else if (callback != null)