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;
21 import gr.ebs.gss.client.dnd.DnDSimpleFocusPanel;
22 import gr.ebs.gss.client.dnd.DnDTreeItem;
23 import gr.ebs.gss.client.rest.GetCommand;
24 import gr.ebs.gss.client.rest.MultipleHeadCommand;
25 import gr.ebs.gss.client.rest.RestCommand;
26 import gr.ebs.gss.client.rest.RestException;
27 import gr.ebs.gss.client.rest.resource.FileResource;
28 import gr.ebs.gss.client.rest.resource.FolderResource;
29 import gr.ebs.gss.client.rest.resource.OtherUserResource;
30 import gr.ebs.gss.client.rest.resource.SharedResource;
31 import gr.ebs.gss.client.rest.resource.TrashResource;
32 import gr.ebs.gss.client.rest.resource.UserResource;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.Comparator;
37 import java.util.List;
39 import com.google.gwt.core.client.GWT;
40 import com.google.gwt.dom.client.NativeEvent;
41 import com.google.gwt.event.dom.client.ClickEvent;
42 import com.google.gwt.event.dom.client.ClickHandler;
43 import com.google.gwt.http.client.URL;
44 import com.google.gwt.i18n.client.DateTimeFormat;
45 import com.google.gwt.resources.client.ClientBundle;
46 import com.google.gwt.resources.client.ImageResource;
47 import com.google.gwt.user.client.DOM;
48 import com.google.gwt.user.client.DeferredCommand;
49 import com.google.gwt.user.client.Event;
50 import com.google.gwt.user.client.IncrementalCommand;
51 import com.google.gwt.user.client.Window;
52 import com.google.gwt.user.client.ui.AbstractImagePrototype;
53 import com.google.gwt.user.client.ui.Composite;
54 import com.google.gwt.user.client.ui.Grid;
55 import com.google.gwt.user.client.ui.HTML;
56 import com.google.gwt.user.client.ui.HTMLTable.Cell;
57 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
58 import com.google.gwt.user.client.ui.HorizontalPanel;
59 import com.google.gwt.user.client.ui.TreeItem;
60 import com.google.gwt.user.client.ui.Widget;
63 * A composite that displays the list of files in a particular folder.
65 public class FileList extends Composite implements ClickHandler {
67 private HTML prevButton = new HTML("<a href='javascript:;'>< Previous</a>", true);
69 private HTML nextButton = new HTML("<a href='javascript:;'>Next ></a>", true);
71 private String showingStats = "";
73 private int startIndex = 0;
76 * A constant that denotes the completion of an IncrementalCommand.
78 public static final boolean DONE = false;
80 private boolean clickControl = false;
82 private boolean clickShift = false;
84 private int firstShift = -1;
86 private ArrayList<Integer> selectedRows = new ArrayList<Integer>();
89 * The context menu for the selected file.
91 final DnDSimpleFocusPanel contextMenu;
94 * Specifies that the images available for this composite will be the ones
95 * available in FileContextMenu.
97 public interface Images extends ClientBundle,FileContextMenu.Images, Folders.Images {
99 @Source("gr/ebs/gss/resources/blank.gif")
100 ImageResource blank();
102 @Source("gr/ebs/gss/resources/asc.png")
105 @Source("gr/ebs/gss/resources/desc.png")
106 ImageResource desc();
108 @Source("gr/ebs/gss/resources/mimetypes/document_shared.png")
109 ImageResource documentShared();
111 @Source("gr/ebs/gss/resources/mimetypes/kcmfontinst.png")
112 ImageResource wordprocessor();
114 @Source("gr/ebs/gss/resources/mimetypes/log.png")
115 ImageResource spreadsheet();
117 @Source("gr/ebs/gss/resources/mimetypes/kpresenter_kpr.png")
118 ImageResource presentation();
120 @Source("gr/ebs/gss/resources/mimetypes/acroread.png")
123 @Source("gr/ebs/gss/resources/mimetypes/image.png")
124 ImageResource image();
126 @Source("gr/ebs/gss/resources/mimetypes/video2.png")
127 ImageResource video();
129 @Source("gr/ebs/gss/resources/mimetypes/knotify.png")
130 ImageResource audio();
132 @Source("gr/ebs/gss/resources/mimetypes/html.png")
133 ImageResource html();
135 @Source("gr/ebs/gss/resources/mimetypes/txt.png")
138 @Source("gr/ebs/gss/resources/mimetypes/ark2.png")
141 @Source("gr/ebs/gss/resources/mimetypes/kcmfontinst_shared.png")
142 ImageResource wordprocessorShared();
144 @Source("gr/ebs/gss/resources/mimetypes/log_shared.png")
145 ImageResource spreadsheetShared();
147 @Source("gr/ebs/gss/resources/mimetypes/kpresenter_kpr_shared.png")
148 ImageResource presentationShared();
150 @Source("gr/ebs/gss/resources/mimetypes/acroread_shared.png")
151 ImageResource pdfShared();
153 @Source("gr/ebs/gss/resources/mimetypes/image_shared.png")
154 ImageResource imageShared();
156 @Source("gr/ebs/gss/resources/mimetypes/video2_shared.png")
157 ImageResource videoShared();
159 @Source("gr/ebs/gss/resources/mimetypes/knotify_shared.png")
160 ImageResource audioShared();
162 @Source("gr/ebs/gss/resources/mimetypes/html_shared.png")
163 ImageResource htmlShared();
165 @Source("gr/ebs/gss/resources/mimetypes/txt_shared.png")
166 ImageResource txtShared();
168 @Source("gr/ebs/gss/resources/mimetypes/ark2_shared.png")
169 ImageResource zipShared();
174 * A label with the number of files in this folder.
176 private HTML countLabel = new HTML();
179 * The table widget with the file list.
181 private FileTable table = new FileTable(GSS.VISIBLE_FILE_COUNT + 1, 8);
184 * The navigation bar for paginating the results.
186 private HorizontalPanel navBar = new HorizontalPanel();
189 * The number of files in this folder.
196 long folderTotalSize;
199 * A cache of the files in the list.
201 private List<FileResource> files;
204 * The widget's image bundle.
206 private final Images images;
208 private String sortingProperty = "name";
210 private boolean sortingType = true;
212 private HTML nameLabel;
214 private HTML versionLabel;
216 private HTML sizeLabel;
218 private HTML dateLabel;
220 private HTML ownerLabel;
222 private HTML pathLabel;
225 * Construct the file list widget. This entails setting up the widget
226 * layout, fetching the number of files in the current folder from the
227 * server and filling the local file cache of displayed files with data from
228 * the server, as well.
232 public FileList(Images _images) {
235 prevButton.addClickHandler(this);
236 nextButton.addClickHandler(this);
238 contextMenu = new DnDSimpleFocusPanel(new HTML(AbstractImagePrototype.create(images.fileContextMenu()).getHTML()));
239 GSS.get().getDragController().makeDraggable(contextMenu);
242 table.setCellSpacing(0);
243 table.setCellPadding(2);
244 table.setWidth("100%");
245 table.addClickHandler(new ClickHandler() {
248 public void onClick(ClickEvent event) {
249 Cell cell = table.getCellForEvent(event);
250 GWT.log("row clicked:"+cell.getRowIndex(), null);
251 onRowClicked(cell.getRowIndex(), true);
254 // Create the 'navigation' bar at the upper-right.
255 HorizontalPanel innerNavBar = new HorizontalPanel();
256 innerNavBar.setStyleName("gss-ListNavBar");
257 innerNavBar.setSpacing(8);
258 innerNavBar.add(prevButton);
259 innerNavBar.add(countLabel);
260 innerNavBar.add(nextButton);
261 navBar.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
262 navBar.add(innerNavBar);
263 navBar.setWidth("100%");
266 setStyleName("gss-List");
269 DeferredCommand.addCommand(new IncrementalCommand() {
272 public boolean execute() {
273 return fetchRootFolder();
276 sinkEvents(Event.ONCONTEXTMENU);
277 sinkEvents(Event.ONMOUSEUP);
278 sinkEvents(Event.ONMOUSEDOWN);
279 sinkEvents(Event.ONCLICK);
280 sinkEvents(Event.ONKEYDOWN);
281 sinkEvents(Event.ONDBLCLICK);
282 GSS.preventIESelection();
286 public void onClick(ClickEvent event) {
287 if(event.getSource() == nextButton){
288 // Move forward a page.
290 startIndex += GSS.VISIBLE_FILE_COUNT;
291 if (startIndex >= folderFileCount)
292 startIndex -= GSS.VISIBLE_FILE_COUNT;
295 } else if (event.getSource() == prevButton) {
298 startIndex -= GSS.VISIBLE_FILE_COUNT;
308 public void onBrowserEvent(Event event) {
309 if (DOM.eventGetType(event) == Event.ONMOUSEDOWN && DOM.eventGetButton(event) == NativeEvent.BUTTON_RIGHT){
310 if (DOM.eventGetCtrlKey(event))
313 clickControl = false;
314 if (DOM.eventGetShiftKey(event)) {
316 if (selectedRows.size() == 1)
317 firstShift = selectedRows.get(0) - startIndex;
318 //event.preventDefault();
322 //event.preventDefault();
324 int ri = table.getRowForEvent2(event);
326 if(!selectedRows.contains(ri-1))
327 onRowClicked(ri, false);
330 if (files == null || files.size() == 0) {
331 if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && selectedRows.size() == 0) {
332 FileContextMenu fm = new FileContextMenu(images, false, true);
333 fm.onEmptyEvent(event);
337 if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && selectedRows.size() != 0) {
338 FileContextMenu fm = new FileContextMenu(images, false, false);
340 } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && selectedRows.size() == 0) {
341 FileContextMenu fm = new FileContextMenu(images, false, true);
342 fm.onEmptyEvent(event);
343 } else if (DOM.eventGetType(event) == Event.ONDBLCLICK)
344 if (getSelectedFiles().size() == 1) {
346 FileResource file = getSelectedFiles().get(0);
347 String dateString = RestCommand.getDate();
348 String resource = file.getUri().substring(app.getApiPath().length() - 1, file.getUri().length());
349 String sig = app.getCurrentUserResource().getUsername() + " " +
350 RestCommand.calculateSig("GET", dateString, resource,
351 RestCommand.base64decode(app.getToken()));
352 Window.open(file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date=" + URL.encodeComponent(dateString), "_blank", "");
353 event.preventDefault();
356 if (DOM.eventGetType(event) == Event.ONCLICK) {
357 if (DOM.eventGetCtrlKey(event))
360 clickControl = false;
361 if (DOM.eventGetShiftKey(event)) {
363 if (selectedRows.size() == 1)
364 firstShift = selectedRows.get(0) - startIndex;
365 //event.preventDefault();
369 //event.preventDefault();
372 super.onBrowserEvent(event);
376 * Retrieve the root folder for the current user.
378 * @return true if the retrieval was successful
380 protected boolean fetchRootFolder() {
381 UserResource user = GSS.get().getCurrentUserResource();
384 // Update cache and clear selection.
385 updateFileCache(true);
389 private void onRowClicked(int row, boolean toggleSelection) {
390 // Select the row that was clicked (-1 to account for header row).
391 if (row > folderFileCount)
394 GWT.log("Row is: " + row + " fs: " + firstShift, null);
395 if (firstShift == -1)
397 else if (row > firstShift) {
399 for (int i = firstShift; i < row; i++) {
400 selectedRows.add(startIndex + i);
403 GSS.get().setCurrentSelection(getSelectedFiles());
404 contextMenu.setFiles(getSelectedFiles());
405 makeRowDraggable(row);
406 } else if (row != -1 && row == firstShift) {
407 selectedRows.add(row - 1);
409 styleRow(row - 1, true);
410 GSS.get().setCurrentSelection(getSelectedFiles());
411 contextMenu.setFiles(getSelectedFiles());
412 makeRowDraggable(row);
413 } else if (row < firstShift) {
414 GWT.log("Row is:" + row + " fs:" + firstShift, null);
417 for (int i = firstShift; i >= row - 1; i--) {
418 selectedRows.add(startIndex + i);
421 GSS.get().setCurrentSelection(getSelectedFiles());
422 makeRowDraggable(row);
423 contextMenu.setFiles(getSelectedFiles());
426 selectRow(row - 1, toggleSelection);
430 * Initializes the table so that it contains enough rows for a full page of
433 private void initTable() {
434 nameLabel = new HTML("Name");
435 nameLabel.addClickHandler(new ClickHandler() {
438 public void onClick(ClickEvent event) {
444 versionLabel = new HTML("Version");
445 versionLabel.addClickHandler(new ClickHandler() {
448 public void onClick(ClickEvent event) {
449 sortFiles("version");
453 sizeLabel = new HTML("Size");
454 sizeLabel.addClickHandler(new ClickHandler() {
457 public void onClick(ClickEvent event) {
462 dateLabel = new HTML("Last modified");
463 dateLabel.addClickHandler(new ClickHandler() {
466 public void onClick(ClickEvent event) {
471 ownerLabel = new HTML("Owner");
472 ownerLabel.addClickHandler(new ClickHandler() {
475 public void onClick(ClickEvent event) {
480 pathLabel = new HTML("Path");
481 pathLabel.addClickHandler(new ClickHandler() {
484 public void onClick(ClickEvent event) {
489 // Create the header row.
490 table.setText(0, 0, "");
491 table.setWidget(0, 1, nameLabel);
492 table.setWidget(0, 2, ownerLabel);
493 table.setWidget(0, 3, pathLabel);
494 table.setWidget(0, 4, versionLabel);
495 table.setWidget(0, 5, sizeLabel);
496 table.setWidget(0, 6, dateLabel);
497 table.setWidget(0, 7, navBar);
498 table.getRowFormatter().setStyleName(0, "gss-ListHeader");
500 // Initialize the rest of the rows.
501 for (int i = 1; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
502 table.setText(i, 0, "");
503 table.setText(i, 1, "");
504 table.setText(i, 2, "");
505 table.setText(i, 3, "");
506 table.setText(i, 4, "");
507 table.setText(i, 5, "");
508 table.setText(i, 6, "");
509 table.setText(i, 7, "");
510 table.getCellFormatter().setWordWrap(i, 0, false);
511 table.getCellFormatter().setWordWrap(i, 1, false);
512 table.getCellFormatter().setWordWrap(i, 2, false);
513 table.getCellFormatter().setWordWrap(i, 3, false);
514 table.getCellFormatter().setWordWrap(i, 4, false);
515 table.getCellFormatter().setWordWrap(i, 5, false);
516 table.getCellFormatter().setWordWrap(i, 6, false);
517 table.getCellFormatter().setWordWrap(i, 7, false);
518 table.getCellFormatter().setHorizontalAlignment(i, 4, HasHorizontalAlignment.ALIGN_CENTER);
520 prevButton.setVisible(false);
521 nextButton.setVisible(false);
525 * Selects the given row (relative to the current page).
527 * @param row the row to be selected
529 private void selectRow(final int row, boolean toggleSelection) {
530 if (row < folderFileCount) {
532 if (selectedRows.contains(row) && toggleSelection) {
533 int i = selectedRows.indexOf(startIndex + row);
534 selectedRows.remove(i);
535 styleRow(row, false);
537 selectedRows.add(startIndex + row);
540 else if (selectedRows.size() == 1 && selectedRows.contains(row) && toggleSelection){
546 selectedRows.add(startIndex + row);
549 if (selectedRows.size() == 1)
550 GSS.get().setCurrentSelection(files.get(selectedRows.get(0)));
551 else if(selectedRows.size() == 0)
552 GSS.get().setCurrentSelection(null);
554 GSS.get().setCurrentSelection(getSelectedFiles());
555 contextMenu.setFiles(getSelectedFiles());
556 makeRowDraggable(row+1);
560 public List<FileResource> getSelectedFiles() {
561 List<FileResource> result = new ArrayList();
562 for (int i : selectedRows)
563 result.add(files.get(i));
568 * Make the specified row look like selected or not, according to the
569 * <code>selected</code> flag.
574 void styleRow(final int row, final boolean selected) {
575 if (row != -1 && row >= 0)
577 table.getRowFormatter().addStyleName(row + 1, "gss-SelectedRow");
579 table.getRowFormatter().removeStyleName(row + 1, "gss-SelectedRow");
583 * Update the display of the file list.
585 void update(boolean sort) {
586 int count = folderFileCount;
587 int max = startIndex + GSS.VISIBLE_FILE_COUNT;
592 if (sort && files != null && files.size() != 0) {
596 Collections.sort(files, new Comparator<FileResource>() {
599 public int compare(FileResource arg0, FileResource arg1) {
600 AbstractImagePrototype descPrototype = AbstractImagePrototype.create(images.desc());
601 AbstractImagePrototype ascPrototype = AbstractImagePrototype.create(images.asc());
603 if (sortingProperty.equals("version")) {
604 versionLabel.setHTML("Version " + descPrototype.getHTML());
605 return arg0.getVersion().compareTo(arg1.getVersion());
606 } else if (sortingProperty.equals("owner")) {
607 ownerLabel.setHTML("Owner " + descPrototype.getHTML());
608 return arg0.getOwner().compareTo(arg1.getOwner());
609 } else if (sortingProperty.equals("date")) {
610 dateLabel.setHTML("Date modified " + descPrototype.getHTML());
611 return arg0.getModificationDate().compareTo(arg1.getModificationDate());
612 } else if (sortingProperty.equals("size")) {
613 sizeLabel.setHTML("Size " + descPrototype.getHTML());
614 return arg0.getContentLength().compareTo(arg1.getContentLength());
615 } else if (sortingProperty.equals("name")) {
616 nameLabel.setHTML("Name " + descPrototype.getHTML());
617 return arg0.getName().compareTo(arg1.getName());
618 } else if (sortingProperty.equals("path")) {
619 pathLabel.setHTML("Path " + descPrototype.getHTML());
620 return arg0.getUri().compareTo(arg1.getUri());
622 nameLabel.setHTML("Name " + descPrototype.getHTML());
623 return arg0.getName().compareTo(arg1.getName());
625 else if (sortingProperty.equals("version")) {
626 versionLabel.setHTML("Version " + ascPrototype.getHTML());
627 return arg1.getVersion().compareTo(arg0.getVersion());
628 } else if (sortingProperty.equals("owner")) {
629 ownerLabel.setHTML("Owner " + ascPrototype.getHTML());
630 return arg1.getOwner().compareTo(arg0.getOwner());
631 } else if (sortingProperty.equals("date")) {
632 dateLabel.setHTML("Date modified " + ascPrototype.getHTML());
633 return arg1.getModificationDate().compareTo(arg0.getModificationDate());
634 } else if (sortingProperty.equals("size")) {
635 sizeLabel.setHTML("Size " + ascPrototype.getHTML());
636 return arg1.getContentLength().compareTo(arg0.getContentLength());
637 } else if (sortingProperty.equals("name")) {
638 nameLabel.setHTML("Name " + ascPrototype.getHTML());
639 return arg1.getName().compareTo(arg0.getName());
640 } else if (sortingProperty.equals("path")) {
641 pathLabel.setHTML("Path " + ascPrototype.getHTML());
642 return arg1.getUri().compareTo(arg0.getUri());
644 nameLabel.setHTML("Name " + ascPrototype.getHTML());
645 return arg1.getName().compareTo(arg0.getName());
653 // Show the selected files.
655 for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
656 // Don't read past the end.
657 // if (i > folderFileCount)
659 if (startIndex + i > folderFileCount)
661 // Add a new row to the table, then set each of its columns to the
663 FileResource file = files.get(startIndex + i - 1);
664 table.setWidget(i, 0, getFileIcon(file).createImage());
665 table.getRowFormatter().addStyleName(i, "gss-fileRow");
667 //add view image link for image files
668 String contentType = file.getContentType();
669 if (contentType.endsWith("png") || contentType.endsWith("gif") || contentType.endsWith("jpeg") )
670 table.setHTML(i, 1, file.getName() + " <a href='" +
671 GSS.get().getTopPanel().getFileMenu().getDownloadURL(file) +
672 "' title='" + file.getName() + "' rel='lytebox[p]' " +
673 "onclick='myLytebox.start(this, false, false)'>" +
676 table.setHTML(i, 1, file.getName());
677 table.setText(i, 2, file.getOwner());
678 table.setText(i, 3, file.getPath());
679 table.setText(i, 4, String.valueOf(file.getVersion()));
680 table.setText(i, 5, String.valueOf(file.getFileSizeAsString()));
681 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
682 table.setText(i, 6, formatter.format(file.getModificationDate()));
683 folderTotalSize += file.getContentLength();
686 // Clear any remaining slots.
687 for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
688 table.setHTML(i, 0, " ");
689 table.setHTML(i, 1, " ");
690 table.setHTML(i, 2, " ");
691 table.setHTML(i, 3, " ");
692 table.setHTML(i, 4, " ");
693 table.setHTML(i, 5, " ");
694 table.setHTML(i, 6, " ");
695 table.setHTML(i, 7, " ");
698 if (folderFileCount == 0) {
699 showingStats = "no files";
700 prevButton.setVisible(false);
701 nextButton.setVisible(false);
702 } else if (folderFileCount < GSS.VISIBLE_FILE_COUNT) {
703 if (folderFileCount == 1)
704 showingStats = "1 file";
706 showingStats = folderFileCount + " files";
707 showingStats += " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
708 prevButton.setVisible(false);
709 nextButton.setVisible(false);
711 showingStats = "" + (startIndex + 1) + " - " + max + " of " + count + " files" + " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
712 prevButton.setVisible(startIndex != 0);
713 nextButton.setVisible(startIndex + GSS.VISIBLE_FILE_COUNT < count);
715 updateCurrentlyShowingStats();
720 * Return the proper icon based on the MIME type of the file.
725 private AbstractImagePrototype getFileIcon(FileResource file) {
726 String mimetype = file.getContentType();
727 boolean shared=false;
728 Folders folders = GSS.get().getFolders();
729 if(folders.getCurrent() != null && folders.isOthersSharedItem(folders.getCurrent())){
730 DnDTreeItem otherUser = (DnDTreeItem) folders.getUserOfSharedItem(folders.getCurrent());
734 String uname = otherUser.getOtherUserResource().getUsername();
736 uname = ((DnDTreeItem)folders.getSharesItem()).getOthersResource().getUsernameOfUri(otherUser.getOtherUserResource().getUri());
738 shared = file.isShared();
742 shared = file.isShared();
743 if (mimetype == null)
744 return shared ? AbstractImagePrototype.create(images.documentShared()) : AbstractImagePrototype.create(images.document());
745 mimetype = mimetype.toLowerCase();
746 if (mimetype.startsWith("application/pdf"))
747 return shared ? AbstractImagePrototype.create(images.pdfShared()) : AbstractImagePrototype.create(images.pdf());
748 else if (mimetype.endsWith("excel"))
749 return shared ? AbstractImagePrototype.create(images.spreadsheetShared()) : AbstractImagePrototype.create(images.spreadsheet());
750 else if (mimetype.endsWith("msword"))
751 return shared ? AbstractImagePrototype.create(images.wordprocessorShared()) : AbstractImagePrototype.create(images.wordprocessor());
752 else if (mimetype.endsWith("powerpoint"))
753 return shared ? AbstractImagePrototype.create(images.presentationShared()) : AbstractImagePrototype.create(images.presentation());
754 else if (mimetype.startsWith("application/zip") ||
755 mimetype.startsWith("application/gzip") ||
756 mimetype.startsWith("application/x-gzip") ||
757 mimetype.startsWith("application/x-tar") ||
758 mimetype.startsWith("application/x-gtar"))
759 return shared ? AbstractImagePrototype.create(images.zipShared()) : AbstractImagePrototype.create(images.zip());
760 else if (mimetype.startsWith("text/html"))
761 return shared ? AbstractImagePrototype.create(images.htmlShared()) : AbstractImagePrototype.create(images.html());
762 else if (mimetype.startsWith("text/plain"))
763 return shared ? AbstractImagePrototype.create(images.txtShared()) : AbstractImagePrototype.create(images.txt());
764 else if (mimetype.startsWith("image/"))
765 return shared ? AbstractImagePrototype.create(images.imageShared()) : AbstractImagePrototype.create(images.image());
766 else if (mimetype.startsWith("video/"))
767 return shared ? AbstractImagePrototype.create(images.videoShared()) : AbstractImagePrototype.create(images.video());
768 else if (mimetype.startsWith("audio/"))
769 return shared ? AbstractImagePrototype.create(images.audioShared()) : AbstractImagePrototype.create(images.audio());
770 return shared ? AbstractImagePrototype.create(images.documentShared()) : AbstractImagePrototype.create(images.document());
774 * Update status panel with currently showing file stats.
776 public void updateCurrentlyShowingStats() {
777 GSS.get().getStatusPanel().updateCurrentlyShowing(showingStats);
781 * Adjust the height of the table by adding and removing rows as necessary.
783 * @param newHeight the new height to reach
785 void resizeTableHeight(final int newHeight) {
786 GWT.log("Panel: " + newHeight + ", parent: " + table.getParent().getOffsetHeight(), null);
787 // Fill the rest with empty slots.
788 if (newHeight > table.getOffsetHeight())
789 while (newHeight > table.getOffsetHeight()) {
790 table.resizeRows(table.getRowCount() + 1);
791 GWT.log("Table: " + table.getOffsetHeight() + ", rows: " + table.getRowCount(), null);
794 while (newHeight < table.getOffsetHeight()) {
795 table.resizeRows(table.getRowCount() - 1);
796 GWT.log("Table: " + table.getOffsetHeight() + ", rows: " + table.getRowCount(), null);
800 public void updateFileCache(boolean updateSelectedFolder, final boolean clearSelection) {
801 updateFileCache(updateSelectedFolder, clearSelection, null);
804 public void updateFileCache(boolean updateSelectedFolder, final boolean clearSelection, final String newFilename) {
805 if (!updateSelectedFolder && !GSS.get().getFolders().getTrashItem().equals(GSS.get().getFolders().getCurrent()))
806 updateFileCache(clearSelection);
807 else if (GSS.get().getFolders().getCurrent() != null) {
808 final DnDTreeItem folderItem = (DnDTreeItem) GSS.get().getFolders().getCurrent();
809 if( folderItem.getFolderResource()!= null){
810 if(GSS.get().getFolders().isFileItem(folderItem) || GSS.get().getFolders().isMySharedItem(folderItem) || GSS.get().getFolders().isOthersSharedItem(folderItem) ){
812 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, folderItem.getFolderResource().getUri(),folderItem.getFolderResource()) {
815 public void onComplete() {
816 folderItem.setUserObject(getResult());
817 if(GSS.get().getFolders().isFileItem(folderItem)){
818 String[] filePaths = new String[folderItem.getFolderResource().getFilePaths().size()];
820 for(String fpath : folderItem.getFolderResource().getFilePaths()){
821 filePaths[c] = fpath + "?" + Math.random();
824 MultipleHeadCommand<FileResource> getFiles = new MultipleHeadCommand<FileResource>(FileResource.class, filePaths, folderItem.getFolderResource().getFileCache()){
827 public void onComplete(){
828 List<FileResource> result = getResult();
829 //remove random from path
830 for(FileResource r : result){
831 String p = r.getUri();
832 int indexOfQuestionMark = p.lastIndexOf('?');
833 if(indexOfQuestionMark>0)
834 r.setUri(p.substring(0, indexOfQuestionMark));
835 GWT.log("FETCHED:"+r.getLastModifiedSince(), null);
837 folderItem.getFolderResource().setFiles(result);
838 folderItem.getFolderResource().setFilesExpanded(true);
839 updateFileCache(clearSelection, newFilename);
843 public void onError(String p, Throwable throwable) {
844 if(throwable instanceof RestException)
845 GSS.get().displayError("Unable to retrieve file details:"+((RestException)throwable).getHttpStatusText());
849 public void onError(Throwable t) {
851 GSS.get().displayError("Unable to fetch files for folder " + folderItem.getFolderResource().getName());
855 DeferredCommand.addCommand(getFiles);
858 updateFileCache(clearSelection, newFilename);
862 public void onError(Throwable t) {
864 GSS.get().displayError("Unable to fetch folder " + folderItem.getFolderResource().getName());
867 DeferredCommand.addCommand(gf);
870 else if (folderItem.getTrashResource() != null) {
871 GetCommand<TrashResource> gt = new GetCommand<TrashResource>(TrashResource.class, folderItem.getTrashResource().getUri(), null) {
874 public void onComplete() {
875 folderItem.setUserObject(getResult());
876 updateFileCache(clearSelection);
880 public void onError(Throwable t) {
881 if (t instanceof RestException && (((RestException) t).getHttpStatusCode() == 204 || ((RestException) t).getHttpStatusCode() == 1223)) {
882 folderItem.setUserObject(new TrashResource(folderItem.getTrashResource().getUri()));
883 updateFileCache(clearSelection);
886 GSS.get().displayError("Unable to fetch trash resource");
890 DeferredCommand.addCommand(gt);
891 } else if (folderItem.getSharedResource() != null) {
892 GetCommand<SharedResource> gt = new GetCommand<SharedResource>(SharedResource.class, folderItem.getSharedResource().getUri(), null) {
895 public void onComplete() {
896 folderItem.setUserObject(getResult());
897 updateFileCache(clearSelection, newFilename);
898 String[] filePaths = new String[folderItem.getSharedResource().getFilePaths().size()];
900 for(String fpath : folderItem.getSharedResource().getFilePaths()){
901 filePaths[c] = fpath + "?" + Math.random();
904 MultipleHeadCommand<FileResource> getFiles = new MultipleHeadCommand<FileResource>(FileResource.class, filePaths, folderItem.getSharedResource().getFileCache()){
907 public void onComplete(){
908 List<FileResource> result = getResult();
909 //remove random from path
910 for(FileResource r : result){
911 String p = r.getUri();
912 int indexOfQuestionMark = p.lastIndexOf('?');
913 if(indexOfQuestionMark>0)
914 r.setUri(p.substring(0, indexOfQuestionMark));
915 GWT.log("FETCHED:"+r.getLastModifiedSince(), null);
917 folderItem.getSharedResource().setFiles(result);
918 folderItem.getSharedResource().setFilesExpanded(true);
919 updateFileCache(clearSelection, newFilename);
923 public void onError(String p, Throwable throwable) {
924 if(throwable instanceof RestException)
925 GSS.get().displayError("Unable to retrieve file details:"+((RestException)throwable).getHttpStatusText());
929 public void onError(Throwable t) {
931 GSS.get().displayError("Unable to fetch files for folder " + folderItem.getFolderResource().getName());
935 DeferredCommand.addCommand(getFiles);
939 public void onError(Throwable t) {
941 GSS.get().displayError("Unable to fetch My Shares resource");
944 DeferredCommand.addCommand(gt);
945 } else if (folderItem.getOtherUserResource() != null) {
946 GetCommand<OtherUserResource> gt = new GetCommand<OtherUserResource>(OtherUserResource.class, folderItem.getOtherUserResource().getUri(), null) {
949 public void onComplete() {
950 folderItem.setUserObject(getResult());
951 updateFileCache(clearSelection, newFilename);
952 String[] filePaths = new String[folderItem.getOtherUserResource().getFilePaths().size()];
954 for(String fpath : folderItem.getOtherUserResource().getFilePaths()){
955 filePaths[c] = fpath + "?" + Math.random();
958 MultipleHeadCommand<FileResource> getFiles = new MultipleHeadCommand<FileResource>(FileResource.class, filePaths, folderItem.getOtherUserResource().getFileCache()){
961 public void onComplete(){
962 List<FileResource> result = getResult();
963 //remove random from path
964 for(FileResource r : result){
965 String p = r.getUri();
966 int indexOfQuestionMark = p.lastIndexOf('?');
967 if(indexOfQuestionMark>0)
968 r.setUri(p.substring(0, indexOfQuestionMark));
969 GWT.log("FETCHED:"+r.getLastModifiedSince(), null);
971 folderItem.getOtherUserResource().setFiles(result);
972 folderItem.getOtherUserResource().setFilesExpanded(true);
973 updateFileCache(clearSelection, newFilename);
977 public void onError(String p, Throwable throwable) {
978 if(throwable instanceof RestException)
979 GSS.get().displayError("Unable to retrieve file details:"+((RestException)throwable).getHttpStatusText());
983 public void onError(Throwable t) {
985 GSS.get().displayError("Unable to fetch files for folder " + folderItem.getFolderResource().getName());
989 DeferredCommand.addCommand(getFiles);
993 public void onError(Throwable t) {
995 GSS.get().displayError("Unable to fetch My Shares resource");
998 DeferredCommand.addCommand(gt);
1001 updateFileCache(clearSelection);
1004 private void updateFileCache(boolean clearSelection) {
1005 updateFileCache(clearSelection, null);
1009 * Update the file cache with data from the server.
1011 * @param userId the ID of the current user
1012 * @param newFilename the new name of the previously selected file,
1013 * if a rename operation has taken place
1015 private void updateFileCache(boolean clearSelection, String newFilename) {
1017 clearSelectedRows();
1020 final TreeItem folderItem = GSS.get().getFolders().getCurrent();
1022 if (folderItem == null || GSS.get().getFolders().isOthersShared(folderItem)) {
1023 setFiles(new ArrayList<FileResource>());
1027 if (folderItem instanceof DnDTreeItem) {
1028 DnDTreeItem dnd = (DnDTreeItem) folderItem;
1029 if (dnd.getFolderResource() != null) {
1030 if (GSS.get().getFolders().isTrashItem(dnd))
1031 setFiles(new ArrayList<FileResource>());
1033 setFiles(dnd.getFolderResource().getFiles());
1035 } else if (dnd.getTrashResource() != null)
1036 setFiles(dnd.getTrashResource().getFiles());
1037 else if (dnd.getSharedResource() != null)
1038 setFiles(dnd.getSharedResource().getFiles());
1039 else if (dnd.getOtherUserResource() != null)
1040 setFiles(dnd.getOtherUserResource().getFiles());
1042 setFiles(dnd.getFolderResource().getFiles());
1045 if (!clearSelection && selectedRows.size()==1 && newFilename!=null) {
1047 for (int i=1; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
1048 if (startIndex + i > folderFileCount)
1050 FileResource file = files.get(startIndex + i - 1);
1051 if (newFilename.equals(file.getName())) {
1056 clearSelectedRows();
1058 selectRow(row, true);
1064 * Fill the file cache with data.
1066 public void setFiles(final List<FileResource> _files) {
1067 if (_files.size() > 0 && !GSS.get().getFolders().isTrash(GSS.get().getFolders().getCurrent())) {
1068 files = new ArrayList<FileResource>();
1069 for (FileResource fres : _files)
1070 if (!fres.isDeleted())
1074 Collections.sort(files, new Comparator<FileResource>() {
1077 public int compare(FileResource arg0, FileResource arg1) {
1078 return arg0.getName().compareTo(arg1.getName());
1082 folderFileCount = files.size();
1085 private void sortFiles(final String sortProperty) {
1086 if (sortProperty.equals(sortingProperty))
1087 sortingType = !sortingType;
1089 sortingProperty = sortProperty;
1095 private void clearLabels() {
1096 nameLabel.setText("Name");
1097 versionLabel.setText("Version");
1098 sizeLabel.setText("Size");
1099 dateLabel.setText("Last modified");
1100 ownerLabel.setText("Owner");
1101 pathLabel.setText("Path");
1105 * Retrieve the table.
1114 * Does the list contains the requested filename
1117 * @return true/false
1119 public boolean contains(String fileName) {
1120 for (int i = 0; i < files.size(); i++)
1121 if (files.get(i).getName().equals(fileName))
1126 public void clearSelectedRows() {
1127 for (int r : selectedRows) {
1128 int row = r - startIndex;
1129 styleRow(row, false);
1131 selectedRows.clear();
1132 Object sel = GSS.get().getCurrentSelection();
1133 if (sel instanceof FileResource || sel instanceof List)
1134 GSS.get().setCurrentSelection(null);
1140 public void selectAllRows() {
1141 clearSelectedRows();
1142 int count = folderFileCount;
1145 int max = startIndex + GSS.VISIBLE_FILE_COUNT;
1149 for (; i < GSS.VISIBLE_FILE_COUNT + 1; ++i) {
1150 // Don't read past the end.
1151 // if (i > folderFileCount)
1153 if (startIndex + i > folderFileCount)
1155 selectedRows.add(startIndex + i - 1);
1156 styleRow(i - 1, true);
1158 GSS.get().setCurrentSelection(getSelectedFiles());
1159 contextMenu.setFiles(getSelectedFiles());
1160 makeRowDraggable(i-1);
1164 private void makeRowDraggable(int row){
1165 int contextRow = getWidgetRow(contextMenu, table);
1166 if (contextRow != -1)
1167 table.setWidget(contextRow, 0, getFileIcon(files.get(contextRow - 1)).createImage());
1168 contextMenu.setWidget(new HTML(getFileIcon(files.get(row - 1)).getHTML()));
1169 table.setWidget(row, 0, contextMenu);
1172 private int getWidgetRow(Widget widget, Grid grid) {
1173 for (int row = 0; row < grid.getRowCount(); row++)
1174 for (int col = 0; col < grid.getCellCount(row); col++) {
1175 Widget w = table.getWidget(row, col);