Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / SearchResults.java @ fd294c94

History | View | Annotate | Download (25.2 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.DnDSimpleFocusPanel;
22
import gr.ebs.gss.client.rest.GetCommand;
23
import gr.ebs.gss.client.rest.RestCommand;
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.dom.client.NativeEvent;
36
import com.google.gwt.event.dom.client.ClickEvent;
37
import com.google.gwt.event.dom.client.ClickHandler;
38
import com.google.gwt.http.client.URL;
39
import com.google.gwt.i18n.client.DateTimeFormat;
40
import com.google.gwt.resources.client.ClientBundle;
41
import com.google.gwt.resources.client.ImageResource;
42
import com.google.gwt.user.client.DOM;
43
import com.google.gwt.user.client.DeferredCommand;
44
import com.google.gwt.user.client.Event;
45
import com.google.gwt.user.client.IncrementalCommand;
46
import com.google.gwt.user.client.Window;
47
import com.google.gwt.user.client.ui.AbstractImagePrototype;
48
import com.google.gwt.user.client.ui.Composite;
49
import com.google.gwt.user.client.ui.Grid;
50
import com.google.gwt.user.client.ui.HTML;
51
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
52
import com.google.gwt.user.client.ui.HorizontalPanel;
53
import com.google.gwt.user.client.ui.VerticalPanel;
54
import com.google.gwt.user.client.ui.HTMLTable.Cell;
55

    
56
/**
57
 * A composite that displays a list of search results for a particular query on
58
 * files.
59
 */
60
public class SearchResults extends Composite implements  ClickHandler {
61

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

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

    
66
        private String showingStats = "";
67

    
68
        private int startIndex = 0;
69

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

    
75
        private boolean clickControl = false;
76

    
77
        private boolean clickShift = false;
78

    
79
        private int firstShift = -1;
80

    
81
        private ArrayList<Integer> selectedRows = new ArrayList<Integer>();
82

    
83
        /**
84
         * The context menu for the selected file.
85
         */
86
        final DnDSimpleFocusPanel contextMenu;
87

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

    
94
                @Source("gr/ebs/gss/resources/blank.gif")
95
                ImageResource blank();
96

    
97
                @Source("gr/ebs/gss/resources/asc.png")
98
                ImageResource asc();
99

    
100
                @Source("gr/ebs/gss/resources/desc.png")
101
                ImageResource desc();
102
        }
103

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

    
109
        /**
110
         * The table widget with the file list.
111
         */
112
        private FileTable table;
113

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

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

    
124
        /**
125
         * Total search results size
126
         */
127
        private long folderTotalSize;
128

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

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

    
139
        private String sortingProperty = "name";
140

    
141
        private boolean sortingType = true;
142

    
143
        private HTML nameLabel;
144

    
145
        private HTML versionLabel;
146

    
147
        private HTML sizeLabel;
148

    
149
        private HTML dateLabel;
150

    
151
        private HTML ownerLabel;
152

    
153
        private HTML pathLabel;
154

    
155
        private HTML searchResults = new HTML("Results for search:");
156

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

    
170
                        @Override
171
                        public void onBrowserEvent(Event event) {
172
                                if (DOM.eventGetType(event) == Event.ONMOUSEDOWN && DOM.eventGetButton(event) == NativeEvent.BUTTON_RIGHT){
173
                                        if (DOM.eventGetCtrlKey(event))
174
                                                clickControl = true;
175
                                        else
176
                                                clickControl = false;
177
                                        if (DOM.eventGetShiftKey(event)) {
178
                                                clickShift = true;
179
                                                if (selectedRows.size() == 1)
180
                                                        firstShift = selectedRows.get(0) - startIndex;
181
                                                //event.preventDefault();
182
                                        } else {
183
                                                clickShift = false;
184
                                                firstShift = -1;
185
                                                //event.preventDefault();
186
                                        }
187
                                        int ri = table.getRowForEvent2(event);
188
                                        if(ri != -1)
189
                                                if(!selectedRows.contains(ri-1))
190
                                                        onRowClicked(ri, false);
191
                                }
192

    
193
                                if (files == null || files.size() == 0) {
194
                                        if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && selectedRows.size() == 0) {
195
                                                FileContextMenu fm = new FileContextMenu(images, false, true);
196
                                                fm.onEmptyEvent(event);
197
                                        }
198
                                        return;
199
                                }
200
                                if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && selectedRows.size() != 0) {
201
                                        FileContextMenu fm = new FileContextMenu(images, false, false);
202
                                        fm.onEvent(event);
203
                                } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && selectedRows.size() == 0) {
204
                                        FileContextMenu fm = new FileContextMenu(images, false, true);
205
                                        fm.onEmptyEvent(event);
206
                                } else if (DOM.eventGetType(event) == Event.ONDBLCLICK)
