Statistics
| Branch: | Tag: | Revision:

root / gss / src / gr / ebs / gss / client / SearchResults.java @ 780606ca

History | View | Annotate | Download (23.7 kB)

1
/*
2
 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
6
 * GSS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GSS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
package gr.ebs.gss.client;
20

    
21
import gr.ebs.gss.client.dnd.DnDFocusPanel;
22
import gr.ebs.gss.client.rest.AbstractRestCommand;
23
import gr.ebs.gss.client.rest.ExecuteGet;
24
import gr.ebs.gss.client.rest.RestException;
25
import gr.ebs.gss.client.rest.resource.FileResource;
26
import gr.ebs.gss.client.rest.resource.SearchResource;
27
import gr.ebs.gss.client.rest.resource.UserResource;
28

    
29
import java.util.ArrayList;
30
import java.util.Collections;
31
import java.util.Comparator;
32
import java.util.List;
33

    
34
import com.google.gwt.core.client.GWT;
35
import com.google.gwt.http.client.URL;
36
import com.google.gwt.i18n.client.DateTimeFormat;
37
import com.google.gwt.user.client.DOM;
38
import com.google.gwt.user.client.DeferredCommand;
39
import com.google.gwt.user.client.Event;
40
import com.google.gwt.user.client.IncrementalCommand;
41
import com.google.gwt.user.client.Window;
42
import com.google.gwt.user.client.ui.AbstractImagePrototype;
43
import com.google.gwt.user.client.ui.ClickListener;
44
import com.google.gwt.user.client.ui.Composite;
45
import com.google.gwt.user.client.ui.Grid;
46
import com.google.gwt.user.client.ui.HTML;
47
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
48
import com.google.gwt.user.client.ui.HorizontalPanel;
49
import com.google.gwt.user.client.ui.SourcesTableEvents;
50
import com.google.gwt.user.client.ui.TableListener;
51
import com.google.gwt.user.client.ui.VerticalPanel;
52
import com.google.gwt.user.client.ui.Widget;
53

    
54
/**
55
 * A composite that displays a list of search results for a particular query on
56
 * files.
57
 */
