Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / FileList.java @ a39e5b47

History | View | Annotate | Download (27.7 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
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.
16
 *
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.
29
 *
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.
34
 */
35

    
36
package gr.grnet.pithos.web.client;
37

    
38
import com.google.gwt.event.dom.client.ContextMenuEvent;
39
import com.google.gwt.event.dom.client.ContextMenuHandler;
40
import static com.google.gwt.query.client.GQuery.$;
41

    
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;
46
import gr.grnet.pithos.web.client.rest.resource.FileResource;
47
import gwtquery.plugins.draggable.client.DraggableOptions;
48
import gwtquery.plugins.draggable.client.StopDragException;
49
import gwtquery.plugins.draggable.client.DraggableOptions.DragFunction;
50
import gwtquery.plugins.draggable.client.DraggableOptions.RevertOption;
51
import gwtquery.plugins.draggable.client.events.DragContext;
52
import gwtquery.plugins.draggable.client.events.DragStartEvent;
53
import gwtquery.plugins.draggable.client.events.DragStopEvent;
54
import gwtquery.plugins.draggable.client.events.DragStartEvent.DragStartEventHandler;
55
import gwtquery.plugins.draggable.client.events.DragStopEvent.DragStopEventHandler;
56
import gwtquery.plugins.droppable.client.gwt.DragAndDropCellTable;
57
import gwtquery.plugins.droppable.client.gwt.DragAndDropColumn;
58

    
59
import java.util.ArrayList;
60
import java.util.Collections;
61
import java.util.Comparator;
62
import java.util.Iterator;
63
import java.util.List;
64

    
65
import com.google.gwt.cell.client.ImageResourceCell;
66
import com.google.gwt.cell.client.SafeHtmlCell;
67
import com.google.gwt.cell.client.TextCell;
68
import com.google.gwt.cell.client.ValueUpdater;
69
import com.google.gwt.core.client.GWT;
70
import com.google.gwt.dom.client.Style.Cursor;
71
import com.google.gwt.event.dom.client.ClickEvent;
72
import com.google.gwt.event.dom.client.ClickHandler;
73
import com.google.gwt.i18n.client.DateTimeFormat;
74
import com.google.gwt.resources.client.ClientBundle;
75
import com.google.gwt.resources.client.ImageResource;
76
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
77
import com.google.gwt.safehtml.shared.SafeHtml;
78
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
79
import com.google.gwt.user.cellview.client.CellTable;
80
import com.google.gwt.user.cellview.client.GssSimplePager;
81
import com.google.gwt.user.client.Event;
82
import com.google.gwt.user.client.ui.AbstractImagePrototype;
83
import com.google.gwt.user.client.ui.Button;
84
import com.google.gwt.user.client.ui.Composite;
85
import com.google.gwt.user.client.ui.HorizontalPanel;
86
import com.google.gwt.user.client.ui.VerticalPanel;
87
import com.google.gwt.view.client.ListDataProvider;
88
import com.google.gwt.view.client.MultiSelectionModel;
89
import com.google.gwt.view.client.ProvidesKey;
90
import com.google.gwt.view.client.SelectionChangeEvent;
91
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
92

    
93
/**
94
 * A composite that displays the list of files in a particular folder.
95
 */
96
public class FileList extends Composite {
97

    
98
        ListDataProvider<File> provider = new ListDataProvider<File>();
99

    
100
    /**
101
       * The styles applied to the table.
102
       */
103
    interface TableStyle extends CellTable.Style {
104
    }
105

    
106
        interface TableResources extends DragAndDropCellTable.Resources {
107
            @Source({CellTable.Style.DEFAULT_CSS, "GssCellTable.css"})
108
            TableStyle cellTableStyle();
109
        }
110
        
111
        static interface Templates extends SafeHtmlTemplates {
112
            Templates INSTANCE = GWT.create(Templates.class);
113

    
114
            @Template("<div id='dragHelper' style='border:1px solid black; background-color:#ffffff; color:black; width:150px;z-index:100'></div>")
115
            SafeHtml outerHelper();
116

    
117
        @Template("<span id='{0}'>{0}</span>")
118
        public SafeHtml filenameSpan(String filename);
119

    
120
        @Template("<a href='{0}' title='{1}' rel='lytebox[mnf]' onclick='myLytebox.start(this, false, false); return false;'>(view)</a>")
121
        public SafeHtml viewLink(String link, String title);
122

    
123
        @Template("<table><tr><td rowspan='3'>{0}</td><td style='font-size:95%;' id='{1}'>{1}</td></tr><tr><td>{2}</td></tr></table>")
124
        public SafeHtml rendelContactCell(String imageHtml, String name, String fileSize);
125

    
126
        @Template("<span id='{0}' class='{1}'>{2}</span>")
127
        public SafeHtml spanWithIdAndClass(String id, String cssClass, String content);
128
        }
129

    
130
    private String showingStats = "";
131

    
132
        private int startIndex = 0;
133

    
134
        /**
135
         * A constant that denotes the completion of an IncrementalCommand.
136
         */
137
        public static final boolean DONE = false;
138

    
139
        private final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
140

    
141
        /**
142
         * Specifies that the images available for this composite will be the ones
143
         * available in FileContextMenu.
144
         */
145
        public interface Images extends ClientBundle,FileContextMenu.Images, CellTreeView.Images {
146

    
147
                @Source("gr/grnet/pithos/resources/blank.gif")
148
                ImageResource blank();
149

    
150
                @Source("gr/grnet/pithos/resources/asc.png")
151
                ImageResource asc();
152

    
153
                @Source("gr/grnet/pithos/resources/desc.png")
154
                ImageResource desc();
155

    
156
                @Source("gr/grnet/pithos/resources/mimetypes/document_shared.png")
157
                ImageResource documentShared();
158

    
159
                @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst.png")
160
                ImageResource wordprocessor();
161

    
162
                @Source("gr/grnet/pithos/resources/mimetypes/log.png")
163
                ImageResource spreadsheet();
164

    
165
                @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr.png")
166
                ImageResource presentation();
167

    
168
                @Source("gr/grnet/pithos/resources/mimetypes/acroread.png")
169
                ImageResource pdf();
170

    
171
                @Source("gr/grnet/pithos/resources/mimetypes/image.png")
172
                ImageResource image();
173

    
174
                @Source("gr/grnet/pithos/resources/mimetypes/video2.png")
175
                ImageResource video();
176

    
177
                @Source("gr/grnet/pithos/resources/mimetypes/knotify.png")
178
                ImageResource audio();
179

    
180
                @Source("gr/grnet/pithos/resources/mimetypes/html.png")
181
                ImageResource html();
182

    
183
                @Source("gr/grnet/pithos/resources/mimetypes/txt.png")
184
                ImageResource txt();
185

    
186
                @Source("gr/grnet/pithos/resources/mimetypes/ark2.png")
187
                ImageResource zip();
188

    
189
                @Source("gr/grnet/pithos/resources/mimetypes/kcmfontinst_shared.png")
190
                ImageResource wordprocessorShared();
191

    
192
                @Source("gr/grnet/pithos/resources/mimetypes/log_shared.png")
193
                ImageResource spreadsheetShared();
194

    
195
                @Source("gr/grnet/pithos/resources/mimetypes/kpresenter_kpr_shared.png")
196
                ImageResource presentationShared();
197

    
198
                @Source("gr/grnet/pithos/resources/mimetypes/acroread_shared.png")
199
                ImageResource pdfShared();
200

    
201
                @Source("gr/grnet/pithos/resources/mimetypes/image_shared.png")
202
                ImageResource imageShared();
203

    
204
                @Source("gr/grnet/pithos/resources/mimetypes/video2_shared.png")
205
                ImageResource videoShared();
206

    
207
                @Source("gr/grnet/pithos/resources/mimetypes/knotify_shared.png")
208
                ImageResource audioShared();
209

    
210
                @Source("gr/grnet/pithos/resources/mimetypes/html_shared.png")
211
                ImageResource htmlShared();
212

    
213
                @Source("gr/grnet/pithos/resources/mimetypes/txt_shared.png")
214
                ImageResource txtShared();
215

    
216
                @Source("gr/grnet/pithos/resources/mimetypes/ark2_shared.png")
217
                ImageResource zipShared();
218

    
219
        }
220
        
221
        /**
222
         * The number of files in this folder.
223
         */
224
        int folderFileCount;
225

    
226
        /**
227
         * Total folder size
228
         */
229
        long folderTotalSize;
230

    
231
        /**
232
         * A cache of the files in the list.
233
         */
234
        private List<File> files;
235

    
236
        /**
237
         * The widget's image bundle.
238
         */
239
        private final Images images;
240
        
241
        private DragAndDropCellTable<File> celltable;
242

    
243
        private final MultiSelectionModel<File> selectionModel;
244

    
245
        private final List<SortableHeader> allHeaders = new ArrayList<SortableHeader>();
246

    
247
        SortableHeader nameHeader;
248

    
249
        GssSimplePager pagerBottom;
250

    
251
        GssSimplePager pagerTop;
252

    
253
        Button uploadButtonBottom;
254

    
255
        Button uploadButtonTop;
256

    
257
    FolderTreeView treeView;
258

    
259
    /**
260
         * Construct the file list widget. This entails setting up the widget
261
         * layout, fetching the number of files in the current folder from the
262
         * server and filling the local file cache of displayed files with data from
263
         * the server, as well.
264
         *
265
         * @param _images
266
         */
267
        public FileList(Images _images, FolderTreeView _treeView) {
268
                images = _images;
269
        this.treeView = _treeView;
270

    
271
        DragAndDropCellTable.Resources resources = GWT.create(TableResources.class);
272

    
273
        ProvidesKey<File> keyProvider = new ProvidesKey<File>(){
274

    
275
                        @Override
276
                        public Object getKey(File item) {
277
                                return item.getUri();
278
                        }
279
                };
280

    
281
                celltable = new DragAndDropCellTable<File>(Pithos.VISIBLE_FILE_COUNT, resources, keyProvider);
282
        celltable.setWidth("100%");
283
        celltable.setStyleName("pithos-List");
284

    
285
                DragAndDropColumn<File, ImageResource> status = new DragAndDropColumn<File, ImageResource>(new ImageResourceCell() {
286
                    @Override
287
                public boolean handlesSelection() {
288
                    return false;
289
                }
290
                })
291
        {
292
                 @Override
293
                 public ImageResource getValue(File entity) {
294
                     return getFileIcon(entity);
295
                 }
296
            };
297
            celltable.addColumn(status,"");
298
            initDragOperation(status);
299

    
300
        final DragAndDropColumn<File,SafeHtml> nameColumn = new DragAndDropColumn<File,SafeHtml>(new SafeHtmlCell()) {
301

    
302
                        @Override
303
                        public SafeHtml getValue(File object) {
304
                                SafeHtmlBuilder sb = new SafeHtmlBuilder();
305
                sb.append(Templates.INSTANCE.filenameSpan(object.getName()));
306
                                if (object.getContentType().endsWith("png") || object.getContentType().endsWith("gif") || object.getContentType().endsWith("jpeg")) {
307
                                sb.appendHtmlConstant("&nbsp;")
308
                      .append(Templates.INSTANCE.viewLink(object.getUri(), object.getOwner() + " : " + object.getPath() + object.getName()));
309
                                }
310
                                
311
                                return sb.toSafeHtml();
312
                        }
313
                        
314
                };
315
        initDragOperation(nameColumn);
316
        celltable.addColumn(nameColumn, nameHeader = new SortableHeader("Name"));
317
                allHeaders.add(nameHeader);
318
                nameHeader.setUpdater(new FileValueUpdater(nameHeader, "name"));
319

    
320
                celltable.redrawHeaders();
321
                
322
            DragAndDropColumn<File,String> aColumn = new DragAndDropColumn<File, String>(new TextCell()) {
323
                        @Override
324
                        public String getValue(File object) {
325
                                return object.getOwner();
326
                        }
327
                };
328
        SortableHeader aheader = new SortableHeader("Owner");
329
                celltable.addColumn(aColumn, aheader);
330
                initDragOperation(aColumn);
331
                allHeaders.add(aheader);
332
        aheader.setUpdater(new FileValueUpdater(aheader, "owner"));
333

    
334
        aColumn = new DragAndDropColumn<File,String>(new TextCell()) {
335
                        @Override
336
                        public String getValue(File object) {
337
                                return object.getPath();
338
                        }
339
                };
340
        aheader = new SortableHeader("Path");
341
                celltable.addColumn(aColumn, aheader);
342
                initDragOperation(aColumn);
343
                allHeaders.add(aheader);
344
                aheader.setUpdater(new FileValueUpdater(aheader, "path"));
345

    
346
        aColumn = new DragAndDropColumn<File,String>(new TextCell()) {
347
                        @Override
348
                        public String getValue(File object) {
349
                            return String.valueOf(object.getVersion());
350
                        }
351
                };
352
        aheader = new SortableHeader("Version");
353
                celltable.addColumn(aColumn, aheader);
354
                initDragOperation(aColumn);
355
                allHeaders.add(aheader);
356
                aheader.setUpdater(new FileValueUpdater(aheader, "version"));
357

    
358
        aColumn = new DragAndDropColumn<File,String>(new TextCell()) {
359
                        @Override
360
                        public String getValue(File object) {
361
                                // TODO Auto-generated method stub
362
                                return object.getSizeAsString();
363
                        }
364
                };
365
        aheader = new SortableHeader("Size");
366
        celltable.addColumn(aColumn, aheader);
367
                initDragOperation(aColumn);
368
                allHeaders.add(aheader);
369
                aheader.setUpdater(new FileValueUpdater(aheader, "size"));
370

    
371
        aColumn = new DragAndDropColumn<File,String>(new TextCell()) {
372
                        @Override
373
                        public String getValue(File object) {
374
                                return formatter.format(object.getLastModified());
375
                        }
376
                };
377
        aheader = new SortableHeader("Last Modified");
378
                celltable.addColumn(aColumn, aheader);
379
                allHeaders.add(aheader);
380
                aheader.setUpdater(new FileValueUpdater(aheader, "date"));
381
               
382
                provider.addDataDisplay(celltable);
383

    
384
                celltable.addDragStopHandler(new DragStopEventHandler() {
385

    
386
                    @Override
387
                    public void onDragStop(DragStopEvent event) {
388
                            GWT.log("DRAG STOPPED");
389
                    }
390
            });
391
                celltable.addDragStartHandler(new DragStartEventHandler() {
392

    
393
                    public void onDragStart(DragStartEvent event) {
394
                        FileResource value = event.getDraggableData();
395
                        
396
                        com.google.gwt.dom.client.Element helper = event.getHelper();
397
                        SafeHtmlBuilder sb = new SafeHtmlBuilder();
398
                        sb.appendHtmlConstant("<b>");
399
                        DisplayHelper.log(value.getName());
400
                        if(getSelectedFiles().size()==1)
401
                                sb.appendEscaped(value.getName());
402
                        else
403
                                sb.appendEscaped(getSelectedFiles().size()+" files");
404
                        sb.appendHtmlConstant("</b>");
405
                        helper.setInnerHTML(sb.toSafeHtml().asString());
406
                    }
407
                });
408

    
409
                VerticalPanel vp = new VerticalPanel();
410
                vp.setWidth("100%");
411

    
412
                pagerTop = new GssSimplePager(GssSimplePager.TextLocation.CENTER);
413
        pagerTop.setVisible(false);
414
                pagerTop.setDisplay(celltable);
415
                uploadButtonTop = new Button("<span id='topMenu.file.upload'>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + "&nbsp;Upload</span>");
416
                uploadButtonTop.addClickHandler(new ClickHandler() {
417
                        
418
                        @Override
419
                        public void onClick(ClickEvent event) {
420
                                new UploadFileCommand(null, treeView.getSelection()).execute();
421
                        }
422
                });
423
                HorizontalPanel topPanel = new HorizontalPanel();
424
                topPanel.add(pagerTop);
425
                topPanel.add(uploadButtonTop);
426
                vp.add(topPanel);
427

    
428
        vp.add(celltable);
429

    
430
                pagerBottom = new GssSimplePager(GssSimplePager.TextLocation.CENTER);
431
        pagerBottom.setVisible(false);
432
                pagerBottom.setDisplay(celltable);
433
                uploadButtonBottom=new Button("<span id='topMenu.file.upload'>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + "&nbsp;Upload</span>");
434
                uploadButtonBottom.addClickHandler(new ClickHandler() {
435
                        
436
                        @Override
437
                        public void onClick(ClickEvent event) {
438
                                new UploadFileCommand(null, treeView.getSelection()).execute();
439
                        }
440
                });
441
        HorizontalPanel bottomPanel = new HorizontalPanel();
442
        bottomPanel.add(pagerBottom);
443
                bottomPanel.add(uploadButtonBottom);
444

    
445
                vp.add(bottomPanel);
446
                vp.setCellWidth(celltable, "100%");
447
        vp.addHandler(new ContextMenuHandler() {
448
            @Override
449
            public void onContextMenu(ContextMenuEvent event) {
450
                Folder selectedFolder = treeView.getSelection();
451
                if (!selectedFolder.isTrash()) {
452
                    FileContextMenu contextMenu = new FileContextMenu(images, selectedFolder, getSelectedFiles(), false);
453
                    int x = event.getNativeEvent().getClientX();
454
                    int y = event.getNativeEvent().getClientY();
455
                    contextMenu.setPopupPosition(x, y);
456
                    contextMenu.show();
457
                }
458
            }
459
        }, ContextMenuEvent.getType());
460
                initWidget(vp);
461

    
462
                selectionModel = new MultiSelectionModel<File>(keyProvider);
463

    
464
                 Handler selectionHandler = new SelectionChangeEvent.Handler() {
465
             @Override 
466
             public void onSelectionChange(SelectionChangeEvent event) {
467
                     if(getSelectedFiles().size() == 1)
468
                             Pithos.get().setCurrentSelection(getSelectedFiles().get(0));
469
                     else
470
                             Pithos.get().setCurrentSelection(getSelectedFiles());
471
             }
472
         };
473
         selectionModel.addSelectionChangeHandler(selectionHandler);
474
         
475
                celltable.setSelectionModel(selectionModel, GSSSelectionEventManager.<File> createDefaultManager());
476
                celltable.setPageSize(Pithos.VISIBLE_FILE_COUNT);
477
                
478
                sinkEvents(Event.ONCONTEXTMENU);
479
//                sinkEvents(Event.ONMOUSEUP);
480
//                sinkEvents(Event.ONMOUSEDOWN);
481
//                sinkEvents(Event.ONCLICK);
482
//                sinkEvents(Event.ONKEYDOWN);
483
//                sinkEvents(Event.ONDBLCLICK);
484
                Pithos.preventIESelection();
485
        }
486

    
487
        public List<File> getSelectedFiles() {
488
        return new ArrayList<File>(selectionModel.getSelectedSet());
489
        }
490
        
491
        private void initDragOperation(DragAndDropColumn<?, ?> column) {
492
        // retrieve draggableOptions on the column
493
                DraggableOptions draggableOptions = column.getDraggableOptions();
494
                // use template to construct the helper. The content of the div will be set
495
                // after
496
                draggableOptions.setHelper($(Templates.INSTANCE.outerHelper().asString()));
497
                //draggableOptions.setZIndex(100);
498
                // opacity of the helper
499
                draggableOptions.setAppendTo("body");
500
                //draggableOptions.setOpacity((float) 0.8);
501
                draggableOptions.setContainment("document");
502
                // cursor to use during the drag operation
503
                draggableOptions.setCursor(Cursor.MOVE);
504
                // set the revert option
505
                draggableOptions.setRevert(RevertOption.ON_INVALID_DROP);
506
                // prevents dragging when user click on the category drop-down list
507
                draggableOptions.setCancel("select");
508
            draggableOptions.setOnBeforeDragStart(new DragFunction() {
509
                        @Override
510
                        public void f(DragContext context) {
511
                        File value = context.getDraggableData();
512
                                if (!selectionModel.isSelected(value)) {
513
                                   throw new StopDragException();
514
                        }
515
                        }
516
                });
517
    }
518
        
519
//        @Override
520
//        public void onBrowserEvent(Event event) {
521
//
522
//                if (files == null || files.size() == 0) {
523
//                        if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
524
//                                contextMenu = new FileContextMenu(images, false, true);
525
//                contextMenu.show();
526
//                                event.preventDefault();
527
//                                event.cancelBubble(true);
528
//                        }
529
//                        return;
530
//                }
531
//                if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() != 0) {
532
//                        GWT.log("*****GOING TO SHOW CONTEXT MENU ****", null);
533
//                        contextMenu =  new FileContextMenu(images, false, false);
534
//                        contextMenu = contextMenu.onEvent(event);
535
//                        event.cancelBubble(true);
536
//                        event.preventDefault();
537
//                } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
538
//                        contextMenu = new FileContextMenu(images, false, true);
539
//                        contextMenu = contextMenu.onEmptyEvent(event);
540
//                        event.cancelBubble(true);
541
//                        event.preventDefault();
542
//                } else if (DOM.eventGetType(event) == Event.ONDBLCLICK)
543
//                        if (getSelectedFiles().size() == 1) {
544
//                                Pithos app = Pithos.get();
545
//                                File file = getSelectedFiles().get(0);
546
//                                Window.open(file.getUri(), "_blank", "");
547
//                                event.preventDefault();
548
//                                return;
549
//                        }
550
//                super.onBrowserEvent(event);
551
//        }
552

    
553
        /**
554
         * Update the display of the file list.
555
         */
556
        void update(boolean sort) {
557
                int count = folderFileCount;
558
                int max = startIndex + Pithos.VISIBLE_FILE_COUNT;
559
                if (max > count)
560
                        max = count;
561
                folderTotalSize = 0;
562
                
563
                for(File f : files){
564
                        folderTotalSize += f.getBytes();
565
                }
566
                if (folderFileCount == 0) {
567
                        showingStats = "no files";
568
                } else if (folderFileCount < Pithos.VISIBLE_FILE_COUNT) {
569
                        if (folderFileCount == 1)
570
                                showingStats = "1 file";
571
                        else
572
                                showingStats = folderFileCount + " files";
573
                        showingStats += " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
574
                } else {
575
                        showingStats = "" + (startIndex + 1) + " - " + max + " of " + count + " files" + " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
576
                }
577
                showCellTable();
578
                updateCurrentlyShowingStats();
579

    
580
        }
581

    
582
        /**
583
         * Return the proper icon based on the MIME type of the file.
584
         *
585
         * @param file
586
         * @return the icon
587
         */
588
        private ImageResource getFileIcon(File file) {
589
                String mimetype = file.getContentType();
590
                boolean shared = file.isShared();
591
                if (mimetype == null)
592
                        return shared ? images.documentShared() : images.document();
593
                mimetype = mimetype.toLowerCase();
594
                if (mimetype.startsWith("application/pdf"))
595
                        return shared ? images.pdfShared() : images.pdf();
596
                else if (mimetype.endsWith("excel"))
597
                        return shared ? images.spreadsheetShared() : images.spreadsheet();
598
                else if (mimetype.endsWith("msword"))
599
                        return shared ? images.wordprocessorShared() : images.wordprocessor();
600
                else if (mimetype.endsWith("powerpoint"))
601
                        return shared ? images.presentationShared() : images.presentation();
602
                else if (mimetype.startsWith("application/zip") ||
603
                                        mimetype.startsWith("application/gzip") ||
604
                                        mimetype.startsWith("application/x-gzip") ||
605
                                        mimetype.startsWith("application/x-tar") ||
606
                                        mimetype.startsWith("application/x-gtar"))
607
                        return shared ? images.zipShared() : images.zip();
608
                else if (mimetype.startsWith("text/html"))
609
                        return shared ? images.htmlShared() : images.html();
610
                else if (mimetype.startsWith("text/plain"))
611
                        return shared ? images.txtShared() : images.txt();
612
                else if (mimetype.startsWith("image/"))
613
                        return shared ? images.imageShared() : images.image();
614
                else if (mimetype.startsWith("video/"))
615
                        return shared ? images.videoShared() : images.video();
616
                else if (mimetype.startsWith("audio/"))
617
                        return shared ? images.audioShared() : images.audio();
618
                return shared ? images.documentShared() : images.document();
619
        }
620

    
621
        /**
622
         * Update status panel with currently showing file stats.
623
         */
624
        public void updateCurrentlyShowingStats() {
625
                Pithos.get().getStatusPanel().updateCurrentlyShowing(showingStats);
626
        }
627
        
628
        /**
629
         * Fill the file cache with data.
630
         */
631
        public void setFiles(final List<File> _files) {
632
                files = new ArrayList<File>();
633
            for (File fres : _files)
634
                    if (!fres.isInTrash())
635
                                files.add(fres);
636
                Collections.sort(files, new Comparator<File>() {
637

    
638
                        @Override
639
                        public int compare(File arg0, File arg1) {
640
                                return arg0.getName().compareTo(arg1.getName());
641
                        }
642

    
643
                });
644
                folderFileCount = files.size();
645
                
646
                nameHeader.setSorted(true);
647
                nameHeader.toggleReverseSort();
648
                for (SortableHeader otherHeader : allHeaders) {
649
                if (otherHeader != nameHeader) {
650
                    otherHeader.setSorted(false);
651
                    otherHeader.setReverseSort(true);
652
                }
653
            }
654

    
655
        if(files.size() > Pithos.VISIBLE_FILE_COUNT){
656
            pagerBottom.setVisible(true);
657
            pagerTop.setVisible(true);
658
        }
659
        else{
660
            pagerTop.setVisible(false);
661
            pagerBottom.setVisible(false);
662
        }
663
        Folder selectedItem = treeView.getSelection();
664

    
665
        provider.setList(files);
666
        selectionModel.clear();
667
        }
668

    
669
        /**
670
         * Does the list contains the requested filename
671
         *
672
         * @param fileName
673
         * @return true/false
674
         */
675
        public boolean contains(String fileName) {
676
                for (int i = 0; i < files.size(); i++)
677
                        if (files.get(i).getName().equals(fileName))
678
                                return true;
679
                return false;
680
        }
681

    
682
        public void clearSelectedRows() {
683
                Iterator<File> it = selectionModel.getSelectedSet().iterator();
684
                while(it.hasNext()){
685
                        selectionModel.setSelected(it.next(),false);
686
                }
687
        }
688
        
689

    
690
        /**
691
         *
692
         */
693
        public void selectAllRows() {
694
                Iterator<File> it = provider.getList().iterator();
695
                while(it.hasNext()){
696
                        selectionModel.setSelected(it.next(),true);
697
                }
698

    
699

    
700
        }
701

    
702
        
703
        private void sortFiles(final String sortingProperty, final boolean sortingType){
704
                Collections.sort(files, new Comparator<File>() {
705

    
706
            @Override
707
            public int compare(File arg0, File arg1) {
708
                    AbstractImagePrototype descPrototype = AbstractImagePrototype.create(images.desc());
709
                    AbstractImagePrototype ascPrototype = AbstractImagePrototype.create(images.asc());
710
                    if (sortingType){
711
                            if (sortingProperty.equals("version")) {
712
                                    return arg0.getVersion() - arg1.getVersion();
713
                            } else if (sortingProperty.equals("owner")) {
714
                                    return arg0.getOwner().compareTo(arg1.getOwner());
715
                            } else if (sortingProperty.equals("date")) {
716
                                    return arg0.getLastModified().compareTo(arg1.getLastModified());
717
                            } else if (sortingProperty.equals("size")) {
718
                                    return (int) (arg0.getBytes() - arg1.getBytes());
719
                            } else if (sortingProperty.equals("name")) {
720
                                    return arg0.getName().compareTo(arg1.getName());
721
                            } else if (sortingProperty.equals("path")) {
722
                                    return arg0.getUri().compareTo(arg1.getUri());
723
                            } else {
724
                                    return arg0.getName().compareTo(arg1.getName());
725
                            }
726
                    }
727
                    else if (sortingProperty.equals("version")) {
728
                            
729
                            return arg1.getVersion() - arg0.getVersion();
730
                    } else if (sortingProperty.equals("owner")) {
731
                            
732
                            return arg1.getOwner().compareTo(arg0.getOwner());
733
                    } else if (sortingProperty.equals("date")) {
734
                            
735
                            return arg1.getLastModified().compareTo(arg0.getLastModified());
736
                    } else if (sortingProperty.equals("size")) {
737
                            return (int) (arg1.getBytes() - arg0.getBytes());
738
                    } else if (sortingProperty.equals("name")) {
739
                            
740
                            return arg1.getName().compareTo(arg0.getName());
741
                    } else if (sortingProperty.equals("path")) {
742
                            
743
                            return arg1.getUri().compareTo(arg0.getUri());
744
                    } else {
745
                            
746
                            return arg1.getName().compareTo(arg0.getName());
747
                    }
748
            }
749

    
750
                });
751
        }
752
        
753
        final class FileValueUpdater implements ValueUpdater<String>{
754
                private String property;
755
                private SortableHeader header;
756
                /**
757
                 * 
758
                 */
759
                public FileValueUpdater(SortableHeader header,String property) {
760
                        this.property=property;
761
                        this.header=header;
762
                }
763
                @Override
764
                public void update(String value) {
765
                        header.setSorted(true);
766
                        header.toggleReverseSort();
767

    
768
                for (SortableHeader otherHeader : allHeaders) {
769
                  if (otherHeader != header) {
770
                    otherHeader.setSorted(false);
771
                    otherHeader.setReverseSort(true);
772
                  }
773
                }
774
                celltable.redrawHeaders();
775
                sortFiles(property, header.getReverseSort());
776
                FileList.this.update(true);                        
777
                }
778
                
779
        }
780

    
781
        /**
782
         * Shows the files in the cellTable 
783
     */
784
        private void showCellTable(){
785
                if(files.size()> Pithos.VISIBLE_FILE_COUNT){
786
                        pagerBottom.setVisible(true);
787
                        pagerTop.setVisible(true);
788
                }
789
                else{
790
                        pagerTop.setVisible(false);
791
                        pagerBottom.setVisible(false);
792
                }
793
                provider.setList(files);
794
                
795
                provider.refresh();
796
                
797
                //celltable.redraw();
798
                celltable.redrawHeaders();                
799
        }
800

    
801
    public void showTrash() {
802
        uploadButtonBottom.setVisible(false);
803
        uploadButtonTop.setVisible(false);
804
    }
805

    
806
    public void showFiles() {
807
        uploadButtonBottom.setVisible(true);
808
        uploadButtonTop.setVisible(true);
809
    }
810
}