Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (23.4 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.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.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
                final GSS app = GSS.get();
166
                table = new Grid(GSS.VISIBLE_FILE_COUNT + 1, 8) {
167

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

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

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

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

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

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

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

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

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

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

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

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

    
321
        }
322

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

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

    
332
                        public void onClick(Widget sender) {
333
                                sortFiles("name");
334
                        }
335

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

    
340
                        public void onClick(Widget sender) {
341
                                sortFiles("version");
342
                        }
343

    
344
                });
345
                sizeLabel = new HTML("Size");
346
                sizeLabel.addClickListener(new ClickListener() {
347

    
348
                        public void onClick(Widget sender) {
349
                                sortFiles("size");
350
                        }
351

    
352
                });
353
                dateLabel = new HTML("Last modified");
354
                dateLabel.addClickListener(new ClickListener() {
355

    
356
                        public void onClick(Widget sender) {
357
                                sortFiles("date");
358
                        }
359

    
360
                });
361
                ownerLabel = new HTML("Owner");
362
                ownerLabel.addClickListener(new ClickListener() {
363

    
364
                        public void onClick(Widget sender) {
365
                                sortFiles("owner");
366
                        }
367

    
368
                });
369

    
370
                pathLabel = new HTML("Path");
371
                pathLabel.addClickListener(new ClickListener() {
372

    
373
                        public void onClick(Widget sender) {
374
                                sortFiles("path");
375
                        }
376

    
377
                });
378
                // Create the header row.
379
                table.setText(0, 0, "");
380
                // table.setText(0, 1, "Name");
381
                table.setWidget(0, 1, nameLabel);
382
                table.setWidget(0, 2, ownerLabel);
383
                // table.setText(0, 3, "Version");
384
                table.setWidget(0, 3, pathLabel);
385
                table.setWidget(0, 4, versionLabel);
386
                // table.setText(0, 4, "Size");
387
                table.setWidget(0, 5, sizeLabel);
388
                // table.setText(0, 5, "Last modified");
389
                table.setWidget(0, 6, dateLabel);
390
                table.setWidget(0, 7, navBar);
391
                table.getRowFormatter().setStyleName(0, "gss-ListHeader");
392

    
393
                // Initialize the rest of the rows.
394
                for (int i = 1; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
395
                        table.setText(i, 0, "");
396
                        table.setText(i, 1, "");
397
                        table.setText(i, 2, "");
398
                        table.setText(i, 3, "");
399
                        table.setText(i, 4, "");
400
                        table.setText(i, 5, "");
401
                        table.setText(i, 6, "");
402
                        table.getCellFormatter().setWordWrap(i, 0, false);
403
                        table.getCellFormatter().setWordWrap(i, 1, false);
404
                        table.getCellFormatter().setWordWrap(i, 2, false);
405
                        table.getCellFormatter().setWordWrap(i, 3, false);
406
                        table.getCellFormatter().setWordWrap(i, 4, false);
407
                        table.getCellFormatter().setWordWrap(i, 5, false);
408
                        table.getCellFormatter().setWordWrap(i, 6, false);
409
                        table.getCellFormatter().setHorizontalAlignment(i, 4, HasHorizontalAlignment.ALIGN_CENTER);
410
                }
411
                prevButton.setVisible(false);
412
                nextButton.setVisible(false);
413
        }
414

    
415
        /**
416
         * Selects the given row (relative to the current page).
417
         *
418
         * @param row the row to be selected
419
         */
420
        private void selectRow(final int row) {
421
                if (row < folderFileCount) {
422
                        if (clickControl)
423
                                if (selectedRows.contains(row)) {
424
                                        int i = selectedRows.indexOf(startIndex + row);
425
                                        selectedRows.remove(i);
426
                                        styleRow(row, false);
427
                                } else {
428
                                        for (int r : selectedRows) {
429
                                                int prow = r - startIndex;
430
                                                table.setWidget(prow + 1, 0, images.document().createImage());
431
                                        }
432
                                        selectedRows.add(startIndex + row);
433
                                        styleRow(row, true);
434
                                }
435
                        else {
436
                                clearSelectedRows();
437
                                selectedRows.add(startIndex + row);
438
                                styleRow(row, true);
439
                        }
440
                        if (selectedRows.size() == 1)
441
                                GSS.get().setCurrentSelection(files.get(selectedRows.get(0)));
442
                        else
443
                                GSS.get().setCurrentSelection(getSelectedFiles());
444
                        //contextMenu.setFiles(getSelectedFiles());
445
                        table.setWidget(row + 1, 0, contextMenu);
446
                }
447
        }
