Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / SearchResults.java @ 76f91765

History | View | Annotate | Download (30.4 kB)

1 023f6f1e Panagiotis Astithas
/*
2 023f6f1e Panagiotis Astithas
 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3 023f6f1e Panagiotis Astithas
 *
4 023f6f1e Panagiotis Astithas
 * This file is part of GSS.
5 023f6f1e Panagiotis Astithas
 *
6 023f6f1e Panagiotis Astithas
 * GSS is free software: you can redistribute it and/or modify
7 023f6f1e Panagiotis Astithas
 * it under the terms of the GNU General Public License as published by
8 023f6f1e Panagiotis Astithas
 * the Free Software Foundation, either version 3 of the License, or
9 023f6f1e Panagiotis Astithas
 * (at your option) any later version.
10 023f6f1e Panagiotis Astithas
 *
11 023f6f1e Panagiotis Astithas
 * GSS is distributed in the hope that it will be useful,
12 023f6f1e Panagiotis Astithas
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 023f6f1e Panagiotis Astithas
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 023f6f1e Panagiotis Astithas
 * GNU General Public License for more details.
15 023f6f1e Panagiotis Astithas
 *
16 023f6f1e Panagiotis Astithas
 * You should have received a copy of the GNU General Public License
17 023f6f1e Panagiotis Astithas
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 023f6f1e Panagiotis Astithas
 */
19 023f6f1e Panagiotis Astithas
package gr.ebs.gss.client;
20 023f6f1e Panagiotis Astithas
21 5b09ea6c koutsoub
22 08880e98 koutsoub
import static com.google.gwt.query.client.GQuery.$;
23 08880e98 koutsoub
import gr.ebs.gss.client.FileList.FileValueUpdater;
24 08880e98 koutsoub
import gr.ebs.gss.client.FileList.Images;
25 08880e98 koutsoub
import gr.ebs.gss.client.FileList.TableResources;
26 08880e98 koutsoub
import gr.ebs.gss.client.FileList.TableStyle;
27 08880e98 koutsoub
import gr.ebs.gss.client.FileList.Templates;
28 023f6f1e Panagiotis Astithas
import gr.ebs.gss.client.rest.GetCommand;
29 023f6f1e Panagiotis Astithas
import gr.ebs.gss.client.rest.RestCommand;
30 023f6f1e Panagiotis Astithas
import gr.ebs.gss.client.rest.RestException;
31 023f6f1e Panagiotis Astithas
import gr.ebs.gss.client.rest.resource.FileResource;
32 08880e98 koutsoub
import gr.ebs.gss.client.rest.resource.RestResource;
33 08880e98 koutsoub
import gr.ebs.gss.client.rest.resource.RestResourceWrapper;
34 023f6f1e Panagiotis Astithas
import gr.ebs.gss.client.rest.resource.SearchResource;
35 08880e98 koutsoub
import gr.ebs.gss.client.rest.resource.SharedResource;
36 08880e98 koutsoub
import gr.ebs.gss.client.rest.resource.TrashResource;
37 023f6f1e Panagiotis Astithas
import gr.ebs.gss.client.rest.resource.UserResource;
38 08880e98 koutsoub
import gr.ebs.gss.client.rest.resource.UserSearchResource;
39 08880e98 koutsoub
import gwtquery.plugins.draggable.client.DraggableOptions;
40 08880e98 koutsoub
import gwtquery.plugins.draggable.client.StopDragException;
41 08880e98 koutsoub
import gwtquery.plugins.draggable.client.DraggableOptions.DragFunction;
42 08880e98 koutsoub
import gwtquery.plugins.draggable.client.DraggableOptions.RevertOption;
43 08880e98 koutsoub
import gwtquery.plugins.draggable.client.events.DragContext;
44 08880e98 koutsoub
import gwtquery.plugins.draggable.client.events.DragStartEvent;
45 08880e98 koutsoub
import gwtquery.plugins.draggable.client.events.DragStopEvent;
46 08880e98 koutsoub
import gwtquery.plugins.draggable.client.events.DragStartEvent.DragStartEventHandler;
47 08880e98 koutsoub
import gwtquery.plugins.draggable.client.events.DragStopEvent.DragStopEventHandler;
48 08880e98 koutsoub
import gwtquery.plugins.droppable.client.gwt.DragAndDropCellTable;
49 08880e98 koutsoub
import gwtquery.plugins.droppable.client.gwt.DragAndDropColumn;
50 023f6f1e Panagiotis Astithas
51 023f6f1e Panagiotis Astithas
import java.util.ArrayList;
52 023f6f1e Panagiotis Astithas
import java.util.Collections;
53 023f6f1e Panagiotis Astithas
import java.util.Comparator;
54 08880e98 koutsoub
import java.util.Iterator;
55 023f6f1e Panagiotis Astithas
import java.util.List;
56 023f6f1e Panagiotis Astithas
57 9b11a5ca koutsoub
import org.hibernate.mapping.Array;
58 9b11a5ca koutsoub
59 08880e98 koutsoub
import com.google.gwt.cell.client.AbstractCell;
60 08880e98 koutsoub
import com.google.gwt.cell.client.ImageResourceCell;
61 08880e98 koutsoub
import com.google.gwt.cell.client.SafeHtmlCell;
62 f0cd9b29 koutsoub
import com.google.gwt.cell.client.TextCell;
63 08880e98 koutsoub
import com.google.gwt.cell.client.ValueUpdater;
64 08880e98 koutsoub
import com.google.gwt.cell.client.Cell.Context;
65 023f6f1e Panagiotis Astithas
import com.google.gwt.core.client.GWT;
66 08880e98 koutsoub
import com.google.gwt.core.client.Scheduler;
67 08880e98 koutsoub
import com.google.gwt.core.client.Scheduler.RepeatingCommand;
68 023f6f1e Panagiotis Astithas
import com.google.gwt.dom.client.NativeEvent;
69 08880e98 koutsoub
import com.google.gwt.dom.client.Style.Cursor;
70 023f6f1e Panagiotis Astithas
import com.google.gwt.event.dom.client.ClickEvent;
71 023f6f1e Panagiotis Astithas
import com.google.gwt.event.dom.client.ClickHandler;
72 023f6f1e Panagiotis Astithas
import com.google.gwt.http.client.URL;
73 023f6f1e Panagiotis Astithas
import com.google.gwt.i18n.client.DateTimeFormat;
74 023f6f1e Panagiotis Astithas
import com.google.gwt.resources.client.ClientBundle;
75 023f6f1e Panagiotis Astithas
import com.google.gwt.resources.client.ImageResource;
76 08880e98 koutsoub
import com.google.gwt.resources.client.ClientBundle.Source;
77 08880e98 koutsoub
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
78 08880e98 koutsoub
import com.google.gwt.safehtml.client.SafeHtmlTemplates.Template;
79 08880e98 koutsoub
import com.google.gwt.safehtml.shared.SafeHtml;
80 08880e98 koutsoub
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
81 08880e98 koutsoub
import com.google.gwt.user.cellview.client.CellTable;
82 08880e98 koutsoub
import com.google.gwt.user.cellview.client.Column;
83 aedc7cfb koutsoub
import com.google.gwt.user.cellview.client.GssSimplePager;
84 08880e98 koutsoub
import com.google.gwt.user.cellview.client.TextColumn;
85 08880e98 koutsoub
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
86 023f6f1e Panagiotis Astithas
import com.google.gwt.user.client.DOM;
87 023f6f1e Panagiotis Astithas
import com.google.gwt.user.client.DeferredCommand;
88 023f6f1e Panagiotis Astithas
import com.google.gwt.user.client.Event;
89 023f6f1e Panagiotis Astithas
import com.google.gwt.user.client.IncrementalCommand;
90 023f6f1e Panagiotis Astithas
import com.google.gwt.user.client.Window;
91 023f6f1e Panagiotis Astithas
import com.google.gwt.user.client.ui.AbstractImagePrototype;
92 023f6f1e Panagiotis Astithas
import com.google.gwt.user.client.ui.Composite;
93 023f6f1e Panagiotis Astithas
import com.google.gwt.user.client.ui.Grid;
94 023f6f1e Panagiotis Astithas
import com.google.gwt.user.client.ui.HTML;
95 023f6f1e Panagiotis Astithas
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
96 023f6f1e Panagiotis Astithas
import com.google.gwt.user.client.ui.HorizontalPanel;
97 023f6f1e Panagiotis Astithas
import com.google.gwt.user.client.ui.VerticalPanel;
98 5b09ea6c koutsoub
import com.google.gwt.user.client.ui.HTMLTable.Cell;
99 9b11a5ca koutsoub
import com.google.gwt.view.client.AsyncDataProvider;
100 9b11a5ca koutsoub
import com.google.gwt.view.client.HasData;
101 08880e98 koutsoub
import com.google.gwt.view.client.ListDataProvider;
102 08880e98 koutsoub
import com.google.gwt.view.client.MultiSelectionModel;
103 08880e98 koutsoub
import com.google.gwt.view.client.ProvidesKey;
104 08880e98 koutsoub
import com.google.gwt.view.client.SelectionChangeEvent;
105 08880e98 koutsoub
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
106 023f6f1e Panagiotis Astithas
107 023f6f1e Panagiotis Astithas
/**
108 023f6f1e Panagiotis Astithas
 * A composite that displays a list of search results for a particular query on
109 023f6f1e Panagiotis Astithas
 * files.
110 023f6f1e Panagiotis Astithas
 */
