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.http.client.URL;
56 import com.google.gwt.i18n.client.DateTimeFormat;
57 import com.google.gwt.resources.client.ImageResource;
58 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
59 import com.google.gwt.safehtml.shared.SafeHtml;
60 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
61 import com.google.gwt.user.cellview.client.CellTable;
62 import com.google.gwt.user.cellview.client.Column;
63 import com.google.gwt.user.client.Command;
64 import com.google.gwt.user.client.DOM;
65 import com.google.gwt.user.client.Event;
66 import com.google.gwt.user.client.Window;
67 import com.google.gwt.user.client.ui.Composite;
68 import com.google.gwt.user.client.ui.VerticalPanel;
69 import com.google.gwt.view.client.ListDataProvider;
70 import com.google.gwt.view.client.MultiSelectionModel;
71 import com.google.gwt.view.client.ProvidesKey;
72 import com.google.gwt.view.client.SelectionChangeEvent;
75 * A composite that displays the list of files in a particular folder.
77 public class FileList extends Composite {
79 ListDataProvider<File> provider = new ListDataProvider<File>();
82 * The styles applied to the table.
84 interface TableStyle extends CellTable.Style {
87 interface TableResources extends CellTable.Resources {
89 @Source({CellTable.Style.DEFAULT_CSS, "PithosCellTable.css"})
90 TableStyle cellTableStyle();
93 static interface Templates extends SafeHtmlTemplates {
94 Templates INSTANCE = GWT.create(Templates.class);
96 @Template("<div id='dragHelper' style='border:1px solid black; background-color:#ffffff; color:black; width:150px;z-index:100'></div>")
97 SafeHtml outerHelper();
99 @Template("<span id='{0}'>{0}</span>")
100 public SafeHtml filenameSpan(String filename);
102 @Template("<a href='{0}' title='{1}' rel='lytebox[mnf]' onclick='myLytebox.start(this, false, false); return false;'>(view)</a>")
103 public SafeHtml viewLink(String link, String title);
105 @Template("<table><tr><td rowspan='3'>{0}</td><td style='font-size:95%;' id='{1}'>{1}</td></tr><tr><td>{2}</td></tr></table>")
106 public SafeHtml rendelContactCell(String imageHtml, String name, String fileSize);
108 @Template("<span id='{0}' class='{1}'>{2}</span>")
109 public SafeHtml spanWithIdAndClass(String id, String cssClass, String content);
112 protected final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
115 * Specifies that the images available for this composite will be the ones
116 * available in FileContextMenu.
118 public interface Images extends FolderTreeView.Images {
120 @Source("gr/grnet/pithos/resources/blank.gif")
121 ImageResource blank();
123 @Source("gr/grnet/pithos/resources/asc.png")
126 @Source("gr/grnet/pithos/resources/desc.png")
127 ImageResource desc();
129 @Source("gr/grnet/pithos/resources/mimetypes/document_shared.png")
130 ImageResource documentShared();
132 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst.png")
133 ImageResource wordprocessor();
135 @Source("gr/grnet/pithos/resources/mimetypes/log.png")
136 ImageResource spreadsheet();
138 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr.png")
139 ImageResource presentation();
141 @Source("gr/grnet/pithos/resources/mimetypes/acroread.png")
144 @Source("gr/grnet/pithos/resources/mimetypes/image.png")
145 ImageResource image();
147 @Source("gr/grnet/pithos/resources/mimetypes/video2.png")
148 ImageResource video();
150 @Source("gr/grnet/pithos/resources/mimetypes/knotify.png")
151 ImageResource audio();
153 @Source("gr/grnet/pithos/resources/mimetypes/html.png")
154 ImageResource html();
156 @Source("gr/grnet/pithos/resources/mimetypes/txt.png")
159 @Source("gr/grnet/pithos/resources/mimetypes/ark2.png")
162 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst_shared.png")
163 ImageResource wordprocessorShared();
165 @Source("gr/grnet/pithos/resources/mimetypes/log_shared.png")
166 ImageResource spreadsheetShared();
168 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr_shared.png")
169 ImageResource presentationShared();
171 @Source("gr/grnet/pithos/resources/mimetypes/acroread_shared.png")
172 ImageResource pdfShared();
174 @Source("gr/grnet/pithos/resources/mimetypes/image_shared.png")
175 ImageResource imageShared();
177 @Source("gr/grnet/pithos/resources/mimetypes/video2_shared.png")
178 ImageResource videoShared();
180 @Source("gr/grnet/pithos/resources/mimetypes/knotify_shared.png")
181 ImageResource audioShared();
183 @Source("gr/grnet/pithos/resources/mimetypes/html_shared.png")
184 ImageResource htmlShared();
186 @Source("gr/grnet/pithos/resources/mimetypes/txt_shared.png")
187 ImageResource txtShared();
189 @Source("gr/grnet/pithos/resources/mimetypes/ark2_shared.png")
190 ImageResource zipShared();
195 * The number of files in this folder.
202 long folderTotalSize;
205 * A cache of the files in the list.
207 private List<File> files;
210 * The widget's image bundle.
212 protected final Images images;
214 protected CellTable<File> celltable;
216 private final MultiSelectionModel<File> selectionModel;
218 protected final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
220 SortableHeader nameHeader;
222 FolderTreeView treeView;
224 protected Pithos app;
227 * Construct the file list widget. This entails setting up the widget
228 * layout, fetching the number of files in the current folder from the
229 * server and filling the local file cache of displayed files with data from
230 * the server, as well.
234 public FileList(final Pithos _app, Images _images, FolderTreeView _treeView) {
237 this.treeView = _treeView;
239 CellTable.Resources resources = GWT.create(TableResources.class);
241 ProvidesKey<File> keyProvider = new ProvidesKey<File>(){
244 public Object getKey(File item) {
245 return item.getUri();
249 celltable = new CellTable<File>(10, resources, keyProvider);
250 celltable.setWidth("100%");
251 celltable.setStyleName("pithos-List");
253 Column<File, ImageResource> status = new Column<File, ImageResource>(new ImageResourceCell() {
255 public boolean handlesSelection() {
261 public ImageResource getValue(File entity) {
262 return getFileIcon(entity);
265 celltable.addColumn(status,"");
267 final Column<File,SafeHtml> nameColumn = new Column<File,SafeHtml>(new SafeHtmlCell()) {
270 public SafeHtml getValue(File object) {
271 SafeHtmlBuilder sb = new SafeHtmlBuilder();
272 sb.append(Templates.INSTANCE.filenameSpan(object.getName()));
273 if (object.getContentType() != null && (object.getContentType().endsWith("png") || object.getContentType().endsWith("gif") || object.getContentType().endsWith("jpeg"))) {
274 sb.appendHtmlConstant(" ")
275 .append(Templates.INSTANCE.viewLink(app.getApiPath() + object.getOwner() + object.getUri() + "?X-Auth-Token=" + app.getToken(), object.getName()));
278 return sb.toSafeHtml();
282 celltable.addColumn(nameColumn, nameHeader = new SortableHeader("Name"));
283 allHeaders.add(nameHeader);
284 nameHeader.setUpdater(new FileValueUpdater(nameHeader, "name"));
286 celltable.redrawHeaders();
288 Column<File,String> aColumn = new Column<File,String>(new TextCell()) {
290 public String getValue(File object) {
291 // TODO Auto-generated method stub
292 return object.getSizeAsString();
295 SortableHeader aheader = new SortableHeader("Size");
296 celltable.addColumn(aColumn, aheader);
297 allHeaders.add(aheader);
298 aheader.setUpdater(new FileValueUpdater(aheader, "size"));
300 aColumn = new Column<File,String>(new TextCell()) {
302 public String getValue(File object) {
303 return object.getLastModified() != null ? formatter.format(object.getLastModified()) : "";
306 aheader = new SortableHeader("Last Modified");
307 celltable.addColumn(aColumn, aheader);
308 allHeaders.add(aheader);
309 aheader.setUpdater(new FileValueUpdater(aheader, "date"));
311 provider.addDataDisplay(celltable);
313 VerticalPanel vp = new VerticalPanel();
315 vp.addStyleName("pithos-FileListContainer");
319 vp.setCellWidth(celltable, "100%");
320 vp.addHandler(new ContextMenuHandler() {
322 public void onContextMenu(final ContextMenuEvent event) {
323 final TreeView tree = app.getSelectedTree();
324 if (tree != null && (tree.equals(app.getFolderTreeView()) || tree.equals(app.getOtherSharedTreeView()))) {
325 final int x = event.getNativeEvent().getClientX();
326 final int y = event.getNativeEvent().getClientY();
327 final Folder selectedFolder = app.getSelection();
328 app.scheduleFolderHeadCommand(selectedFolder, new Command() {
331 public void execute() {
332 final List<File> selectedFiles = getSelectedFiles();
333 Iterator<File> iter = selectedFiles.iterator();
334 iterateFilesHeadCommand(iter, new Command() {
337 public void execute() {
338 FileContextMenu contextMenu = new FileContextMenu(app, images, tree, selectedFolder, selectedFiles);
339 contextMenu.setPopupPosition(x, y);
347 }, ContextMenuEvent.getType());
350 selectionModel = new MultiSelectionModel<File>(keyProvider);
351 selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
354 public void onSelectionChange(SelectionChangeEvent event) {
355 app.showRelevantToolbarButtons();
359 celltable.setSelectionModel(selectionModel, PithosSelectionEventManager.<File> createDefaultManager());
360 // celltable.setPageSize(Pithos.VISIBLE_FILE_COUNT);
362 sinkEvents(Event.ONCONTEXTMENU);
363 // sinkEvents(Event.ONMOUSEUP);
364 // sinkEvents(Event.ONMOUSEDOWN);
365 // sinkEvents(Event.ONCLICK);
366 // sinkEvents(Event.ONKEYDOWN);
367 sinkEvents(Event.ONDBLCLICK);
368 Pithos.preventIESelection();
371 public List<File> getSelectedFiles() {
372 return new ArrayList<File>(selectionModel.getSelectedSet());
376 public void onBrowserEvent(Event event) {
378 // if (files == null || files.size() == 0) {
379 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
380 // contextMenu = new FileContextMenu(images, false, true);
381 // contextMenu.show();
382 // event.preventDefault();
383 // event.cancelBubble(true);
387 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() != 0) {
388 // GWT.log("*****GOING TO SHOW CONTEXT MENU ****", null);
389 // contextMenu = new FileContextMenu(images, false, false);
390 // contextMenu = contextMenu.onEvent(event);
391 // event.cancelBubble(true);
392 // event.preventDefault();
393 // } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
394 // contextMenu = new FileContextMenu(images, false, true);
395 // contextMenu = contextMenu.onEmptyEvent(event);
396 // event.cancelBubble(true);
397 // event.preventDefault();
399 if (DOM.eventGetType(event) == Event.ONDBLCLICK)
400 if (getSelectedFiles().size() == 1) {
401 File file = getSelectedFiles().get(0);
402 Window.open(app.getApiPath() + file.getOwner() + file.getUri() + "?X-Auth-Token=" + URL.encodeQueryString(app.getToken()), "_blank", "");
403 event.preventDefault();
406 super.onBrowserEvent(event);
410 * Update the display of the file list.
417 * Return the proper icon based on the MIME type of the file.
422 protected ImageResource getFileIcon(File file) {
423 String mimetype = file.getContentType();
424 boolean shared = file.isShared();
425 if (mimetype == null)
426 return shared ? images.documentShared() : images.document();
427 mimetype = mimetype.toLowerCase();
428 if (mimetype.startsWith("application/pdf"))
429 return shared ? images.pdfShared() : images.pdf();
430 else if (mimetype.endsWith("excel"))
431 return shared ? images.spreadsheetShared() : images.spreadsheet();
432 else if (mimetype.endsWith("msword"))
433 return shared ? images.wordprocessorShared() : images.wordprocessor();
434 else if (mimetype.endsWith("powerpoint"))
435 return shared ? images.presentationShared() : images.presentation();
436 else if (mimetype.startsWith("application/zip") ||
437 mimetype.startsWith("application/gzip") ||
438 mimetype.startsWith("application/x-gzip") ||
439 mimetype.startsWith("application/x-tar") ||
440 mimetype.startsWith("application/x-gtar"))
441 return shared ? images.zipShared() : images.zip();
442 else if (mimetype.startsWith("text/html"))
443 return shared ? images.htmlShared() : images.html();
444 else if (mimetype.startsWith("text/plain"))
445 return shared ? images.txtShared() : images.txt();
446 else if (mimetype.startsWith("image/"))
447 return shared ? images.imageShared() : images.image();
448 else if (mimetype.startsWith("video/"))
449 return shared ? images.videoShared() : images.video();
450 else if (mimetype.startsWith("audio/"))
451 return shared ? images.audioShared() : images.audio();
452 return shared ? images.documentShared() : images.document();
456 * Fill the file cache with data.
458 public void setFiles(final List<File> _files) {
459 files = new ArrayList<File>();
460 for (File fres : _files)
462 Collections.sort(files, new Comparator<File>() {
465 public int compare(File arg0, File arg1) {
466 return arg0.getName().compareTo(arg1.getName());
470 folderFileCount = files.size();
472 nameHeader.setSorted(true);
473 nameHeader.toggleReverseSort();
474 for (SortableHeader otherHeader : allHeaders) {
475 if (otherHeader != nameHeader) {
476 otherHeader.setSorted(false);
477 otherHeader.setReverseSort(true);
481 provider.getList().clear();
482 provider.setList(files);
483 selectionModel.clear();
484 app.showFolderStatistics(folderFileCount);
485 celltable.setPageSize(folderFileCount);
489 * Does the list contains the requested filename
494 public boolean contains(String fileName) {
495 for (int i = 0; i < files.size(); i++)
496 if (files.get(i).getName().equals(fileName))
501 public void clearSelectedRows() {
502 Iterator<File> it = selectionModel.getSelectedSet().iterator();
504 selectionModel.setSelected(it.next(),false);
511 public void selectAllRows() {
512 Iterator<File> it = provider.getList().iterator();
514 selectionModel.setSelected(it.next(),true);
518 protected void sortFiles(final String sortingProperty, final boolean sortingType){
519 Collections.sort(files, new Comparator<File>() {
522 public int compare(File arg0, File arg1) {
524 if (sortingProperty.equals("version")) {
525 return arg0.getVersion() - arg1.getVersion();
526 } else if (sortingProperty.equals("owner")) {
527 return arg0.getOwner().compareTo(arg1.getOwner());
528 } else if (sortingProperty.equals("date")) {
529 if (arg0.getLastModified() != null && arg1.getLastModified() != null)
530 return arg0.getLastModified().compareTo(arg1.getLastModified());
532 } else if (sortingProperty.equals("size")) {
533 return (int) (arg0.getBytes() - arg1.getBytes());
534 } else if (sortingProperty.equals("name")) {
535 return arg0.getName().compareTo(arg1.getName());
536 } else if (sortingProperty.equals("path")) {
537 return arg0.getUri().compareTo(arg1.getUri());
539 return arg0.getName().compareTo(arg1.getName());
542 else if (sortingProperty.equals("version")) {
544 return arg1.getVersion() - arg0.getVersion();
545 } else if (sortingProperty.equals("owner")) {
547 return arg1.getOwner().compareTo(arg0.getOwner());
548 } else if (sortingProperty.equals("date")) {
550 return arg1.getLastModified().compareTo(arg0.getLastModified());
551 } else if (sortingProperty.equals("size")) {
552 return (int) (arg1.getBytes() - arg0.getBytes());
553 } else if (sortingProperty.equals("name")) {
555 return arg1.getName().compareTo(arg0.getName());
556 } else if (sortingProperty.equals("path")) {
558 return arg1.getUri().compareTo(arg0.getUri());
561 return arg1.getName().compareTo(arg0.getName());
568 final class FileValueUpdater implements ValueUpdater<String>{
569 private String property;
570 private SortableHeader header;
574 public FileValueUpdater(SortableHeader header,String property) {
575 this.property=property;
579 public void update(String value) {
580 header.setSorted(true);
581 header.toggleReverseSort();
583 for (SortableHeader otherHeader : allHeaders) {
584 if (otherHeader != header) {
585 otherHeader.setSorted(false);
586 otherHeader.setReverseSort(true);
589 celltable.redrawHeaders();
590 sortFiles(property, header.getReverseSort());
591 FileList.this.update();
597 * Shows the files in the cellTable
599 private void showCellTable(){
600 provider.setList(files);
604 //celltable.redraw();
605 celltable.redrawHeaders();
608 void iterateFilesHeadCommand(final Iterator<File> iter, final Command callback) {
609 if (iter.hasNext()) {
610 File f = iter.next();
611 app.scheduleFileHeadCommand(f, new Command() {
614 public void execute() {
615 iterateFilesHeadCommand(iter, callback);
619 else if (callback != null)