Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / PopupTree.java @ fc0fa492

History | View | Annotate | Download (9.6 kB)

1 14ad7326 pastith
/*
2 06b48563 Natasa Kapravelou
 * Copyright 2008, 2009, 2010 Electronic Business Systems Ltd.
3 06b48563 Natasa Kapravelou
 *
4 06b48563 Natasa Kapravelou
 * This file is part of GSS.
5 06b48563 Natasa Kapravelou
 *
6 06b48563 Natasa Kapravelou
 * GSS is free software: you can redistribute it and/or modify
7 06b48563 Natasa Kapravelou
 * it under the terms of the GNU General Public License as published by
8 06b48563 Natasa Kapravelou
 * the Free Software Foundation, either version 3 of the License, or
9 06b48563 Natasa Kapravelou
 * (at your option) any later version.
10 06b48563 Natasa Kapravelou
 *
11 06b48563 Natasa Kapravelou
 * GSS is distributed in the hope that it will be useful,
12 06b48563 Natasa Kapravelou
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 06b48563 Natasa Kapravelou
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 06b48563 Natasa Kapravelou
 * GNU General Public License for more details.
15 06b48563 Natasa Kapravelou
 *
16 06b48563 Natasa Kapravelou
 * You should have received a copy of the GNU General Public License
17 06b48563 Natasa Kapravelou
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 14ad7326 pastith
 */
19 14ad7326 pastith
package gr.ebs.gss.client;
20 14ad7326 pastith
21 14ad7326 pastith
import gr.ebs.gss.client.Folders.Images;
22 a52ea5e4 pastith
import gr.ebs.gss.client.dnd.DnDTreeItem;
23 9f712d7c koutsoub
import gr.ebs.gss.client.rest.resource.FolderResource;
24 9f712d7c koutsoub
import gr.ebs.gss.client.rest.resource.OtherUserResource;
25 a90cda9b Natasa Kapravelou
import gr.ebs.gss.client.rest.resource.OthersResource;
26 c5070ebe Natasa Kapravelou
import gr.ebs.gss.client.rest.resource.RestResource;
27 a90cda9b Natasa Kapravelou
import gr.ebs.gss.client.rest.resource.SharedResource;
28 a90cda9b Natasa Kapravelou
import gr.ebs.gss.client.rest.resource.TrashResource;
29 14ad7326 pastith
30 08e5b7c7 Natasa Kapravelou
import java.util.Iterator;
31 08e5b7c7 Natasa Kapravelou
32 afd3a0ef Giannis Koutsoubos
import com.google.gwt.dom.client.NativeEvent;
33 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.ContextMenuEvent;
34 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.ContextMenuHandler;
35 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.KeyCodes;
36 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.logical.shared.OpenEvent;
37 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.logical.shared.OpenHandler;
38 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.logical.shared.SelectionEvent;
39 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.logical.shared.SelectionHandler;
40 14ad7326 pastith
import com.google.gwt.user.client.DOM;
41 14ad7326 pastith
import com.google.gwt.user.client.Event;
42 14ad7326 pastith
import com.google.gwt.user.client.ui.Tree;
43 14ad7326 pastith
import com.google.gwt.user.client.ui.TreeItem;
44 14ad7326 pastith
45 14ad7326 pastith
/**
46 14ad7326 pastith
 * @author kman
47 14ad7326 pastith
 */