111 08880e98 koutsoub
public class SearchResults extends Composite{
112 08880e98 koutsoub
        private HTML searchResults = new HTML("Results for search:");
113 9b11a5ca koutsoub
        private String lastQuery;
114 9b11a5ca koutsoub
        SearchDataProvider provider = new SearchDataProvider();
115 08880e98 koutsoub
        /**
116 08880e98 koutsoub
         * Specifies that the images available for this composite will be the ones
117 08880e98 koutsoub
         * available in FileContextMenu.
118 08880e98 koutsoub
         */
119 08880e98 koutsoub
        public interface Images extends ClientBundle,FileContextMenu.Images, CellTreeView.Images, FileList.Images {
120 08880e98 koutsoub
121 08880e98 koutsoub
                @Source("gr/ebs/gss/resources/blank.gif")
122 08880e98 koutsoub
                ImageResource blank();
123 023f6f1e Panagiotis Astithas
124 08880e98 koutsoub
                @Source("gr/ebs/gss/resources/asc.png")
125 08880e98 koutsoub
                ImageResource asc();
126 08880e98 koutsoub
127 08880e98 koutsoub
                @Source("gr/ebs/gss/resources/desc.png")
128 08880e98 koutsoub
                ImageResource desc();
129 08880e98 koutsoub
        }
130 08880e98 koutsoub
131 9b11a5ca koutsoub
        
132 08880e98 koutsoub
        interface TableResources extends DragAndDropCellTable.Resources {
133 08880e98 koutsoub
            @Source({CellTable.Style.DEFAULT_CSS, "GssCellTable.css"})
134 08880e98 koutsoub
            TableStyle cellTableStyle();
135 08880e98 koutsoub
          }
136 08880e98 koutsoub
        
137 08880e98 koutsoub
        static interface Templates extends SafeHtmlTemplates {
138 08880e98 koutsoub
            Templates INSTANCE = GWT.create(Templates.class);
139 023f6f1e Panagiotis Astithas
140 08880e98 koutsoub
            @Template("<div id='dragHelper' style='border:1px solid black; background-color:#ffffff; color:black; width:150px;z-index:100'></div>")
141 08880e98 koutsoub
            SafeHtml outerHelper();
142 08880e98 koutsoub
          }
143 08880e98 koutsoub
        
144 08880e98 koutsoub
        
145 08880e98 koutsoub
        /**
146 08880e98 koutsoub
           * The styles applied to the table.
147 08880e98 koutsoub
           */
148 08880e98 koutsoub
          interface TableStyle extends CellTable.Style {
149 08880e98 koutsoub
          }
150 023f6f1e Panagiotis Astithas
151 023f6f1e Panagiotis Astithas
        private String showingStats = "";
152 023f6f1e Panagiotis Astithas
153 023f6f1e Panagiotis Astithas
        private int startIndex = 0;
154 023f6f1e Panagiotis Astithas
155 023f6f1e Panagiotis Astithas
        /**
156 023f6f1e Panagiotis Astithas
         * A constant that denotes the completion of an IncrementalCommand.
157 023f6f1e Panagiotis Astithas
         */
158 023f6f1e Panagiotis Astithas
        public static final boolean DONE = false;
159 023f6f1e Panagiotis Astithas
160 08880e98 koutsoub
        
161 08880e98 koutsoub
        
162 08880e98 koutsoub
        private final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
163 023f6f1e Panagiotis Astithas
164 08880e98 koutsoub
        
165 08880e98 koutsoub
        
166 08880e98 koutsoub
        DragStopEventHandler dragStop = new DragStopEventHandler() {
167 08880e98 koutsoub
                
168 08880e98 koutsoub
                @Override
169 08880e98 koutsoub
                public void onDragStop(DragStopEvent event) {
170 08880e98 koutsoub
                        GWT.log("DRAG STOPPED");
171 08880e98 koutsoub
                        
172 08880e98 koutsoub
                }
173 08880e98 koutsoub
        };
174 08880e98 koutsoub
        
175 08880e98 koutsoub
        private static class ContactCell extends AbstractCell<gr.ebs.gss.client.rest.resource.FileResource> {
176 023f6f1e Panagiotis Astithas
177 08880e98 koutsoub
            /**
178 08880e98 koutsoub
             * The html of the image used for contacts.
179 08880e98 koutsoub
             * 
180 08880e98 koutsoub
             */
181 08880e98 koutsoub
            private final String imageHtml;
182 023f6f1e Panagiotis Astithas
183 08880e98 koutsoub
            public ContactCell(ImageResource image) {
184 08880e98 koutsoub
              this.imageHtml = AbstractImagePrototype.create(image).getHTML();
185 08880e98 koutsoub
            }
186 023f6f1e Panagiotis Astithas
187 08880e98 koutsoub
            
188 023f6f1e Panagiotis Astithas
189 08880e98 koutsoub
                
190 023f6f1e Panagiotis Astithas
191 08880e98 koutsoub
            @Override
192 08880e98 koutsoub
            public void render(Context context, FileResource value, SafeHtmlBuilder sb) {
193 08880e98 koutsoub
              // Value can be null, so do a null check..
194 08880e98 koutsoub
              if (value == null) {
195 08880e98 koutsoub
                return;
196 08880e98 koutsoub
              }
197 023f6f1e Panagiotis Astithas
198 08880e98 koutsoub
              sb.appendHtmlConstant("<table>");
199 023f6f1e Panagiotis Astithas
200 08880e98 koutsoub
              // Add the contact image.
201 08880e98 koutsoub
              sb.appendHtmlConstant("<tr><td rowspan='3'>");
202 08880e98 koutsoub
              sb.appendHtmlConstant(imageHtml);
203 08880e98 koutsoub
              sb.appendHtmlConstant("</td>");
204 023f6f1e Panagiotis Astithas
205 08880e98 koutsoub
              // Add the name and address.
206 08880e98 koutsoub
              DisplayHelper.log("value.getName()");
207 08880e98 koutsoub
              sb.appendHtmlConstant("<td style='font-size:95%;' id='"+value.getName()+"'>");
208 08880e98 koutsoub
              sb.appendEscaped(value.getName());
209 08880e98 koutsoub
              sb.appendHtmlConstant("</td></tr><tr><td>");
210 08880e98 koutsoub
              sb.appendEscaped(value.getFileSizeAsString());
211 08880e98 koutsoub
              sb.appendHtmlConstant("</td></tr></table>");
212 08880e98 koutsoub
            }
213 023f6f1e Panagiotis Astithas
214 023f6f1e Panagiotis Astithas
215 08880e98 koutsoub
          }
216 023f6f1e Panagiotis Astithas
        /**
217 08880e98 koutsoub
         * Retrieve the celltable.
218 08880e98 koutsoub
         *
219 08880e98 koutsoub
         * @return the celltable
220 023f6f1e Panagiotis Astithas
         */
221 08880e98 koutsoub
        public DragAndDropCellTable<FileResource> getCelltable() {
222 08880e98 koutsoub
                return celltable;
223 08880e98 koutsoub
        }
224 08880e98 koutsoub
        
225 023f6f1e Panagiotis Astithas
226 08880e98 koutsoub
        
227 023f6f1e Panagiotis Astithas
        /**
228 08880e98 koutsoub
         * The number of files in this folder.
229 023f6f1e Panagiotis Astithas
         */
230 08880e98 koutsoub
        int folderFileCount;
231 023f6f1e Panagiotis Astithas
232 023f6f1e Panagiotis Astithas
        /**
233 08880e98 koutsoub
         * Total folder size
234 023f6f1e Panagiotis Astithas
         */
235 08880e98 koutsoub
        long folderTotalSize;
236 023f6f1e Panagiotis Astithas
237 023f6f1e Panagiotis Astithas
        /**
238 023f6f1e Panagiotis Astithas
         * A cache of the files in the list.
239 023f6f1e Panagiotis Astithas
         */
240 023f6f1e Panagiotis Astithas
        private List<FileResource> files;
241 023f6f1e Panagiotis Astithas
242 023f6f1e Panagiotis Astithas
        /**
243 023f6f1e Panagiotis Astithas
         * The widget's image bundle.
244 023f6f1e Panagiotis Astithas
         */
245 023f6f1e Panagiotis Astithas
        private final Images images;
246 08880e98 koutsoub
        
247 08880e98 koutsoub
        private FileContextMenu menuShowing;
248 08880e98 koutsoub
        private DragAndDropCellTable<FileResource> celltable;
249 08880e98 koutsoub
        private final MultiSelectionModel<FileResource> selectionModel;
250 76f91765 koutsoub
        
251 aedc7cfb koutsoub
        GssSimplePager pager;
252 aedc7cfb koutsoub
        GssSimplePager pagerTop;
253 023f6f1e Panagiotis Astithas
        /**
254 023f6f1e Panagiotis Astithas
         * Construct the file list widget. This entails setting up the widget
255 023f6f1e Panagiotis Astithas
         * layout, fetching the number of files in the current folder from the
256 023f6f1e Panagiotis Astithas
         * server and filling the local file cache of displayed files with data from
257 023f6f1e Panagiotis Astithas
         * the server, as well.
258 023f6f1e Panagiotis Astithas
         *
259 023f6f1e Panagiotis Astithas
         * @param _images
260 023f6f1e Panagiotis Astithas
         */
261 08880e98 koutsoub
        public SearchResults(Images _images) {
262 023f6f1e Panagiotis Astithas
                images = _images;
263 08880e98 koutsoub
                DragAndDropCellTable.Resources resources = GWT.create(TableResources.class);
264 08880e98 koutsoub
                ProvidesKey<FileResource> keyProvider = new ProvidesKey<FileResource>(){
265 023f6f1e Panagiotis Astithas
266 023f6f1e Panagiotis Astithas
                        @Override
267 08880e98 koutsoub
                        public Object getKey(FileResource item) {
268 08880e98 koutsoub
                                return item.getUri();
269 08880e98 koutsoub
                        }
270 08880e98 koutsoub
                        
271 08880e98 koutsoub
                };
272 f0cd9b29 koutsoub
                
273 08880e98 koutsoub
                celltable = new DragAndDropCellTable<FileResource>(GSS.VISIBLE_FILE_COUNT,resources,keyProvider){
274 023f6f1e Panagiotis Astithas
                        @Override
275 08880e98 koutsoub
                        protected void onBrowserEvent2(Event event) {
276 08880e98 koutsoub
                                /*if (DOM.eventGetType((Event) event) == Event.ONMOUSEDOWN && DOM.eventGetButton((Event) event) == NativeEvent.BUTTON_RIGHT){
277 08880e98 koutsoub
                                        fireClickEvent((Element) event.getEventTarget().cast());                                        
278 08880e98 koutsoub
                                }*/
279 08880e98 koutsoub
                                super.onBrowserEvent2(event);
280 023f6f1e Panagiotis Astithas
                        }
281 08880e98 koutsoub
                };
282 08880e98 koutsoub
                provider.addDataDisplay(celltable);
283 08880e98 koutsoub
                celltable.addDragStopHandler(dragStop);
284 08880e98 koutsoub
                celltable.addDragStartHandler(new DragStartEventHandler() {
285 08880e98 koutsoub
286 08880e98 koutsoub
                      public void onDragStart(DragStartEvent event) {
287 08880e98 koutsoub
                        FileResource value = event.getDraggableData();
288 f0cd9b29 koutsoub
                        
289 08880e98 koutsoub
                        com.google.gwt.dom.client.Element helper = event.getHelper();
290 08880e98 koutsoub
                        SafeHtmlBuilder sb = new SafeHtmlBuilder();
291 08880e98 koutsoub
                        sb.appendHtmlConstant("<b>");
292 08880e98 koutsoub
                        DisplayHelper.log(value.getName());
293 f0cd9b29 koutsoub
                        if(getSelectedFiles().size()==1)
294 f0cd9b29 koutsoub
                                sb.appendEscaped(value.getName());
295 f0cd9b29 koutsoub
                        else
296 f0cd9b29 koutsoub
                                sb.appendEscaped(getSelectedFiles().size()+" files");
297 08880e98 koutsoub
                        sb.appendHtmlConstant("</b>");
298 08880e98 koutsoub
                        helper.setInnerHTML(sb.toSafeHtml().asString());
299 08880e98 koutsoub
300 08880e98 koutsoub
                      }
301 08880e98 koutsoub
                    });
302 f0cd9b29 koutsoub
                DragAndDropColumn<FileResource, ImageResource> status = new DragAndDropColumn<FileResource, ImageResource>(new ImageResourceCell(){
303 f0cd9b29 koutsoub
                        @Override
304 f0cd9b29 koutsoub
                  public boolean handlesSelection() {
305 f0cd9b29 koutsoub
                            return false;
306 f0cd9b29 koutsoub
                          }
307 f0cd9b29 koutsoub
                }) {
308 08880e98 koutsoub
                  @Override
309 08880e98 koutsoub
                  public ImageResource getValue(FileResource entity) {
310 08880e98 koutsoub
                    return getFileIcon(entity);
311 08880e98 koutsoub
                  }
312 f0cd9b29 koutsoub
                  
313 08880e98 koutsoub
               };
314 f0cd9b29 koutsoub
            celltable.addColumn(status,"");
315 f0cd9b29 koutsoub
            
316 f0cd9b29 koutsoub
            initDragOperation(status);
317 f0cd9b29 koutsoub
                final DragAndDropColumn<FileResource,SafeHtml> nameColumn = new DragAndDropColumn<FileResource,SafeHtml>(new SafeHtmlCell()) {
318 f0cd9b29 koutsoub
319 f0cd9b29 koutsoub
320 f0cd9b29 koutsoub
                        @Override
321 f0cd9b29 koutsoub
                        public SafeHtml getValue(FileResource object) {
322 f0cd9b29 koutsoub
                                SafeHtmlBuilder sb = new SafeHtmlBuilder();
323 f0cd9b29 koutsoub
                                if (object.getContentType().endsWith("png") || object.getContentType().endsWith("gif") || object.getContentType().endsWith("jpeg") ){                                        
324 f0cd9b29 koutsoub
                                        sb.appendHtmlConstant("<span id='fileList."+ object.getName() +"'>");
325 f0cd9b29 koutsoub
                                        sb.appendEscaped(object.getName());
326 f0cd9b29 koutsoub
                                        sb.appendHtmlConstant("</span>");
327 f0cd9b29 koutsoub
                                        sb.appendHtmlConstant(" <a href='" +
328 f0cd9b29 koutsoub
                                GSS.get().getTopPanel().getFileMenu().getDownloadURL(object) +
329 f0cd9b29 koutsoub
                                "' title='" + object.getOwner() + " : " + object.getPath() + object.getName() +
330 f0cd9b29 koutsoub
                                "' rel='lytebox[mnf]' " +
331 f0cd9b29 koutsoub
                                "onclick='myLytebox.start(this, false, false); return false;'>" +
332 f0cd9b29 koutsoub
                                "(view)" + "</a>");
333 f0cd9b29 koutsoub
                                        
334 f0cd9b29 koutsoub
                                        
335 f0cd9b29 koutsoub
                                }
336 f0cd9b29 koutsoub
                                else{                                        
337 f0cd9b29 koutsoub
                                        sb.appendHtmlConstant("<span id='fileList."+ object.getName() +"'>");
338 f0cd9b29 koutsoub
                                        sb.appendEscaped(object.getName());
339 f0cd9b29 koutsoub
                                        sb.appendHtmlConstant("</span>");
340 f0cd9b29 koutsoub
                                }
341 f0cd9b29 koutsoub
                                
342 f0cd9b29 koutsoub
                                return sb.toSafeHtml();
343 f0cd9b29 koutsoub
                        }
344 f0cd9b29 koutsoub
                        
345 f0cd9b29 koutsoub
                };
346 f0cd9b29 koutsoub
                initDragOperation(nameColumn);
347 76f91765 koutsoub
                celltable.addColumn(nameColumn,"Name");
348 f0cd9b29 koutsoub
                
349 f0cd9b29 koutsoub
            DragAndDropColumn<FileResource,String> aColumn;
350 f0cd9b29 koutsoub
                celltable.addColumn(aColumn=new DragAndDropColumn<FileResource,String>(new TextCell()) {
351 08880e98 koutsoub
                        @Override
352 08880e98 koutsoub
                        public String getValue(FileResource object) {
353 08880e98 koutsoub
                                return GSS.get().findUserFullName(object.getOwner());
354 08880e98 koutsoub
                        }                        
355 76f91765 koutsoub
                },"Owner");
356 f0cd9b29 koutsoub
                initDragOperation(aColumn);
357 76f91765 koutsoub
                
358 f0cd9b29 koutsoub
                celltable.addColumn(aColumn=new DragAndDropColumn<FileResource,String>(new TextCell()) {
359 08880e98 koutsoub
                        @Override
360 08880e98 koutsoub
                        public String getValue(FileResource object) {
361 08880e98 koutsoub
                                if(object.isDeleted())
362 08880e98 koutsoub
                                        return object.getPath()+" (In Trash)";
363 08880e98 koutsoub
                                return object.getPath();
364 08880e98 koutsoub
                        }                        
365 76f91765 koutsoub
                },"Path");
366 f0cd9b29 koutsoub
                initDragOperation(aColumn);
367 f0cd9b29 koutsoub
                
368 76f91765 koutsoub
                
369 76f91765 koutsoub
                        
370 f0cd9b29 koutsoub
                celltable.addColumn(aColumn=new DragAndDropColumn<FileResource,String>(new TextCell()) {
371 08880e98 koutsoub
                        @Override
372 08880e98 koutsoub
                        public String getValue(FileResource object) {
373 08880e98 koutsoub
                                return object.getVersion().toString();
374 08880e98 koutsoub
                        }                        
375 76f91765 koutsoub
                },"Version");
376 f0cd9b29 koutsoub
                initDragOperation(aColumn);
377 76f91765 koutsoub
                
378 76f91765 koutsoub
                
379 f0cd9b29 koutsoub
                celltable.addColumn(aColumn=new DragAndDropColumn<FileResource,String>(new TextCell()) {
380 08880e98 koutsoub
                        @Override
381 08880e98 koutsoub
                        public String getValue(FileResource object) {
382 08880e98 koutsoub
                                // TODO Auto-generated method stub
383 08880e98 koutsoub
                                return object.getFileSizeAsString();
384 08880e98 koutsoub
                        }                        
385 76f91765 koutsoub
                },"Size");
386 f0cd9b29 koutsoub
                initDragOperation(aColumn);
387 76f91765 koutsoub
                        
388 f0cd9b29 koutsoub
                celltable.addColumn(aColumn=new DragAndDropColumn<FileResource,String>(new TextCell()) {
389 08880e98 koutsoub
                        @Override
390 08880e98 koutsoub
                        public String getValue(FileResource object) {
391 08880e98 koutsoub
                                return formatter.format(object.getModificationDate());
392 08880e98 koutsoub
                        }                        
393 76f91765 koutsoub
                },"Last Modified");
394 76f91765 koutsoub
                
395 76f91765 koutsoub
                
396 f0cd9b29 koutsoub
               
397 f0cd9b29 koutsoub
398 023f6f1e Panagiotis Astithas
                VerticalPanel vp = new VerticalPanel();
399 08880e98 koutsoub
                vp.setWidth("100%");
400 08880e98 koutsoub
                celltable.setWidth("100%");
401 023f6f1e Panagiotis Astithas
                vp.add(searchResults);
402 023f6f1e Panagiotis Astithas
                searchResults.addStyleName("gss-searchLabel");
403 aedc7cfb koutsoub
                pagerTop = new GssSimplePager(GssSimplePager.TextLocation.CENTER);
404 9b11a5ca koutsoub
                pagerTop.setDisplay(celltable);
405 9b11a5ca koutsoub
                vp.add(pagerTop);
406 08880e98 koutsoub
                vp.add(celltable);
407 aedc7cfb koutsoub
                pager = new GssSimplePager(GssSimplePager.TextLocation.CENTER);
408 08880e98 koutsoub
                pager.setDisplay(celltable);
409 08880e98 koutsoub
                //celltable.setPageSize(2);
410 08880e98 koutsoub
                
411 08880e98 koutsoub
                vp.add(pager);
412 08880e98 koutsoub
                vp.setCellWidth(celltable, "100%");
413 08880e98 koutsoub
                
414 023f6f1e Panagiotis Astithas
                initWidget(vp);
415 08880e98 koutsoub
                
416 08880e98 koutsoub
                //initWidget(celltable);
417 08880e98 koutsoub
                celltable.setStyleName("gss-List");
418 08880e98 koutsoub
                selectionModel = new MultiSelectionModel<FileResource>();
419 08880e98 koutsoub
                
420 08880e98 koutsoub
421 08880e98 koutsoub
                 Handler selectionHandler = new SelectionChangeEvent.Handler() { 
422 08880e98 koutsoub
             @Override 
423 08880e98 koutsoub
             public void onSelectionChange(com.google.gwt.view.client.SelectionChangeEvent event) {
424 08880e98 koutsoub
                     if(getSelectedFiles().size()==1)
425 08880e98 koutsoub
                             GSS.get().setCurrentSelection(getSelectedFiles().get(0));
426 08880e98 koutsoub
                     else
427 08880e98 koutsoub
                             GSS.get().setCurrentSelection(getSelectedFiles());
428 08880e98 koutsoub
                                 //contextMenu.setFiles(getSelectedFiles());
429 08880e98 koutsoub
             }
430 08880e98 koutsoub
         };
431 08880e98 koutsoub
         selectionModel.addSelectionChangeHandler(selectionHandler);
432 08880e98 koutsoub
         
433 08880e98 koutsoub
                celltable.setSelectionModel(selectionModel,GSSSelectionEventManager.<FileResource>createDefaultManager());
434 08880e98 koutsoub
                celltable.setPageSize(GSS.VISIBLE_FILE_COUNT);
435 08880e98 koutsoub
                celltable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
436 08880e98 koutsoub
                Scheduler.get().scheduleIncremental(new RepeatingCommand() {
437 023f6f1e Panagiotis Astithas
438 023f6f1e Panagiotis Astithas
                        @Override
439 023f6f1e Panagiotis Astithas
                        public boolean execute() {
440 023f6f1e Panagiotis Astithas
                                return fetchRootFolder();
441 023f6f1e Panagiotis Astithas
                        }
442 023f6f1e Panagiotis Astithas
                });
443 08880e98 koutsoub
                sinkEvents(Event.ONCONTEXTMENU);
444 08880e98 koutsoub
                sinkEvents(Event.ONMOUSEUP);
445 08880e98 koutsoub
                sinkEvents(Event.ONMOUSEDOWN);
446 08880e98 koutsoub
                sinkEvents(Event.ONCLICK);
447 08880e98 koutsoub
                sinkEvents(Event.ONKEYDOWN);
448 08880e98 koutsoub
                sinkEvents(Event.ONDBLCLICK);
449 08880e98 koutsoub
                GSS.preventIESelection();
450 023f6f1e Panagiotis Astithas
        }
451 08880e98 koutsoub
        
452 08880e98 koutsoub
        //public native void fireClickEvent(Element element) /*-{
453 08880e98 koutsoub
          //  var evObj = $doc.createEvent('MouseEvents');
454 08880e98 koutsoub
            //evObj.initEvent('click', true, true);
455 08880e98 koutsoub
            //element.dispatchEvent(evObj);
456 08880e98 koutsoub
          //}-*/;
457 08880e98 koutsoub
458 08880e98 koutsoub
         public List<FileResource> getSelectedFiles() {
459 08880e98 koutsoub
         return new ArrayList<FileResource>(selectionModel.getSelectedSet());
460 08880e98 koutsoub
         }
461 08880e98 koutsoub
        
462 08880e98 koutsoub
         private void initDragOperation(DragAndDropColumn<?, ?> column) {
463 08880e98 koutsoub
464 08880e98 koutsoub
                    // retrieve draggableOptions on the column
465 08880e98 koutsoub
                    DraggableOptions draggableOptions = column.getDraggableOptions();
466 08880e98 koutsoub
                    // use template to construct the helper. The content of the div will be set
467 08880e98 koutsoub
                    // after
468 08880e98 koutsoub
                    draggableOptions.setHelper($(Templates.INSTANCE.outerHelper().asString()));
469 08880e98 koutsoub
                    //draggableOptions.setZIndex(100);
470 08880e98 koutsoub
                    // opacity of the helper
471 08880e98 koutsoub
                    draggableOptions.setAppendTo("body"); 
472 08880e98 koutsoub
                    //draggableOptions.setOpacity((float) 0.8);
473 08880e98 koutsoub
                    draggableOptions.setContainment("document");
474 08880e98 koutsoub
                    // cursor to use during the drag operation
475 08880e98 koutsoub
                    draggableOptions.setCursor(Cursor.MOVE);
476 08880e98 koutsoub
                    // set the revert option
477 08880e98 koutsoub
                    draggableOptions.setRevert(RevertOption.ON_INVALID_DROP);
478 08880e98 koutsoub
                    // prevents dragging when user click on the category drop-down list
479 08880e98 koutsoub
                    draggableOptions.setCancel("select");
480 08880e98 koutsoub
                    
481 08880e98 koutsoub
                    draggableOptions.setOnBeforeDragStart(new DragFunction() {
482 08880e98 koutsoub
                                
483 08880e98 koutsoub
                                @Override
484 08880e98 koutsoub
                                public void f(DragContext context) {
485 08880e98 koutsoub
                                         FileResource value = context.getDraggableData();
486 08880e98 koutsoub
                                     if(!selectionModel.isSelected(value)){
487 08880e98 koutsoub
                                               throw new StopDragException();
488 08880e98 koutsoub
                                      }
489 08880e98 koutsoub
                                        
490 08880e98 koutsoub
                                }
491 08880e98 koutsoub
                        });
492 08880e98 koutsoub
                  }
493 08880e98 koutsoub
        
494 08880e98 koutsoub
         public void showContextMenu(Event event){
495 08880e98 koutsoub
                 menuShowing = new FileContextMenu(images, false, true);
496 08880e98 koutsoub
                        menuShowing=menuShowing.onEmptyEvent(event);
497 08880e98 koutsoub
         }
498 023f6f1e Panagiotis Astithas
        @Override
499 08880e98 koutsoub
        public void onBrowserEvent(Event event) {
500 08880e98 koutsoub
                
501 08880e98 koutsoub
                if (files == null || files.size() == 0) {
502 08880e98 koutsoub
                        if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
503 08880e98 koutsoub
                                menuShowing = new FileContextMenu(images, false, true);
504 08880e98 koutsoub
                                menuShowing=menuShowing.onEmptyEvent(event);
505 08880e98 koutsoub
                                event.preventDefault();
506 08880e98 koutsoub
                                event.cancelBubble(true);
507 08880e98 koutsoub
                        }
508 08880e98 koutsoub
                        return;
509 023f6f1e Panagiotis Astithas
                }
510 08880e98 koutsoub
                if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() != 0) {
511 08880e98 koutsoub
                        GWT.log("*****GOING TO SHOW CONTEXT MENU ****", null);
512 08880e98 koutsoub
                        menuShowing =  new FileContextMenu(images, false, false);
513 08880e98 koutsoub
                        menuShowing=menuShowing.onEvent(event);
514 08880e98 koutsoub
                        event.cancelBubble(true);
515 08880e98 koutsoub
                        event.preventDefault();
516 08880e98 koutsoub
                } else if (DOM.eventGetType(event) == Event.ONCONTEXTMENU && getSelectedFiles().size() == 0) {
517 08880e98 koutsoub
                        menuShowing = new FileContextMenu(images, false, true);
518 08880e98 koutsoub
                        menuShowing=menuShowing.onEmptyEvent(event);
519 08880e98 koutsoub
                        event.cancelBubble(true);
520 08880e98 koutsoub
                        event.preventDefault();
521 08880e98 koutsoub
                } else if (DOM.eventGetType(event) == Event.ONDBLCLICK)