58
public class SearchResults extends Composite implements TableListener, ClickListener {
59

    
60
        private HTML prevButton = new HTML("<a href='javascript:;'>&lt; Previous</a>", true);
61

    
62
        private HTML nextButton = new HTML("<a href='javascript:;'>Next &gt;</a>", true);
63

    
64
        private String showingStats = "";
65

    
66
        private int startIndex = 0;
67

    
68
        /**
69
         * A constant that denotes the completion of an IncrementalCommand.
70
         */
71
        public static final boolean DONE = false;
72

    
73
        private boolean clickControl = false;
74

    
75
        private boolean clickShift = false;
76

    
77
        private int firstShift = -1;
78

    
79
        private ArrayList<Integer> selectedRows = new ArrayList<Integer>();
80

    
81
        /**
82
         * The context menu for the selected file.
83
         */
84
        final DnDFocusPanel contextMenu;
85

    
86
        /**
87
         * Specifies that the images available for this composite will be the ones
88
         * available in FileContextMenu.
89
         */
90
        public interface Images extends FileContextMenu.Images, Folders.Images {
91

    
92
                @Resource("gr/ebs/gss/resources/blank.gif")
93
                AbstractImagePrototype blank();
94

    
95
                @Resource("gr/ebs/gss/resources/asc.png")
96
                AbstractImagePrototype asc();
97

    
98
                @Resource("gr/ebs/gss/resources/desc.png")
99
                AbstractImagePrototype desc();
100
        }
101

    
102
        /**
103
         * A label with the number of files in this folder.
104
         */
105
        private HTML countLabel = new HTML();
106

    
107
        /**
108
         * The table widget with the file list.
109
         */
110
        private Grid table;
111

    
112
        /**
113
         * The navigation bar for paginating the results.
114
         */
115
        private HorizontalPanel navBar = new HorizontalPanel();
116

    
117
        /**
118
         * The number of files in the search results
119
         */
120
        private int folderFileCount;
121

    
122
        /**
123
         * Total search results size
124
         */
125
        private long folderTotalSize;
126

    
127
        /**
128
         * A cache of the files in the list.
129
         */
130
        private List<FileResource> files;
131

    
132
        /**
133
         * The widget's image bundle.
134
         */
135
        private final Images images;
136

    
137
        private String sortingProperty = "name";
138

    
139
        private boolean sortingType = true;
140

    
141
        private HTML nameLabel;
142

    
143
        private HTML versionLabel;
144

    
145
        private HTML sizeLabel;
146

    
147
        private HTML dateLabel;
148

    
149
        private HTML ownerLabel;
150

    
151
        private HTML pathLabel;
152

    
153
        private HTML searchResults = new HTML("Results for search:");
154

    
155
        /**
156
         * Construct the file list widget. This entails setting up the widget
157
         * layout, fetching the number of files in the current folder from the
158
         * server and filling the local file cache of displayed files with data from
159
         * the server, as well.
160
         *
161
         * @param _images
162
         */
163
        public SearchResults(final Images _images) {
164
                images = _images;
165
                table = new Grid(GSS.VISIBLE_FILE_COUNT + 1, 8){
166

    
167
                        @Override
168
                        public void onBrowserEvent(Event event) {
169
                                if (files == null || files.size() == 0)
170
                                        return;
171
                                if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && selectedRows.size() != 0) {
172
                                        FileContextMenu fm = new FileContextMenu(images, false, false);
173
                                        fm.onClick(contextMenu);
174
                                }
175
                                else if (DOM.eventGetType(event) == Event.ONDBLCLICK)
176
                                        if(getSelectedFiles().size() == 1){
177
                                                FileResource file = getSelectedFiles().get(0);
178
                                                String dateString = AbstractRestCommand.getDate();
179
                                                String resource = file.getPath().substring(GSS.GSS_REST_PATH.length()-1,file.getPath().length());
180
                                                String sig = GSS.get().getCurrentUserResource().getUsername()+" "+AbstractRestCommand.calculateSig("GET", dateString, resource, AbstractRestCommand.base64decode(GSS.get().getToken()));
181
                                                Window.open(file.getPath() + "?Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString), "_blank", "");
182
                                        }
183
                                if (DOM.eventGetType(event) == Event.ONCLICK) {
184
                                        if (DOM.eventGetCtrlKey(event))
185
                                                clickControl = true;
186
                                        else
187
                                                clickControl = false;
188
                                        if (DOM.eventGetShiftKey(event)) {
189
                                                clickShift = true;
190
                                                if (selectedRows.size() == 1)
191
                                                        firstShift = selectedRows.get(0) - startIndex;
192
                                                //event.preventDefault();
193
                                        } else {
194
                                                clickShift = false;
195
                                                firstShift = -1;
196
                                                //event.preventDefault();
197
                                        }
198
                                }
199

    
200
                                super.onBrowserEvent(event);
201
                        }
202
                };
203
                prevButton.addClickListener(this);
204
                nextButton.addClickListener(this);
205

    
206
                contextMenu = new DnDFocusPanel(new HTML(images.fileContextMenu().getHTML()));
207
                contextMenu.addClickListener(new FileContextMenu(images, false, false));
208
                GSS.get().getDragController().makeDraggable(contextMenu);
209

    
210
                // Setup the table.
211
                table.setCellSpacing(0);
212
                table.setCellPadding(2);
213
                table.setWidth("100%");
214

    
215
                // Hook up events.
216
                table.addTableListener(this);
217

    
218
                // Create the 'navigation' bar at the upper-right.
219
                HorizontalPanel innerNavBar = new HorizontalPanel();
220
                innerNavBar.setStyleName("gss-ListNavBar");
221
                innerNavBar.setSpacing(8);
222
                innerNavBar.add(prevButton);
223
                innerNavBar.add(countLabel);
224
                innerNavBar.add(nextButton);
225
                navBar.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
226
                navBar.add(innerNavBar);
227
                navBar.setWidth("100%");
228
                VerticalPanel vp = new VerticalPanel();
229
                vp.add(searchResults);
230
                searchResults.addStyleName("gss-searchlabel");
231
                vp.add(table);
232
                initWidget(vp);
233
                table.setStyleName("gss-List");
234
                initTable();
235
                DeferredCommand.addCommand(new IncrementalCommand() {
236

    
237
                        public boolean execute() {
238
                                return fetchRootFolder();
239
                        }
240
                });
241
                table.sinkEvents(Event.ONCONTEXTMENU);
242
                table.sinkEvents(Event.ONMOUSEUP);
243
                table.sinkEvents(Event.ONCLICK);
244
                table.sinkEvents(Event.ONKEYDOWN);
245
                table.sinkEvents(Event.ONDBLCLICK);
246
                preventIESelection();
247
        }
248

    
249
        public void onClick(Widget sender) {
250
                if (sender == nextButton) {
251
                        // Move forward a page.
252
                        clearSelectedRows();
253
                        startIndex += GSS.VISIBLE_FILE_COUNT;
254
                        if (startIndex >= folderFileCount)
255
                                startIndex -= GSS.VISIBLE_FILE_COUNT;
256
                        else
257
                                update();
258
                } else if (sender == prevButton) {
259
                        clearSelectedRows();
260
                        // Move back a page.
261
                        startIndex -= GSS.VISIBLE_FILE_COUNT;
262
                        if (startIndex < 0)
263
                                startIndex = 0;
264
                        else
265
                                update();
266
                }
267
        }
268

    
269
        /**
270
         * Retrieve the root folder for the current user.
271
         *
272
         * @return true if the retrieval was successful
273
         */
274
        protected boolean fetchRootFolder() {
275
                UserResource user = GSS.get().getCurrentUserResource();
276
                if (user == null)
277
                        return !DONE;
278
                updateFileCache("");
279
                return DONE;
280
        }
281

    
282
        public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
283
                // Select the row that was clicked (-1 to account for header row).
284
                if (row > folderFileCount)
285
                        return;
286
                if (clickShift) {
287
                        GWT.log("ROW is:" + row + " fs:" + firstShift, null);
288
                        if (firstShift == -1)
289
                                firstShift = row;
290
                        else if (row > firstShift) {
291
                                clearSelectedRows();
292
                                for (int i = firstShift; i < row; i++) {
293
                                        selectedRows.add(startIndex + i);
294
                                        styleRow(i, true);
295
                                }
296
                                GSS.get().setCurrentSelection(getSelectedFiles());
297
                                //contextMenu.setFiles(getSelectedFiles());
298
                                table.setWidget(row, 0, contextMenu);
299
                        } else if (row != -1 && row == firstShift) {
300
                                selectedRows.add(row);
301
                                selectedRows.add(row - 1);
302
                                styleRow(row, true);
303
                                styleRow(row - 1, true);
304
                        } else if (row < firstShift) {
305
                                GWT.log("ROW is:" + row + " fs:" + firstShift, null);
306
                                clearSelectedRows();
307

    
308
                                for (int i = firstShift; i >= row - 1; i--) {
309
                                        selectedRows.add(startIndex + i);
310
                                        styleRow(i, true);
311
                                }
312
                                GSS.get().setCurrentSelection(getSelectedFiles());
313
                                table.setWidget(row, 0, contextMenu);
314
                                //contextMenu.setFiles(getSelectedFiles());
315
                        }
316

    
317
                } else if (row > 0)
318
                        selectRow(row - 1);
319

    
320
        }
