display tags issues - another try
[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.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                                 Label label = null;\r
91                                 for(String usrTag : userTags){\r
92                                         tag = new Anchor(usrTag.toString(), false);\r
93                                         tag.addStyleName("gss-tag");\r
94                                         allTagsContent.add(tag);\r
95                                         Label separator = new Label(", ");\r
96                                         separator.addStyleName("gss-tag");\r
97                                         allTagsContent.add(separator);\r
98                                         tag.addClickHandler( new ClickHandler() {\r
99 \r
100                                                 @Override\r
101                                                 public void onClick(ClickEvent event) {\r
102                                                         String existing = tags.getText();\r
103                                                         if (MULTIPLE_VALUES_TEXT.equals(existing)) existing = "";\r
104                                                         String newTag = ((Anchor) event.getSource()).getText().trim();\r
105                                                         // insert the new tag only if it is not in the list\r
106                                                         // already\r
107                                                         if (existing.indexOf(newTag) == -1 && !existing.trim().endsWith(newTag))\r
108                                                                 tags.setText(existing.trim()\r
109                                                                                         + (existing.length() > 0 ? ", " : "")\r
110                                                                                         + newTag);\r
111                                                 }\r
112                                         });\r
113                                 }\r
114 \r
115                                 allTagsContent.add(label);\r
116                         }\r
117 \r
118                         @Override\r
119                         public void onError(Throwable t) {\r
120                                 GWT.log("", t);\r
121                                 GSS.get().displayError("Unable to fetch user tags");\r
122                         }\r
123                 };\r
124                 DeferredCommand.addCommand(tc);\r
125 \r
126         }\r
127 \r
128         /**\r
129          * Accepts any change and updates the file\r
130          *\r
131          */\r
132         protected abstract void accept();\r
133 \r
134         @Override\r
135         @SuppressWarnings("fallthrough")\r
136         protected void onPreviewNativeEvent(NativePreviewEvent preview) {\r
137             super.onPreviewNativeEvent(preview);\r
138 \r
139             NativeEvent evt = preview.getNativeEvent();\r
140             if (evt.getType().equals("keydown"))\r
141                         // Use the popup's key preview hooks to close the dialog when either\r
142                           // enter or escape is pressed.\r
143                           switch (evt.getKeyCode()) {\r
144                             case KeyCodes.KEY_ENTER:\r
145                                 accept();\r
146                             case KeyCodes.KEY_ESCAPE:\r
147                               closeDialog();\r
148                               break;\r
149                           }\r
150           }\r
151 \r
152 \r
153 \r
154         public void selectTab(int _tab) {\r
155                 inner.selectTab(_tab);\r
156         }\r
157 \r
158 \r
159         /**\r
160          * Enables IE selection prevention and hides the dialog\r
161          * (we disable the prevention on creation of the dialog)\r
162          */\r
163         public void closeDialog() {\r
164                 GSS.preventIESelection();\r
165                 hide();\r
166         }\r
167 \r
168 }\r