522 08880e98 koutsoub
                        if (getSelectedFiles().size() == 1) {
523 08880e98 koutsoub
                                GSS app = GSS.get();
524 08880e98 koutsoub
                                FileResource file = getSelectedFiles().get(0);
525 08880e98 koutsoub
                                String dateString = RestCommand.getDate();
526 08880e98 koutsoub
                                String resource = file.getUri().substring(app.getApiPath().length() - 1, file.getUri().length());
527 08880e98 koutsoub
                                String sig = app.getCurrentUserResource().getUsername() + " " +
528 08880e98 koutsoub
                                                RestCommand.calculateSig("GET", dateString, resource,
529 08880e98 koutsoub
                                                RestCommand.base64decode(app.getToken()));
530 0fee7cb5 Natasa Kapravelou
                                if(!file.isDeleted()){
531 0fee7cb5 Natasa Kapravelou
                                        Window.open(file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date=" + URL.encodeComponent(dateString), "_blank", "");
532 0fee7cb5 Natasa Kapravelou
                                }
533 08880e98 koutsoub
                                event.preventDefault();
534 08880e98 koutsoub
                                return;
535 08880e98 koutsoub
                        }
536 08880e98 koutsoub
                super.onBrowserEvent(event);
537 023f6f1e Panagiotis Astithas
        }