207
                                        if (getSelectedFiles().size() == 1) {
208
                                                FileResource file = getSelectedFiles().get(0);
209
                                                String dateString = RestCommand.getDate();
210
                                                String resource = file.getUri().substring(app.getApiPath().length() - 1, file.getUri().length());
211
                                                String sig = app.getCurrentUserResource().getUsername() + " " +
212
                                                                RestCommand.calculateSig("GET", dateString, resource,
213
                                                                RestCommand.base64decode(app.getToken()));
214
                                                Window.open(file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date=" + URL.encodeComponent(dateString), "_blank", "");
215
                                                event.preventDefault();
216
                                                return;
217
                                        }
218
                                if (DOM.eventGetType(event) == Event.ONCLICK) {
219
                                        if (DOM.eventGetCtrlKey(event))
220
                                                clickControl = true;
221
                                        else
222
                                                clickControl = false;
223
                                        if (DOM.eventGetShiftKey(event)) {
224
                                                clickShift = true;
225
                                                if (selectedRows.size() == 1)
226
                                                        firstShift = selectedRows.get(0) - startIndex;
227
                                                //event.preventDefault();
228
                                        } else {
229
                                                clickShift = false;
230
                                                firstShift = -1;
231
                                                //event.preventDefault();
232
                                        }
233
                                }
234
                                super.onBrowserEvent(event);
235
                        }
236
                };
237
                prevButton.addClickHandler(this);
238
                nextButton.addClickHandler(this);
239

    
240
                contextMenu = new DnDSimpleFocusPanel(new HTML(AbstractImagePrototype.create(images.fileContextMenu()).getHTML()));
241
                contextMenu.addClickHandler(new FileContextMenu(images, false, false));
242
                app.getDragController().makeDraggable(contextMenu);
243

    
244
                // Setup the table.
245
                table.setCellSpacing(0);
246
                table.setCellPadding(2);
247
                table.setWidth("100%");
248

    
249
                // Hook up events.
250
                table.addClickHandler(new ClickHandler() {
251

    
252
                        @Override
253
                        public void onClick(ClickEvent event) {
254
                                Cell cell = table.getCellForEvent(event);
255
                                onRowClicked(cell.getRowIndex(),true);
256
                        }
257
                });
258

    
259
                // Create the 'navigation' bar at the upper-right.
260
                HorizontalPanel innerNavBar = new HorizontalPanel();
261
                innerNavBar.setStyleName("gss-ListNavBar");
262
                innerNavBar.setSpacing(8);
263
                innerNavBar.add(prevButton);
264
                innerNavBar.add(countLabel);
265
                innerNavBar.add(nextButton);
266
                navBar.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
267
                navBar.add(innerNavBar);
268
                navBar.setWidth("100%");
269
                VerticalPanel vp = new VerticalPanel();
270
                vp.add(searchResults);
271
                searchResults.addStyleName("gss-searchLabel");
272
                vp.add(table);
273
                initWidget(vp);
274
                table.setStyleName("gss-List");
275
                initTable();
276
                DeferredCommand.addCommand(new IncrementalCommand() {
277

    
278
                        public boolean execute() {
279
                                return fetchRootFolder();
280
                        }
281
                });
282
                table.sinkEvents(Event.ONCONTEXTMENU);
283
                table.sinkEvents(Event.ONMOUSEUP);
284
                table.sinkEvents(Event.ONMOUSEDOWN);
285
                table.sinkEvents(Event.ONCLICK);
286
                table.sinkEvents(Event.ONKEYDOWN);
287
                table.sinkEvents(Event.ONDBLCLICK);
288
                preventIESelection();
289
        }
