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.ImageResourceCell;
49 import com.google.gwt.cell.client.SafeHtmlCell;
50 import com.google.gwt.cell.client.TextCell;
51 import com.google.gwt.cell.client.ValueUpdater;
52 import com.google.gwt.core.client.GWT;
53 import com.google.gwt.event.dom.client.ContextMenuEvent;
54 import com.google.gwt.event.dom.client.ContextMenuHandler;
55 import com.google.gwt.i18n.client.DateTimeFormat;
56 import com.google.gwt.resources.client.ImageResource;
57 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
58 import com.google.gwt.safehtml.shared.SafeHtml;
59 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
60 import com.google.gwt.user.cellview.client.CellTable;
61 import com.google.gwt.user.cellview.client.Column;
62 import com.google.gwt.user.client.DOM;
63 import com.google.gwt.user.client.Event;
64 import com.google.gwt.user.client.Window;
65 import com.google.gwt.user.client.ui.Composite;
66 import com.google.gwt.user.client.ui.VerticalPanel;
67 import com.google.gwt.view.client.ListDataProvider;
68 import com.google.gwt.view.client.MultiSelectionModel;
69 import com.google.gwt.view.client.ProvidesKey;
70 import com.google.gwt.view.client.SelectionChangeEvent;
73 * A composite that displays the list of files in a particular folder.
75 public class FileList extends Composite {
77 ListDataProvider<File> provider = new ListDataProvider<File>();
80 * The styles applied to the table.
82 interface TableStyle extends CellTable.Style {
85 interface TableResources extends CellTable.Resources {
87 @Source({CellTable.Style.DEFAULT_CSS, "PithosCellTable.css"})
88 TableStyle cellTableStyle();
91 static interface Templates extends SafeHtmlTemplates {
92 Templates INSTANCE = GWT.create(Templates.class);
94 @Template("<div id='dragHelper' style='border:1px solid black; background-color:#ffffff; color:black; width:150px;z-index:100'></div>")
95 SafeHtml outerHelper();
97 @Template("<span id='{0}'>{0}</span>")
98 public SafeHtml filenameSpan(String filename);
100 @Template("<a href='{0}' title='{1}' rel='lytebox[mnf]' onclick='myLytebox.start(this, false, false); return false;'>(view)</a>")
101 public SafeHtml viewLink(String link, String title);
103 @Template("<table><tr><td rowspan='3'>{0}</td><td style='font-size:95%;' id='{1}'>{1}</td></tr><tr><td>{2}</td></tr></table>")
104 public SafeHtml rendelContactCell(String imageHtml, String name, String fileSize);
106 @Template("<span id='{0}' class='{1}'>{2}</span>")
107 public SafeHtml spanWithIdAndClass(String id, String cssClass, String content);
110 protected final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
113 * Specifies that the images available for this composite will be the ones
114 * available in FileContextMenu.
116 public interface Images extends FolderTreeView.Images {
118 @Source("gr/grnet/pithos/resources/blank.gif")
119 ImageResource blank();
121 @Source("gr/grnet/pithos/resources/asc.png")
124 @Source("gr/grnet/pithos/resources/desc.png")
125 ImageResource desc();
127 @Source("gr/grnet/pithos/resources/mimetypes/document_shared.png")
128 ImageResource documentShared();
130 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst.png")
131 ImageResource wordprocessor();
133 @Source("gr/grnet/pithos/resources/mimetypes/log.png")
134 ImageResource spreadsheet();
136 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr.png")
137 ImageResource presentation();
139 @Source("gr/grnet/pithos/resources/mimetypes/acroread.png")
142 @Source("gr/grnet/pithos/resources/mimetypes/image.png")
143 ImageResource image();
145 @Source("gr/grnet/pithos/resources/mimetypes/video2.png")
146 ImageResource video();
148 @Source("gr/grnet/pithos/resources/mimetypes/knotify.png")
149 ImageResource audio();
151 @Source("gr/grnet/pithos/resources/mimetypes/html.png")
152 ImageResource html();
154 @Source("gr/grnet/pithos/resources/mimetypes/txt.png")
157 @Source("gr/grnet/pithos/resources/mimetypes/ark2.png")
160 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst_shared.png")
161 ImageResource wordprocessorShared();
163 @Source("gr/grnet/pithos/resources/mimetypes/log_shared.png")
164 ImageResource spreadsheetShared();
166 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr_shared.png")
167 ImageResource presentationShared();
169 @Source("gr/grnet/pithos/resources/mimetypes/acroread_shared.png")
170 ImageResource pdfShared();
172 @Source("gr/grnet/pithos/resources/mimetypes/image_shared.png")
173 ImageResource imageShared();
175 @Source("gr/grnet/pithos/resources/mimetypes/video2_shared.png")
176 ImageResource videoShared();
178 @Source("gr/grnet/pithos/resources/mimetypes/knotify_shared.png")
179 ImageResource audioShared();
181 @Source("gr/grnet/pithos/resources/mimetypes/html_shared.png")
182 ImageResource htmlShared();
184 @Source("gr/grnet/pithos/resources/mimetypes/txt_shared.png")
185 ImageResource txtShared();
187 @Source("gr/grnet/pithos/resources/mimetypes/ark2_shared.png")
188 ImageResource zipShared();
193 * The number of files in this folder.
200 long folderTotalSize;
203 * A cache of the files in the list.
205 private List<File> files;
208 * The widget's image bundle.
210 protected final Images images;
212 protected CellTable<File> celltable;
214 private final MultiSelectionModel<File> selectionModel;
216 protected final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
218 SortableHeader nameHeader;
220 FolderTreeView treeView;
222 protected Pithos app;
225 * Construct the file list widget. This entails setting up the widget
226 * layout, fetching the number of files in the current folder from the
227 * server and filling the local file cache of displayed files with data from
228 * the server, as well.
232 public FileList(final Pithos _app, Images _images, FolderTreeView _treeView) {
235 this.treeView = _treeView;
237 CellTable.Resources resources = GWT.create(TableResources.class);
239 ProvidesKey<File> keyProvider = new ProvidesKey<File>(){
242 public Object getKey(File item) {
243 return item.getUri();
247 celltable = new CellTable<File>(10, resources, keyProvider);
248 celltable.setWidth("100%");
249 celltable.setStyleName("pithos-List");
251 Column<File, ImageResource> status = new Column<File, ImageResource>(new ImageResourceCell() {
253 public boolean handlesSelection() {
259 public ImageResource getValue(File entity) {
260 return getFileIcon(entity);
263 celltable.addColumn(status,"");
265 final Column<File,SafeHtml> nameColumn = new Column<File,SafeHtml>(new SafeHtmlCell()) {
268 public SafeHtml getValue(File object) {
269 SafeHtmlBuilder sb = new SafeHtmlBuilder();
270 sb.append(Templates.INSTANCE.filenameSpan(object.getName()));
271 if (object.getContentType().endsWith("png") || object.getContentType().endsWith("gif") || object.getContentType().endsWith("jpeg")) {
272 sb.appendHtmlConstant(" ")
273 .append(Templates.INSTANCE.viewLink(app.getApiPath() + object.getOwner() + object.getUri() + "?X-Auth-Token=" + app.getToken(), object.getName()));
276 return sb.toSafeHtml();
280 celltable.addColumn(nameColumn, nameHeader = new SortableHeader("Name"));
281 allHeaders.add(nameHeader);
282 nameHeader.setUpdater(new FileValueUpdater(nameHeader, "name"));
284 celltable.redrawHeaders();
286 Column<File,String> aColumn = new Column<File,String>(new TextCell()) {
288 public String getValue(File object) {
289 // TODO Auto-generated method stub
290 return object.getSizeAsString();
293 SortableHeader aheader = new SortableHeader("Size");
294 celltable.addColumn(aColumn, aheader);
295 allHeaders.add(aheader);
296 aheader.setUpdater(new FileValueUpdater(aheader, "size"));
298 aColumn = new Column<File,String>(new TextCell()) {
300 public String getValue(File object) {
301 return object.getLastModified() != null ? formatter.format(object.getLastModified()) : "";
304 aheader = new SortableHeader("Last Modified");
305 celltable.addColumn(aColumn, aheader);
306 allHeaders.add(aheader);
307 aheader.setUpdater(new FileValueUpdater(aheader, "date"));
309 provider.addDataDisplay(celltable);
311 VerticalPanel vp = new VerticalPanel();
313 vp.addStyleName("pithos-FileListContainer");
317 vp.setCellWidth(celltable, "100%");
318 vp.addHandler(new ContextMenuHandler() {
320 public void onContextMenu(ContextMenuEvent event) {
321 TreeView tree = app.getSelectedTree();
322 if (tree != null && (tree.equals(app.getFolderTreeView()) || tree.equals(app.getOtherSharedTreeView()))) {
323 Folder selectedFolder = app.getSelection();
324 FileContextMenu contextMenu = new FileContextMenu(app, images, tree, selectedFolder, getSelectedFiles());
325 int x = event.getNativeEvent().getClientX();
326 int y = event.getNativeEvent().getClientY();
327 contextMenu.setPopupPosition(x, y);
331 }, ContextMenuEvent.getType());
334 selectionModel = new MultiSelectionModel<File>(keyProvider);
335 selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
338 public void onSelectionChange(SelectionChangeEvent event) {
339 app.showRelevantToolbarButtons();
343 celltable.setSelectionModel(selectionModel, PithosSelectionEventManager.<File> createDefaultManager());
344 // celltable.setPageSize(Pithos.VISIBLE_FILE_COUNT);
346 sinkEvents(Event.ONCONTEXTMENU);
347 // sinkEvents(Event.ONMOUSEUP);
348 // sinkEvents(Event.ONMOUSEDOWN);
349 // sinkEvents(Event.ONCLICK);
350 // sinkEvents(Event.ONKEYDOWN);
351 sinkEvents(Event.ONDBLCLICK);
352 Pithos.preventIESelection();
355 public List<File> getSelectedFiles() {
356 return new ArrayList<File>(selectionModel.getSelectedSet());
360 public void onBrowserEvent(Event event) {
362 // if (files == null || files.size() == 0) {
363 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
364 // contextMenu = new FileContextMenu(images, false, true);
365 // contextMenu.show();
366 // event.preventDefault();
367 // event.cancelBubble(true);
371 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() != 0) {
372 // GWT.log("*****GOING TO SHOW CONTEXT MENU ****", null);
373 // contextMenu = new FileContextMenu(images, false, false);
374 // contextMenu = contextMenu.onEvent(event);
375 // event.cancelBubble(true);
376 // event.preventDefault();
377 // } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
378 // contextMenu = new FileContextMenu(images, false, true);
379 // contextMenu = contextMenu.onEmptyEvent(event);
380 // event.cancelBubble(true);
381 // event.preventDefault();
383 if (DOM.eventGetType(event) == Event.ONDBLCLICK)
384 if (getSelectedFiles().size() == 1) {
385 File file = getSelectedFiles().get(0);
386 Window.open(app.getApiPath() + file.getOwner() + file.getUri() + "?X-Auth-Token=" + app.getToken(), "_blank", "");
387 event.preventDefault();
390 super.onBrowserEvent(event);
394 * Update the display of the file list.
401 * Return the proper icon based on the MIME type of the file.
406 protected ImageResource getFileIcon(File file) {
407 String mimetype = file.getContentType();
408 boolean shared = file.isShared();
409 if (mimetype == null)
410 return shared ? images.documentShared() : images.document();
411 mimetype = mimetype.toLowerCase();
412 if (mimetype.startsWith("application/pdf"))
413 return shared ? images.pdfShared() : images.pdf();
414 else if (mimetype.endsWith("excel"))
415 return shared ? images.spreadsheetShared() : images.spreadsheet();
416 else if (mimetype.endsWith("msword"))
417 return shared ? images.wordprocessorShared() : images.wordprocessor();
418 else if (mimetype.endsWith("powerpoint"))
419 return shared ? images.presentationShared() : images.presentation();
420 else if (mimetype.startsWith("application/zip") ||
421 mimetype.startsWith("application/gzip") ||
422 mimetype.startsWith("application/x-gzip") ||
423 mimetype.startsWith("application/x-tar") ||
424 mimetype.startsWith("application/x-gtar"))
425 return shared ? images.zipShared() : images.zip();
426 else if (mimetype.startsWith("text/html"))
427 return shared ? images.htmlShared() : images.html();
428 else if (mimetype.startsWith("text/plain"))
429 return shared ? images.txtShared() : images.txt();
430 else if (mimetype.startsWith("image/"))
431 return shared ? images.imageShared() : images.image();
432 else if (mimetype.startsWith("video/"))
433 return shared ? images.videoShared() : images.video();
434 else if (mimetype.startsWith("audio/"))
435 return shared ? images.audioShared() : images.audio();
436 return shared ? images.documentShared() : images.document();
440 * Fill the file cache with data.
442 public void setFiles(final List<File> _files) {
443 files = new ArrayList<File>();
444 for (File fres : _files)
446 Collections.sort(files, new Comparator<File>() {
449 public int compare(File arg0, File arg1) {
450 return arg0.getName().compareTo(arg1.getName());
454 folderFileCount = files.size();
456 nameHeader.setSorted(true);
457 nameHeader.toggleReverseSort();
458 for (SortableHeader otherHeader : allHeaders) {
459 if (otherHeader != nameHeader) {
460 otherHeader.setSorted(false);
461 otherHeader.setReverseSort(true);
465 provider.getList().clear();
466 provider.setList(files);
467 selectionModel.clear();
468 app.showFolderStatistics(folderFileCount);
469 celltable.setPageSize(folderFileCount);
473 * Does the list contains the requested filename
478 public boolean contains(String fileName) {
479 for (int i = 0; i < files.size(); i++)
480 if (files.get(i).getName().equals(fileName))
485 public void clearSelectedRows() {
486 Iterator<File> it = selectionModel.getSelectedSet().iterator();
488 selectionModel.setSelected(it.next(),false);
495 public void selectAllRows() {
496 Iterator<File> it = provider.getList().iterator();
498 selectionModel.setSelected(it.next(),true);
502 protected void sortFiles(final String sortingProperty, final boolean sortingType){
503 Collections.sort(files, new Comparator<File>() {
506 public int compare(File arg0, File arg1) {
508 if (sortingProperty.equals("version")) {
509 return arg0.getVersion() - arg1.getVersion();
510 } else if (sortingProperty.equals("owner")) {
511 return arg0.getOwner().compareTo(arg1.getOwner());
512 } else if (sortingProperty.equals("date")) {
513 if (arg0.getLastModified() != null && arg1.getLastModified() != null)
514 return arg0.getLastModified().compareTo(arg1.getLastModified());
516 } else if (sortingProperty.equals("size")) {
517 return (int) (arg0.getBytes() - arg1.getBytes());
518 } else if (sortingProperty.equals("name")) {
519 return arg0.getName().compareTo(arg1.getName());
520 } else if (sortingProperty.equals("path")) {
521 return arg0.getUri().compareTo(arg1.getUri());
523 return arg0.getName().compareTo(arg1.getName());
526 else if (sortingProperty.equals("version")) {
528 return arg1.getVersion() - arg0.getVersion();
529 } else if (sortingProperty.equals("owner")) {
531 return arg1.getOwner().compareTo(arg0.getOwner());
532 } else if (sortingProperty.equals("date")) {
534 return arg1.getLastModified().compareTo(arg0.getLastModified());
535 } else if (sortingProperty.equals("size")) {
536 return (int) (arg1.getBytes() - arg0.getBytes());
537 } else if (sortingProperty.equals("name")) {
539 return arg1.getName().compareTo(arg0.getName());
540 } else if (sortingProperty.equals("path")) {
542 return arg1.getUri().compareTo(arg0.getUri());
545 return arg1.getName().compareTo(arg0.getName());
552 final class FileValueUpdater implements ValueUpdater<String>{
553 private String property;
554 private SortableHeader header;
558 public FileValueUpdater(SortableHeader header,String property) {
559 this.property=property;
563 public void update(String value) {
564 header.setSorted(true);
565 header.toggleReverseSort();
567 for (SortableHeader otherHeader : allHeaders) {
568 if (otherHeader != header) {
569 otherHeader.setSorted(false);
570 otherHeader.setReverseSort(true);
573 celltable.redrawHeaders();
574 sortFiles(property, header.getReverseSort());
575 FileList.this.update();
581 * Shows the files in the cellTable
583 private void showCellTable(){
584 provider.setList(files);
588 //celltable.redraw();
589 celltable.redrawHeaders();