538 023f6f1e Panagiotis Astithas
539 023f6f1e Panagiotis Astithas
        /**
540 023f6f1e Panagiotis Astithas
         * Retrieve the root folder for the current user.
541 023f6f1e Panagiotis Astithas
         *
542 023f6f1e Panagiotis Astithas
         * @return true if the retrieval was successful
543 023f6f1e Panagiotis Astithas
         */
544 023f6f1e Panagiotis Astithas
        protected boolean fetchRootFolder() {
545 023f6f1e Panagiotis Astithas
                UserResource user = GSS.get().getCurrentUserResource();
546 023f6f1e Panagiotis Astithas
                if (user == null)
547 023f6f1e Panagiotis Astithas
                        return !DONE;
548 08880e98 koutsoub
                // Update cache and clear selection.
549 08880e98 koutsoub
                updateFileCache(null);
550 023f6f1e Panagiotis Astithas
                return DONE;
551 023f6f1e Panagiotis Astithas
        }
552 023f6f1e Panagiotis Astithas
553 023f6f1e Panagiotis Astithas
554 023f6f1e Panagiotis Astithas
        /**
555 023f6f1e Panagiotis Astithas
         * Update the display of the file list.
556 023f6f1e Panagiotis Astithas
         */
557 023f6f1e Panagiotis Astithas
        void update(boolean sort) {
558 023f6f1e Panagiotis Astithas
                int count = folderFileCount;
559 023f6f1e Panagiotis Astithas
                int max = startIndex + GSS.VISIBLE_FILE_COUNT;
560 023f6f1e Panagiotis Astithas
                if (max > count)
561 023f6f1e Panagiotis Astithas
                        max = count;
562 023f6f1e Panagiotis Astithas
                folderTotalSize = 0;
563 08880e98 koutsoub
                
564 08880e98 koutsoub
                copyListAndContinue(files);
565 08880e98 koutsoub
                for(FileResource f : files){
566 08880e98 koutsoub
                        folderTotalSize += f.getContentLength();
567 023f6f1e Panagiotis Astithas
                }
568 023f6f1e Panagiotis Astithas
                if (folderFileCount == 0) {
569 023f6f1e Panagiotis Astithas
                        showingStats = "no files";
570 023f6f1e Panagiotis Astithas
                } else if (folderFileCount < GSS.VISIBLE_FILE_COUNT) {
571 023f6f1e Panagiotis Astithas
                        if (folderFileCount == 1)
572 023f6f1e Panagiotis Astithas
                                showingStats = "1 file";
573 023f6f1e Panagiotis Astithas
                        else
574 023f6f1e Panagiotis Astithas
                                showingStats = folderFileCount + " files";
575 023f6f1e Panagiotis Astithas
                        showingStats += " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
576 023f6f1e Panagiotis Astithas
                } else {
577 023f6f1e Panagiotis Astithas
                        showingStats = "" + (startIndex + 1) + " - " + max + " of " + count + " files" + " (" + FileResource.getFileSizeAsString(folderTotalSize) + ")";
578 023f6f1e Panagiotis Astithas
                }
579 023f6f1e Panagiotis Astithas
                updateCurrentlyShowingStats();
580 023f6f1e Panagiotis Astithas
581 023f6f1e Panagiotis Astithas
        }
