Statistics
| Branch: | Tag: | Revision:

root / gss / src / gr / ebs / gss / client / SearchResults.java @ 4bb05581

History | View | Annotate | Download (25.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.AbstractRestCommand;
23
import gr.ebs.gss.client.rest.ExecuteGet;
24
import gr.ebs.gss.client.rest.ExecuteMultipleHead;
25
import gr.ebs.gss.client.rest.RestException;
26
import gr.ebs.gss.client.rest.resource.FileResource;
27
import gr.ebs.gss.client.rest.resource.SearchResource;
28
import gr.ebs.gss.client.rest.resource.UserResource;
29

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

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

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

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

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

    
65
        private String showingStats = "";
66

    
67
        private int startIndex = 0;
68

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

    
74
        private boolean clickControl = false;
75

    
76
        private boolean clickShift = false;
77

    
78
        private int firstShift = -1;
79

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

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

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

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

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

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

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

    
108
        /**
109
         * The table widget with the file list.
110
         */
111
        private Grid table;// = new Grid(GSS.VISIBLE_FILE_COUNT + 1, 8);
112

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

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

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

    
128

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

    
134

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

    
140
        private String sortingProperty = "name";
141

    
142
        private boolean sortingType = true;
143

    
144
        private HTML nameLabel;
145

    
146
        private HTML versionLabel;
147

    
148
        private HTML sizeLabel;
149

    
150
        private HTML dateLabel;
151

    
152
        private HTML ownerLabel;
153

    
154
        private HTML pathLabel;
155

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

    
158
        /**
159
         * Construct the file list widget. This entails setting up the widget
160
         * layout, fetching the number of files in the current folder from the
161
         * server and filling the local file cache of displayed files with data from
162
         * the server, as well.
163
         *
164
         * @param _images
165
         */
166
        public SearchResults(final Images _images) {
167
                images = _images;
168
                table = new Grid(GSS.VISIBLE_FILE_COUNT + 1, 8){
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 = AbstractRestCommand.getDate();
180
                                                String resource = file.getPath().substring(GSS.GSS_REST_PATH.length()-1,file.getPath().length());
181
                                                String sig = GSS.get().getCurrentUserResource().getUsername()+" "+AbstractRestCommand.calculateSig("GET", dateString, resource, AbstractRestCommand.base64decode(GSS.get().getToken()));
182
                                                Window.open(file.getPath() + "?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
                GSS.get().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
                final 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();
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();
267
                }
268
        }
269

    
270

    
271

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

    
285
        /*
286
         * (non-Javadoc)
287
         *
288
         * @see com.google.gwt.user.client.ui.TableListener#onCellClicked(com.google.gwt.user.client.ui.SourcesTableEvents,
289
         *      int, int)
290
         */
291
        public void onCellClicked(@SuppressWarnings("unused") SourcesTableEvents sender, int row, @SuppressWarnings("unused") int cell) {
292
                // Select the row that was clicked (-1 to account for header row).
293
                if (row > folderFileCount)
294
                        return;
295
                if (clickShift) {
296
                        GWT.log("ROW is:" + row + " fs:" + firstShift, null);
297
                        if (firstShift == -1)
298
                                firstShift = row;
299
                        else if (row > firstShift) {
300
                                clearSelectedRows();
301
                                for (int i = firstShift; i < row; i++) {
302
                                        selectedRows.add(startIndex + i);
303
                                        styleRow(i, true);
304
                                }
305
                                GSS.get().setCurrentSelection(getSelectedFiles());
306
                                //contextMenu.setFiles(getSelectedFiles());
307
                                table.setWidget(row, 0, contextMenu);
308
                        } else if (row != -1 && row == firstShift) {
309
                                selectedRows.add(row);
310
                                selectedRows.add(row - 1);
311
                                styleRow(row, true);
312
                                styleRow(row - 1, true);
313
                        } else if (row < firstShift) {
314
                                GWT.log("ROW is:" + row + " fs:" + firstShift, null);
315
                                clearSelectedRows();
316

    
317
                                for (int i = firstShift; i >= row - 1; i--) {
318
                                        selectedRows.add(startIndex + i);
319
                                        styleRow(i, true);
320
                                }
321
                                GSS.get().setCurrentSelection(getSelectedFiles());
322
                                table.setWidget(row, 0, contextMenu);
323
                                //contextMenu.setFiles(getSelectedFiles());
324
                        }
325

    
326
                } else if (row > 0)
327
                        selectRow(row - 1);
328

    
329
        }
330

    
331
        /**
332
         * Initializes the table so that it contains enough rows for a full page of
333
         * files.
334
         */
335
        private void initTable() {
336

    
337
                nameLabel = new HTML("Name");
338
                nameLabel.addClickListener(new ClickListener() {
339

    
340
                        public void onClick(Widget sender) {
341
                                sortFiles("name");
342
                                update();
343

    
344
                        }
345

    
346
                });
347
                versionLabel = new HTML("Version");
348
                versionLabel.addClickListener(new ClickListener() {
349

    
350
                        public void onClick(Widget sender) {
351
                                sortFiles("version");
352
                                update();
353

    
354
                        }
355

    
356
                });
357
                sizeLabel = new HTML("Size");
358
                sizeLabel.addClickListener(new ClickListener() {
359

    
360
                        public void onClick(Widget sender) {
361
                                sortFiles("size");
362
                                update();
363

    
364
                        }
365

    
366
                });
367
                dateLabel = new HTML("Date");
368
                dateLabel.addClickListener(new ClickListener() {
369

    
370
                        public void onClick(Widget sender) {
371
                                sortFiles("date");
372
                                update();
373

    
374
                        }
375

    
376
                });
377
                ownerLabel = new HTML("Owner");
378
                ownerLabel.addClickListener(new ClickListener() {
379

    
380
                        public void onClick(Widget sender) {
381
                                sortFiles("owner");
382
                                update();
383

    
384
                        }
385

    
386
                });
387

    
388
                pathLabel = new HTML("Path");
389
                pathLabel.addClickListener(new ClickListener() {
390

    
391
                        public void onClick(Widget sender) {
392
                                sortFiles("path");
393
                                update();
394

    
395
                        }
396

    
397
                });
398
                // Create the header row.
399
                table.setText(0, 0, "");
400
                // table.setText(0, 1, "Name");
401
                table.setWidget(0, 1, nameLabel);
402
                table.setWidget(0, 2, ownerLabel);
403
                // table.setText(0, 3, "Version");
404
                table.setWidget(0, 3, pathLabel);
405
                table.setWidget(0, 4, versionLabel);
406
                // table.setText(0, 4, "Size");
407
                table.setWidget(0, 5, sizeLabel);
408
                // table.setText(0, 5, "Date");
409
                table.setWidget(0, 6, dateLabel);
410
                table.setWidget(0, 7, navBar);
411
                table.getRowFormatter().setStyleName(0, "gss-ListHeader");
412

    
413
                // Initialize the rest of the rows.
414
                for (int i = 1; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
415
                        table.setText(i, 0, "");
416
                        table.setText(i, 1, "");
417
                        table.setText(i, 2, "");
418
                        table.setText(i, 3, "");
419
                        table.setText(i, 4, "");
420
                        table.setText(i, 5, "");
421
                        table.setText(i, 6, "");
422
                        table.getCellFormatter().setWordWrap(i, 0, false);
423
                        table.getCellFormatter().setWordWrap(i, 1, false);
424
                        table.getCellFormatter().setWordWrap(i, 2, false);
425
                        table.getCellFormatter().setWordWrap(i, 3, false);
426
                        table.getCellFormatter().setWordWrap(i, 4, false);
427
                        table.getCellFormatter().setWordWrap(i, 5, false);
428
                        table.getCellFormatter().setWordWrap(i, 6, false);
429
                        table.getCellFormatter().setHorizontalAlignment(i, 4, HasHorizontalAlignment.ALIGN_CENTER);
430
                        // table.getFlexCellFormatter().setColSpan(i, 5, 2);
431
                }
432
                prevButton.setVisible(false);
433
                nextButton.setVisible(false);
434

    
435
        }
436

    
437
        /**
438
         * Selects the given row (relative to the current page).
439
         *
440
         * @param row the row to be selected
441
         */
442
        private void selectRow(final int row) {
443
                if (row < folderFileCount) {
444
                        if (clickControl)
445
                                if (selectedRows.contains(row)) {
446
                                        int i = selectedRows.indexOf(startIndex + row);
447
                                        selectedRows.remove(i);
448
                                        styleRow(row, false);
449
                                } else {
450
                                        for (int r : selectedRows) {
451
                                                int prow = r - startIndex;
452
                                                table.setWidget(prow + 1, 0, images.document().createImage());
453
                                        }
454
                                        selectedRows.add(startIndex + row);
455
                                        styleRow(row, true);
456

    
457
                                }
458
                        else {
459
                                clearSelectedRows();
460
                                selectedRows.add(startIndex + row);
461
                                styleRow(row, true);
462
                        }
463
                        if (selectedRows.size() == 1)
464
                                GSS.get().setCurrentSelection(files.get(selectedRows.get(0)));
465
                        else
466
                                GSS.get().setCurrentSelection(getSelectedFiles());
467
                        //contextMenu.setFiles(getSelectedFiles());
468
                        table.setWidget(row + 1, 0, contextMenu);
469

    
470
                }
471
                /*if (row < folderFileCount) {
472

473
                        styleRow(selectedRow, false);
474
                        styleRow(row, true);
475

476
                        previous = selectedRow;
477
                        selectedRow = row;
478
                        GSS.get().setCurrentSelection(files.get(selectedRow));
479

480
                        if (previous >= 0)
481
                                table.setWidget(previous + 1, 0, images.document().createImage());
482
                        table.setWidget(selectedRow + 1, 0, contextMenu);
483
                }
484
                */
485
        }
486

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

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

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

    
519
                // Show the selected files.
520
                int i = 1;
521
                for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
522
                        // Don't read past the end.
523
                        // if (i > folderFileCount)
524
                        // break;
525
                        if (startIndex + i > folderFileCount)
526
                                break;
527
                        // Add a new row to the table, then set each of its columns to the
528
                        // proper values.
529
                        table.setWidget(i, 0, images.document().createImage());
530
                        FileResource fileHeader = files.get(startIndex + i - 1);
531
                        table.getRowFormatter().addStyleName(i, "gss-fileRow");
532
                        table.setHTML(i, 1, fileHeader.getName());
533
                        table.setText(i, 2, fileHeader.getOwner());
534
                        table.setText(i, 3, fileHeader.getPath());
535
                        table.setText(i, 4, String.valueOf(fileHeader.getVersion()));
536
                        table.setText(i, 5, String.valueOf(fileHeader.getFileSizeAsString()));
537
                        final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
538
                        table.setText(i, 6, formatter.format(fileHeader.getCreationDate()));
539
                        folderTotalSize += fileHeader.getContentLength();
540
                }
541

    
542
                // Clear any remaining slots.
543
                for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
544
                        table.setHTML(i, 0, "&nbsp;");
545
                        table.setHTML(i, 1, "&nbsp;");
546
                        table.setHTML(i, 2, "&nbsp;");
547
                        table.setHTML(i, 3, "&nbsp;");
548
                        table.setHTML(i, 4, "&nbsp;");
549
                        table.setHTML(i, 5, "&nbsp;");
550
                        table.setHTML(i, 6, "&nbsp;");
551
                        table.setHTML(i, 7, "&nbsp;");
552
                }