290

    
291
        public void onClick(ClickEvent event) {
292
                if (event.getSource() == nextButton) {
293
                        // Move forward a page.
294
                        clearSelectedRows();
295
                        startIndex += GSS.VISIBLE_FILE_COUNT;
296
                        if (startIndex >= folderFileCount)
297
                                startIndex -= GSS.VISIBLE_FILE_COUNT;
298
                        else
299
                                update(false);
300
                } else if (event.getSource() == prevButton) {
301
                        clearSelectedRows();
302
                        // Move back a page.
303
                        startIndex -= GSS.VISIBLE_FILE_COUNT;
304
                        if (startIndex < 0)
305
                                startIndex = 0;
306
                        else
307
                                update(false);
308
                }
309
        }
310

    
311
        /**
312
         * Retrieve the root folder for the current user.
313
         *
314
         * @return true if the retrieval was successful
315
         */
316
        protected boolean fetchRootFolder() {
317
                UserResource user = GSS.get().getCurrentUserResource();
318
                if (user == null)
319
                        return !DONE;
320
                updateFileCache("");
321
                return DONE;
322
        }
323

    
324
        public void onRowClicked(int row, boolean toggleSelection) {
325
                // Select the row that was clicked (-1 to account for header row).
326
                if (row > folderFileCount)
327
                        return;
328
                if (clickShift) {
329
                        GWT.log("ROW is:" + row + " fs:" + firstShift, null);
330
                        if (firstShift == -1)
331
                                firstShift = row;
332
                        else if (row > firstShift) {
333
                                clearSelectedRows();
334
                                for (int i = firstShift; i < row; i++) {
335
                                        selectedRows.add(startIndex + i);
336
                                        styleRow(i, true);
337
                                }
338
                                GSS.get().setCurrentSelection(getSelectedFiles());
339
                                //contextMenu.setFiles(getSelectedFiles());
340
                                table.setWidget(row, 0, contextMenu);
341
                        } else if (row != -1 && row == firstShift) {
342
                                selectedRows.add(row);
343
                                selectedRows.add(row - 1);
344
                                styleRow(row, true);
345
                                styleRow(row - 1, true);
346
                        } else if (row < firstShift) {
347
                                GWT.log("ROW is:" + row + " fs:" + firstShift, null);
348
                                clearSelectedRows();
349

    
350
                                for (int i = firstShift; i >= row - 1; i--) {
351
                                        selectedRows.add(startIndex + i);
352
                                        styleRow(i, true);
353
                                }
354
                                GSS.get().setCurrentSelection(getSelectedFiles());
355
                                table.setWidget(row, 0, contextMenu);
356
                                //contextMenu.setFiles(getSelectedFiles());
357
                        }
358

    
359
                } else if (row > 0)
360
                        selectRow(row - 1, toggleSelection);
361

    
362
        }
363

    
364
        /**
365
         * Initializes the table so that it contains enough rows for a full page of
366
         * files.
367
         */