582 023f6f1e Panagiotis Astithas
583 023f6f1e Panagiotis Astithas
        /**
584 08880e98 koutsoub
         * Return the proper icon based on the MIME type of the file.
585 023f6f1e Panagiotis Astithas
         *
586 08880e98 koutsoub
         * @param file
587 08880e98 koutsoub
         * @return the icon
588 023f6f1e Panagiotis Astithas
         */
589 08880e98 koutsoub
        private ImageResource getFileIcon(FileResource file) {
590 08880e98 koutsoub
                String mimetype = file.getContentType();
591 9b11a5ca koutsoub
                boolean shared = file.isShared();
592 08880e98 koutsoub
                if (mimetype == null)
593 08880e98 koutsoub
                        return shared ? images.documentShared() : images.document();
594 08880e98 koutsoub
                mimetype = mimetype.toLowerCase();
595 08880e98 koutsoub
                if (mimetype.startsWith("application/pdf"))
596 08880e98 koutsoub
                        return shared ? images.pdfShared() : images.pdf();
597 08880e98 koutsoub
                else if (mimetype.endsWith("excel"))
598 08880e98 koutsoub
                        return shared ? images.spreadsheetShared() : images.spreadsheet();
599 08880e98 koutsoub
                else if (mimetype.endsWith("msword"))
600 08880e98 koutsoub
                        return shared ? images.wordprocessorShared() : images.wordprocessor();
601 08880e98 koutsoub
                else if (mimetype.endsWith("powerpoint"))
602 08880e98 koutsoub
                        return shared ? images.presentationShared() : images.presentation();
603 08880e98 koutsoub
                else if (mimetype.startsWith("application/zip") ||
604 08880e98 koutsoub
                                        mimetype.startsWith("application/gzip") ||
605 08880e98 koutsoub
                                        mimetype.startsWith("application/x-gzip") ||
606 08880e98 koutsoub
                                        mimetype.startsWith("application/x-tar") ||
607 08880e98 koutsoub
                                        mimetype.startsWith("application/x-gtar"))
608 08880e98 koutsoub
                        return shared ? images.zipShared() : images.zip();
609 08880e98 koutsoub
                else if (mimetype.startsWith("text/html"))
610 08880e98 koutsoub
                        return shared ? images.htmlShared() : images.html();
611 08880e98 koutsoub
                else if (mimetype.startsWith("text/plain"))
612 08880e98 koutsoub
                        return shared ? images.txtShared() : images.txt();
613 08880e98 koutsoub
                else if (mimetype.startsWith("image/"))
614 08880e98 koutsoub
                        return shared ? images.imageShared() : images.image();
615 08880e98 koutsoub
                else if (mimetype.startsWith("video/"))
616 08880e98 koutsoub
                        return shared ? images.videoShared() : images.video();
617 08880e98 koutsoub
                else if (mimetype.startsWith("audio/"))
618 08880e98 koutsoub
                        return shared ? images.audioShared() : images.audio();
619 08880e98 koutsoub
                return shared ? images.documentShared() : images.document();
620 023f6f1e Panagiotis Astithas
        }