448

    
449
        public List<FileResource> getSelectedFiles() {
450
                List<FileResource> result = new ArrayList();
451
                for (int i : selectedRows)
452
                        result.add(files.get(i));
453
                return result;
454
        }
455

    
456
        /**
457
         * Make the specified row look like selected or not, according to the
458
         * <code>selected</code> flag.
459
         *
460
         * @param row
461
         * @param selected
462
         */
463
        void styleRow(final int row, final boolean selected) {
464
                if (row != -1 && row >= 0)
465
                        if (selected)
466
                                table.getRowFormatter().addStyleName(row + 1, "gss-SelectedRow");
467
                        else
468
                                table.getRowFormatter().removeStyleName(row + 1, "gss-SelectedRow");
469
        }
470

    
471
        /**
472
         * Update the display of the file list.
473
         */
474
        void update(boolean sort) {
475
                int count = folderFileCount;
476
                int max = startIndex + GSS.VISIBLE_FILE_COUNT;
477
                if (max > count)
478
                        max = count;
479
                folderTotalSize = 0;
480

    
481
                if (sort && files != null && files.size() != 0) {
482
                        clearLabels();
483
                        clearSelectedRows();
484

    
485
                        Collections.sort(files, new Comparator<FileResource>() {
486

    
487
                                public int compare(FileResource arg0, FileResource arg1) {
488
                                        if (sortingType)
489
                                                if (sortingProperty.equals("version")) {
490
                                                        versionLabel.setHTML("Version&nbsp;" + images.desc().getHTML());
491
                                                        return arg0.getVersion().compareTo(arg1.getVersion());
492
                                                } else if (sortingProperty.equals("owner")) {
493
                                                        ownerLabel.setHTML("Owner&nbsp;" + images.desc().getHTML());
494
                                                        return arg0.getOwner().compareTo(arg1.getOwner());
495
                                                } else if (sortingProperty.equals("date")) {
496
                                                        dateLabel.setHTML("Last modified&nbsp;" + images.desc().getHTML());
497
                                                        return arg0.getModificationDate().compareTo(arg1.getModificationDate());
498
                                                } else if (sortingProperty.equals("size")) {
499
                                                        sizeLabel.setHTML("Size&nbsp;" + images.desc().getHTML());
500
                                                        return arg0.getContentLength().compareTo(arg1.getContentLength());
501
                                                } else if (sortingProperty.equals("name")) {
502
                                                        nameLabel.setHTML("Name&nbsp;" + images.desc().getHTML());
503
                                                        return arg0.getName().compareTo(arg1.getName());
504
                                                } else if (sortingProperty.equals("path")) {
505
                                                        pathLabel.setHTML("Path&nbsp;" + images.desc().getHTML());
506
                                                        return arg0.getUri().compareTo(arg1.getUri());
507
                                                } else {
508
                                                        nameLabel.setHTML("Name&nbsp;" + images.desc().getHTML());
509
                                                        return arg0.getName().compareTo(arg1.getName());
510
                                                }
511
                                        else if (sortingProperty.equals("version")) {
512
                                                versionLabel.setHTML("Version&nbsp;" + images.asc().getHTML());
513
                                                return arg1.getVersion().compareTo(arg0.getVersion());
514
                                        } else if (sortingProperty.equals("owner")) {
515
                                                ownerLabel.setHTML("Owner&nbsp;" + images.asc().getHTML());
516
                                                return arg1.getOwner().compareTo(arg0.getOwner());
517
                                        } else if (sortingProperty.equals("date")) {
518
                                                dateLabel.setHTML("Last modified&nbsp;" + images.asc().getHTML());
519
                                                return arg1.getModificationDate().compareTo(arg0.getModificationDate());
520
                                        } else if (sortingProperty.equals("size")) {
521
                                                sizeLabel.setHTML("Size&nbsp;" + images.asc().getHTML());
522
                                                return arg1.getContentLength().compareTo(arg0.getContentLength());
523
                                        } else if (sortingProperty.equals("name")) {
524
                                                nameLabel.setHTML("Name&nbsp;" + images.asc().getHTML());
525
                                                return arg1.getName().compareTo(arg0.getName());
526
                                        } else if (sortingProperty.equals("path")) {
527
                                                pathLabel.setHTML("Path&nbsp;" + images.asc().getHTML());
528
                                                return arg1.getUri().compareTo(arg0.getUri());
529
                                        } else {
530
                                                nameLabel.setHTML("Name&nbsp;" + images.asc().getHTML());
531
                                                return arg1.getName().compareTo(arg0.getName());
532
                                        }
533
                                }
534

    
535
                        });
536

    
537
                }
538
                // Show the selected files.
539
                int i = 1;
540
                for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
541
                        // Don't read past the end.
542
                        // if (i > folderFileCount)
543
                        // break;
544
                        if (startIndex + i > folderFileCount)
545
                                break;
546
                        // Add a new row to the table, then set each of its columns to the
547
                        // proper values.
548
                        table.setWidget(i, 0, images.document().createImage());
549
                        FileResource fileHeader = files.get(startIndex + i - 1);
550
                        table.getRowFormatter().addStyleName(i, "gss-fileRow");
551
                        table.setHTML(i, 1, fileHeader.getName());
552
                        table.setText(i, 2, fileHeader.getOwner());
553
                        table.setText(i, 3, fileHeader.getPath());
554
                        table.setText(i, 4, String.valueOf(fileHeader.getVersion()));
555
                        table.setText(i, 5, String.valueOf(fileHeader.getFileSizeAsString()));
556
                        final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
557
                        table.setText(i, 6, formatter.format(fileHeader.getModificationDate()));
558
                        folderTotalSize += fileHeader.getContentLength();
559
                }