321

    
322
        /**
323
         * Initializes the table so that it contains enough rows for a full page of
324
         * files.
325
         */
326
        private void initTable() {
327

    
328
                nameLabel = new HTML("Name");
329
                nameLabel.addClickListener(new ClickListener() {
330

    
331
                        public void onClick(Widget sender) {
332
                                sortFiles("name");
333
                                update();
334

    
335
                        }
336

    
337
                });
338
                versionLabel = new HTML("Version");
339
                versionLabel.addClickListener(new ClickListener() {
340

    
341
                        public void onClick(Widget sender) {
342
                                sortFiles("version");
343
                                update();
344

    
345
                        }
346

    
347
                });
348
                sizeLabel = new HTML("Size");
349
                sizeLabel.addClickListener(new ClickListener() {
350

    
351
                        public void onClick(Widget sender) {
352
                                sortFiles("size");
353
                                update();
354

    
355
                        }
356

    
357
                });
358
                dateLabel = new HTML("Date");
359
                dateLabel.addClickListener(new ClickListener() {
360

    
361
                        public void onClick(Widget sender) {
362
                                sortFiles("date");
363
                                update();
364

    
365
                        }
366

    
367
                });
368
                ownerLabel = new HTML("Owner");
369
                ownerLabel.addClickListener(new ClickListener() {
370

    
371
                        public void onClick(Widget sender) {
372
                                sortFiles("owner");
373
                                update();
374

    
375
                        }
376

    
377
                });
378

    
379
                pathLabel = new HTML("Path");
380
                pathLabel.addClickListener(new ClickListener() {
381

    
382
                        public void onClick(Widget sender) {
383
                                sortFiles("path");
384
                                update();
385

    
386
                        }
387

    
388
                });
389
                // Create the header row.
390
                table.setText(0, 0, "");
391
                // table.setText(0, 1, "Name");
392
                table.setWidget(0, 1, nameLabel);
393
                table.setWidget(0, 2, ownerLabel);
394
                // table.setText(0, 3, "Version");
395
                table.setWidget(0, 3, pathLabel);
396
                table.setWidget(0, 4, versionLabel);
397
                // table.setText(0, 4, "Size");
398
                table.setWidget(0, 5, sizeLabel);
399
                // table.setText(0, 5, "Date");
400
                table.setWidget(0, 6, dateLabel);
401
                table.setWidget(0, 7, navBar);
402
                table.getRowFormatter().setStyleName(0, "gss-ListHeader");
403

    
404
                // Initialize the rest of the rows.
405
                for (int i = 1; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
406
                        table.setText(i, 0, "");
407
                        table.setText(i, 1, "");
408
                        table.setText(i, 2, "");
409
                        table.setText(i, 3, "");
410
                        table.setText(i, 4, "");
411
                        table.setText(i, 5, "");
412
                        table.setText(i, 6, "");
413
                        table.getCellFormatter().setWordWrap(i, 0, false);
414
                        table.getCellFormatter().setWordWrap(i, 1, false);
415
                        table.getCellFormatter().setWordWrap(i, 2, false);
416
                        table.getCellFormatter().setWordWrap(i, 3, false);
417
                        table.getCellFormatter().setWordWrap(i, 4, false);
418
                        table.getCellFormatter().setWordWrap(i, 5, false);
419
                        table.getCellFormatter().setWordWrap(i, 6, false);
420
                        table.getCellFormatter().setHorizontalAlignment(i, 4, HasHorizontalAlignment.ALIGN_CENTER);
421
                }
422
                prevButton.setVisible(false);
423
                nextButton.setVisible(false);
424
        }
