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 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.Event;
63 import com.google.gwt.user.client.ui.Composite;
64 import com.google.gwt.user.client.ui.VerticalPanel;
65 import com.google.gwt.view.client.ListDataProvider;
66 import com.google.gwt.view.client.MultiSelectionModel;
67 import com.google.gwt.view.client.ProvidesKey;
70 * A composite that displays the list of files in a particular folder.
72 public class FileList extends Composite {
74 ListDataProvider<File> provider = new ListDataProvider<File>();
77 * The styles applied to the table.
79 interface TableStyle extends CellTable.Style {
82 interface TableResources extends CellTable.Resources {
84 @Source({CellTable.Style.DEFAULT_CSS, "GssCellTable.css"})
85 TableStyle cellTableStyle();
88 static interface Templates extends SafeHtmlTemplates {
89 Templates INSTANCE = GWT.create(Templates.class);
91 @Template("<div id='dragHelper' style='border:1px solid black; background-color:#ffffff; color:black; width:150px;z-index:100'></div>")
92 SafeHtml outerHelper();
94 @Template("<span id='{0}'>{0}</span>")
95 public SafeHtml filenameSpan(String filename);
97 @Template("<a href='{0}' title='{1}' rel='lytebox[mnf]' onclick='myLytebox.start(this, false, false); return false;'>(view)</a>")
98 public SafeHtml viewLink(String link, String title);
100 @Template("<table><tr><td rowspan='3'>{0}</td><td style='font-size:95%;' id='{1}'>{1}</td></tr><tr><td>{2}</td></tr></table>")
101 public SafeHtml rendelContactCell(String imageHtml, String name, String fileSize);
103 @Template("<span id='{0}' class='{1}'>{2}</span>")
104 public SafeHtml spanWithIdAndClass(String id, String cssClass, String content);
107 protected final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
110 * Specifies that the images available for this composite will be the ones
111 * available in FileContextMenu.
113 public interface Images extends FolderTreeView.Images {
115 @Source("gr/grnet/pithos/resources/blank.gif")
116 ImageResource blank();
118 @Source("gr/grnet/pithos/resources/asc.png")
121 @Source("gr/grnet/pithos/resources/desc.png")
122 ImageResource desc();
124 @Source("gr/grnet/pithos/resources/mimetypes/document_shared.png")
125 ImageResource documentShared();
127 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst.png")
128 ImageResource wordprocessor();
130 @Source("gr/grnet/pithos/resources/mimetypes/log.png")
131 ImageResource spreadsheet();
133 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr.png")
134 ImageResource presentation();
136 @Source("gr/grnet/pithos/resources/mimetypes/acroread.png")
139 @Source("gr/grnet/pithos/resources/mimetypes/image.png")
140 ImageResource image();
142 @Source("gr/grnet/pithos/resources/mimetypes/video2.png")
143 ImageResource video();
145 @Source("gr/grnet/pithos/resources/mimetypes/knotify.png")
146 ImageResource audio();
148 @Source("gr/grnet/pithos/resources/mimetypes/html.png")
149 ImageResource html();
151 @Source("gr/grnet/pithos/resources/mimetypes/txt.png")
154 @Source("gr/grnet/pithos/resources/mimetypes/ark2.png")
157 @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst_shared.png")
158 ImageResource wordprocessorShared();
160 @Source("gr/grnet/pithos/resources/mimetypes/log_shared.png")
161 ImageResource spreadsheetShared();
163 @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr_shared.png")
164 ImageResource presentationShared();
166 @Source("gr/grnet/pithos/resources/mimetypes/acroread_shared.png")
167 ImageResource pdfShared();
169 @Source("gr/grnet/pithos/resources/mimetypes/image_shared.png")
170 ImageResource imageShared();
172 @Source("gr/grnet/pithos/resources/mimetypes/video2_shared.png")
173 ImageResource videoShared();
175 @Source("gr/grnet/pithos/resources/mimetypes/knotify_shared.png")
176 ImageResource audioShared();
178 @Source("gr/grnet/pithos/resources/mimetypes/html_shared.png")
179 ImageResource htmlShared();
181 @Source("gr/grnet/pithos/resources/mimetypes/txt_shared.png")
182 ImageResource txtShared();
184 @Source("gr/grnet/pithos/resources/mimetypes/ark2_shared.png")
185 ImageResource zipShared();
190 * The number of files in this folder.
197 long folderTotalSize;
200 * A cache of the files in the list.
202 private List<File> files;
205 * The widget's image bundle.
207 protected final Images images;
209 protected CellTable<File> celltable;
211 private final MultiSelectionModel<File> selectionModel;
213 protected final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
215 SortableHeader nameHeader;
217 FolderTreeView treeView;
219 protected Pithos app;
222 * Construct the file list widget. This entails setting up the widget
223 * layout, fetching the number of files in the current folder from the
224 * server and filling the local file cache of displayed files with data from
225 * the server, as well.
229 public FileList(final Pithos _app, Images _images, FolderTreeView _treeView) {
232 this.treeView = _treeView;
234 CellTable.Resources resources = GWT.create(TableResources.class);
236 ProvidesKey<File> keyProvider = new ProvidesKey<File>(){
239 public Object getKey(File item) {
240 return item.getUri();
244 celltable = new CellTable<File>(10, resources, keyProvider);
245 celltable.setWidth("100%");
246 celltable.setStyleName("pithos-List");
248 Column<File, ImageResource> status = new Column<File, ImageResource>(new ImageResourceCell() {
250 public boolean handlesSelection() {
256 public ImageResource getValue(File entity) {
257 return getFileIcon(entity);
260 celltable.addColumn(status,"");
262 final Column<File,SafeHtml> nameColumn = new Column<File,SafeHtml>(new SafeHtmlCell()) {
265 public SafeHtml getValue(File object) {
266 SafeHtmlBuilder sb = new SafeHtmlBuilder();
267 sb.append(Templates.INSTANCE.filenameSpan(object.getName()));
268 if (object.getContentType().endsWith("png") || object.getContentType().endsWith("gif") || object.getContentType().endsWith("jpeg")) {
269 sb.appendHtmlConstant(" ")
270 .append(Templates.INSTANCE.viewLink(object.getUri(), object.getOwner() + " : " + object.getPath() + object.getName()));
273 return sb.toSafeHtml();
277 celltable.addColumn(nameColumn, nameHeader = new SortableHeader("Name"));
278 allHeaders.add(nameHeader);
279 nameHeader.setUpdater(new FileValueUpdater(nameHeader, "name"));
281 celltable.redrawHeaders();
283 Column<File,String> aColumn = new Column<File,String>(new TextCell()) {
285 public String getValue(File object) {
286 // TODO Auto-generated method stub
287 return object.getSizeAsString();
290 SortableHeader aheader = new SortableHeader("Size");
291 celltable.addColumn(aColumn, aheader);
292 allHeaders.add(aheader);
293 aheader.setUpdater(new FileValueUpdater(aheader, "size"));
295 aColumn = new Column<File,String>(new TextCell()) {
297 public String getValue(File object) {
298 return formatter.format(object.getLastModified());
301 aheader = new SortableHeader("Last Modified");
302 celltable.addColumn(aColumn, aheader);
303 allHeaders.add(aheader);
304 aheader.setUpdater(new FileValueUpdater(aheader, "date"));
306 provider.addDataDisplay(celltable);
308 VerticalPanel vp = new VerticalPanel();
313 vp.setCellWidth(celltable, "100%");
314 vp.addHandler(new ContextMenuHandler() {
316 public void onContextMenu(ContextMenuEvent event) {
317 TreeView tree = app.getSelectedTree();
318 if (tree != null && (tree.equals(app.getFolderTreeView()) || tree.equals(app.getOtherSharedTreeView()))) {
319 Folder selectedFolder = app.getSelection();
320 FileContextMenu contextMenu = new FileContextMenu(app, images, tree, selectedFolder, getSelectedFiles());
321 int x = event.getNativeEvent().getClientX();
322 int y = event.getNativeEvent().getClientY();
323 contextMenu.setPopupPosition(x, y);
327 }, ContextMenuEvent.getType());
330 selectionModel = new MultiSelectionModel<File>(keyProvider);
332 celltable.setSelectionModel(selectionModel, GSSSelectionEventManager.<File> createDefaultManager());
333 // celltable.setPageSize(Pithos.VISIBLE_FILE_COUNT);
335 sinkEvents(Event.ONCONTEXTMENU);
336 // sinkEvents(Event.ONMOUSEUP);
337 // sinkEvents(Event.ONMOUSEDOWN);
338 // sinkEvents(Event.ONCLICK);
339 // sinkEvents(Event.ONKEYDOWN);
340 // sinkEvents(Event.ONDBLCLICK);
341 Pithos.preventIESelection();
344 public List<File> getSelectedFiles() {
345 return new ArrayList<File>(selectionModel.getSelectedSet());
349 // public void onBrowserEvent(Event event) {
351 // if (files == null || files.size() == 0) {
352 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
353 // contextMenu = new FileContextMenu(images, false, true);
354 // contextMenu.show();
355 // event.preventDefault();
356 // event.cancelBubble(true);
360 // if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() != 0) {
361 // GWT.log("*****GOING TO SHOW CONTEXT MENU ****", null);
362 // contextMenu = new FileContextMenu(images, false, false);
363 // contextMenu = contextMenu.onEvent(event);
364 // event.cancelBubble(true);
365 // event.preventDefault();
366 // } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
367 // contextMenu = new FileContextMenu(images, false, true);
368 // contextMenu = contextMenu.onEmptyEvent(event);
369 // event.cancelBubble(true);
370 // event.preventDefault();
371 // } else if (DOM.eventGetType(event) == Event.ONDBLCLICK)
372 // if (getSelectedFiles().size() == 1) {
374 // File file = getSelectedFiles().get(0);
375 // Window.open(file.getUri(), "_blank", "");
376 // event.preventDefault();
379 // super.onBrowserEvent(event);
383 * Update the display of the file list.
385 void update(@SuppressWarnings("unused") boolean sort) {
390 * Return the proper icon based on the MIME type of the file.
395 protected ImageResource getFileIcon(File file) {
396 String mimetype = file.getContentType();
397 boolean shared = file.isShared();
398 if (mimetype == null)
399 return shared ? images.documentShared() : images.document();
400 mimetype = mimetype.toLowerCase();
401 if (mimetype.startsWith("application/pdf"))
402 return shared ? images.pdfShared() : images.pdf();
403 else if (mimetype.endsWith("excel"))
404 return shared ? images.spreadsheetShared() : images.spreadsheet();
405 else if (mimetype.endsWith("msword"))
406 return shared ? images.wordprocessorShared() : images.wordprocessor();
407 else if (mimetype.endsWith("powerpoint"))
408 return shared ? images.presentationShared() : images.presentation();
409 else if (mimetype.startsWith("application/zip") ||
410 mimetype.startsWith("application/gzip") ||
411 mimetype.startsWith("application/x-gzip") ||
412 mimetype.startsWith("application/x-tar") ||
413 mimetype.startsWith("application/x-gtar"))
414 return shared ? images.zipShared() : images.zip();
415 else if (mimetype.startsWith("text/html"))
416 return shared ? images.htmlShared() : images.html();
417 else if (mimetype.startsWith("text/plain"))
418 return shared ? images.txtShared() : images.txt();
419 else if (mimetype.startsWith("image/"))
420 return shared ? images.imageShared() : images.image();
421 else if (mimetype.startsWith("video/"))
422 return shared ? images.videoShared() : images.video();
423 else if (mimetype.startsWith("audio/"))
424 return shared ? images.audioShared() : images.audio();
425 return shared ? images.documentShared() : images.document();
429 * Fill the file cache with data.
431 public void setFiles(final List<File> _files) {
432 files = new ArrayList<File>();
433 for (File fres : _files)
435 Collections.sort(files, new Comparator<File>() {
438 public int compare(File arg0, File arg1) {
439 return arg0.getName().compareTo(arg1.getName());
443 folderFileCount = files.size();
445 nameHeader.setSorted(true);
446 nameHeader.toggleReverseSort();
447 for (SortableHeader otherHeader : allHeaders) {
448 if (otherHeader != nameHeader) {
449 otherHeader.setSorted(false);
450 otherHeader.setReverseSort(true);
454 provider.setList(files);
455 selectionModel.clear();
456 app.showFolderStatistics(folderFileCount);
457 celltable.setPageSize(folderFileCount);
461 * Does the list contains the requested filename
466 public boolean contains(String fileName) {
467 for (int i = 0; i < files.size(); i++)
468 if (files.get(i).getName().equals(fileName))
473 public void clearSelectedRows() {
474 Iterator<File> it = selectionModel.getSelectedSet().iterator();
476 selectionModel.setSelected(it.next(),false);
483 public void selectAllRows() {
484 Iterator<File> it = provider.getList().iterator();
486 selectionModel.setSelected(it.next(),true);
490 protected void sortFiles(final String sortingProperty, final boolean sortingType){
491 Collections.sort(files, new Comparator<File>() {
494 public int compare(File arg0, File arg1) {
496 if (sortingProperty.equals("version")) {
497 return arg0.getVersion() - arg1.getVersion();
498 } else if (sortingProperty.equals("owner")) {
499 return arg0.getOwner().compareTo(arg1.getOwner());
500 } else if (sortingProperty.equals("date")) {
501 return arg0.getLastModified().compareTo(arg1.getLastModified());
502 } else if (sortingProperty.equals("size")) {
503 return (int) (arg0.getBytes() - arg1.getBytes());
504 } else if (sortingProperty.equals("name")) {
505 return arg0.getName().compareTo(arg1.getName());
506 } else if (sortingProperty.equals("path")) {
507 return arg0.getUri().compareTo(arg1.getUri());
509 return arg0.getName().compareTo(arg1.getName());
512 else if (sortingProperty.equals("version")) {
514 return arg1.getVersion() - arg0.getVersion();
515 } else if (sortingProperty.equals("owner")) {
517 return arg1.getOwner().compareTo(arg0.getOwner());
518 } else if (sortingProperty.equals("date")) {
520 return arg1.getLastModified().compareTo(arg0.getLastModified());
521 } else if (sortingProperty.equals("size")) {
522 return (int) (arg1.getBytes() - arg0.getBytes());
523 } else if (sortingProperty.equals("name")) {
525 return arg1.getName().compareTo(arg0.getName());
526 } else if (sortingProperty.equals("path")) {
528 return arg1.getUri().compareTo(arg0.getUri());
531 return arg1.getName().compareTo(arg0.getName());
538 final class FileValueUpdater implements ValueUpdater<String>{
539 private String property;
540 private SortableHeader header;
544 public FileValueUpdater(SortableHeader header,String property) {
545 this.property=property;
549 public void update(@SuppressWarnings("unused") String value) {
550 header.setSorted(true);
551 header.toggleReverseSort();
553 for (SortableHeader otherHeader : allHeaders) {
554 if (otherHeader != header) {
555 otherHeader.setSorted(false);
556 otherHeader.setReverseSort(true);
559 celltable.redrawHeaders();
560 sortFiles(property, header.getReverseSort());
561 FileList.this.update(true);
567 * Shows the files in the cellTable
569 private void showCellTable(){
570 provider.setList(files);
574 //celltable.redraw();
575 celltable.redrawHeaders();