48 14ad7326 pastith
public class PopupTree extends Tree {
49 14ad7326 pastith
50 14ad7326 pastith
        private FolderContextMenu menu;
51 14ad7326 pastith
52 14ad7326 pastith
        private Images images;
53 14ad7326 pastith
54 14ad7326 pastith
        private boolean ctrlKeyPressed = false;
55 14ad7326 pastith
56 14ad7326 pastith
        private boolean leftClicked = false;
57 14ad7326 pastith
58 14ad7326 pastith
        private boolean rightClicked = false;
59 14ad7326 pastith
60 14ad7326 pastith
        private TreeItem treeSelectedItem = null;
61 14ad7326 pastith
62 aa07a34b Panagiotis Astithas
        public PopupTree(Images theImages) {
63 aa07a34b Panagiotis Astithas
                super(theImages);
64 aa07a34b Panagiotis Astithas
                images = theImages;
65 14ad7326 pastith
                sinkEvents(Event.ONCONTEXTMENU);
66 14ad7326 pastith
                sinkEvents(Event.ONMOUSEUP);
67 06b48563 Natasa Kapravelou
//                sinkEvents(Event.ONMOUSEDOWN);
68 ff8eb545 Giannis Koutsoubos
69 afd3a0ef Giannis Koutsoubos
                addSelectionHandler(new SelectionHandler<TreeItem>() {
70 14ad7326 pastith
71 afd3a0ef Giannis Koutsoubos
                        @Override
72 afd3a0ef Giannis Koutsoubos
                        public void onSelection(SelectionEvent<TreeItem> event) {
73 98b59163 Natasa Kapravelou
                                TreeItem treeItem = event.getSelectedItem();
74 98b59163 Natasa Kapravelou
                                processItemSelected(treeItem);
75 c24efcc9 Natasa Kapravelou
                                String path = GSS.get().getApiPath() + GSS.get().getCurrentUserResource().getUsername()+ "/";
76 98b59163 Natasa Kapravelou
                                String constructedUri = ((RestResource) GSS.get().getFolders().getCurrent().getUserObject()).constructUri(treeItem, path);
77 98b59163 Natasa Kapravelou
                                GSS.get().updateHistory(constructedUri);
78 2487ac7b Natasa Kapravelou
                        }
79 afd3a0ef Giannis Koutsoubos
                });
80 8c999b8c Natasa Kapravelou
81 afd3a0ef Giannis Koutsoubos
                addOpenHandler(new OpenHandler<TreeItem>() {
82 afd3a0ef Giannis Koutsoubos
83 afd3a0ef Giannis Koutsoubos
                        @Override
84 afd3a0ef Giannis Koutsoubos
                        public void onOpen(OpenEvent<TreeItem> event) {
85 afd3a0ef Giannis Koutsoubos
                                TreeItem item = event.getTarget();
86 8c999b8c Natasa Kapravelou
                                if (item != null && item.getState())
87 afd3a0ef Giannis Koutsoubos
                                        GSS.get().getFolders().update(item);
88 14ad7326 pastith
89 14ad7326 pastith
                        }
90 14ad7326 pastith
                });
91 afd3a0ef Giannis Koutsoubos
                addHandler(new ContextMenuHandler() {
92 afd3a0ef Giannis Koutsoubos
93 afd3a0ef Giannis Koutsoubos
                        @Override
94 afd3a0ef Giannis Koutsoubos
                        public void onContextMenu(ContextMenuEvent event) {
95 8c999b8c Natasa Kapravelou
                                TreeItem item = getSelectedItem();
96 8c999b8c Natasa Kapravelou
                                if (item != null) {
97 afd3a0ef Giannis Koutsoubos
                                        int left = item.getAbsoluteLeft() + 40;
98 afd3a0ef Giannis Koutsoubos
                                        int top = item.getAbsoluteTop() + 20;
99 afd3a0ef Giannis Koutsoubos
                                        showPopup(left, top);
100 afd3a0ef Giannis Koutsoubos
                                }
101 afd3a0ef Giannis Koutsoubos
102 afd3a0ef Giannis Koutsoubos
                        }
103 afd3a0ef Giannis Koutsoubos
                }, ContextMenuEvent.getType());
104 8c999b8c Natasa Kapravelou
                // DOM.setStyleAttribute(getElement(), "position", "static");
105 a52ea5e4 pastith
106 14ad7326 pastith
        }
107 14ad7326 pastith
108 aa07a34b Panagiotis Astithas
        @Override
109 14ad7326 pastith
        public void onBrowserEvent(Event event) {
110 8c999b8c Natasa Kapravelou
                if (DOM.eventGetType(event) == Event.ONCLICK)
111 8c999b8c Natasa Kapravelou
                        return;
112 a52ea5e4 pastith
113 14ad7326 pastith
                switch (DOM.eventGetType(event)) {
114 14ad7326 pastith
                        case Event.ONKEYDOWN:
115 14ad7326 pastith
                                int key = DOM.eventGetKeyCode(event);
116 afd3a0ef Giannis Koutsoubos
                                if (key == KeyCodes.KEY_CTRL)
117 14ad7326 pastith
                                        ctrlKeyPressed = true;
118 14ad7326 pastith
                                break;
119 14ad7326 pastith
120 14ad7326 pastith
                        case Event.ONKEYUP:
121 14ad7326 pastith
                                key = DOM.eventGetKeyCode(event);
122 afd3a0ef Giannis Koutsoubos
                                if (key == KeyCodes.KEY_CTRL)
123 14ad7326 pastith
                                        ctrlKeyPressed = false;
124 14ad7326 pastith
                                break;
125 14ad7326 pastith
126 14ad7326 pastith
                        case Event.ONMOUSEDOWN:
127 afd3a0ef Giannis Koutsoubos
                                if (DOM.eventGetButton(event) == NativeEvent.BUTTON_RIGHT)
128 14ad7326 pastith
                                        rightClicked = true;
129 afd3a0ef Giannis Koutsoubos
                                else if (DOM.eventGetButton(event) == NativeEvent.BUTTON_LEFT)
130 14ad7326 pastith
                                        leftClicked = true;
131 14ad7326 pastith
                                break;
132 14ad7326 pastith
133 14ad7326 pastith
                        case Event.ONMOUSEUP:
134 afd3a0ef Giannis Koutsoubos
                                if (DOM.eventGetButton(event) == NativeEvent.BUTTON_RIGHT)
135 14ad7326 pastith
                                        rightClicked = false;
136 afd3a0ef Giannis Koutsoubos
                                else if (DOM.eventGetButton(event) == NativeEvent.BUTTON_LEFT)
137 14ad7326 pastith
                                        leftClicked = false;
138 14ad7326 pastith
                                break;
139 14ad7326 pastith
                }
140 14ad7326 pastith
141 14ad7326 pastith
                super.onBrowserEvent(event);
142 14ad7326 pastith
        }
143 14ad7326 pastith
144 14ad7326 pastith
        protected void showPopup(final int x, final int y) {
145 14ad7326 pastith
                if (treeSelectedItem == null)
146 14ad7326 pastith
                        return;
147 8c999b8c Natasa Kapravelou
                if (menu != null)
148 14ad7326 pastith
                        menu.hide();
149 14ad7326 pastith
                menu = new FolderContextMenu(images);
150 14ad7326 pastith
                menu.setPopupPosition(x, y);
151 14ad7326 pastith
                menu.show();
152 14ad7326 pastith
        }
153 14ad7326 pastith
154 6ba2b21a Panagiotis Astithas
        public void processItemSelected(TreeItem item) {
155 0174fdb2 Natasa Kapravelou
                 if (GSS.get().getCurrentSelection() == null || !GSS.get().getCurrentSelection().equals(item.getUserObject()))
156 a52ea5e4 pastith
                        GSS.get().setCurrentSelection(item.getUserObject());
157 8c999b8c Natasa Kapravelou
                if (!GSS.get().isFileListShowing())
158 14ad7326 pastith
                        GSS.get().showFileList();
159 14ad7326 pastith
160 8c999b8c Natasa Kapravelou
                // refresh Others Shared Node
161 0174fdb2 Natasa Kapravelou
                if (GSS.get().getFolders().isOthersShared(item)){
162 a52ea5e4 pastith
                        GSS.get().getFolders().update(item);
163 b42845d6 koutsoub
                        GSS.get().showFileList();
164 b42845d6 koutsoub
                }
165 14ad7326 pastith
166 14ad7326 pastith
                if (!item.equals(treeSelectedItem))
167 14ad7326 pastith
                        processSelection(item);
168 14ad7326 pastith
                if (rightClicked) {
169 8c999b8c Natasa Kapravelou
                        rightClicked = false;
170 14ad7326 pastith
                        int left = item.getAbsoluteLeft() + 40;
171 14ad7326 pastith
                        int top = item.getAbsoluteTop() + 20;
172 14ad7326 pastith
                        showPopup(left, top);
173 14ad7326 pastith
                } else if (leftClicked && ctrlKeyPressed) {
174 8c999b8c Natasa Kapravelou
                        leftClicked = false;
175 8c999b8c Natasa Kapravelou
                        ctrlKeyPressed = false;
176 14ad7326 pastith
                        int left = item.getAbsoluteLeft() + 40;
177 14ad7326 pastith
                        int top = item.getAbsoluteTop() + 20;
178 14ad7326 pastith
                        showPopup(left, top);
179 14ad7326 pastith
                }
180 14ad7326 pastith
        }
181 14ad7326 pastith
182 8c999b8c Natasa Kapravelou
        public void clearSelection() {
183 14ad7326 pastith
                if (treeSelectedItem != null)
184 8c999b8c Natasa Kapravelou
                        ((DnDTreeItem) treeSelectedItem).getContent().removeStyleName("gss-SelectedRow");
185 8c999b8c Natasa Kapravelou
                // treeSelectedItem.getWidget().removeStyleName("gss-SelectedRow");
186 ff8eb545 Giannis Koutsoubos
187 14ad7326 pastith
                treeSelectedItem = null;
188 a52ea5e4 pastith
                setSelectedItem(null, true);
189 14ad7326 pastith
                GSS.get().setCurrentSelection(null);
190 14ad7326 pastith
        }
191 14ad7326 pastith
192 14ad7326 pastith
        private void processSelection(TreeItem item) {
193 14ad7326 pastith
                if (treeSelectedItem != null) {
194 14ad7326 pastith
                        GSS.get().setCurrentSelection(null);
195 8c999b8c Natasa Kapravelou
                        // treeSelectedItem.getWidget().removeStyleName("gss-SelectedRow");
196 8c999b8c Natasa Kapravelou
                        ((DnDTreeItem) treeSelectedItem).getContent().removeStyleName("gss-SelectedRow");
197 14ad7326 pastith
                        treeSelectedItem = null;
198 a52ea5e4 pastith
                        setSelectedItem(null, true);
199 14ad7326 pastith
                }
200 14ad7326 pastith
                treeSelectedItem = item;
201 a52ea5e4 pastith
                setSelectedItem(item, true);
202 8c999b8c Natasa Kapravelou
                // ensureSelectedItemVisible();
203 8c999b8c Natasa Kapravelou
                if (((DnDTreeItem) item).getFolderResource() != null)
204 8c999b8c Natasa Kapravelou
                        GSS.get().setCurrentSelection(((DnDTreeItem) item).getFolderResource());
205 9f712d7c koutsoub
                if (item.getUserObject() instanceof FolderResource)
206 14ad7326 pastith
                        GSS.get().setCurrentSelection(item.getUserObject());
207 8c999b8c Natasa Kapravelou
                else if (item.getUserObject() instanceof OtherUserResource)
208 14ad7326 pastith
                        GSS.get().setCurrentSelection(item.getUserObject());
209 8c999b8c Natasa Kapravelou
                else if (GSS.get().getFolders().isTrash(item))
210 14ad7326 pastith
                        GSS.get().setCurrentSelection(null);
211 8c999b8c Natasa Kapravelou
                // item.getWidget().addStyleName("gss-SelectedRow");
212 8c999b8c Natasa Kapravelou
                ((DnDTreeItem) item).getContent().addStyleName("gss-SelectedRow");
213 8c999b8c Natasa Kapravelou
                // if(GSS.get().getFolders().isFileItem(item)||GSS.get().getFolders().isTrashItem(item)||GSS.get().getFolders().isMySharedItem(item))
214 8c999b8c Natasa Kapravelou
                GSS.get().showFileList(true);
215 14ad7326 pastith
        }
216 14ad7326 pastith
217 14ad7326 pastith
        /**
218 14ad7326 pastith
         * Retrieve the selectedItem.
219 14ad7326 pastith
         *
220 14ad7326 pastith
         * @return the selectedItem
221 14ad7326 pastith
         */
222 14ad7326 pastith
        public TreeItem getTreeSelectedItem() {
223 14ad7326 pastith
                return treeSelectedItem;
224 14ad7326 pastith
        }
225 14ad7326 pastith
226 14ad7326 pastith
        /**
227 14ad7326 pastith
         * Modify the selectedItem.
228 14ad7326 pastith
         *
229 aa07a34b Panagiotis Astithas
         * @param newSelectedItem the selectedItem to set
230 14ad7326 pastith
         */
231 14ad7326 pastith
        public void setTreeSelectedItem(TreeItem newSelectedItem) {
232 14ad7326 pastith
                treeSelectedItem = newSelectedItem;
233 14ad7326 pastith
        }
234 08e5b7c7 Natasa Kapravelou
        /**
235 08e5b7c7 Natasa Kapravelou
         * Method that takes a folderName and finds the corresponding treeItem object and returns it
236 a90cda9b Natasa Kapravelou
         * @param historyToken
237 08e5b7c7 Natasa Kapravelou
         * @return treeItem
238 08e5b7c7 Natasa Kapravelou
         */
239 08e5b7c7 Natasa Kapravelou
240 a90cda9b Natasa Kapravelou
        public TreeItem getTreeItem (String historyToken){
241 2487ac7b Natasa Kapravelou
//                String historyTokenOriginal = historyToken.replace("+", " ");
242 a90cda9b Natasa Kapravelou
                String path = GSS.get().getApiPath() + GSS.get().getCurrentUserResource().getUsername()+ "/";
243 bddcde7b Natasa Kapravelou
                String rootText = DisplayHelper.trim(GSS.get().getFolders().getRootItem().getText());
244 bddcde7b Natasa Kapravelou
                String uriToGet = "Files/files/" + rootText;
245 08e5b7c7 Natasa Kapravelou
                Iterator<TreeItem> it = GSS.get().getFolders().getPopupTree().treeItemIterator() ;
246 08e5b7c7 Natasa Kapravelou
                while(it.hasNext()){
247 98b59163 Natasa Kapravelou
                        String constructedUri = "";
248 98b59163 Natasa Kapravelou
                        TreeItem treeItem = it.next();
249 98b59163 Natasa Kapravelou
                        if(treeItem.getUserObject() instanceof TrashResource){
250 98b59163 Natasa Kapravelou
                                TrashResource currentObject = (TrashResource) treeItem.getUserObject();
251 2487ac7b Natasa Kapravelou
                                constructedUri = constructedUri + currentObject.constructUri(treeItem,path);
252 a90cda9b Natasa Kapravelou
                        }
253 98b59163 Natasa Kapravelou
                        if(treeItem.getUserObject() instanceof SharedResource){
254 98b59163 Natasa Kapravelou
                                SharedResource currentObject = (SharedResource) treeItem.getUserObject();
255 2487ac7b Natasa Kapravelou
                                constructedUri = constructedUri + currentObject.constructUri(treeItem, path);
256 a90cda9b Natasa Kapravelou
                        }
257 98b59163 Natasa Kapravelou
                        if(treeItem.getUserObject() instanceof OthersResource){
258 98b59163 Natasa Kapravelou
                                OthersResource currentObject = (OthersResource) treeItem.getUserObject();
259 2487ac7b Natasa Kapravelou
                                constructedUri = constructedUri + currentObject.constructUri(treeItem, path);
260 a90cda9b Natasa Kapravelou
                        }
261 98b59163 Natasa Kapravelou
                        if(treeItem.getUserObject() instanceof OtherUserResource){
262 98b59163 Natasa Kapravelou
                                OtherUserResource currentObject = (OtherUserResource) treeItem.getUserObject();
263 2487ac7b Natasa Kapravelou
                                constructedUri = constructedUri + currentObject.constructUri(treeItem, path);
264 a90cda9b Natasa Kapravelou
                        }
265 98b59163 Natasa Kapravelou
                        if(treeItem.getUserObject() instanceof FolderResource){
266 98b59163 Natasa Kapravelou
                                FolderResource currentObject = (FolderResource) treeItem.getUserObject();
267 98b59163 Natasa Kapravelou
                                constructedUri = constructedUri + currentObject.constructUri(treeItem, path);
268 08e5b7c7 Natasa Kapravelou
                        }
269 2487ac7b Natasa Kapravelou
                        if(constructedUri.equals(historyToken))
270 98b59163 Natasa Kapravelou
                                return treeItem;
271 2487ac7b Natasa Kapravelou
                }
272 bddcde7b Natasa Kapravelou
                return expandHiddenItems(uriToGet);
273 a90cda9b Natasa Kapravelou
274 a90cda9b Natasa Kapravelou
        }
275 08e5b7c7 Natasa Kapravelou
276 2487ac7b Natasa Kapravelou
277 2487ac7b Natasa Kapravelou
        public TreeItem expandHiddenItems(String tokenInput){
278 bddcde7b Natasa Kapravelou
                //TODO
279 2487ac7b Natasa Kapravelou
                // this method handles objects that are not constructed during loading the application
280 bddcde7b Natasa Kapravelou
                // and when there's a need to seek them inside some non-expanded folder
281 2487ac7b Natasa Kapravelou
                // This method is not implemented yet and in case of a non-constructed object
282 bddcde7b Natasa Kapravelou
                // returns the home/root folder
283 2487ac7b Natasa Kapravelou
                // treeItem.getState() method returns always false so the OpenEvent.fire() method
284 2487ac7b Natasa Kapravelou
                // fails to expand the parent folder in the UI
285 2487ac7b Natasa Kapravelou
286 2487ac7b Natasa Kapravelou
                GSS.get().getFolders().getRootItem().getText();
287 2487ac7b Natasa Kapravelou
                TreeItem treeItem = getTreeItem(tokenInput);
288 2487ac7b Natasa Kapravelou
                OpenEvent.fire(GSS.get().getFolders().getPopupTree(), treeItem);
289 2487ac7b Natasa Kapravelou
                return treeItem;
290 2487ac7b Natasa Kapravelou
        }
291 df9802b9 Natasa Kapravelou
292 df9802b9 Natasa Kapravelou
293 14ad7326 pastith
}