553

    
554
                if (folderFileCount == 0) {
555
                        showingStats = "no files";
556
                        prevButton.setVisible(false);
557
                        nextButton.setVisible(false);
558
                } else if (folderFileCount < GSS.VISIBLE_FILE_COUNT) {
559
                        showingStats = folderFileCount + " files" + " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
560
                        prevButton.setVisible(false);
561
                        nextButton.setVisible(false);
562
                } else {
563
                        showingStats = "" + (startIndex + 1) + " - " + max + " of " + count + " files" + " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
564
                        prevButton.setVisible(startIndex != 0);
565
                        nextButton.setVisible(startIndex + GSS.VISIBLE_FILE_COUNT < count);
566
                }
567
                updateCurrentlyShowingStats();
568

    
569
                // Reset the selected line.
570
                // styleRow(selectedRow, false);
571
                // selectedRow = -1;
572

    
573
        }
574

    
575
        /**
576
         *  update status panel with currently showing file stats
577
         */
578
        public void updateCurrentlyShowingStats() {
579
                GSS.get().getStatusPanel().updateCurrentlyShowing(showingStats);
580
        }
581

    
582
        /**
583
         * Adjust the height of the table by adding and removing rows as necessary.
584
         *
585
         * @param newHeight the new height to reach
586
         */
