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.safehtml.client.SafeHtmlTemplates;
60 import com.google.gwt.safehtml.shared.SafeHtml;
61 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
62 import com.google.gwt.user.cellview.client.CellTable;
63 import com.google.gwt.user.cellview.client.Column;
64 import com.google.gwt.user.client.Command;
65 import com.google.gwt.user.client.DOM;
66 import com.google.gwt.user.client.Event;
67 import com.google.gwt.user.client.Window;
68 import com.google.gwt.user.client.ui.Composite;
69 import com.google.gwt.user.client.ui.VerticalPanel;
70 import com.google.gwt.view.client.ListDataProvider;
71 import com.google.gwt.view.client.MultiSelectionModel;
72 import com.google.gwt.view.client.ProvidesKey;
73 import com.google.gwt.view.client.SelectionChangeEvent;
76 * A composite that displays the list of files in a particular folder.
78 public class FileList extends Composite {
80 ListDataProvider<File> provider = new ListDataProvider<File>();
83 * The styles applied to the table.
85 interface TableStyle extends CellTable.Style {
86 String cellTableFirstColumnShared();
89 interface TableResources extends CellTable.Resources {
91 @Source({CellTable.Style.DEFAULT_CSS, "PithosCellTable.css"})
92 TableStyle cellTableStyle();
95 static interface Templates extends SafeHtmlTemplates {
96 Templates INSTANCE = GWT.create(Templates.class);
98 @Template("<div id='dragHelper' style='border:1px solid black; background-color:#ffffff; color:black; width:150px;z-index:100'></div>")
99 SafeHtml outerHelper();
101 @Template("<span id='{0}'>{0}</span>")
102 public SafeHtml filenameSpan(String filename);
104 @Template("<a href='{0}' title='{1}' rel='lytebox[mnf]' onclick='myLytebox.start(this, false, false); return false;'>(view)</a>")
105 public SafeHtml viewLink(String link, String title);
107 @Template("<table><tr><td rowspan='3'>{0}</td><td style='font-size:95%;' id='{1}'>{1}</td></tr><tr><td>{2}</td></tr></table>")
108 public SafeHtml rendelContactCell(String imageHtml, String name, String fileSize);
110 @Template("<span id='{0}' class='{1}'>{2}</span>")
111 public SafeHtml spanWithIdAndClass(String id, String cssClass, String content);
114 protected final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
117 * Specifies that the images available for this composite will be the ones
118 * available in FileContextMenu.
120 public interface Images extends FolderTreeView.Images {
122 @Source("gr/grnet/pithos/resources/blank.gif")
123 ImageResource blank();
125 @Source("gr/grnet/pithos/resources/asc.png")
128 @Source("gr/grnet/pithos/resources/desc.png")
129 ImageResource desc();
131 @Source("gr/grnet/pithos/resources/mimetypes/document_shared.png")
132 ImageResource documentShared();
134 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst.png")
135 ImageResource wordprocessor();
137 @Source("gr/grnet/pithos/resources/mimetypes/log.png")
138 ImageResource spreadsheet();
140 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr.png")
141 ImageResource presentation();
143 @Source("gr/grnet/pithos/resources/mimetypes/acroread.png")
146 @Source("gr/grnet/pithos/resources/mimetypes/image.png")
147 ImageResource image();
149 @Source("gr/grnet/pithos/resources/mimetypes/video2.png")
150 ImageResource video();
152 @Source("gr/grnet/pithos/resources/mimetypes/knotify.png")
153 ImageResource audio();
155 @Source("gr/grnet/pithos/resources/mimetypes/html.png")
156 ImageResource html();
158 @Source("gr/grnet/pithos/resources/mimetypes/txt.png")
161 @Source("gr/grnet/pithos/resources/mimetypes/ark2.png")
164 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst_shared.png")
165 ImageResource wordprocessorShared();
167 @Source("gr/grnet/pithos/resources/mimetypes/log_shared.png")
168 ImageResource spreadsheetShared();
170 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr_shared.png")
171 ImageResource presentationShared();
173 @Source("gr/grnet/pithos/resources/mimetypes/acroread_shared.png")
174 ImageResource pdfShared();
176 @Source("gr/grnet/pithos/resources/mimetypes/image_shared.png")
177 ImageResource imageShared();
179 @Source("gr/grnet/pithos/resources/mimetypes/video2_shared.png")
180 ImageResource videoShared();
182 @Source("gr/grnet/pithos/resources/mimetypes/knotify_shared.png")
183 ImageResource audioShared();
185 @Source("gr/grnet/pithos/resources/mimetypes/html_shared.png")
186 ImageResource htmlShared();
188 @Source("gr/grnet/pithos/resources/mimetypes/txt_shared.png")
189 ImageResource txtShared();
191 @Source("gr/grnet/pithos/resources/mimetypes/ark2_shared.png")
192 ImageResource zipShared();
197 * The number of files in this folder.
204 long folderTotalSize;
207 * A cache of the files in the list.
209 private List<File> files;
212 * The widget's image bundle.
214 protected final Images images;
216 protected CellTable<File> celltable;
218 private final MultiSelectionModel<File> selectionModel;
220 protected final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
222 SortableHeader nameHeader;
224 FolderTreeView treeView;
226 protected Pithos app;
229 * Construct the file list widget. This entails setting up the widget
230 * layout, fetching the number of files in the current folder from the
231 * server and filling the local file cache of displayed files with data from
232 * the server, as well.
236 public FileList(final Pithos _app, Images _images, FolderTreeView _treeView) {
239 this.treeView = _treeView;
241 final CellTable.Resources resources = GWT.create(TableResources.class);
243 ProvidesKey<File> keyProvider = new ProvidesKey<File>(){
246 public Object getKey(File item) {
247 return item.getUri();
251 celltable = new CellTable<File>(10, resources, keyProvider);
252 celltable.setWidth("100%");
253 celltable.setStyleName("pithos-List");
255 Column<File, ImageResource> status = new Column<File, ImageResource>(new ImageResourceCell() {
257 public boolean handlesSelection() {
263 public ImageResource getValue(File entity) {
264 return getFileIcon(entity);
268 public String getCellStyleNames(Context context, File object) {
269 if (!object.getPermissions().isEmpty() && !object.isPublished())
270 return ((TableStyle) resources.cellTableStyle()).cellTableFirstColumnShared();
271 return super.getCellStyleNames(context, object);
274 celltable.addColumn(status,"");
276 final Column<File,SafeHtml> nameColumn = new Column<File,SafeHtml>(new SafeHtmlCell()) {
279 public SafeHtml getValue(File object) {
280 SafeHtmlBuilder sb = new SafeHtmlBuilder();
281 sb.append(Templates.INSTANCE.filenameSpan(object.getName()));
282 if (object.getContentType() != null && (object.getContentType().endsWith("png") || object.getContentType().endsWith("gif") || object.getContentType().endsWith("jpeg"))) {
283 sb.appendHtmlConstant(" ")
284 .append(Templates.INSTANCE.viewLink(app.getApiPath() + object.getOwner() + object.getUri(), object.getName()));
287 return sb.toSafeHtml();
291 celltable.addColumn(nameColumn, nameHeader = new SortableHeader("Name", "name"));
292 allHeaders.add(nameHeader);
293 nameHeader.setUpdater(new FileValueUpdater(nameHeader));
294 nameHeader.setSorted(true);
295 nameHeader.setReverseSort(true);
297 celltable.redrawHeaders();
299 Column<File,String> aColumn = new Column<File,String>(new TextCell()) {
301 public String getValue(File object) {
302 return object.getSizeAsString();
305 SortableHeader aheader = new SortableHeader("Size", "size");
306 celltable.addColumn(aColumn, aheader);
307 allHeaders.add(aheader);
308 aheader.setUpdater(new FileValueUpdater(aheader));
310 aColumn = new Column<File,String>(new TextCell()) {
312 public String getValue(File object) {
313 return object.getLastModified() != null ? formatter.format(object.getLastModified()) : "";
316 aheader = new SortableHeader("Last Modified", "date");
317 celltable.addColumn(aColumn, aheader);
318 allHeaders.add(aheader);
319 aheader.setUpdater(new FileValueUpdater(aheader));
321 provider.addDataDisplay(celltable);
323 VerticalPanel vp = new VerticalPanel();
325 vp.addStyleName("pithos-FileListContainer");
329 vp.setCellWidth(celltable, "100%");
330 vp.addHandler(new ContextMenuHandler() {
332 public void onContextMenu(final ContextMenuEvent event) {
333 final TreeView tree = app.getSelectedTree();
334 if (tree != null && (tree.equals(app.getFolderTreeView()) || tree.equals(app.getOtherSharedTreeView()))) {
335 final int x = event.getNativeEvent().getClientX();
336 final int y = event.getNativeEvent().getClientY();
337 final Folder selectedFolder = app.getSelection();
338 app.scheduleFolderHeadCommand(selectedFolder, new Command() {
341 public void execute() {
342 final List<File> selectedFiles = getSelectedFiles();
343 Iterator<File> iter = selectedFiles.iterator();
344 iterateFilesHeadCommand(iter, new Command() {
347 public void execute() {
348 FileContextMenu contextMenu = new FileContextMenu(app, images, tree, selectedFolder, selectedFiles);
349 contextMenu.setPopupPosition(x, y);
357 }, ContextMenuEvent.getType());
360 selectionModel = new MultiSelectionModel<File>(keyProvider);
361 selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
364 public void onSelectionChange(SelectionChangeEvent event) {
365 app.showRelevantToolbarButtons();
369 celltable.setSelectionModel(selectionModel, PithosSelectionEventManager.<File> createDefaultManager());
370 // celltable.setPageSize(Pithos.VISIBLE_FILE_COUNT);
372 sinkEvents(Event.ONCONTEXTMENU);
373 // sinkEvents(Event.ONMOUSEUP);
374 // sinkEvents(Event.ONMOUSEDOWN);
375 // sinkEvents(Event.ONCLICK);
376 // sinkEvents(Event.ONKEYDOWN);
377 sinkEvents(Event.ONDBLCLICK);
378 Pithos.preventIESelection();
381 public List<File> getSelectedFiles() {
382 return new ArrayList<File>(selectionModel.getSelectedSet());
386 public void onBrowserEvent(Event event) {
388 // if (files == null || files.size() == 0) {
389 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
390 // contextMenu = new FileContextMenu(images, false, true);
391 // contextMenu.show();
392 // event.preventDefault();
393 // event.cancelBubble(true);
397 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() != 0) {
398 // GWT.log("*****GOING TO SHOW CONTEXT MENU ****", null);
399 // contextMenu = new FileContextMenu(images, false, false);
400 // contextMenu = contextMenu.onEvent(event);
401 // event.cancelBubble(true);
402 // event.preventDefault();
403 // } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
404 // contextMenu = new FileContextMenu(images, false, true);
405 // contextMenu = contextMenu.onEmptyEvent(event);
406 // event.cancelBubble(true);
407 // event.preventDefault();
409 if (DOM.eventGetType(event) == Event.ONDBLCLICK)
410 if (getSelectedFiles().size() == 1) {
411 File file = getSelectedFiles().get(0);
412 Window.open(app.getApiPath() + file.getOwner() + file.getUri(), "_blank", "");
413 event.preventDefault();
416 super.onBrowserEvent(event);
420 * Update the display of the file list.
427 * Return the proper icon based on the MIME type of the file.
432 protected ImageResource getFileIcon(File file) {
433 String mimetype = file.getContentType();
434 boolean published = file.isPublished();
435 if (mimetype == null)
436 return published ? images.documentShared() : images.document();
437 mimetype = mimetype.toLowerCase();
438 if (mimetype.startsWith("application/pdf"))
439 return published ? images.pdfShared() : images.pdf();
440 else if (mimetype.endsWith("excel"))
441 return published ? images.spreadsheetShared() : images.spreadsheet();
442 else if (mimetype.endsWith("msword"))
443 return published ? images.wordprocessorShared() : images.wordprocessor();
444 else if (mimetype.endsWith("powerpoint"))
445 return published ? images.presentationShared() : images.presentation();
446 else if (mimetype.startsWith("application/zip") ||
447 mimetype.startsWith("application/gzip") ||
448 mimetype.startsWith("application/x-gzip") ||
449 mimetype.startsWith("application/x-tar") ||
450 mimetype.startsWith("application/x-gtar"))
451 return published ? images.zipShared() : images.zip();
452 else if (mimetype.startsWith("text/html"))
453 return published ? images.htmlShared() : images.html();
454 else if (mimetype.startsWith("text/plain"))
455 return published ? images.txtShared() : images.txt();
456 else if (mimetype.startsWith("image/"))
457 return published ? images.imageShared() : images.image();
458 else if (mimetype.startsWith("video/"))
459 return published ? images.videoShared() : images.video();
460 else if (mimetype.startsWith("audio/"))
461 return published ? images.audioShared() : images.audio();
462 return published ? images.documentShared() : images.document();
466 * Fill the file cache with data.
468 public void setFiles(final List<File> _files) {
469 files = new ArrayList<File>();
470 for (File fres : _files) {
474 folderFileCount = files.size();
476 for (SortableHeader header : allHeaders) {
477 if (header.isSorted())
478 sortFiles(header.getProperty(), header.getReverseSort());
481 List<File> previousSelection = getSelectedFiles(); //Keep the previous selection
483 provider.getList().clear();
484 provider.setList(files);
485 selectionModel.clear();
486 for (File f : files) {
487 if (previousSelection.contains(f))
488 selectionModel.setSelected(f, true);
491 app.showFolderStatistics(folderFileCount);
492 celltable.setPageSize(folderFileCount);
496 * Does the list contains the requested filename
501 public boolean contains(String fileName) {
502 for (int i = 0; i < files.size(); i++)
503 if (files.get(i).getName().equals(fileName))
508 public void clearSelectedRows() {
509 Iterator<File> it = selectionModel.getSelectedSet().iterator();
511 selectionModel.setSelected(it.next(),false);
518 public void selectAllRows() {
519 Iterator<File> it = provider.getList().iterator();
521 selectionModel.setSelected(it.next(),true);
525 protected void sortFiles(final String sortingProperty, final boolean sortingType){
526 Collections.sort(files, new Comparator<File>() {
529 public int compare(File arg0, File arg1) {
531 if (sortingProperty.equals("version")) {
532 return arg0.getVersion() - arg1.getVersion();
533 } else if (sortingProperty.equals("owner")) {
534 return arg0.getOwner().compareTo(arg1.getOwner());
535 } else if (sortingProperty.equals("date")) {
536 if (arg0.getLastModified() != null && arg1.getLastModified() != null)
537 return arg0.getLastModified().compareTo(arg1.getLastModified());
539 } else if (sortingProperty.equals("size")) {
540 return (int) (arg0.getBytes() - arg1.getBytes());
541 } else if (sortingProperty.equals("name")) {
542 return arg0.getName().compareTo(arg1.getName());
543 } else if (sortingProperty.equals("path")) {
544 return arg0.getUri().compareTo(arg1.getUri());
546 return arg0.getName().compareTo(arg1.getName());
549 else if (sortingProperty.equals("version")) {
551 return arg1.getVersion() - arg0.getVersion();
552 } else if (sortingProperty.equals("owner")) {
554 return arg1.getOwner().compareTo(arg0.getOwner());
555 } else if (sortingProperty.equals("date")) {
557 return arg1.getLastModified().compareTo(arg0.getLastModified());
558 } else if (sortingProperty.equals("size")) {
559 return (int) (arg1.getBytes() - arg0.getBytes());
560 } else if (sortingProperty.equals("name")) {
562 return arg1.getName().compareTo(arg0.getName());
563 } else if (sortingProperty.equals("path")) {
565 return arg1.getUri().compareTo(arg0.getUri());
568 return arg1.getName().compareTo(arg0.getName());
575 final class FileValueUpdater implements ValueUpdater<String>{
576 private SortableHeader header;
580 public FileValueUpdater(SortableHeader header) {
584 public void update(String value) {
585 header.setSorted(true);
586 header.toggleReverseSort();
588 for (SortableHeader otherHeader : allHeaders) {
589 if (otherHeader != header) {
590 otherHeader.setSorted(false);
591 otherHeader.setReverseSort(true);
594 celltable.redrawHeaders();
595 sortFiles(header.getProperty(), header.getReverseSort());
596 FileList.this.update();
602 * Shows the files in the cellTable
604 private void showCellTable(){
605 provider.setList(files);
609 //celltable.redraw();
610 celltable.redrawHeaders();
613 void iterateFilesHeadCommand(final Iterator<File> iter, final Command callback) {
614 if (iter.hasNext()) {
615 File f = iter.next();
616 app.scheduleFileHeadCommand(f, new Command() {
619 public void execute() {
620 iterateFilesHeadCommand(iter, callback);
624 else if (callback != null)