2 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.client;
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;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.Comparator;
32 import java.util.List;
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;
57 * A composite that displays a list of search results for a particular query on
60 public class SearchResults extends Composite implements ClickHandler {
62 private HTML prevButton = new HTML("<a href='javascript:;'>< Previous</a>", true);
64 private HTML nextButton = new HTML("<a href='javascript:;'>Next ></a>", true);
66 private String showingStats = "";
68 private int startIndex = 0;
71 * A constant that denotes the completion of an IncrementalCommand.
73 public static final boolean DONE = false;
75 private boolean clickControl = false;
77 private boolean clickShift = false;
79 private int firstShift = -1;
81 private ArrayList<Integer> selectedRows = new ArrayList<Integer>();
86 * Specifies that the images available for this composite will be the ones
87 * available in FileContextMenu.
89 public interface Images extends ClientBundle,FileContextMenu.Images, CellTreeView.Images {
91 @Source("gr/ebs/gss/resources/blank.gif")
92 ImageResource blank();
94 @Source("gr/ebs/gss/resources/asc.png")
97 @Source("gr/ebs/gss/resources/desc.png")
102 * A label with the number of files in this folder.
104 private HTML countLabel = new HTML();
107 * The table widget with the file list.
109 private FileTable table;
112 * The navigation bar for paginating the results.
114 private HorizontalPanel navBar = new HorizontalPanel();
117 * The number of files in the search results
119 private int folderFileCount;
122 * Total search results size
124 private long folderTotalSize;
127 * A cache of the files in the list.
129 private List<FileResource> files;
132 * The widget's image bundle.
134 private final Images images;
136 private String sortingProperty = "name";
138 private boolean sortingType = true;
140 private HTML nameLabel;
142 private HTML versionLabel;
144 private HTML sizeLabel;
146 private HTML dateLabel;
148 private HTML ownerLabel;
150 private HTML pathLabel;
152 private HTML searchResults = new HTML("Results for search:");
155 * Construct the file list widget. This entails setting up the widget
156 * layout, fetching the number of files in the current folder from the
157 * server and filling the local file cache of displayed files with data from
158 * the server, as well.
162 public SearchResults(final Images _images) {
164 final GSS app = GSS.get();
165 table = new FileTable(GSS.VISIBLE_FILE_COUNT + 1, 8) {
168 public void onBrowserEvent(Event event) {
169 if (DOM.eventGetType(event) == Event.ONMOUSEDOWN && DOM.eventGetButton(event) == NativeEvent.BUTTON_RIGHT){
170 if (DOM.eventGetCtrlKey(event))
173 clickControl = false;
174 if (DOM.eventGetShiftKey(event)) {
176 if (selectedRows.size() == 1)
177 firstShift = selectedRows.get(0) - startIndex;
178 //event.preventDefault();
182 //event.preventDefault();
184 int ri = table.getRowForEvent2(event);
186 if(!selectedRows.contains(ri-1))
187 onRowClicked(ri, false);
190 if (files == null || files.size() == 0) {
191 if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && selectedRows.size() == 0) {
192 FileContextMenu fm = new FileContextMenu(images, false, true);
193 fm.onEmptyEvent(event);
197 if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && selectedRows.size() != 0) {
198 FileContextMenu fm = new FileContextMenu(images, false, false);
200 } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && selectedRows.size() == 0) {
201 FileContextMenu fm = new FileContextMenu(images, false, true);
202 fm.onEmptyEvent(event);
203 } else if (DOM.eventGetType(event) == Event.ONDBLCLICK)
204 if (getSelectedFiles().size() == 1) {
205 FileResource file = getSelectedFiles().get(0);
206 String dateString = RestCommand.getDate();
207 String resource = file.getUri().substring(app.getApiPath().length() - 1, file.getUri().length());
208 String sig = app.getCurrentUserResource().getUsername() + " " +
209 RestCommand.calculateSig("GET", dateString, resource,
210 RestCommand.base64decode(app.getToken()));
211 Window.open(file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date=" + URL.encodeComponent(dateString), "_blank", "");
212 event.preventDefault();
215 if (DOM.eventGetType(event) == Event.ONCLICK) {
216 if (DOM.eventGetCtrlKey(event))
219 clickControl = false;
220 if (DOM.eventGetShiftKey(event)) {
222 if (selectedRows.size() == 1)
223 firstShift = selectedRows.get(0) - startIndex;
224 //event.preventDefault();
228 //event.preventDefault();
231 super.onBrowserEvent(event);
234 prevButton.addClickHandler(this);
235 nextButton.addClickHandler(this);
239 table.setCellSpacing(0);
240 table.setCellPadding(2);
241 table.setWidth("100%");
244 table.addClickHandler(new ClickHandler() {
247 public void onClick(ClickEvent event) {
248 Cell cell = table.getCellForEvent(event);
249 onRowClicked(cell.getRowIndex(),true);
253 // Create the 'navigation' bar at the upper-right.
254 HorizontalPanel innerNavBar = new HorizontalPanel();
255 innerNavBar.setStyleName("gss-ListNavBar");
256 innerNavBar.setSpacing(8);
257 innerNavBar.add(prevButton);
258 innerNavBar.add(countLabel);
259 innerNavBar.add(nextButton);
260 navBar.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
261 navBar.add(innerNavBar);
262 navBar.setWidth("100%");
263 VerticalPanel vp = new VerticalPanel();
264 vp.add(searchResults);
265 searchResults.addStyleName("gss-searchLabel");
268 table.setStyleName("gss-List");
270 DeferredCommand.addCommand(new IncrementalCommand() {
273 public boolean execute() {
274 return fetchRootFolder();
277 table.sinkEvents(Event.ONCONTEXTMENU);
278 table.sinkEvents(Event.ONMOUSEUP);
279 table.sinkEvents(Event.ONMOUSEDOWN);
280 table.sinkEvents(Event.ONCLICK);
281 table.sinkEvents(Event.ONKEYDOWN);
282 table.sinkEvents(Event.ONDBLCLICK);
283 preventIESelection();
287 public void onClick(ClickEvent event) {
288 if (event.getSource() == nextButton) {
289 // Move forward a page.
291 startIndex += GSS.VISIBLE_FILE_COUNT;
292 if (startIndex >= folderFileCount)
293 startIndex -= GSS.VISIBLE_FILE_COUNT;
296 } else if (event.getSource() == prevButton) {
299 startIndex -= GSS.VISIBLE_FILE_COUNT;
308 * Retrieve the root folder for the current user.
310 * @return true if the retrieval was successful
312 protected boolean fetchRootFolder() {
313 UserResource user = GSS.get().getCurrentUserResource();
320 public void onRowClicked(int row, boolean toggleSelection) {
321 // Select the row that was clicked (-1 to account for header row).
322 if (row > folderFileCount)
325 GWT.log("ROW is:" + row + " fs:" + firstShift, null);
326 if (firstShift == -1)
328 else if (row > firstShift) {
330 for (int i = firstShift; i < row; i++) {
331 selectedRows.add(startIndex + i);
334 GSS.get().setCurrentSelection(getSelectedFiles());
335 //contextMenu.setFiles(getSelectedFiles());
336 //table.setWidget(row, 0, contextMenu);
337 } else if (row != -1 && row == firstShift) {
338 selectedRows.add(row);
339 selectedRows.add(row - 1);
341 styleRow(row - 1, true);
342 } else if (row < firstShift) {
343 GWT.log("ROW is:" + row + " fs:" + firstShift, null);
346 for (int i = firstShift; i >= row - 1; i--) {
347 selectedRows.add(startIndex + i);
350 GSS.get().setCurrentSelection(getSelectedFiles());
351 //table.setWidget(row, 0, contextMenu);
352 //contextMenu.setFiles(getSelectedFiles());
356 selectRow(row - 1, toggleSelection);
361 * Initializes the table so that it contains enough rows for a full page of
364 private void initTable() {
366 nameLabel = new HTML("Name");
367 nameLabel.addClickHandler(new ClickHandler() {
369 public void onClick(ClickEvent event) {
374 versionLabel = new HTML("Version");
375 versionLabel.addClickHandler(new ClickHandler() {
377 public void onClick(ClickEvent event) {
378 sortFiles("version");
382 sizeLabel = new HTML("Size");
383 sizeLabel.addClickHandler(new ClickHandler() {
385 public void onClick(ClickEvent event) {
390 dateLabel = new HTML("Last modified");
391 dateLabel.addClickHandler(new ClickHandler() {
393 public void onClick(ClickEvent event) {
398 ownerLabel = new HTML("Owner");
399 ownerLabel.addClickHandler(new ClickHandler() {
401 public void onClick(ClickEvent event) {
407 pathLabel = new HTML("Path");
408 pathLabel.addClickHandler(new ClickHandler() {
410 public void onClick(ClickEvent event) {
415 // Create the header row.
416 table.setText(0, 0, "");
417 // table.setText(0, 1, "Name");
418 table.setWidget(0, 1, nameLabel);
419 table.setWidget(0, 2, ownerLabel);
420 // table.setText(0, 3, "Version");
421 table.setWidget(0, 3, pathLabel);
422 table.setWidget(0, 4, versionLabel);
423 // table.setText(0, 4, "Size");
424 table.setWidget(0, 5, sizeLabel);
425 // table.setText(0, 5, "Last modified");
426 table.setWidget(0, 6, dateLabel);
427 table.setWidget(0, 7, navBar);
428 table.getRowFormatter().setStyleName(0, "gss-ListHeader");
430 // Initialize the rest of the rows.
431 for (int i = 1; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
432 table.setText(i, 0, "");
433 table.setText(i, 1, "");
434 table.setText(i, 2, "");
435 table.setText(i, 3, "");
436 table.setText(i, 4, "");
437 table.setText(i, 5, "");
438 table.setText(i, 6, "");
439 table.getCellFormatter().setWordWrap(i, 0, false);
440 table.getCellFormatter().setWordWrap(i, 1, false);
441 table.getCellFormatter().setWordWrap(i, 2, false);
442 table.getCellFormatter().setWordWrap(i, 3, false);
443 table.getCellFormatter().setWordWrap(i, 4, false);
444 table.getCellFormatter().setWordWrap(i, 5, false);
445 table.getCellFormatter().setWordWrap(i, 6, false);
446 table.getCellFormatter().setHorizontalAlignment(i, 4, HasHorizontalAlignment.ALIGN_CENTER);
448 prevButton.setVisible(false);
449 nextButton.setVisible(false);
453 * Selects the given row (relative to the current page).
455 * @param row the row to be selected
457 private void selectRow(final int row, boolean toggleSelection) {
458 if (row < folderFileCount) {
460 if (selectedRows.contains(row) && toggleSelection) {
461 int i = selectedRows.indexOf(startIndex + row);
462 selectedRows.remove(i);
463 styleRow(row, false);
465 for (int r : selectedRows) {
466 int prow = r - startIndex;
467 table.setWidget(prow + 1, 0, AbstractImagePrototype.create(images.document()).createImage());
469 selectedRows.add(startIndex + row);
474 selectedRows.add(startIndex + row);
477 if (selectedRows.size() == 1)
478 GSS.get().setCurrentSelection(files.get(selectedRows.get(0)));
480 GSS.get().setCurrentSelection(getSelectedFiles());
481 //contextMenu.setFiles(getSelectedFiles());
482 //table.setWidget(row + 1, 0, contextMenu);
486 public List<FileResource> getSelectedFiles() {
487 List<FileResource> result = new ArrayList();
488 for (int i : selectedRows)
489 result.add(files.get(i));
494 * Make the specified row look like selected or not, according to the
495 * <code>selected</code> flag.
500 void styleRow(final int row, final boolean selected) {
501 if (row != -1 && row >= 0)
503 table.getRowFormatter().addStyleName(row + 1, "gss-SelectedRow");
505 table.getRowFormatter().removeStyleName(row + 1, "gss-SelectedRow");
509 * Update the display of the file list.
511 void update(boolean sort) {
512 int count = folderFileCount;
513 int max = startIndex + GSS.VISIBLE_FILE_COUNT;
518 if (sort && files != null && files.size() != 0) {
522 Collections.sort(files, new Comparator<FileResource>() {
525 public int compare(FileResource arg0, FileResource arg1) {
526 AbstractImagePrototype descPrototype = AbstractImagePrototype.create(images.desc());
527 AbstractImagePrototype ascPrototype = AbstractImagePrototype.create(images.asc());
529 if (sortingProperty.equals("version")) {
530 versionLabel.setHTML("Version " + descPrototype.getHTML());
531 return arg0.getVersion().compareTo(arg1.getVersion());
532 } else if (sortingProperty.equals("owner")) {
533 ownerLabel.setHTML("Owner " + descPrototype.getHTML());
534 return arg0.getOwner().compareTo(arg1.getOwner());
535 } else if (sortingProperty.equals("date")) {
536 dateLabel.setHTML("Last modified " + descPrototype.getHTML());
537 return arg0.getModificationDate().compareTo(arg1.getModificationDate());
538 } else if (sortingProperty.equals("size")) {
539 sizeLabel.setHTML("Size " + descPrototype.getHTML());
540 return arg0.getContentLength().compareTo(arg1.getContentLength());
541 } else if (sortingProperty.equals("name")) {
542 nameLabel.setHTML("Name " + descPrototype.getHTML());
543 return arg0.getName().compareTo(arg1.getName());
544 } else if (sortingProperty.equals("path")) {
545 pathLabel.setHTML("Path " + descPrototype.getHTML());
546 return arg0.getUri().compareTo(arg1.getUri());
548 nameLabel.setHTML("Name " + descPrototype.getHTML());
549 return arg0.getName().compareTo(arg1.getName());
551 else if (sortingProperty.equals("version")) {
552 versionLabel.setHTML("Version " + ascPrototype.getHTML());
553 return arg1.getVersion().compareTo(arg0.getVersion());
554 } else if (sortingProperty.equals("owner")) {
555 ownerLabel.setHTML("Owner " + ascPrototype.getHTML());
556 return arg1.getOwner().compareTo(arg0.getOwner());
557 } else if (sortingProperty.equals("date")) {
558 dateLabel.setHTML("Last modified " + ascPrototype.getHTML());
559 return arg1.getModificationDate().compareTo(arg0.getModificationDate());
560 } else if (sortingProperty.equals("size")) {
561 sizeLabel.setHTML("Size " + ascPrototype.getHTML());
562 return arg1.getContentLength().compareTo(arg0.getContentLength());
563 } else if (sortingProperty.equals("name")) {
564 nameLabel.setHTML("Name " + ascPrototype.getHTML());
565 return arg1.getName().compareTo(arg0.getName());
566 } else if (sortingProperty.equals("path")) {
567 pathLabel.setHTML("Path " + ascPrototype.getHTML());
568 return arg1.getUri().compareTo(arg0.getUri());
570 nameLabel.setHTML("Name " + ascPrototype.getHTML());
571 return arg1.getName().compareTo(arg0.getName());
578 // Show the selected files.
580 for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
581 // Don't read past the end.
582 // if (i > folderFileCount)
584 if (startIndex + i > folderFileCount)
586 // Add a new row to the table, then set each of its columns to the
588 table.setWidget(i, 0, AbstractImagePrototype.create(images.document()).createImage());
589 FileResource fileHeader = files.get(startIndex + i - 1);
590 table.getRowFormatter().addStyleName(i, "gss-fileRow");
591 table.setHTML(i, 1, fileHeader.getName());
592 table.setText(i, 2, fileHeader.getOwner());
593 table.setText(i, 3, fileHeader.getPath());
594 table.setText(i, 4, String.valueOf(fileHeader.getVersion()));
595 table.setText(i, 5, String.valueOf(fileHeader.getFileSizeAsString()));
596 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
597 table.setText(i, 6, formatter.format(fileHeader.getModificationDate()));
598 folderTotalSize += fileHeader.getContentLength();
601 // Clear any remaining slots.
602 for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
603 table.setHTML(i, 0, " ");
604 table.setHTML(i, 1, " ");
605 table.setHTML(i, 2, " ");
606 table.setHTML(i, 3, " ");
607 table.setHTML(i, 4, " ");
608 table.setHTML(i, 5, " ");
609 table.setHTML(i, 6, " ");
610 table.setHTML(i, 7, " ");
613 if (folderFileCount == 0) {
614 showingStats = "no files";
615 prevButton.setVisible(false);
616 nextButton.setVisible(false);
617 } else if (folderFileCount < GSS.VISIBLE_FILE_COUNT) {
618 if (folderFileCount == 1)
619 showingStats = "1 file";
621 showingStats = folderFileCount + " files";
622 showingStats += " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
623 prevButton.setVisible(false);
624 nextButton.setVisible(false);
626 showingStats = "" + (startIndex + 1) + " - " + max + " of " + count + " files" + " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
627 prevButton.setVisible(startIndex != 0);
628 nextButton.setVisible(startIndex + GSS.VISIBLE_FILE_COUNT < count);
630 updateCurrentlyShowingStats();
634 * update status panel with currently showing file stats
636 public void updateCurrentlyShowingStats() {
637 GSS.get().getStatusPanel().updateCurrentlyShowing(showingStats);
641 * Adjust the height of the table by adding and removing rows as necessary.
643 * @param newHeight the new height to reach
645 void resizeTableHeight(final int newHeight) {
646 GWT.log("Panel: " + newHeight + ", parent: " + table.getParent().getOffsetHeight(), null);
647 // Fill the rest with empty slots.
648 if (newHeight > table.getOffsetHeight())
649 while (newHeight > table.getOffsetHeight()) {
650 // table.setHTML(table.getRowCount(), 5, " ");
651 table.resizeRows(table.getRowCount() + 1);
652 GWT.log("Table: " + table.getOffsetHeight() + ", rows: " + table.getRowCount(), null);
655 while (newHeight < table.getOffsetHeight()) {
656 // table.setHTML(table.getRowCount(), 5, " ");
657 table.resizeRows(table.getRowCount() - 1);
658 GWT.log("Table: " + table.getOffsetHeight() + ", rows: " + table.getRowCount(), null);
663 * Update the file cache with data from the server.
665 public void updateFileCache(String query) {
666 final GSS app = GSS.get();
670 app.showLoadingIndicator();
671 if (query == null || query.trim().equals("")) {
672 searchResults.setHTML("You must specify a query.");
673 setFiles(new ArrayList());
675 app.hideLoadingIndicator();
676 } else if (!GSS.isValidResourceName(query)) {
677 searchResults.setHTML("The query was invalid. Try to use words that appear in the file's name, contents or tags.");
678 setFiles(new ArrayList());
680 app.hideLoadingIndicator();
682 searchResults.setHTML("Search results for " + query);
684 GetCommand<SearchResource> eg = new GetCommand<SearchResource>(SearchResource.class,
685 app.getApiPath() + "search/" + URL.encodeComponent(query), null) {
688 public void onComplete() {
689 SearchResource s = getResult();
690 setFiles(s.getFiles());
695 public void onError(Throwable t) {
696 if(t instanceof RestException)
697 app.displayError("Unable to perform search:"+((RestException)t).getHttpStatusText());
699 app.displayError("System error performing search:"+t.getMessage());
704 DeferredCommand.addCommand(eg);
709 * Fill the file cache with data.
712 * @param filePaths the files to set
714 private void setFiles(List<FileResource> _files) {
716 Collections.sort(files, new Comparator<FileResource>() {
719 public int compare(FileResource arg0, FileResource arg1) {
720 return arg0.getName().compareTo(arg1.getName());
724 folderFileCount = files.size();
725 GWT.log("File count:" + folderFileCount, null);
728 private void sortFiles(final String sortProperty) {
729 if (sortProperty.equals(sortingProperty))
730 sortingType = !sortingType;
732 sortingProperty = sortProperty;
738 private void clearLabels() {
739 nameLabel.setText("Name");
740 versionLabel.setText("Version");
741 sizeLabel.setText("Size");
742 dateLabel.setText("Last modified");
743 ownerLabel.setText("Owner");
744 pathLabel.setText("Path");
748 * Retrieve the table.
757 * Does the list contains the requested filename
762 public boolean contains(String fileName) {
763 for (int i = 0; i < files.size(); i++)
764 if (files.get(i).getName().equals(fileName))
769 public void clearSelectedRows() {
770 for (int r : selectedRows) {
771 int row = r - startIndex;
772 styleRow(row, false);
773 table.setWidget(row + 1, 0, AbstractImagePrototype.create(images.document()).createImage());
775 selectedRows.clear();
776 Object sel = GSS.get().getCurrentSelection();
777 if (sel instanceof FileResource || sel instanceof List)
778 GSS.get().setCurrentSelection(null);
781 public static native void preventIESelection() /*-{
782 $doc.body.onselectstart = function () { return false; };
785 public static native void enableIESelection() /*-{
786 if ($doc.body.onselectstart != null)
787 $doc.body.onselectstart = null;
790 public void selectAllRows() {
792 int count = folderFileCount;
795 int max = startIndex + GSS.VISIBLE_FILE_COUNT;
799 for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
800 // Don't read past the end.
801 // if (i > folderFileCount)
803 if (startIndex + i > folderFileCount)
805 selectedRows.add(startIndex + i - 1);
806 styleRow(i - 1, true);
808 GSS.get().setCurrentSelection(getSelectedFiles());
809 //contextMenu.setFiles(getSelectedFiles());
810 //table.setWidget(i - 1, 0, contextMenu);