368
        private void initTable() {
369

    
370
                nameLabel = new HTML("Name");
371
                nameLabel.addClickHandler(new ClickHandler() {
372
                        @Override
373
                        public void onClick(ClickEvent event) {
374
                                sortFiles("name");
375
                        }
376

    
377
                });
378
                versionLabel = new HTML("Version");
379
                versionLabel.addClickHandler(new ClickHandler() {
380
                        @Override
381
                        public void onClick(ClickEvent event) {
382
                                sortFiles("version");
383
                        }
384

    
385
                });
386
                sizeLabel = new HTML("Size");
387
                sizeLabel.addClickHandler(new ClickHandler() {
388
                        @Override
389
                        public void onClick(ClickEvent event) {
390
                                sortFiles("size");
391
                        }
392

    
393
                });
394
                dateLabel = new HTML("Last modified");
395
                dateLabel.addClickHandler(new ClickHandler() {
396
                        @Override
397
                        public void onClick(ClickEvent event) {
398
                                sortFiles("date");
399
                        }
400

    
401
                });
402
                ownerLabel = new HTML("Owner");
403
                ownerLabel.addClickHandler(new ClickHandler() {
404
                        @Override
405
                        public void onClick(ClickEvent event) {
406
                                sortFiles("owner");
407
                        }
408

    
409
                });
410

    
411
                pathLabel = new HTML("Path");
412
                pathLabel.addClickHandler(new ClickHandler() {
413
                        @Override
414
                        public void onClick(ClickEvent event) {
415
                                sortFiles("path");
416
                        }
417

    
418
                });
419
                // Create the header row.
420
                table.setText(0, 0, "");
421
                // table.setText(0, 1, "Name");
422
                table.setWidget(0, 1, nameLabel);
423
                table.setWidget(0, 2, ownerLabel);
424
                // table.setText(0, 3, "Version");
425
                table.setWidget(0, 3, pathLabel);
426
                table.setWidget(0, 4, versionLabel);
427
                // table.setText(0, 4, "Size");
428
                table.setWidget(0, 5, sizeLabel);
429
                // table.setText(0, 5, "Last modified");
430
                table.setWidget(0, 6, dateLabel);
431
                table.setWidget(0, 7, navBar);
432
                table.getRowFormatter().setStyleName(0, "gss-ListHeader");
433

    
434
                // Initialize the rest of the rows.
435
                for (int i = 1; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
436
                        table.setText(i, 0, "");
437
                        table.setText(i, 1, "");
438
                        table.setText(i, 2, "");
439
                        table.setText(i, 3, "");
440
                        table.setText(i, 4, "");
441
                        table.setText(i, 5, "");
442
                        table.setText(i, 6, "");
443
                        table.getCellFormatter().setWordWrap(i, 0, false);
444
                        table.getCellFormatter().setWordWrap(i, 1, false);
445
                        table.getCellFormatter().setWordWrap(i, 2, false);
446
                        table.getCellFormatter().setWordWrap(i, 3, false);
447
                        table.getCellFormatter().setWordWrap(i, 4, false);
448
                        table.getCellFormatter().setWordWrap(i, 5, false);
449
                        table.getCellFormatter().setWordWrap(i, 6, false);
450
                        table.getCellFormatter().setHorizontalAlignment(i, 4, HasHorizontalAlignment.ALIGN_CENTER);
451
                }
452
                prevButton.setVisible(false);
453
                nextButton.setVisible(false);
454
        }
455

    
456
        /**
457
         * Selects the given row (relative to the current page).
458
         *
459
         * @param row the row to be selected
460
         */
461
        private void selectRow(final int row, boolean toggleSelection) {
462
                if (row < folderFileCount) {
463
                        if (clickControl)
464
                                if (selectedRows.contains(row) && toggleSelection) {
465
                                        int i = selectedRows.indexOf(startIndex + row);
466
                                        selectedRows.remove(i);
467
                                        styleRow(row, false);
468
                                } else {
469
                                        for (int r : selectedRows) {
470
                                                int prow = r - startIndex;
471
                                                table.setWidget(prow + 1, 0, AbstractImagePrototype.create(images.document()).createImage());
472
                                        }
473
                                        selectedRows.add(startIndex + row);
474
                                        styleRow(row, true);
475
                                }
476
                        else {
477
                                clearSelectedRows();
478
                                selectedRows.add(startIndex + row);
479
                                styleRow(row, true);
480
                        }
481
                        if (selectedRows.size() == 1)
482
                                GSS.get().setCurrentSelection(files.get(selectedRows.get(0)));
483
                        else
484
                                GSS.get().setCurrentSelection(getSelectedFiles());
485
                        //contextMenu.setFiles(getSelectedFiles());
486
                        table.setWidget(row + 1, 0, contextMenu);
487
                }
488
        }
489

    
490
        public List<FileResource> getSelectedFiles() {
491
                List<FileResource> result = new ArrayList();
492
                for (int i : selectedRows)
493
                        result.add(files.get(i));
494
                return result;
495
        }
496

    
497
        /**
498
         * Make the specified row look like selected or not, according to the
499
         * <code>selected</code> flag.
500
         *
501
         * @param row
502
         * @param selected
503
         */
504
        void styleRow(final int row, final boolean selected) {
505
                if (row != -1 && row >= 0)
506
                        if (selected)
507
                                table.getRowFormatter().addStyleName(row + 1, "gss-SelectedRow");
508
                        else
509
                                table.getRowFormatter().removeStyleName(row + 1, "gss-SelectedRow");
510
        }
511

    
512
        /**
513
         * Update the display of the file list.
514
         */