560

    
561
                // Clear any remaining slots.
562
                for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
563
                        table.setHTML(i, 0, "&nbsp;");
564
                        table.setHTML(i, 1, "&nbsp;");
565
                        table.setHTML(i, 2, "&nbsp;");
566
                        table.setHTML(i, 3, "&nbsp;");
567
                        table.setHTML(i, 4, "&nbsp;");
568
                        table.setHTML(i, 5, "&nbsp;");
569
                        table.setHTML(i, 6, "&nbsp;");
570
                        table.setHTML(i, 7, "&nbsp;");
571
                }
572

    
573
                if (folderFileCount == 0) {
574
                        showingStats = "no files";
575
                        prevButton.setVisible(false);
576
                        nextButton.setVisible(false);
577
                } else if (folderFileCount < GSS.VISIBLE_FILE_COUNT) {
578
                        if (folderFileCount == 1)
579
                                showingStats = "1 file";
580
                        else
581
                                showingStats = folderFileCount + " files";
582
                        showingStats += " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
583
                        prevButton.setVisible(false);
584
                        nextButton.setVisible(false);
585
                } else {
586
                        showingStats = "" + (startIndex + 1) + " - " + max + " of " + count + " files" + " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
587
                        prevButton.setVisible(startIndex != 0);
588
                        nextButton.setVisible(startIndex + GSS.VISIBLE_FILE_COUNT < count);
589
                }
590
                updateCurrentlyShowingStats();
591
        }
592

    
593
        /**
594
         *  update status panel with currently showing file stats
595
         */
596
        public void updateCurrentlyShowingStats() {
597
                GSS.get().getStatusPanel().updateCurrentlyShowing(showingStats);
598
        }
599

    
600
        /**
601
         * Adjust the height of the table by adding and removing rows as necessary.
602
         *
603
         * @param newHeight the new height to reach
604
         */
605
        void resizeTableHeight(final int newHeight) {
606
                GWT.log("Panel: " + newHeight + ", parent: " + table.getParent().getOffsetHeight(), null);
607
                // Fill the rest with empty slots.
608
                if (newHeight > table.getOffsetHeight())
609
                        while (newHeight > table.getOffsetHeight()) {
610
                                // table.setHTML(table.getRowCount(), 5, "&nbsp;");
611
                                table.resizeRows(table.getRowCount() + 1);
612
                                GWT.log("Table: " + table.getOffsetHeight() + ", rows: " + table.getRowCount(), null);
613
                        }
614
                else
615
                        while (newHeight < table.getOffsetHeight()) {
616
                                // table.setHTML(table.getRowCount(), 5, "&nbsp;");
617
                                table.resizeRows(table.getRowCount() - 1);
618
                                GWT.log("Table: " + table.getOffsetHeight() + ", rows: " + table.getRowCount(), null);
619
                        }
620
        }
621

    
622
        /**
623
         * Update the file cache with data from the server.
624
         */