425

    
426
        /**
427
         * Selects the given row (relative to the current page).
428
         *
429
         * @param row the row to be selected
430
         */
431
        private void selectRow(final int row) {
432
                if (row < folderFileCount) {
433
                        if (clickControl)
434
                                if (selectedRows.contains(row)) {
435
                                        int i = selectedRows.indexOf(startIndex + row);
436
                                        selectedRows.remove(i);
437
                                        styleRow(row, false);
438
                                } else {
439
                                        for (int r : selectedRows) {
440
                                                int prow = r - startIndex;
441
                                                table.setWidget(prow + 1, 0, images.document().createImage());
442
                                        }
443
                                        selectedRows.add(startIndex + row);
444
                                        styleRow(row, true);
445
                                }
446
                        else {
447
                                clearSelectedRows();
448
                                selectedRows.add(startIndex + row);
449
                                styleRow(row, true);
450
                        }
451
                        if (selectedRows.size() == 1)
452
                                GSS.get().setCurrentSelection(files.get(selectedRows.get(0)));
453
                        else
454
                                GSS.get().setCurrentSelection(getSelectedFiles());
455
                        //contextMenu.setFiles(getSelectedFiles());
456
                        table.setWidget(row + 1, 0, contextMenu);
457
                }
458
        }
459

    
460
        public List<FileResource> getSelectedFiles() {
461
                List<FileResource> result = new ArrayList();
462
                for (int i : selectedRows)
463
                        result.add(files.get(i));
464
                return result;
465
        }
466

    
467
        /**
468
         * Make the specified row look like selected or not, according to the
469
         * <code>selected</code> flag.
470
         *
471
         * @param row
472
         * @param selected
473
         */
474
        void styleRow(final int row, final boolean selected) {
475
                if (row != -1 && row >= 0)
476
                        if (selected)
477
                                table.getRowFormatter().addStyleName(row + 1, "gss-SelectedRow");
478
                        else
479
                                table.getRowFormatter().removeStyleName(row + 1, "gss-SelectedRow");
480
        }