515
        void update(boolean sort) {
516
                int count = folderFileCount;
517
                int max = startIndex + GSS.VISIBLE_FILE_COUNT;
518
                if (max > count)
519
                        max = count;
520
                folderTotalSize = 0;
521

    
522
                if (sort && files != null && files.size() != 0) {
523
                        clearLabels();
524
                        clearSelectedRows();
525

    
526
                        Collections.sort(files, new Comparator<FileResource>() {
527

    
528
                                public int compare(FileResource arg0, FileResource arg1) {
529
                                        AbstractImagePrototype descPrototype = AbstractImagePrototype.create(images.desc());
530
                                        AbstractImagePrototype ascPrototype = AbstractImagePrototype.create(images.asc());
531
                                        if (sortingType)
532
                                                if (sortingProperty.equals("version")) {
533
                                                        versionLabel.setHTML("Version&nbsp;" + descPrototype.getHTML());
534
                                                        return arg0.getVersion().compareTo(arg1.getVersion());
535
                                                } else if (sortingProperty.equals("owner")) {
536
                                                        ownerLabel.setHTML("Owner&nbsp;" + descPrototype.getHTML());
537
                                                        return arg0.getOwner().compareTo(arg1.getOwner());
538
                                                } else if (sortingProperty.equals("date")) {
539
                                                        dateLabel.setHTML("Last modified&nbsp;" + descPrototype.getHTML());
540
                                                        return arg0.getModificationDate().compareTo(arg1.getModificationDate());
541
                                                } else if (sortingProperty.equals("size")) {
542
                                                        sizeLabel.setHTML("Size&nbsp;" + descPrototype.getHTML());
543
                                                        return arg0.getContentLength().compareTo(arg1.getContentLength());
544
                                                } else if (sortingProperty.equals("name")) {
545
                                                        nameLabel.setHTML("Name&nbsp;" + descPrototype.getHTML());
546
                                                        return arg0.getName().compareTo(arg1.getName());
547
                                                } else if (sortingProperty.equals("path")) {
548
                                                        pathLabel.setHTML("Path&nbsp;" + descPrototype.getHTML());
549
                                                        return arg0.getUri().compareTo(arg1.getUri());
550
                                                } else {
551
                                                        nameLabel.setHTML("Name&nbsp;" + descPrototype.getHTML());
552
                                                        return arg0.getName().compareTo(arg1.getName());
553
                                                }
554
                                        else if (sortingProperty.equals("version")) {
555
                                                versionLabel.setHTML("Version&nbsp;" + ascPrototype.getHTML());
556
                                                return arg1.getVersion().compareTo(arg0.getVersion());
557
                                        } else if (sortingProperty.equals("owner")) {
558
                                                ownerLabel.setHTML("Owner&nbsp;" + ascPrototype.getHTML());
559
                                                return arg1.getOwner().compareTo(arg0.getOwner());
560
                                        } else if (sortingProperty.equals("date")) {
561
                                                dateLabel.setHTML("Last modified&nbsp;" + ascPrototype.getHTML());
562
                                                return arg1.getModificationDate().compareTo(arg0.getModificationDate());
563
                                        } else if (sortingProperty.equals("size")) {
564
                                                sizeLabel.setHTML("Size&nbsp;" + ascPrototype.getHTML());
565
                                                return arg1.getContentLength().compareTo(arg0.getContentLength());
566
                                        } else if (sortingProperty.equals("name")) {
567
                                                nameLabel.setHTML("Name&nbsp;" + ascPrototype.getHTML());
568
                                                return arg1.getName().compareTo(arg0.getName());
569
                                        } else if (sortingProperty.equals("path")) {
570
                                                pathLabel.setHTML("Path&nbsp;" + ascPrototype.getHTML());
571
                                                return arg1.getUri().compareTo(arg0.getUri());
572
                                        } else {
573
                                                nameLabel.setHTML("Name&nbsp;" + ascPrototype.getHTML());
574
                                                return arg1.getName().compareTo(arg0.getName());
575
                                        }
576
                                }
577

    
578
                        });
579

    
580
                }
581
                // Show the selected files.
582
                int i = 1;
583
                for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
584
                        // Don't read past the end.
585
                        // if (i > folderFileCount)
586
                        // break;
587
                        if (startIndex + i > folderFileCount)
588
                                break;
589
                        // Add a new row to the table, then set each of its columns to the
590
                        // proper values.
591
                        table.setWidget(i, 0, AbstractImagePrototype.create(images.document()).createImage());
592
                        FileResource fileHeader = files.get(startIndex + i - 1);
593
                        table.getRowFormatter().addStyleName(i, "gss-fileRow");
594
                        table.setHTML(i, 1, fileHeader.getName());
595
                        table.setText(i, 2, fileHeader.getOwner());
596
                        table.setText(i, 3, fileHeader.getPath());
597
                        table.setText(i, 4, String.valueOf(fileHeader.getVersion()));
598
                        table.setText(i, 5, String.valueOf(fileHeader.getFileSizeAsString()));
599
                        final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
600
                        table.setText(i, 6, formatter.format(fileHeader.getModificationDate()));
601
                        folderTotalSize += fileHeader.getContentLength();
602
                }
