moved towards gwt version 2.0 - updated dnd to version 3 -removed all deprecations
[pithos] / src / gr / ebs / gss / client / AbstractPropertiesDialog.java
1 /*\r
2  * Copyright 2007, 2008, 2009 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.Iterator;\r
25 import java.util.List;\r
26 \r
27 import com.google.gwt.core.client.GWT;\r
28 import com.google.gwt.dom.client.NativeEvent;\r
29 import com.google.gwt.event.dom.client.ClickEvent;\r
30 import com.google.gwt.event.dom.client.ClickHandler;\r
31 import com.google.gwt.event.dom.client.KeyCodes;\r
32 import com.google.gwt.user.client.DeferredCommand;\r
33 import com.google.gwt.user.client.Event.NativePreviewEvent;\r
34 import com.google.gwt.user.client.ui.Button;\r
35 import com.google.gwt.user.client.ui.DialogBox;\r
36 import com.google.gwt.user.client.ui.FlowPanel;\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         /**\r
78          * Retrieves all user tags from the server and updates the FlowPanel\r
79          *\r
80          * @param userId\r
81          */\r
82         protected void updateTags() {\r
83                 GetCommand<TagsResource> tc = new GetCommand<TagsResource>(TagsResource.class, GSS.get().getCurrentUserResource().getTagsPath()) {\r
84 \r
85                         @Override\r
86                         public void onComplete() {\r
87                                 allTagsContent.clear();\r
88                                 TagsResource tagr = getResult();\r
89                                 List<String> userTags = tagr.getTags();\r
90                                 Iterator t = userTags.iterator();\r
91                                 while (t.hasNext()) {\r
92                                         final Button tag = new Button((String) t.next(), new ClickHandler() {\r
93 \r
94                                                 @Override\r
95                                                 public void onClick(ClickEvent event) {\r
96                                                         String existing = tags.getText();\r
97                                                         if (MULTIPLE_VALUES_TEXT.equals(existing)) existing = "";\r
98                                                         String newTag = ((Button) event.getSource()).getText().trim();\r
99                                                         // insert the new tag only if it is not in the list\r
100                                                         // already\r
101                                                         if (existing.indexOf(newTag + ",") == -1 && !existing.trim().endsWith(newTag))\r
102                                                                 tags.setText(existing.trim() + (existing.length() > 0 ? ", " : "") + newTag);\r
103                                                 }\r
104                                         });\r
105                                         allTagsContent.add(tag);\r
106                                 }\r
107                         }\r
108 \r
109                         @Override\r
110                         public void onError(Throwable t) {\r
111                                 GWT.log("", t);\r
112                                 GSS.get().displayError("Unable to fetch user tags");\r
113                         }\r
114                 };\r
115                 DeferredCommand.addCommand(tc);\r
116 \r
117         }\r
118 \r
119         /**\r
120          * Accepts any change and updates the file\r
121          *\r
122          */\r
123         protected abstract void accept();\r
124 \r
125         @Override\r
126         @SuppressWarnings("fallthrough")\r
127         protected void onPreviewNativeEvent(NativePreviewEvent preview) {\r
128             super.onPreviewNativeEvent(preview);\r
129 \r
130             NativeEvent evt = preview.getNativeEvent();\r
131             if (evt.getType().equals("keydown"))\r
132                         // Use the popup's key preview hooks to close the dialog when either\r
133                           // enter or escape is pressed.\r
134                           switch (evt.getKeyCode()) {\r
135                             case KeyCodes.KEY_ENTER:\r
136                                 accept();\r
137                             case KeyCodes.KEY_ESCAPE:\r
138                               closeDialog();\r
139                               break;\r
140                           }\r
141           }\r
142 \r
143 \r
144 \r
145         public void selectTab(int _tab) {\r
146                 inner.selectTab(_tab);\r
147         }\r
148 \r
149 \r
150         /**\r
151          * Enables IE selection prevention and hides the dialog\r
152          * (we disable the prevention on creation of the dialog)\r
153          */\r
154         public void closeDialog() {\r
155                 GSS.preventIESelection();\r
156                 hide();\r
157         }\r
158 \r
159 }\r