481

    
482
        /**
483
         * Update the display of the file list.
484
         */
485
        void update() {
486
                int count = folderFileCount;
487
                int max = startIndex + GSS.VISIBLE_FILE_COUNT;
488
                if (max > count)
489
                        max = count;
490
                folderTotalSize = 0;
491

    
492
                // Show the selected files.
493
                int i = 1;
494
                for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
495
                        // Don't read past the end.
496
                        // if (i > folderFileCount)
497
                        // break;
498
                        if (startIndex + i > folderFileCount)
499
                                break;
500
                        // Add a new row to the table, then set each of its columns to the
501
                        // proper values.
502
                        table.setWidget(i, 0, images.document().createImage());
503
                        FileResource fileHeader = files.get(startIndex + i - 1);
504
                        table.getRowFormatter().addStyleName(i, "gss-fileRow");
505
                        table.setHTML(i, 1, fileHeader.getName());
506
                        table.setText(i, 2, fileHeader.getOwner());
507
                        table.setText(i, 3, URL.decodeComponent(fileHeader.getPath().substring(GSS.GSS_REST_PATH.length()+fileHeader.getOwner().length()+6,fileHeader.getPath().length()-fileHeader.getName().length())));
508
                        table.setText(i, 4, String.valueOf(fileHeader.getVersion()));
509
                        table.setText(i, 5, String.valueOf(fileHeader.getFileSizeAsString()));
510
                        final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
511
                        table.setText(i, 6, formatter.format(fileHeader.getCreationDate()));
512
                        folderTotalSize += fileHeader.getContentLength();
513
                }
514

    
515
                // Clear any remaining slots.
516
                for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
517
                        table.setHTML(i, 0, "&nbsp;");
518
                        table.setHTML(i, 1, "&nbsp;");
519
                        table.setHTML(i, 2, "&nbsp;");
520
                        table.setHTML(i, 3, "&nbsp;");
521
                        table.setHTML(i, 4, "&nbsp;");
522
                        table.setHTML(i, 5, "&nbsp;");
523
                        table.setHTML(i, 6, "&nbsp;");
524
                        table.setHTML(i, 7, "&nbsp;");
525
                }
526

    
527
                if (folderFileCount == 0) {
528
                        showingStats = "no files";
529
                        prevButton.setVisible(false);
530
                        nextButton.setVisible(false);
531
                } else if (folderFileCount < GSS.VISIBLE_FILE_COUNT) {
532
                        if (folderFileCount == 1)
533
                                showingStats = "1 file";
534
                        else
535
                                showingStats = folderFileCount + " files";
536
                        showingStats += " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
537
                        prevButton.setVisible(false);
538
                        nextButton.setVisible(false);
539
                } else {
540
                        showingStats = "" + (startIndex + 1) + " - " + max + " of " + count + " files" + " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
541
                        prevButton.setVisible(startIndex != 0);
542
                        nextButton.setVisible(startIndex + GSS.VISIBLE_FILE_COUNT < count);
543
                }
544
                updateCurrentlyShowingStats();
545
        }
546

    
547
        /**
548
         *  update status panel with currently showing file stats
549
         */
550
        public void updateCurrentlyShowingStats() {
551
                GSS.get().getStatusPanel().updateCurrentlyShowing(showingStats);
552
        }
553

    
554
        /**
555
         * Adjust the height of the table by adding and removing rows as necessary.
556
         *
557
         * @param newHeight the new height to reach
558
         */
559
        void resizeTableHeight(final int newHeight) {
560
                GWT.log("Panel: " + newHeight + ", parent: " + table.getParent().getOffsetHeight(), null);
561
                // Fill the rest with empty slots.
562
                if (newHeight > table.getOffsetHeight())
563
                        while (newHeight > table.getOffsetHeight()) {
564
                                // table.setHTML(table.getRowCount(), 5, "&nbsp;");
565
                                table.resizeRows(table.getRowCount() + 1);
566
                                GWT.log("Table: " + table.getOffsetHeight() + ", rows: " + table.getRowCount(), null);
567
                        }
568
                else
569
                        while (newHeight < table.getOffsetHeight()) {
570
                                // table.setHTML(table.getRowCount(), 5, "&nbsp;");
571
                                table.resizeRows(table.getRowCount() - 1);
572
                                GWT.log("Table: " + table.getOffsetHeight() + ", rows: " + table.getRowCount(), null);
573
                        }
574
        }
