Added ids in homefolder(tree.homeFolder), Trash(tree.Trash), My Shared (tree.myShared...
[pithos] / src / gr / ebs / gss / client / AbstractPropertiesDialog.java
1 /*\r
2  * Copyright 2007, 2008, 2009, 2010 Electronic Business Systems Ltd.\r
3  *\r
4  * This file is part of GSS.\r
5  *\r
6  * GSS is free software: you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation, either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * GSS is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.\r
18  */\r
19 package gr.ebs.gss.client;\r
20 \r
21 import gr.ebs.gss.client.rest.GetCommand;\r
22 import gr.ebs.gss.client.rest.resource.TagsResource;\r
23 \r
24 import java.util.List;\r
25 \r
26 import com.google.gwt.core.client.GWT;\r
27 import com.google.gwt.dom.client.NativeEvent;\r
28 import com.google.gwt.event.dom.client.ClickEvent;\r
29 import com.google.gwt.event.dom.client.ClickHandler;\r
30 import com.google.gwt.event.dom.client.KeyCodes;\r
31 import com.google.gwt.user.client.DeferredCommand;\r
32 import com.google.gwt.user.client.Event.NativePreviewEvent;\r
33 import com.google.gwt.user.client.ui.Anchor;\r
34 import com.google.gwt.user.client.ui.DialogBox;\r
35 import com.google.gwt.user.client.ui.FlowPanel;\r
36 import com.google.gwt.user.client.ui.Label;\r
37 import com.google.gwt.user.client.ui.TabPanel;\r
38 import com.google.gwt.user.client.ui.TextBox;\r
39 \r
40 /**\r
41  * Abstract class, parent of all 'File properties' dialog boxes.\r
42  *\r
43  * @author droutsis\r
44  */\r
45 public abstract class AbstractPropertiesDialog extends DialogBox {\r
46 \r
47         protected static final String MULTIPLE_VALUES_TEXT = "(Multiple values)";\r
48 \r
49         /**\r
50          * Text box with the tags associated with the file\r
51          */\r
52         protected TextBox tags = new TextBox();\r
53 \r
54         protected String initialTagText;\r
55 \r
56         /**\r
57          * A FlowPanel with all user tags\r
58          */\r
59         protected FlowPanel allTagsContent;\r
60 \r
61 \r
62         protected TabPanel inner = null;\r
63 \r
64         /**\r
65          * The widget's constructor.\r
66          *\r
67          */\r
68         public AbstractPropertiesDialog() {\r
69 \r
70                 // Enable IE selection for the dialog (must disable it upon closing it)\r
71                 GSS.enableIESelection();\r
72 \r
73                 setAnimationEnabled(true);\r
74 \r
75         }\r
76         /**\r
77          * Retrieves all user tags from the server and updates the FlowPanel\r
78          *\r
79          * @param userId\r
80          */\r
81         protected void updateTags() {\r
82                 GetCommand<TagsResource> tc = new GetCommand<TagsResource>(TagsResource.class, GSS.get().getCurrentUserResource().getTagsPath(),null) {\r
83 \r
84                         @Override\r
85                         public void onComplete() {\r
86                                 allTagsContent.clear();\r
87                                 TagsResource tagr = getResult();\r
88                                 List<String> userTags = tagr.getTags();\r
89                                 Anchor tag = null;\r
90                                 for(String usrTag : userTags){\r
91                                         tag = new Anchor(usrTag.toString(), false);\r
92                                         tag.addStyleName("gss-tag");\r
93                                         allTagsContent.add(tag);\r
94                                         Label separator = new Label(", ");\r
95                                         separator.addStyleName("gss-tag");\r
96                                         allTagsContent.add(separator);\r
97                                         tag.addClickHandler( new ClickHandler() {\r
98 \r
99                                                 @Override\r
100                                                 public void onClick(ClickEvent event) {\r
101                                                         String existing = tags.getText();\r
102                                                         if (MULTIPLE_VALUES_TEXT.equals(existing)) existing = "";\r
103                                                         String newTag = ((Anchor) event.getSource()).getText().trim();\r
104                                                         // insert the new tag only if it is not in the list\r
105                                                         // already\r
106                                                         if (existing.indexOf(newTag) == -1 && !existing.trim().endsWith(newTag))\r
107                                                                 tags.setText(existing.trim()\r
108                                                                                         + (existing.length() > 0 ? ", " : "")\r
109                                                                                         + newTag);\r
110                                                 }\r
111                                         });\r
112                                 }\r
113                         }\r
114 \r
115                         @Override\r
116                         public void onError(Throwable t) {\r
117                                 GWT.log("", t);\r
118                                 GSS.get().displayError("Unable to fetch user tags");\r
119                         }\r
120                 };\r
121                 DeferredCommand.addCommand(tc);\r
122 \r
123         }\r
124 \r
125         /**\r
126          * Accepts any change and updates the file\r
127          *\r
128          */\r
129         protected abstract void accept();\r
130 \r
131         @Override\r
132         @SuppressWarnings("fallthrough")\r
133         protected void onPreviewNativeEvent(NativePreviewEvent preview) {\r
134             super.onPreviewNativeEvent(preview);\r
135 \r
136             NativeEvent evt = preview.getNativeEvent();\r
137             if (evt.getType().equals("keydown"))\r
138                         // Use the popup's key preview hooks to close the dialog when either\r
139                           // enter or escape is pressed.\r
140                           switch (evt.getKeyCode()) {\r
141                             case KeyCodes.KEY_ENTER:\r
142                                 accept();\r
143                             case KeyCodes.KEY_ESCAPE:\r
144                               closeDialog();\r
145                               break;\r
146                           }\r
147           }\r
148 \r
149 \r
150 \r
151         public void selectTab(int _tab) {\r
152                 inner.selectTab(_tab);\r
153         }\r
154 \r
155 \r
156         /**\r
157          * Enables IE selection prevention and hides the dialog\r
158          * (we disable the prevention on creation of the dialog)\r
159          */\r
160         public void closeDialog() {\r
161                 GSS.preventIESelection();\r
162                 hide();\r
163         }\r
164 \r
165 }\r