621 023f6f1e Panagiotis Astithas
622 023f6f1e Panagiotis Astithas
        /**
623 08880e98 koutsoub
         * Update status panel with currently showing file stats.
624 023f6f1e Panagiotis Astithas
         */
625 08880e98 koutsoub
        public void updateCurrentlyShowingStats() {
626 08880e98 koutsoub
                GSS.get().getStatusPanel().updateCurrentlyShowing(showingStats);
627 08880e98 koutsoub
        }
628 08880e98 koutsoub
629 023f6f1e Panagiotis Astithas
        public void updateFileCache(String query) {
630 023f6f1e Panagiotis Astithas
                final GSS app = GSS.get();
631 9b11a5ca koutsoub
                setLastQuery(query);
632 023f6f1e Panagiotis Astithas
                clearSelectedRows();
633 08880e98 koutsoub
                //clearLabels();
634 023f6f1e Panagiotis Astithas
                startIndex = 0;
635 023f6f1e Panagiotis Astithas
                app.showLoadingIndicator();
636 023f6f1e Panagiotis Astithas
                if (query == null || query.trim().equals("")) {
637 4315bcc5 Panagiotis Astithas
                        searchResults.setHTML("You must specify a query.");
638 4315bcc5 Panagiotis Astithas
                        setFiles(new ArrayList());
639 4315bcc5 Panagiotis Astithas
                        update(true);
640 4315bcc5 Panagiotis Astithas
                        app.hideLoadingIndicator();
641 4315bcc5 Panagiotis Astithas
                } else if (!GSS.isValidResourceName(query)) {
642 4315bcc5 Panagiotis Astithas
                        searchResults.setHTML("The query was invalid. Try to use words that appear in the file's name, contents or tags.");
643 023f6f1e Panagiotis Astithas
                        setFiles(new ArrayList());
644 023f6f1e Panagiotis Astithas
                        update(true);
645 023f6f1e Panagiotis Astithas
                        app.hideLoadingIndicator();
646 023f6f1e Panagiotis Astithas
                } else{
647 023f6f1e Panagiotis Astithas
                        searchResults.setHTML("Search results for " + query);
648 9b11a5ca koutsoub
                        showCellTable(true);
649 9b11a5ca koutsoub
                        
650 023f6f1e Panagiotis Astithas
                }
651 023f6f1e Panagiotis Astithas
        }
652 023f6f1e Panagiotis Astithas
653 023f6f1e Panagiotis Astithas
        /**
654 023f6f1e Panagiotis Astithas
         * Fill the file cache with data.
655 023f6f1e Panagiotis Astithas
         */
656 08880e98 koutsoub
        public void setFiles(final List<FileResource> _files) {
657 08880e98 koutsoub
                if (_files.size() > 0 && ! (GSS.get().getTreeView().getSelection() instanceof TrashResource)) {
658 08880e98 koutsoub
                        files = new ArrayList<FileResource>();
659 08880e98 koutsoub
                        for (FileResource fres : _files)
660 08880e98 koutsoub
                                files.add(fres);
661 08880e98 koutsoub
                }
662 08880e98 koutsoub
                else
663 08880e98 koutsoub
                        files = _files;
664 023f6f1e Panagiotis Astithas
                Collections.sort(files, new Comparator<FileResource>() {
665 023f6f1e Panagiotis Astithas
666 023f6f1e Panagiotis Astithas
                        @Override
667 023f6f1e Panagiotis Astithas
                        public int compare(FileResource arg0, FileResource arg1) {
668 023f6f1e Panagiotis Astithas
                                return arg0.getName().compareTo(arg1.getName());
669 023f6f1e Panagiotis Astithas
                        }
670 023f6f1e Panagiotis Astithas
671 023f6f1e Panagiotis Astithas
                });
672 023f6f1e Panagiotis Astithas
                folderFileCount = files.size();
673 023f6f1e Panagiotis Astithas
        }
674 023f6f1e Panagiotis Astithas
675 08880e98 koutsoub
        