575

    
576
        /**
577
         * Update the file cache with data from the server.
578
         */
579
        public void updateFileCache(String query) {
580
                clearSelectedRows();
581
                sortingProperty = "name";
582
                clearLabels();
583
                nameLabel.setHTML("Name&nbsp;" + images.desc().getHTML());
584
                sortingType = true;
585
                startIndex = 0;
586
                GSS.get().showLoadingIndicator();
587
                if (query == null || query.trim().equals("")) {
588
                        searchResults.setHTML("You must specify a query");
589
                        setFiles(new ArrayList());
590
                        update();
591
                        GSS.get().hideLoadingIndicator();
592
                } else{
593
                        searchResults.setHTML("Search results for " + query);
594

    
595
                        ExecuteGet<SearchResource> eg = new ExecuteGet<SearchResource>(SearchResource.class,
596
                                                GSS.GSS_REST_PATH+"search/"+URL.encodeComponent(query)){
597

    
598
                                @Override
599
                                public void onComplete() {
600
                                        SearchResource s = getResult();
601
                                        setFiles(s.getFiles());
602
                                        update();
603
                                }
604

    
605
                                @Override
606
                                public void onError(Throwable t) {
607
                                        if(t instanceof RestException)
608
                                                GSS.get().displayError("Unable to perform search:"+((RestException)t).getHttpStatusText());
609
                                        else
610
                                                GSS.get().displayError("System error performing search:"+t.getMessage());
611
                                        updateFileCache("");
612
                                }
613

    
614
                        };
615
                        DeferredCommand.addCommand(eg);
616
                }
617
        }
618

    
619
        /**
620
         * Fill the file cache with data.
621
         *
622
         * @param _files
623
         * @param filePaths the files to set
624
         */
625
        private void setFiles(List<FileResource> _files) {
626
                files = _files;
627
                Collections.sort(files, new Comparator<FileResource>() {
628

    
629
                        public int compare(FileResource arg0, FileResource arg1) {
630
                                return arg0.getName().compareTo(arg1.getName());
631
                        }
632

    
633
                });
634
                folderFileCount = files.size();
635
                GWT.log("File count:" + folderFileCount, null);
636
        }
637

    
638
        private void sortFiles(final String sortProperty) {
639
                if (sortProperty.equals(sortingProperty))
640
                        sortingType = !sortingType;
641
                else {
642
                        sortingProperty = sortProperty;
643
                        sortingType = true;
644
                }
645
                clearLabels();
646
                clearSelectedRows();
647
                if (files == null || files.size() == 0)
648
                        return;
649
                Collections.sort(files, new Comparator<FileResource>() {
650

    
651
                        public int compare(FileResource arg0, FileResource arg1) {
652
                                if (sortingType)
653
                                        if (sortProperty.equals("version")) {
654
                                                versionLabel.setHTML("Version&nbsp;" + images.desc().getHTML());
655
                                                return new Integer(arg0.getVersion()).compareTo(new Integer(arg1.getVersion()));
656
                                        } else if (sortProperty.equals("owner")) {
657
                                                ownerLabel.setHTML("Owner&nbsp;" + images.desc().getHTML());
658
                                                GWT.log(arg0.getOwner() + "/" + arg1.getOwner(), null);
659
                                                return arg0.getOwner().compareTo(arg1.getOwner());
660
                                        } else if (sortProperty.equals("date")) {
661
                                                dateLabel.setHTML("Date&nbsp;" + images.desc().getHTML());
662
                                                return arg0.getCreationDate().compareTo(arg1.getCreationDate());
663
                                        } else if (sortProperty.equals("size")) {
664
                                                sizeLabel.setHTML("Size&nbsp;" + images.desc().getHTML());
665
                                                return new Long(arg0.getContentLength()).compareTo(new Long(arg1.getContentLength()));
666
                                        } else if (sortProperty.equals("name")) {
667
                                                nameLabel.setHTML("Name&nbsp;" + images.desc().getHTML());
668
                                                return arg0.getName().compareTo(arg1.getName());
669
                                        } else if (sortProperty.equals("path")) {
670
                                                pathLabel.setHTML("Path&nbsp;" + images.desc().getHTML());
671
                                                return arg0.getPath().compareTo(arg1.getPath());
672
                                        } else {
673
                                                nameLabel.setHTML("Name&nbsp;" + images.desc().getHTML());
674
                                                return arg0.getName().compareTo(arg1.getName());
675
                                        }
676
                                else if (sortProperty.equals("version")) {
677
                                        versionLabel.setHTML("Version&nbsp;" + images.asc().getHTML());
678
                                        return new Integer(arg1.getVersion()).compareTo(new Integer(arg0.getVersion()));
679
                                } else if (sortProperty.equals("owner")) {
680
                                        ownerLabel.setHTML("Owner&nbsp;" + images.asc().getHTML());
681
                                        return arg1.getOwner().compareTo(arg0.getOwner());
682
                                } else if (sortProperty.equals("date")) {
683
                                        dateLabel.setHTML("Date&nbsp;" + images.asc().getHTML());
684
                                        return arg1.getCreationDate().compareTo(arg0.getCreationDate());
685
                                } else if (sortProperty.equals("size")) {
686
                                        sizeLabel.setHTML("Size&nbsp;" + images.asc().getHTML());
687
                                        return new Long(arg1.getContentLength()).compareTo(new Long(arg0.getContentLength()));
688
                                } else if (sortProperty.equals("name")) {
689
                                        nameLabel.setHTML("Name&nbsp;" + images.asc().getHTML());
690
                                        return arg1.getName().compareTo(arg0.getName());
691
                                } else if (sortProperty.equals("path")) {
692
                                        pathLabel.setHTML("Path&nbsp;" + images.asc().getHTML());
693
                                        return arg1.getPath().compareTo(arg0.getPath());
694
                                } else {
695
                                        nameLabel.setHTML("Name&nbsp;" + images.asc().getHTML());
696
                                        return arg1.getName().compareTo(arg0.getName());
697
                                }
698
                        }
699

    
700
                });
701
        }