587
        void resizeTableHeight(final int newHeight) {
588
                GWT.log("Panel: " + newHeight + ", parent: " + table.getParent().getOffsetHeight(), null);
589
                // Fill the rest with empty slots.
590
                if (newHeight > table.getOffsetHeight())
591
                        while (newHeight > table.getOffsetHeight()) {
592
                                // table.setHTML(table.getRowCount(), 5, "&nbsp;");
593
                                table.resizeRows(table.getRowCount() + 1);
594
                                GWT.log("Table: " + table.getOffsetHeight() + ", rows: " + table.getRowCount(), null);
595
                        }
596
                else
597
                        while (newHeight < table.getOffsetHeight()) {
598
                                // table.setHTML(table.getRowCount(), 5, "&nbsp;");
599
                                table.resizeRows(table.getRowCount() - 1);
600
                                GWT.log("Table: " + table.getOffsetHeight() + ", rows: " + table.getRowCount(), null);
601
                        }
602
        }
603

    
604
        /**
605
         * Update the file cache with data from the server.
606
         *
607
         * @param userId the ID of the current user
608
         */
609
        public void updateFileCache( String query) {
610
                clearSelectedRows();
611
                sortingProperty = "name";
612
                clearLabels();
613
                nameLabel.setHTML("Name&nbsp;" + images.desc().getHTML());
614
                sortingType = true;
615
                startIndex = 0;
616
                GSS.get().showLoadingIndicator();
617
                if (query == null || query.trim().equals("")) {
618
                        searchResults.setHTML("You must specify a query");
619
                        setFiles(new ArrayList());
620
                        update();
621
                        GSS.get().hideLoadingIndicator();
622
                } else{
623
                        searchResults.setHTML("Search results for " + query);
624
                        ExecuteGet<SearchResource> eg = new ExecuteGet<SearchResource>(SearchResource.class, GSS.GSS_REST_PATH+"search/"+URL.encodeComponent(query)){
625

    
626
                                @Override
627
                                public void onComplete() {
628
                                        SearchResource s = getResult();
629
                                        ExecuteMultipleHead<FileResource> ef = new ExecuteMultipleHead<FileResource>(FileResource.class, s.getFiles().toArray(new String[0])){
630

    
631
                                                @Override
632
                                                public void onComplete() {
633
                                                        setFiles(getResult());
634
                                                        update();
635
                                                }
636

    
637
                                                public void onError(String p, Throwable throwable) {
638
                                                        // TODO Auto-generated method stub
639

    
640
                                                }
641

    
642

    
643
                                                public void onError(Throwable t) {
644
                                                        // TODO Auto-generated method stub
645

    
646
                                                }
647

    
648
                                        };
649
                                        DeferredCommand.addCommand(ef);
650
                                }
651

    
652
                                public void onError(Throwable t) {
653
                                        if(t instanceof RestException)
654
                                                GSS.get().displayError("Unable to perform search:"+((RestException)t).getHttpStatusText());
655
                                        else
656
                                                GSS.get().displayError("System error performing search:"+t.getMessage());
657
                                        updateFileCache("");
658
                                }
659

    
660
                        };
661
                        DeferredCommand.addCommand(eg);
662
                }
663
                /*
664
                        service.searchFiles(userId, query, new AsyncCallback() {
665

666
                        public void onSuccess(final Object result) {
667
                                final List<FileResource> f = (List<FileResource>) result;
668
                                // GWT.log(f[0].toString(), null);
669
                                setFiles(f);
670
                                update();
671
                                GSS.get().hideLoadingIndicator();
672
                        }
673

674
                        public void onFailure(final Throwable caught) {
675
                                GWT.log("", caught);
676
                                GSS.get().hideLoadingIndicator();
677
                                if (caught instanceof RpcException)
678
                                        GSS.get().displayError("An error occurred while " + "communicating with the server: " + caught.getMessage());
679
                                else
680
                                        GSS.get().displayError(caught.getMessage());
681
                        }
682

683
                });
684
                */
685
        }