676 023f6f1e Panagiotis Astithas
677 08880e98 koutsoub
        
678 023f6f1e Panagiotis Astithas
        /**
679 023f6f1e Panagiotis Astithas
         * Does the list contains the requested filename
680 023f6f1e Panagiotis Astithas
         *
681 023f6f1e Panagiotis Astithas
         * @param fileName
682 023f6f1e Panagiotis Astithas
         * @return true/false
683 023f6f1e Panagiotis Astithas
         */
684 023f6f1e Panagiotis Astithas
        public boolean contains(String fileName) {
685 023f6f1e Panagiotis Astithas
                for (int i = 0; i < files.size(); i++)
686 023f6f1e Panagiotis Astithas
                        if (files.get(i).getName().equals(fileName))
687 023f6f1e Panagiotis Astithas
                                return true;
688 023f6f1e Panagiotis Astithas
                return false;
689 023f6f1e Panagiotis Astithas
        }
690 023f6f1e Panagiotis Astithas
691 023f6f1e Panagiotis Astithas
        public void clearSelectedRows() {
692 08880e98 koutsoub
                Iterator<FileResource> it = selectionModel.getSelectedSet().iterator();
693 08880e98 koutsoub
                while(it.hasNext()){
694 08880e98 koutsoub
                        selectionModel.setSelected(it.next(),false);
695 023f6f1e Panagiotis Astithas
                }
696 023f6f1e Panagiotis Astithas
        }
697 023f6f1e Panagiotis Astithas
698 08880e98 koutsoub
        /**
699 08880e98 koutsoub
         *
700 08880e98 koutsoub
         */
701 08880e98 koutsoub
        public void selectAllRows() {
702 08880e98 koutsoub
                Iterator<FileResource> it = selectionModel.getSelectedSet().iterator();
703 08880e98 koutsoub
                while(it.hasNext()){
704 08880e98 koutsoub
                        selectionModel.setSelected(it.next(),true);
705 08880e98 koutsoub
                }
706 08880e98 koutsoub
707 023f6f1e Panagiotis Astithas
708 08880e98 koutsoub
        }
709 023f6f1e Panagiotis Astithas
710 08880e98 koutsoub
        
711 08880e98 koutsoub
        private void sortFiles(final String sortingProperty, final boolean sortingType){
712 08880e98 koutsoub
                Collections.sort(files, new Comparator<FileResource>() {
713 08880e98 koutsoub
714 08880e98 koutsoub
            @Override
715 08880e98 koutsoub
            public int compare(FileResource arg0, FileResource arg1) {
716 08880e98 koutsoub
                    AbstractImagePrototype descPrototype = AbstractImagePrototype.create(images.desc());
717 08880e98 koutsoub
                    AbstractImagePrototype ascPrototype = AbstractImagePrototype.create(images.asc());
718 08880e98 koutsoub
                    if (sortingType){
719 08880e98 koutsoub
                            if (sortingProperty.equals("version")) {
720 08880e98 koutsoub
                                    return arg0.getVersion().compareTo(arg1.getVersion());
721 08880e98 koutsoub
                            } else if (sortingProperty.equals("owner")) {
722 08880e98 koutsoub
                                    return arg0.getOwner().compareTo(arg1.getOwner());
723 08880e98 koutsoub
                            } else if (sortingProperty.equals("date")) {
724 08880e98 koutsoub
                                    return arg0.getModificationDate().compareTo(arg1.getModificationDate());
725 08880e98 koutsoub
                            } else if (sortingProperty.equals("size")) {
726 08880e98 koutsoub
                                    return arg0.getContentLength().compareTo(arg1.getContentLength());
727 08880e98 koutsoub
                            } else if (sortingProperty.equals("name")) {
728 08880e98 koutsoub
                                    return arg0.getName().compareTo(arg1.getName());
729 08880e98 koutsoub
                            } else if (sortingProperty.equals("path")) {
730 08880e98 koutsoub
                                    return arg0.getUri().compareTo(arg1.getUri());
731 08880e98 koutsoub
                            } else {
732 08880e98 koutsoub
                                    return arg0.getName().compareTo(arg1.getName());
733 08880e98 koutsoub
                            }
734 08880e98 koutsoub
                    }
735 08880e98 koutsoub
                    else if (sortingProperty.equals("version")) {
736 08880e98 koutsoub
                            
737 08880e98 koutsoub
                            return arg1.getVersion().compareTo(arg0.getVersion());
738 08880e98 koutsoub
                    } else if (sortingProperty.equals("owner")) {
739 08880e98 koutsoub
                            
740 08880e98 koutsoub
                            return arg1.getOwner().compareTo(arg0.getOwner());
741 08880e98 koutsoub
                    } else if (sortingProperty.equals("date")) {
742 08880e98 koutsoub
                            
743 08880e98 koutsoub
                            return arg1.getModificationDate().compareTo(arg0.getModificationDate());
744 08880e98 koutsoub
                    } else if (sortingProperty.equals("size")) {
745 08880e98 koutsoub
                            
746 08880e98 koutsoub
                            return arg1.getContentLength().compareTo(arg0.getContentLength());
747 08880e98 koutsoub
                    } else if (sortingProperty.equals("name")) {
748 08880e98 koutsoub
                            
749 08880e98 koutsoub
                            return arg1.getName().compareTo(arg0.getName());
750 08880e98 koutsoub
                    } else if (sortingProperty.equals("path")) {
751 08880e98 koutsoub
                            
752 08880e98 koutsoub
                            return arg1.getUri().compareTo(arg0.getUri());
753 08880e98 koutsoub
                    } else {
754 08880e98 koutsoub
                            
755 08880e98 koutsoub
                            return arg1.getName().compareTo(arg0.getName());
756 08880e98 koutsoub
                    }
757 08880e98 koutsoub
            }
758 08880e98 koutsoub
759 08880e98 koutsoub
                });
760 08880e98 koutsoub
        }
761 08880e98 koutsoub
        
762 76f91765 koutsoub
        
763 08880e98 koutsoub
        /**
764 08880e98 koutsoub
         * Creates a new ArrayList<FileResources> from the given files ArrayList 
765 08880e98 koutsoub
         * in order that the input files remain untouched 
766 08880e98 koutsoub
         * and continues to find user's full names of each FileResource element
767 08880e98 koutsoub
         * in the new ArrayList
768 08880e98 koutsoub
         *    
769 08880e98 koutsoub
         * @param filesInput
770 08880e98 koutsoub
         */
771 08880e98 koutsoub
        private void copyListAndContinue(List<FileResource> filesInput){
772 08880e98 koutsoub
                List<FileResource> copiedFiles = new ArrayList<FileResource>();                
773 08880e98 koutsoub
                for(FileResource file : filesInput) {
774 08880e98 koutsoub
                        copiedFiles.add(file);
775 08880e98 koutsoub
                }
776 08880e98 koutsoub
                handleFullNames(copiedFiles);
777 08880e98 koutsoub
        }
778 08880e98 koutsoub
        
779 08880e98 koutsoub
        /**
780 08880e98 koutsoub
         * Examines whether or not the user's full name exists in the 
781 08880e98 koutsoub
         * userFullNameMap in the GSS.java for every element of the input list.
782 08880e98 koutsoub
         * If the user's full name does not exist in the map then a command is being made.  
783 08880e98 koutsoub
         * 
784 08880e98 koutsoub
         * @param filesInput
785 08880e98 koutsoub
         */
786 08880e98 koutsoub
        private void handleFullNames(List<FileResource> filesInput){                
787 08880e98 koutsoub
                if(filesInput.size() == 0){
788 9b11a5ca koutsoub
                        showCellTable(false);
789 08880e98 koutsoub
                        return;
790 08880e98 koutsoub
                }                
791 08880e98 koutsoub
792 08880e98 koutsoub
                if(GSS.get().findUserFullName(filesInput.get(0).getOwner()) == null){
793 08880e98 koutsoub
                        findFullNameAndUpdate(filesInput);                
794 023f6f1e Panagiotis Astithas
                        return;
795 023f6f1e Panagiotis Astithas
                }
796 08880e98 koutsoub
                                
797 08880e98 koutsoub
                if(filesInput.size() >= 1){
798 08880e98 koutsoub
                        filesInput.remove(filesInput.get(0));
799 08880e98 koutsoub
                        if(filesInput.isEmpty()){
800 9b11a5ca koutsoub
                                showCellTable(false);                                
801 08880e98 koutsoub
                        }else{
802 08880e98 koutsoub
                                handleFullNames(filesInput);
803 08880e98 koutsoub
                        }
804 08880e98 koutsoub
                }                
805 023f6f1e Panagiotis Astithas
        }