603

    
604
                // Clear any remaining slots.
605
                for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
606
                        table.setHTML(i, 0, "&nbsp;");
607
                        table.setHTML(i, 1, "&nbsp;");
608
                        table.setHTML(i, 2, "&nbsp;");
609
                        table.setHTML(i, 3, "&nbsp;");
610
                        table.setHTML(i, 4, "&nbsp;");
611
                        table.setHTML(i, 5, "&nbsp;");
612
                        table.setHTML(i, 6, "&nbsp;");
613
                        table.setHTML(i, 7, "&nbsp;");
614
                }
615

    
616
                if (folderFileCount == 0) {
617
                        showingStats = "no files";
618
                        prevButton.setVisible(false);
619
                        nextButton.setVisible(false);
620
                } else if (folderFileCount < GSS.VISIBLE_FILE_COUNT) {
621
                        if (folderFileCount == 1)
622
                                showingStats = "1 file";
623
                        else
624
                                showingStats = folderFileCount + " files";
625
                        showingStats += " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
626
                        prevButton.setVisible(false);
627
                        nextButton.setVisible(false);
628
                } else {
629
                        showingStats = "" + (startIndex + 1) + " - " + max + " of " + count + " files" + " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
630
                        prevButton.setVisible(startIndex != 0);
631
                        nextButton.setVisible(startIndex + GSS.VISIBLE_FILE_COUNT < count);
632
                }
633
                updateCurrentlyShowingStats();
634
        }
635

    
636
        /**
637
         *  update status panel with currently showing file stats
638
         */
639
        public void updateCurrentlyShowingStats() {
640
                GSS.get().getStatusPanel().updateCurrentlyShowing(showingStats);
641
        }
642

    
643
        /**
644
         * Adjust the height of the table by adding and removing rows as necessary.
645
         *
646
         * @param newHeight the new height to reach
647
         */
648
        void resizeTableHeight(final int newHeight) {
649
                GWT.log("Panel: " + newHeight + ", parent: " + table.getParent().getOffsetHeight(), null);
650
                // Fill the rest with empty slots.
651
                if (newHeight > table.getOffsetHeight())
652
                        while (newHeight > table.getOffsetHeight()) {
653
                                // table.setHTML(table.getRowCount(), 5, "&nbsp;");
654
                                table.resizeRows(table.getRowCount() + 1);
655
                                GWT.log("Table: " + table.getOffsetHeight() + ", rows: " + table.getRowCount(), null);
656
                        }
657
                else
658
                        while (newHeight < table.getOffsetHeight()) {
659
                                // table.setHTML(table.getRowCount(), 5, "&nbsp;");
660
                                table.resizeRows(table.getRowCount() - 1);
661
                                GWT.log("Table: " + table.getOffsetHeight() + ", rows: " + table.getRowCount(), null);
662
                        }
663
        }
664

    
665
        /**
666
         * Update the file cache with data from the server.
667
         */