686

    
687
        /**
688
         * Fill the file cache with data.
689
         *
690
         * @param _files
691
         * @param filePaths the files to set
692
         */
693
        void setFiles(final List<FileResource> _files) {
694
                files = _files;
695
                Collections.sort(files, new Comparator<FileResource>() {
696

    
697
                        public int compare(FileResource arg0, FileResource arg1) {
698
                                return arg0.getName().compareTo(arg1.getName());
699

    
700
                        }
701

    
702
                });
703
                folderFileCount = files.size();
704
                GWT.log("File count:" + folderFileCount, null);
705
        }
706

    
707
        private void sortFiles(final String sortProperty) {
708
                if (sortProperty.equals(sortingProperty))
709
                        sortingType = !sortingType;
710
                else {
711
                        sortingProperty = sortProperty;
712
                        sortingType = true;
713
                }
714
                clearLabels();
715
                clearSelectedRows();
716
                if (files == null || files.size() == 0)
717
                        return;
718
                Collections.sort(files, new Comparator<FileResource>() {
719

    
720
                        public int compare(FileResource arg0, FileResource arg1) {
721
                                if (sortingType)
722
                                        if (sortProperty.equals("version")) {
723
                                                versionLabel.setHTML("Version&nbsp;" + images.desc().getHTML());
724
                                                return new Integer(arg0.getVersion()).compareTo(new Integer(arg1.getVersion()));
725
                                        } else if (sortProperty.equals("owner")) {
726
                                                ownerLabel.setHTML("Owner&nbsp;" + images.desc().getHTML());
727
                                                GWT.log(arg0.getOwner() + "/" + arg1.getOwner(), null);
728
                                                return arg0.getOwner().compareTo(arg1.getOwner());
729
                                        } else if (sortProperty.equals("date")) {
730
                                                dateLabel.setHTML("Date&nbsp;" + images.desc().getHTML());
731
                                                return arg0.getCreationDate().compareTo(arg1.getCreationDate());
732
                                        } else if (sortProperty.equals("size")) {
733
                                                sizeLabel.setHTML("Size&nbsp;" + images.desc().getHTML());
734
                                                return new Long(arg0.getContentLength()).compareTo(new Long(arg1.getContentLength()));
735
                                        } else if (sortProperty.equals("name")) {
736
                                                nameLabel.setHTML("Name&nbsp;" + images.desc().getHTML());
737
                                                return arg0.getName().compareTo(arg1.getName());
738
                                        } else if (sortProperty.equals("path")) {
739
                                                pathLabel.setHTML("Path&nbsp;" + images.desc().getHTML());
740
                                                return arg0.getPath().compareTo(arg1.getPath());
741
                                        } else {
742
                                                nameLabel.setHTML("Name&nbsp;" + images.desc().getHTML());
743
                                                return arg0.getName().compareTo(arg1.getName());
744
                                        }
745
                                else if (sortProperty.equals("version")) {
746
                                        versionLabel.setHTML("Version&nbsp;" + images.asc().getHTML());
747
                                        return new Integer(arg1.getVersion()).compareTo(new Integer(arg0.getVersion()));
748
                                } else if (sortProperty.equals("owner")) {
749
                                        ownerLabel.setHTML("Owner&nbsp;" + images.asc().getHTML());
750
                                        return arg1.getOwner().compareTo(arg0.getOwner());
751
                                } else if (sortProperty.equals("date")) {
752
                                        dateLabel.setHTML("Date&nbsp;" + images.asc().getHTML());
753
                                        return arg1.getCreationDate().compareTo(arg0.getCreationDate());
754
                                } else if (sortProperty.equals("size")) {
755
                                        sizeLabel.setHTML("Size&nbsp;" + images.asc().getHTML());
756
                                        return new Long(arg1.getContentLength()).compareTo(new Long(arg0.getContentLength()));
757
                                } else if (sortProperty.equals("name")) {
758
                                        nameLabel.setHTML("Name&nbsp;" + images.asc().getHTML());
759
                                        return arg1.getName().compareTo(arg0.getName());
760
                                } else if (sortProperty.equals("path")) {
761
                                        pathLabel.setHTML("Path&nbsp;" + images.asc().getHTML());
762
                                        return arg1.getPath().compareTo(arg0.getPath());
763
                                } else {
764
                                        nameLabel.setHTML("Name&nbsp;" + images.asc().getHTML());
765
                                        return arg1.getName().compareTo(arg0.getName());
766
                                }
767
                        }
768

    
769
                });
770
        }