625
        public void updateFileCache(String query) {
626
                final GSS app = GSS.get();
627
                clearSelectedRows();
628
                clearLabels();
629
                startIndex = 0;
630
                app.showLoadingIndicator();
631
                if (query == null || query.trim().equals("")) {
632
                        searchResults.setHTML("You must specify a query");
633
                        setFiles(new ArrayList());
634
                        update(true);
635
                        app.hideLoadingIndicator();
636
                } else{
637
                        searchResults.setHTML("Search results for " + query);
638

    
639
                        GetCommand<SearchResource> eg = new GetCommand<SearchResource>(SearchResource.class,
640
                                                app.getApiPath() + "search/" + URL.encodeComponent(query)) {
641

    
642
                                @Override
643
                                public void onComplete() {
644
                                        SearchResource s = getResult();
645
                                        setFiles(s.getFiles());
646
                                        update(true);
647
                                }
648

    
649
                                @Override
650
                                public void onError(Throwable t) {
651
                                        if(t instanceof RestException)
652
                                                app.displayError("Unable to perform search:"+((RestException)t).getHttpStatusText());
653
                                        else
654
                                                app.displayError("System error performing search:"+t.getMessage());
655
                                        updateFileCache("");
656
                                }
657

    
658
                        };
659
                        DeferredCommand.addCommand(eg);
660
                }
661
        }
662

    
663
        /**
664
         * Fill the file cache with data.
665
         *
666
         * @param _files
667
         * @param filePaths the files to set
668
         */
669
        private void setFiles(List<FileResource> _files) {
670
                files = _files;
671
                Collections.sort(files, new Comparator<FileResource>() {
672

    
673
                        public int compare(FileResource arg0, FileResource arg1) {
674
                                return arg0.getName().compareTo(arg1.getName());
675
                        }
676

    
677
                });
678
                folderFileCount = files.size();
679
                GWT.log("File count:" + folderFileCount, null);
680
        }
681

    
682
        private void sortFiles(final String sortProperty) {
683
                if (sortProperty.equals(sortingProperty))
684
                        sortingType = !sortingType;
685
                else {
686
                        sortingProperty = sortProperty;
687
                        sortingType = true;
688
                }
689
                update(true);
690
        }
691

    
692
        private void clearLabels() {
693
                nameLabel.setText("Name");
694
                versionLabel.setText("Version");
695
                sizeLabel.setText("Size");
696
                dateLabel.setText("Last modified");
697
                ownerLabel.setText("Owner");
698
                pathLabel.setText("Path");
699
        }
700

    
701
        /**
702
         * Retrieve the table.
703
         *
704
         * @return the table
705
         */
706
        Grid getTable() {
707
                return table;
708
        }
709

    
710
        /**
711
         * Does the list contains the requested filename
712
         *
713
         * @param fileName
714
         * @return true/false
715
         */
716
        public boolean contains(String fileName) {
717
                for (int i = 0; i < files.size(); i++)
718
                        if (files.get(i).getName().equals(fileName))
719
                                return true;
720
                return false;
721
        }
722

    
723
        public void clearSelectedRows() {
724
                for (int r : selectedRows) {
725
                        int row = r - startIndex;
726
                        styleRow(row, false);
727
                        table.setWidget(row + 1, 0, images.document().createImage());
728
                }
729
                selectedRows.clear();
730
                Object sel = GSS.get().getCurrentSelection();
731
                if (sel instanceof FileResource || sel instanceof List)
732
                        GSS.get().setCurrentSelection(null);
733
        }
734

    
735
        public static native void preventIESelection() /*-{
736
             $doc.body.onselectstart = function () { return false; };
737
         }-*/;
738

    
739
        public static native void enableIESelection() /*-{
740
         if ($doc.body.onselectstart != null)
741
             $doc.body.onselectstart = null;
742
         }-*/;
743

    
744
        public void selectAllRows() {
745
                clearSelectedRows();
746
                int count = folderFileCount;
747
                if (count == 0)
748
                        return;
749
                int max = startIndex + GSS.VISIBLE_FILE_COUNT;
750
                if (max > count)
751
                        max = count;
752
                int i = 1;
753
                for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
754
                        // Don't read past the end.
755
                        // if (i > folderFileCount)
756
                        // break;
757
                        if (startIndex + i > folderFileCount)
758
                                break;
759
                        selectedRows.add(startIndex + i - 1);
760
                        styleRow(i - 1, true);
761
                }
762
                GSS.get().setCurrentSelection(getSelectedFiles());
763
                //contextMenu.setFiles(getSelectedFiles());
764
                table.setWidget(i - 1, 0, contextMenu);
765
        }
766

    
767
}