668
        public void updateFileCache(String query) {
669
                final GSS app = GSS.get();
670
                clearSelectedRows();
671
                clearLabels();
672
                startIndex = 0;
673
                app.showLoadingIndicator();
674
                if (query == null || query.trim().equals("")) {
675
                        searchResults.setHTML("You must specify a query");
676
                        setFiles(new ArrayList());
677
                        update(true);
678
                        app.hideLoadingIndicator();
679
                } else{
680
                        searchResults.setHTML("Search results for " + query);
681

    
682
                        GetCommand<SearchResource> eg = new GetCommand<SearchResource>(SearchResource.class,
683
                                                app.getApiPath() + "search/" + URL.encodeComponent(query), null) {
684

    
685
                                @Override
686
                                public void onComplete() {
687
                                        SearchResource s = getResult();
688
                                        setFiles(s.getFiles());
689
                                        update(true);
690
                                }
691

    
692
                                @Override
693
                                public void onError(Throwable t) {
694
                                        if(t instanceof RestException)
695
                                                app.displayError("Unable to perform search:"+((RestException)t).getHttpStatusText());
696
                                        else
697
                                                app.displayError("System error performing search:"+t.getMessage());
698
                                        updateFileCache("");
699
                                }
700

    
701
                        };
702
                        DeferredCommand.addCommand(eg);
703
                }
704
        }
705

    
706
        /**
707
         * Fill the file cache with data.
708
         *
709
         * @param _files
710
         * @param filePaths the files to set
711
         */
712
        private void setFiles(List<FileResource> _files) {
713
                files = _files;
714
                Collections.sort(files, new Comparator<FileResource>() {
715

    
716
                        public int compare(FileResource arg0, FileResource arg1) {
717
                                return arg0.getName().compareTo(arg1.getName());
718
                        }
719

    
720
                });
721
                folderFileCount = files.size();
722
                GWT.log("File count:" + folderFileCount, null);
723
        }
724

    
725
        private void sortFiles(final String sortProperty) {
726
                if (sortProperty.equals(sortingProperty))
727
                        sortingType = !sortingType;
728
                else {
729
                        sortingProperty = sortProperty;
730
                        sortingType = true;
731
                }
732
                update(true);
733
        }
734

    
735
        private void clearLabels() {
736
                nameLabel.setText("Name");
737
                versionLabel.setText("Version");
738
                sizeLabel.setText("Size");
739
                dateLabel.setText("Last modified");
740
                ownerLabel.setText("Owner");
741
                pathLabel.setText("Path");
742
        }
743

    
744
        /**
745
         * Retrieve the table.
746
         *
747
         * @return the table
748
         */
749
        Grid getTable() {
750
                return table;
751
        }
752

    
753
        /**
754
         * Does the list contains the requested filename
755
         *
756
         * @param fileName
757
         * @return true/false
758
         */
759
        public boolean contains(String fileName) {
760
                for (int i = 0; i < files.size(); i++)
761
                        if (files.get(i).getName().equals(fileName))
762
                                return true;
763
                return false;
764
        }
765

    
766
        public void clearSelectedRows() {
767
                for (int r : selectedRows) {
768
                        int row = r - startIndex;
769
                        styleRow(row, false);
770
                        table.setWidget(row + 1, 0, AbstractImagePrototype.create(images.document()).createImage());
771
                }
772
                selectedRows.clear();
773
                Object sel = GSS.get().getCurrentSelection();
774
                if (sel instanceof FileResource || sel instanceof List)
775
                        GSS.get().setCurrentSelection(null);
776
        }
777

    
778
        public static native void preventIESelection() /*-{
779
             $doc.body.onselectstart = function () { return false; };
780
         }-*/;
781

    
782
        public static native void enableIESelection() /*-{
783
         if ($doc.body.onselectstart != null)
784
             $doc.body.onselectstart = null;
785
         }-*/;
786

    
787
        public void selectAllRows() {
788
                clearSelectedRows();
789
                int count = folderFileCount;
790
                if (count == 0)
791
                        return;
792
                int max = startIndex + GSS.VISIBLE_FILE_COUNT;
793
                if (max > count)
794
                        max = count;
795
                int i = 1;
796
                for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
797
                        // Don't read past the end.
798
                        // if (i > folderFileCount)
799
                        // break;
800
                        if (startIndex + i > folderFileCount)
801
                                break;
802
                        selectedRows.add(startIndex + i - 1);
803
                        styleRow(i - 1, true);
804
                }
805
                GSS.get().setCurrentSelection(getSelectedFiles());
806
                //contextMenu.setFiles(getSelectedFiles());
807
                table.setWidget(i - 1, 0, contextMenu);
808
        }
809

    
810
}