771

    
772
        private void clearLabels() {
773
                nameLabel.setText("Name");
774
                versionLabel.setText("Version");
775
                sizeLabel.setText("Size");
776
                dateLabel.setText("Date");
777
                ownerLabel.setText("Owner");
778
                pathLabel.setText("Path");
779
        }
780

    
781
        /**
782
         * Retrieve the table.
783
         *
784
         * @return the table
785
         */
786
        Grid getTable() {
787
                return table;
788
        }
789

    
790
        /**
791
         * Does the list contains the requested filename
792
         *
793
         * @param fileName
794
         * @return true/false
795
         */
796
        public boolean contains(String fileName) {
797
                for (int i = 0; i < files.size(); i++)
798
                        if (files.get(i).getName().equals(fileName))
799
                                return true;
800
                return false;
801
        }
802

    
803
        public void clearSelectedRows() {
804
                for (int r : selectedRows) {
805
                        int row = r - startIndex;
806
                        styleRow(row, false);
807
                        table.setWidget(row + 1, 0, images.document().createImage());
808
                }
809
                selectedRows.clear();
810
                Object sel = GSS.get().getCurrentSelection();
811
                if (sel instanceof FileResource || sel instanceof List)
812
                        GSS.get().setCurrentSelection(null);
813
        }
814

    
815
        public static native void preventIESelection() /*-{
816
             $doc.body.onselectstart = function () { return false; };
817
         }-*/;
818

    
819
        public static native void enableIESelection() /*-{
820
         if ($doc.body.onselectstart != null)
821
             $doc.body.onselectstart = null;
822
         }-*/;
823

    
824
        /**
825
         *
826
         */
827
        public void selectAllRows() {
828
                clearSelectedRows();
829
                int count = folderFileCount;
830
                if (count == 0)
831
                        return;
832
                int max = startIndex + GSS.VISIBLE_FILE_COUNT;
833
                if (max > count)
834
                        max = count;
835
                int i = 1;
836
                for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
837
                        // Don't read past the end.
838
                        // if (i > folderFileCount)
839
                        // break;
840
                        if (startIndex + i > folderFileCount)
841
                                break;
842
                        selectedRows.add(startIndex + i - 1);
843
                        styleRow(i - 1, true);
844
                }
845
                GSS.get().setCurrentSelection(getSelectedFiles());
846
                //contextMenu.setFiles(getSelectedFiles());
847
                table.setWidget(i - 1, 0, contextMenu);
848

    
849
        }
850

    
851
}