2 * Copyright 2011 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 com.google.gwt.event.dom.client.ContextMenuEvent;
39 import com.google.gwt.event.dom.client.ContextMenuHandler;
41 import com.google.gwt.user.cellview.client.Column;
42 import gr.grnet.pithos.web.client.commands.UploadFileCommand;
43 import gr.grnet.pithos.web.client.foldertree.File;
44 import gr.grnet.pithos.web.client.foldertree.Folder;
45 import gr.grnet.pithos.web.client.foldertree.FolderTreeView;
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.Comparator;
50 import java.util.Iterator;
51 import java.util.List;
53 import com.google.gwt.cell.client.ImageResourceCell;
54 import com.google.gwt.cell.client.SafeHtmlCell;
55 import com.google.gwt.cell.client.TextCell;
56 import com.google.gwt.cell.client.ValueUpdater;
57 import com.google.gwt.core.client.GWT;
58 import com.google.gwt.dom.client.Style.Cursor;
59 import com.google.gwt.event.dom.client.ClickEvent;
60 import com.google.gwt.event.dom.client.ClickHandler;
61 import com.google.gwt.i18n.client.DateTimeFormat;
62 import com.google.gwt.resources.client.ClientBundle;
63 import com.google.gwt.resources.client.ImageResource;
64 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
65 import com.google.gwt.safehtml.shared.SafeHtml;
66 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
67 import com.google.gwt.user.cellview.client.CellTable;
68 import com.google.gwt.user.cellview.client.GssSimplePager;
69 import com.google.gwt.user.client.Event;
70 import com.google.gwt.user.client.ui.AbstractImagePrototype;
71 import com.google.gwt.user.client.ui.Button;
72 import com.google.gwt.user.client.ui.Composite;
73 import com.google.gwt.user.client.ui.HorizontalPanel;
74 import com.google.gwt.user.client.ui.VerticalPanel;
75 import com.google.gwt.view.client.ListDataProvider;
76 import com.google.gwt.view.client.MultiSelectionModel;
77 import com.google.gwt.view.client.ProvidesKey;
78 import com.google.gwt.view.client.SelectionChangeEvent;
79 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
82 * A composite that displays the list of files in a particular folder.
84 public class FileList extends Composite {
86 ListDataProvider<File> provider = new ListDataProvider<File>();
89 * The styles applied to the table.
91 interface TableStyle extends CellTable.Style {
94 interface TableResources extends CellTable.Resources {
95 @Source({CellTable.Style.DEFAULT_CSS, "GssCellTable.css"})
96 TableStyle cellTableStyle();
99 static interface Templates extends SafeHtmlTemplates {
100 Templates INSTANCE = GWT.create(Templates.class);
102 @Template("<div id='dragHelper' style='border:1px solid black; background-color:#ffffff; color:black; width:150px;z-index:100'></div>")
103 SafeHtml outerHelper();
105 @Template("<span id='{0}'>{0}</span>")
106 public SafeHtml filenameSpan(String filename);
108 @Template("<a href='{0}' title='{1}' rel='lytebox[mnf]' onclick='myLytebox.start(this, false, false); return false;'>(view)</a>")
109 public SafeHtml viewLink(String link, String title);
111 @Template("<table><tr><td rowspan='3'>{0}</td><td style='font-size:95%;' id='{1}'>{1}</td></tr><tr><td>{2}</td></tr></table>")
112 public SafeHtml rendelContactCell(String imageHtml, String name, String fileSize);
114 @Template("<span id='{0}' class='{1}'>{2}</span>")
115 public SafeHtml spanWithIdAndClass(String id, String cssClass, String content);
118 private final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
121 * Specifies that the images available for this composite will be the ones
122 * available in FileContextMenu.
124 public interface Images extends ClientBundle,FileContextMenu.Images, FolderTreeView.Images {
126 @Source("gr/grnet/pithos/resources/blank.gif")
127 ImageResource blank();
129 @Source("gr/grnet/pithos/resources/asc.png")
132 @Source("gr/grnet/pithos/resources/desc.png")
133 ImageResource desc();
135 @Source("gr/grnet/pithos/resources/mimetypes/document_shared.png")
136 ImageResource documentShared();
138 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst.png")
139 ImageResource wordprocessor();
141 @Source("gr/grnet/pithos/resources/mimetypes/log.png")
142 ImageResource spreadsheet();
144 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr.png")
145 ImageResource presentation();
147 @Source("gr/grnet/pithos/resources/mimetypes/acroread.png")
150 @Source("gr/grnet/pithos/resources/mimetypes/image.png")
151 ImageResource image();
153 @Source("gr/grnet/pithos/resources/mimetypes/video2.png")
154 ImageResource video();
156 @Source("gr/grnet/pithos/resources/mimetypes/knotify.png")
157 ImageResource audio();
159 @Source("gr/grnet/pithos/resources/mimetypes/html.png")
160 ImageResource html();
162 @Source("gr/grnet/pithos/resources/mimetypes/txt.png")
165 @Source("gr/grnet/pithos/resources/mimetypes/ark2.png")
168 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst_shared.png")
169 ImageResource wordprocessorShared();
171 @Source("gr/grnet/pithos/resources/mimetypes/log_shared.png")
172 ImageResource spreadsheetShared();
174 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr_shared.png")
175 ImageResource presentationShared();
177 @Source("gr/grnet/pithos/resources/mimetypes/acroread_shared.png")
178 ImageResource pdfShared();
180 @Source("gr/grnet/pithos/resources/mimetypes/image_shared.png")
181 ImageResource imageShared();
183 @Source("gr/grnet/pithos/resources/mimetypes/video2_shared.png")
184 ImageResource videoShared();
186 @Source("gr/grnet/pithos/resources/mimetypes/knotify_shared.png")
187 ImageResource audioShared();
189 @Source("gr/grnet/pithos/resources/mimetypes/html_shared.png")
190 ImageResource htmlShared();
192 @Source("gr/grnet/pithos/resources/mimetypes/txt_shared.png")
193 ImageResource txtShared();
195 @Source("gr/grnet/pithos/resources/mimetypes/ark2_shared.png")
196 ImageResource zipShared();
201 * The number of files in this folder.
208 long folderTotalSize;
211 * A cache of the files in the list.
213 private List<File> files;
216 * The widget's image bundle.
218 private final Images images;
220 private CellTable<File> celltable;
222 private final MultiSelectionModel<File> selectionModel;
224 private final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
226 SortableHeader nameHeader;
228 FolderTreeView treeView;
233 * Construct the file list widget. This entails setting up the widget
234 * layout, fetching the number of files in the current folder from the
235 * server and filling the local file cache of displayed files with data from
236 * the server, as well.
240 public FileList(final Pithos _app, Images _images, FolderTreeView _treeView) {
243 this.treeView = _treeView;
245 CellTable.Resources resources = GWT.create(TableResources.class);
247 ProvidesKey<File> keyProvider = new ProvidesKey<File>(){
250 public Object getKey(File item) {
251 return item.getUri();
255 celltable = new CellTable<File>(Pithos.VISIBLE_FILE_COUNT, resources, keyProvider);
256 celltable.setWidth("100%");
257 celltable.setStyleName("pithos-List");
259 Column<File, ImageResource> status = new Column<File, ImageResource>(new ImageResourceCell() {
261 public boolean handlesSelection() {
267 public ImageResource getValue(File entity) {
268 return getFileIcon(entity);
271 celltable.addColumn(status,"");
273 final Column<File,SafeHtml> nameColumn = new Column<File,SafeHtml>(new SafeHtmlCell()) {
276 public SafeHtml getValue(File object) {
277 SafeHtmlBuilder sb = new SafeHtmlBuilder();
278 sb.append(Templates.INSTANCE.filenameSpan(object.getName()));
279 if (object.getContentType().endsWith("png") || object.getContentType().endsWith("gif") || object.getContentType().endsWith("jpeg")) {
280 sb.appendHtmlConstant(" ")
281 .append(Templates.INSTANCE.viewLink(object.getUri(), object.getOwner() + " : " + object.getPath() + object.getName()));
284 return sb.toSafeHtml();
288 celltable.addColumn(nameColumn, nameHeader = new SortableHeader("Name"));
289 allHeaders.add(nameHeader);
290 nameHeader.setUpdater(new FileValueUpdater(nameHeader, "name"));
292 celltable.redrawHeaders();
294 Column<File,String> aColumn = new Column<File,String>(new TextCell()) {
296 public String getValue(File object) {
297 // TODO Auto-generated method stub
298 return object.getSizeAsString();
301 SortableHeader aheader = new SortableHeader("Size");
302 celltable.addColumn(aColumn, aheader);
303 allHeaders.add(aheader);
304 aheader.setUpdater(new FileValueUpdater(aheader, "size"));
306 aColumn = new Column<File,String>(new TextCell()) {
308 public String getValue(File object) {
309 return formatter.format(object.getLastModified());
312 aheader = new SortableHeader("Last Modified");
313 celltable.addColumn(aColumn, aheader);
314 allHeaders.add(aheader);
315 aheader.setUpdater(new FileValueUpdater(aheader, "date"));
317 provider.addDataDisplay(celltable);
319 VerticalPanel vp = new VerticalPanel();
324 vp.setCellWidth(celltable, "100%");
325 vp.addHandler(new ContextMenuHandler() {
327 public void onContextMenu(ContextMenuEvent event) {
328 Folder selectedFolder = treeView.getSelection();
329 if (!selectedFolder.isTrash()) {
330 FileContextMenu contextMenu = new FileContextMenu(app, images, selectedFolder, getSelectedFiles(), false);
331 int x = event.getNativeEvent().getClientX();
332 int y = event.getNativeEvent().getClientY();
333 contextMenu.setPopupPosition(x, y);
337 }, ContextMenuEvent.getType());
340 selectionModel = new MultiSelectionModel<File>(keyProvider);
342 celltable.setSelectionModel(selectionModel, GSSSelectionEventManager.<File> createDefaultManager());
343 celltable.setPageSize(Pithos.VISIBLE_FILE_COUNT);
345 sinkEvents(Event.ONCONTEXTMENU);
346 // sinkEvents(Event.ONMOUSEUP);
347 // sinkEvents(Event.ONMOUSEDOWN);
348 // sinkEvents(Event.ONCLICK);
349 // sinkEvents(Event.ONKEYDOWN);
350 // sinkEvents(Event.ONDBLCLICK);
351 Pithos.preventIESelection();
354 public List<File> getSelectedFiles() {
355 return new ArrayList<File>(selectionModel.getSelectedSet());
359 // public void onBrowserEvent(Event event) {
361 // if (files == null || files.size() == 0) {
362 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
363 // contextMenu = new FileContextMenu(images, false, true);
364 // contextMenu.show();
365 // event.preventDefault();
366 // event.cancelBubble(true);
370 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() != 0) {
371 // GWT.log("*****GOING TO SHOW CONTEXT MENU ****", null);
372 // contextMenu = new FileContextMenu(images, false, false);
373 // contextMenu = contextMenu.onEvent(event);
374 // event.cancelBubble(true);
375 // event.preventDefault();
376 // } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
377 // contextMenu = new FileContextMenu(images, false, true);
378 // contextMenu = contextMenu.onEmptyEvent(event);
379 // event.cancelBubble(true);
380 // event.preventDefault();
381 // } else if (DOM.eventGetType(event) == Event.ONDBLCLICK)
382 // if (getSelectedFiles().size() == 1) {
384 // File file = getSelectedFiles().get(0);
385 // Window.open(file.getUri(), "_blank", "");
386 // event.preventDefault();
389 // super.onBrowserEvent(event);
393 * Update the display of the file list.
395 void update(boolean sort) {
400 * Return the proper icon based on the MIME type of the file.
405 private ImageResource getFileIcon(File file) {
406 String mimetype = file.getContentType();
407 boolean shared = file.isShared();
408 if (mimetype == null)
409 return shared ? images.documentShared() : images.document();
410 mimetype = mimetype.toLowerCase();
411 if (mimetype.startsWith("application/pdf"))
412 return shared ? images.pdfShared() : images.pdf();
413 else if (mimetype.endsWith("excel"))
414 return shared ? images.spreadsheetShared() : images.spreadsheet();
415 else if (mimetype.endsWith("msword"))
416 return shared ? images.wordprocessorShared() : images.wordprocessor();
417 else if (mimetype.endsWith("powerpoint"))
418 return shared ? images.presentationShared() : images.presentation();
419 else if (mimetype.startsWith("application/zip") ||
420 mimetype.startsWith("application/gzip") ||
421 mimetype.startsWith("application/x-gzip") ||
422 mimetype.startsWith("application/x-tar") ||
423 mimetype.startsWith("application/x-gtar"))
424 return shared ? images.zipShared() : images.zip();
425 else if (mimetype.startsWith("text/html"))
426 return shared ? images.htmlShared() : images.html();
427 else if (mimetype.startsWith("text/plain"))
428 return shared ? images.txtShared() : images.txt();
429 else if (mimetype.startsWith("image/"))
430 return shared ? images.imageShared() : images.image();
431 else if (mimetype.startsWith("video/"))
432 return shared ? images.videoShared() : images.video();
433 else if (mimetype.startsWith("audio/"))
434 return shared ? images.audioShared() : images.audio();
435 return shared ? images.documentShared() : images.document();
439 * Fill the file cache with data.
441 public void setFiles(final List<File> _files) {
442 files = new ArrayList<File>();
443 for (File fres : _files)
444 if (!fres.isInTrash())
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.setList(files);
466 selectionModel.clear();
470 * Does the list contains the requested filename
475 public boolean contains(String fileName) {
476 for (int i = 0; i < files.size(); i++)
477 if (files.get(i).getName().equals(fileName))
482 public void clearSelectedRows() {
483 Iterator<File> it = selectionModel.getSelectedSet().iterator();
485 selectionModel.setSelected(it.next(),false);
492 public void selectAllRows() {
493 Iterator<File> it = provider.getList().iterator();
495 selectionModel.setSelected(it.next(),true);
499 private void sortFiles(final String sortingProperty, final boolean sortingType){
500 Collections.sort(files, new Comparator<File>() {
503 public int compare(File arg0, File arg1) {
504 AbstractImagePrototype descPrototype = AbstractImagePrototype.create(images.desc());
505 AbstractImagePrototype ascPrototype = AbstractImagePrototype.create(images.asc());
507 if (sortingProperty.equals("version")) {
508 return arg0.getVersion() - arg1.getVersion();
509 } else if (sortingProperty.equals("owner")) {
510 return arg0.getOwner().compareTo(arg1.getOwner());
511 } else if (sortingProperty.equals("date")) {
512 return arg0.getLastModified().compareTo(arg1.getLastModified());
513 } else if (sortingProperty.equals("size")) {
514 return (int) (arg0.getBytes() - arg1.getBytes());
515 } else if (sortingProperty.equals("name")) {
516 return arg0.getName().compareTo(arg1.getName());
517 } else if (sortingProperty.equals("path")) {
518 return arg0.getUri().compareTo(arg1.getUri());
520 return arg0.getName().compareTo(arg1.getName());
523 else if (sortingProperty.equals("version")) {
525 return arg1.getVersion() - arg0.getVersion();
526 } else if (sortingProperty.equals("owner")) {
528 return arg1.getOwner().compareTo(arg0.getOwner());
529 } else if (sortingProperty.equals("date")) {
531 return arg1.getLastModified().compareTo(arg0.getLastModified());
532 } else if (sortingProperty.equals("size")) {
533 return (int) (arg1.getBytes() - arg0.getBytes());
534 } else if (sortingProperty.equals("name")) {
536 return arg1.getName().compareTo(arg0.getName());
537 } else if (sortingProperty.equals("path")) {
539 return arg1.getUri().compareTo(arg0.getUri());
542 return arg1.getName().compareTo(arg0.getName());
549 final class FileValueUpdater implements ValueUpdater<String>{
550 private String property;
551 private SortableHeader header;
555 public FileValueUpdater(SortableHeader header,String property) {
556 this.property=property;
560 public void update(String value) {
561 header.setSorted(true);
562 header.toggleReverseSort();
564 for (SortableHeader otherHeader : allHeaders) {
565 if (otherHeader != header) {
566 otherHeader.setSorted(false);
567 otherHeader.setReverseSort(true);
570 celltable.redrawHeaders();
571 sortFiles(property, header.getReverseSort());
572 FileList.this.update(true);
578 * Shows the files in the cellTable
580 private void showCellTable(){
581 provider.setList(files);
585 //celltable.redraw();
586 celltable.redrawHeaders();