702

    
703
        private void clearLabels() {
704
                nameLabel.setText("Name");
705
                versionLabel.setText("Version");
706
                sizeLabel.setText("Size");
707
                dateLabel.setText("Date");
708
                ownerLabel.setText("Owner");
709
                pathLabel.setText("Path");
710
        }
711

    
712
        /**
713
         * Retrieve the table.
714
         *
715
         * @return the table
716
         */
717
        Grid getTable() {
718
                return table;
719
        }
720

    
721
        /**
722
         * Does the list contains the requested filename
723
         *
724
         * @param fileName
725
         * @return true/false
726
         */
727
        public boolean contains(String fileName) {
728
                for (int i = 0; i < files.size(); i++)
729
                        if (files.get(i).getName().equals(fileName))
730
                                return true;
731
                return false;
732
        }
733

    
734
        public void clearSelectedRows() {
735
                for (int r : selectedRows) {
736
                        int row = r - startIndex;
737
                        styleRow(row, false);
738
                        table.setWidget(row + 1, 0, images.document().createImage());
739
                }
740
                selectedRows.clear();
741
                Object sel = GSS.get().getCurrentSelection();
742
                if (sel instanceof FileResource || sel instanceof List)
743
                        GSS.get().setCurrentSelection(null);
744
        }
745

    
746
        public static native void preventIESelection() /*-{
747
             $doc.body.onselectstart = function () { return false; };
748
         }-*/;
749

    
750
        public static native void enableIESelection() /*-{
751
         if ($doc.body.onselectstart != null)
752
             $doc.body.onselectstart = null;
753
         }-*/;
754

    
755
        public void selectAllRows() {
756
                clearSelectedRows();
757
                int count = folderFileCount;
758
                if (count == 0)
759
                        return;
760
                int max = startIndex + GSS.VISIBLE_FILE_COUNT;
761
                if (max > count)
762
                        max = count;
763
                int i = 1;
764
                for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
765
                        // Don't read past the end.
766
                        // if (i > folderFileCount)
767
                        // break;
768
                        if (startIndex + i > folderFileCount)
769
                                break;
770
                        selectedRows.add(startIndex + i - 1);
771
                        styleRow(i - 1, true);
772
                }
773
                GSS.get().setCurrentSelection(getSelectedFiles());
774
                //contextMenu.setFiles(getSelectedFiles());
775
                table.setWidget(i - 1, 0, contextMenu);
776
        }
777

    
778
}