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 protected final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
229 SortableHeader nameHeader;
231 FolderTreeView treeView;
233 protected Pithos app;
236 * Construct the file list widget. This entails setting up the widget
237 * layout, fetching the number of files in the current folder from the
238 * server and filling the local file cache of displayed files with data from
239 * the server, as well.
243 public FileList(final Pithos _app, Images _images, FolderTreeView _treeView) {
246 this.treeView = _treeView;
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(app.getApiPath() + object.getOwner() + 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 Column<File,String> aColumn = new Column<File,String>(new TextCell()) {
308 public String getValue(File object) {
309 return object.getSizeAsString();
312 SortableHeader aheader = new SortableHeader("Size", "size");
313 celltable.addColumn(aColumn, aheader);
314 allHeaders.add(aheader);
315 aheader.setUpdater(new FileValueUpdater(aheader));
317 aColumn = new Column<File,String>(new TextCell()) {
319 public String getValue(File object) {
320 return object.getLastModified() != null ? formatter.format(object.getLastModified()) : "";
323 aheader = new SortableHeader("Last Modified", "date");
324 celltable.addColumn(aColumn, aheader);
325 allHeaders.add(aheader);
326 aheader.setUpdater(new FileValueUpdater(aheader));
328 provider.addDataDisplay(celltable);
330 VerticalPanel vp = new VerticalPanel();
332 vp.addStyleName("pithos-FileListContainer");
336 vp.setCellWidth(celltable, "100%");
337 vp.addHandler(new ContextMenuHandler() {
339 public void onContextMenu(final ContextMenuEvent event) {
340 final TreeView tree = app.getSelectedTree();
341 if (tree != null && (tree.equals(app.getFolderTreeView()) || tree.equals(app.getOtherSharedTreeView()))) {
342 final int x = event.getNativeEvent().getClientX();
343 final int y = event.getNativeEvent().getClientY();
344 final Folder selectedFolder = app.getSelection();
345 app.scheduleFolderHeadCommand(selectedFolder, new Command() {
348 public void execute() {
349 final List<File> selectedFiles = getSelectedFiles();
350 Iterator<File> iter = selectedFiles.iterator();
351 iterateFilesHeadCommand(iter, new Command() {
354 public void execute() {
355 FileContextMenu contextMenu = new FileContextMenu(app, images, tree, selectedFolder, selectedFiles);
356 contextMenu.setPopupPosition(x, y);
364 }, ContextMenuEvent.getType());
367 selectionModel = new MultiSelectionModel<File>(keyProvider);
368 selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
371 public void onSelectionChange(SelectionChangeEvent event) {
372 app.showRelevantToolbarButtons();
376 celltable.setSelectionModel(selectionModel, PithosSelectionEventManager.<File> createDefaultManager());
377 // celltable.setPageSize(Pithos.VISIBLE_FILE_COUNT);
379 sinkEvents(Event.ONCONTEXTMENU);
380 // sinkEvents(Event.ONMOUSEUP);
381 // sinkEvents(Event.ONMOUSEDOWN);
382 // sinkEvents(Event.ONCLICK);
383 // sinkEvents(Event.ONKEYDOWN);
384 sinkEvents(Event.ONDBLCLICK);
385 Pithos.preventIESelection();
388 public List<File> getSelectedFiles() {
389 return new ArrayList<File>(selectionModel.getSelectedSet());
393 public void onBrowserEvent(Event event) {
395 // if (files == null || files.size() == 0) {
396 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
397 // contextMenu = new FileContextMenu(images, false, true);
398 // contextMenu.show();
399 // event.preventDefault();
400 // event.cancelBubble(true);
404 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() != 0) {
405 // GWT.log("*****GOING TO SHOW CONTEXT MENU ****", null);
406 // contextMenu = new FileContextMenu(images, false, false);
407 // contextMenu = contextMenu.onEvent(event);
408 // event.cancelBubble(true);
409 // event.preventDefault();
410 // } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
411 // contextMenu = new FileContextMenu(images, false, true);
412 // contextMenu = contextMenu.onEmptyEvent(event);
413 // event.cancelBubble(true);
414 // event.preventDefault();
416 if (DOM.eventGetType(event) == Event.ONDBLCLICK)
417 if (getSelectedFiles().size() == 1) {
418 File file = getSelectedFiles().get(0);
419 Window.open(app.getApiPath() + file.getOwner() + file.getUri(), "_blank", "");
420 event.preventDefault();
423 super.onBrowserEvent(event);
427 * Update the display of the file list.
434 * Return the proper icon based on the MIME type of the file.
439 protected ImageResource getFileIcon(File file) {
440 String mimetype = file.getContentType();
441 boolean published = file.isPublished();
442 if (mimetype == null)
443 return published ? images.documentShared() : images.document();
444 mimetype = mimetype.toLowerCase();
445 if (mimetype.startsWith("application/pdf"))
446 return published ? images.pdfShared() : images.pdf();
447 else if (mimetype.endsWith("excel"))
448 return published ? images.spreadsheetShared() : images.spreadsheet();
449 else if (mimetype.endsWith("msword"))
450 return published ? images.wordprocessorShared() : images.wordprocessor();
451 else if (mimetype.endsWith("powerpoint"))
452 return published ? images.presentationShared() : images.presentation();
453 else if (mimetype.startsWith("application/zip") ||
454 mimetype.startsWith("application/gzip") ||
455 mimetype.startsWith("application/x-gzip") ||
456 mimetype.startsWith("application/x-tar") ||
457 mimetype.startsWith("application/x-gtar"))
458 return published ? images.zipShared() : images.zip();
459 else if (mimetype.startsWith("text/html"))
460 return published ? images.htmlShared() : images.html();
461 else if (mimetype.startsWith("text/plain"))
462 return published ? images.txtShared() : images.txt();
463 else if (mimetype.startsWith("image/"))
464 return published ? images.imageShared() : images.image();
465 else if (mimetype.startsWith("video/"))
466 return published ? images.videoShared() : images.video();
467 else if (mimetype.startsWith("audio/"))
468 return published ? images.audioShared() : images.audio();
469 return published ? images.documentShared() : images.document();
473 * Fill the file cache with data.
475 public void setFiles(final List<File> _files) {
476 files = new ArrayList<File>();
477 for (File fres : _files) {
481 folderFileCount = files.size();
483 for (SortableHeader header : allHeaders) {
484 if (header.isSorted())
485 sortFiles(header.getProperty(), header.getReverseSort());
488 List<File> previousSelection = getSelectedFiles(); //Keep the previous selection
490 provider.getList().clear();
491 provider.setList(files);
492 selectionModel.clear();
493 for (File f : files) {
494 if (previousSelection.contains(f))
495 selectionModel.setSelected(f, true);
498 app.showFolderStatistics(folderFileCount);
499 celltable.setPageSize(folderFileCount);
503 * Does the list contains the requested filename
508 public boolean contains(String fileName) {
509 for (int i = 0; i < files.size(); i++)
510 if (files.get(i).getName().equals(fileName))
515 public void clearSelectedRows() {
516 Iterator<File> it = selectionModel.getSelectedSet().iterator();
518 selectionModel.setSelected(it.next(),false);
525 public void selectAllRows() {
526 Iterator<File> it = provider.getList().iterator();
528 selectionModel.setSelected(it.next(),true);
532 protected void sortFiles(final String sortingProperty, final boolean sortingType){
533 Collections.sort(files, new Comparator<File>() {
536 public int compare(File arg0, File arg1) {
538 if (sortingProperty.equals("version")) {
539 return arg0.getVersion() - arg1.getVersion();
540 } else if (sortingProperty.equals("owner")) {
541 return arg0.getOwner().compareTo(arg1.getOwner());
542 } else if (sortingProperty.equals("date")) {
543 if (arg0.getLastModified() != null && arg1.getLastModified() != null)
544 return arg0.getLastModified().compareTo(arg1.getLastModified());
546 } else if (sortingProperty.equals("size")) {
547 return (int) (arg0.getBytes() - arg1.getBytes());
548 } else if (sortingProperty.equals("name")) {
549 return arg0.getName().compareTo(arg1.getName());
550 } else if (sortingProperty.equals("path")) {
551 return arg0.getUri().compareTo(arg1.getUri());
553 return arg0.getName().compareTo(arg1.getName());
556 else if (sortingProperty.equals("version")) {
558 return arg1.getVersion() - arg0.getVersion();
559 } else if (sortingProperty.equals("owner")) {
561 return arg1.getOwner().compareTo(arg0.getOwner());
562 } else if (sortingProperty.equals("date")) {
564 return arg1.getLastModified().compareTo(arg0.getLastModified());
565 } else if (sortingProperty.equals("size")) {
566 return (int) (arg1.getBytes() - arg0.getBytes());
567 } else if (sortingProperty.equals("name")) {
569 return arg1.getName().compareTo(arg0.getName());
570 } else if (sortingProperty.equals("path")) {
572 return arg1.getUri().compareTo(arg0.getUri());
575 return arg1.getName().compareTo(arg0.getName());
582 final class FileValueUpdater implements ValueUpdater<String>{
583 private SortableHeader header;
587 public FileValueUpdater(SortableHeader header) {
591 public void update(String value) {
592 header.setSorted(true);
593 header.toggleReverseSort();
595 for (SortableHeader otherHeader : allHeaders) {
596 if (otherHeader != header) {
597 otherHeader.setSorted(false);
598 otherHeader.setReverseSort(true);
601 celltable.redrawHeaders();
602 sortFiles(header.getProperty(), header.getReverseSort());
603 FileList.this.update();
609 * Shows the files in the cellTable
611 private void showCellTable(){
612 provider.setList(files);
616 //celltable.redraw();
617 celltable.redrawHeaders();
620 void iterateFilesHeadCommand(final Iterator<File> iter, final Command callback) {
621 if (iter.hasNext()) {
622 File f = iter.next();
623 app.scheduleFileHeadCommand(f, new Command() {
626 public void execute() {
627 iterateFilesHeadCommand(iter, callback);
631 else if (callback != null)