806 08880e98 koutsoub
        
807 08880e98 koutsoub
        /**
808 08880e98 koutsoub
         * Makes a command to search for full name from a given username. 
809 08880e98 koutsoub
         * Only after the completion of the command the celltable is shown
810 08880e98 koutsoub
         * or the search for the next full name continues.
811 08880e98 koutsoub
         *  
812 08880e98 koutsoub
         * @param filesInput
813 08880e98 koutsoub
         */
814 08880e98 koutsoub
        private void findFullNameAndUpdate(final List<FileResource> filesInput){                
815 08880e98 koutsoub
                String aUserName = filesInput.get(0).getOwner();
816 08880e98 koutsoub
                String path = GSS.get().getApiPath() + "users/" + aUserName; 
817 08880e98 koutsoub
818 08880e98 koutsoub
                GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
819 08880e98 koutsoub
                        @Override
820 08880e98 koutsoub
                        public void onComplete() {
821 08880e98 koutsoub
                                final UserSearchResource result = getResult();
822 08880e98 koutsoub
                                for (UserResource user : result.getUsers()){
823 08880e98 koutsoub
                                        String username = user.getUsername();
824 08880e98 koutsoub
                                        String userFullName = user.getName();
825 08880e98 koutsoub
                                        GSS.get().putUserToMap(username, userFullName);
826 08880e98 koutsoub
                                        if(filesInput.size() >= 1){
827 08880e98 koutsoub
                                                filesInput.remove(filesInput.get(0));
828 08880e98 koutsoub
                                                if(filesInput.isEmpty()){
829 9b11a5ca koutsoub
                                                        showCellTable(false);
830 08880e98 koutsoub
                                                }else{
831 08880e98 koutsoub
                                                        handleFullNames(filesInput);
832 08880e98 koutsoub
                                                }                                                                                                
833 08880e98 koutsoub
                                        }                                                                        
834 08880e98 koutsoub
                                }
835 08880e98 koutsoub
                        }
836 08880e98 koutsoub
                        @Override
837 08880e98 koutsoub
                        public void onError(Throwable t) {
838 08880e98 koutsoub
                                GWT.log("", t);
839 08880e98 koutsoub
                                GSS.get().displayError("Unable to fetch user's full name from the given username " + filesInput.get(0).getOwner());
840 08880e98 koutsoub
                                if(filesInput.size() >= 1){
841 08880e98 koutsoub
                                        filesInput.remove(filesInput.get(0));
842 08880e98 koutsoub
                                        handleFullNames(filesInput);                                        
843 08880e98 koutsoub
                                }
844 08880e98 koutsoub
                        }
845 08880e98 koutsoub
                };
846 08880e98 koutsoub
                DeferredCommand.addCommand(gg);
847 08880e98 koutsoub
        
848 08880e98 koutsoub
        }
849 08880e98 koutsoub
        /**
850 08880e98 koutsoub
         * Shows the files in the cellTable 
851 08880e98 koutsoub
         */
852 08880e98 koutsoub
853 9b11a5ca koutsoub
        private void showCellTable(boolean update){
854 f0cd9b29 koutsoub
                if(celltable.getRowCount()>GSS.VISIBLE_FILE_COUNT){
855 08880e98 koutsoub
                        pager.setVisible(true);
856 9b11a5ca koutsoub
                        pagerTop.setVisible(true);
857 08880e98 koutsoub
                }
858 08880e98 koutsoub
                else{
859 08880e98 koutsoub
                        pager.setVisible(false);
860 9b11a5ca koutsoub
                        pagerTop.setVisible(false);
861 08880e98 koutsoub
                }
862 9b11a5ca koutsoub
                if(update)
863 9b11a5ca koutsoub
                        provider.onRangeChanged(celltable);
864 08880e98 koutsoub
                celltable.redrawHeaders();                
865 08880e98 koutsoub
        }
866 9b11a5ca koutsoub
        
867 9b11a5ca koutsoub
        
868 9b11a5ca koutsoub
        /**
869 9b11a5ca koutsoub
         * Retrieve the lastQuery.
870 9b11a5ca koutsoub
         *
871 9b11a5ca koutsoub
         * @return the lastQuery
872 9b11a5ca koutsoub
         */
873 9b11a5ca koutsoub
        public String getLastQuery() {
874 9b11a5ca koutsoub
                return lastQuery;
875 9b11a5ca koutsoub
        }
876 9b11a5ca koutsoub
        
877 9b11a5ca koutsoub
        
878 9b11a5ca koutsoub
        /**
879 9b11a5ca koutsoub
         * Modify the lastQuery.
880 9b11a5ca koutsoub
         *
881 9b11a5ca koutsoub
         * @param lastQuery the lastQuery to set
882 9b11a5ca koutsoub
         */
883 9b11a5ca koutsoub
        public void setLastQuery(String lastQuery) {
884 9b11a5ca koutsoub
                this.lastQuery = lastQuery;
885 9b11a5ca koutsoub
        }
886 9b11a5ca koutsoub
        
887 9b11a5ca koutsoub
        class SearchDataProvider extends AsyncDataProvider<FileResource>{
888 9b11a5ca koutsoub
889 9b11a5ca koutsoub
                @Override
890 9b11a5ca koutsoub
                protected void onRangeChanged(final HasData<FileResource> display) {
891 9b11a5ca koutsoub
                        final int start = display.getVisibleRange().getStart();
892 9b11a5ca koutsoub
                        final GSS app = GSS.get();
893 9b11a5ca koutsoub
                        if(getLastQuery()==null||getLastQuery().equals("")){
894 9b11a5ca koutsoub
                                display.setRowCount(0,true);
895 9b11a5ca koutsoub
                                return;
896 9b11a5ca koutsoub
                                
897 9b11a5ca koutsoub
                        }
898 9b11a5ca koutsoub
                        GetCommand<SearchResource> eg = new GetCommand<SearchResource>(SearchResource.class,
899 9b11a5ca koutsoub
                                                app.getApiPath() + "search/" +URL.encodeComponent(getLastQuery())+"?start="+start, null) {
900 9b11a5ca koutsoub
901 9b11a5ca koutsoub
                                @Override
902 9b11a5ca koutsoub
                                public void onComplete() {
903 9b11a5ca koutsoub
                                        SearchResource s = getResult();
904 9b11a5ca koutsoub
                                        display.setRowCount(s.getSize(),true);
905 9b11a5ca koutsoub
                                        display.setRowData(start, s.getFiles());
906 9b11a5ca koutsoub
                                        setFiles(s.getFiles());
907 9b11a5ca koutsoub
                                        update(true);
908 9b11a5ca koutsoub
                                        
909 9b11a5ca koutsoub
                                }
910 08880e98 koutsoub
911 9b11a5ca koutsoub
                                @Override
912 9b11a5ca koutsoub
                                public void onError(Throwable t) {
913 9b11a5ca koutsoub
                                        if(t instanceof RestException)
914 9b11a5ca koutsoub
                                                app.displayError("Unable to perform search:"+((RestException)t).getHttpStatusText());
915 9b11a5ca koutsoub
                                        else
916 9b11a5ca koutsoub
                                                app.displayError("System error performing search:"+t.getMessage());
917 9b11a5ca koutsoub
                                        GWT.log("",t);
918 9b11a5ca koutsoub
                                        updateFileCache("");
919 9b11a5ca koutsoub
                                }
920 08880e98 koutsoub
921 9b11a5ca koutsoub
                        };
922 9b11a5ca koutsoub
                        DeferredCommand.addCommand(eg);
923 9b11a5ca koutsoub
                        
924 9b11a5ca koutsoub
                }
925 9b11a5ca koutsoub
                
926 9b11a5ca koutsoub
        }
927 023f6f1e Panagiotis Astithas
928 023f6f1e Panagiotis Astithas
}