Updated copyright notice
[pithos] / web_client / src / gr / grnet / pithos / web / client / AbstractPropertiesDialog.java
1 /*\r
2  *  Copyright (c) 2011 Greek Research and Technology Network\r
3  */\r
4 package gr.grnet.pithos.web.client;\r
5 \r
6 import gr.grnet.pithos.web.client.rest.GetCommand;\r
7 import gr.grnet.pithos.web.client.rest.resource.TagsResource;\r
8 \r
9 import java.util.List;\r
10 \r
11 import com.google.gwt.core.client.GWT;\r
12 import com.google.gwt.dom.client.NativeEvent;\r
13 import com.google.gwt.event.dom.client.ClickEvent;\r
14 import com.google.gwt.event.dom.client.ClickHandler;\r
15 import com.google.gwt.event.dom.client.KeyCodes;\r
16 import com.google.gwt.user.client.DeferredCommand;\r
17 import com.google.gwt.user.client.Event.NativePreviewEvent;\r
18 import com.google.gwt.user.client.ui.Anchor;\r
19 import com.google.gwt.user.client.ui.DialogBox;\r
20 import com.google.gwt.user.client.ui.FlowPanel;\r
21 import com.google.gwt.user.client.ui.Label;\r
22 import com.google.gwt.user.client.ui.TabPanel;\r
23 import com.google.gwt.user.client.ui.TextBox;\r
24 \r
25 /**\r
26  * Abstract class, parent of all 'File properties' dialog boxes.\r
27  *\r
28  */\r
29 public abstract class AbstractPropertiesDialog extends DialogBox {\r
30 \r
31         protected static final String MULTIPLE_VALUES_TEXT = "(Multiple values)";\r
32 \r
33         /**\r
34          * Text box with the tags associated with the file\r
35          */\r
36         protected TextBox tags = new TextBox();\r
37 \r
38         protected String initialTagText;\r
39 \r
40         /**\r
41          * A FlowPanel with all user tags\r
42          */\r
43         protected FlowPanel allTagsContent;\r
44 \r
45 \r
46         protected TabPanel inner = null;\r
47 \r
48         /**\r
49          * The widget's constructor.\r
50          *\r
51          */\r
52         public AbstractPropertiesDialog() {\r
53 \r
54                 // Enable IE selection for the dialog (must disable it upon closing it)\r
55                 GSS.enableIESelection();\r
56 \r
57                 setAnimationEnabled(true);\r
58 \r
59         }\r
60         /**\r
61          * Retrieves all user tags from the server and updates the FlowPanel\r
62          *\r
63          * @param userId\r
64          */\r
65         protected void updateTags() {\r
66                 GetCommand<TagsResource> tc = new GetCommand<TagsResource>(TagsResource.class, GSS.get().getCurrentUserResource().getTagsPath(),null) {\r
67 \r
68                         @Override\r
69                         public void onComplete() {\r
70                                 allTagsContent.clear();\r
71                                 TagsResource tagr = getResult();\r
72                                 List<String> userTags = tagr.getTags();\r
73                                 Anchor tag = null;\r
74                                 for(String usrTag : userTags){\r
75                                         tag = new Anchor(usrTag.toString(), false);\r
76                                         tag.addStyleName("pithos-tag");\r
77                                         allTagsContent.add(tag);\r
78                                         Label separator = new Label(", ");\r
79                                         separator.addStyleName("pithos-tag");\r
80                                         allTagsContent.add(separator);\r
81                                         tag.addClickHandler( new ClickHandler() {\r
82 \r
83                                                 @Override\r
84                                                 public void onClick(ClickEvent event) {\r
85                                                         String existing = tags.getText();\r
86                                                         if (MULTIPLE_VALUES_TEXT.equals(existing)) existing = "";\r
87                                                         String newTag = ((Anchor) event.getSource()).getText().trim();\r
88                                                         // insert the new tag only if it is not in the list\r
89                                                         // already\r
90                                                         if (existing.indexOf(newTag) == -1 && !existing.trim().endsWith(newTag))\r
91                                                                 tags.setText(existing.trim()\r
92                                                                                         + (existing.length() > 0 ? ", " : "")\r
93                                                                                         + newTag);\r
94                                                 }\r
95                                         });\r
96                                 }\r
97                         }\r
98 \r
99                         @Override\r
100                         public void onError(Throwable t) {\r
101                                 GWT.log("", t);\r
102                                 GSS.get().displayError("Unable to fetch user tags");\r
103                         }\r
104                 };\r
105                 DeferredCommand.addCommand(tc);\r
106 \r
107         }\r
108 \r
109         /**\r
110          * Accepts any change and updates the file\r
111          *\r
112          */\r
113         protected abstract void accept();\r
114 \r
115         @Override\r
116         @SuppressWarnings("fallthrough")\r
117         protected void onPreviewNativeEvent(NativePreviewEvent preview) {\r
118             super.onPreviewNativeEvent(preview);\r
119 \r
120             NativeEvent evt = preview.getNativeEvent();\r
121             if (evt.getType().equals("keydown"))\r
122                         // Use the popup's key preview hooks to close the dialog when either\r
123                           // enter or escape is pressed.\r
124                           switch (evt.getKeyCode()) {\r
125                             case KeyCodes.KEY_ENTER:\r
126                                 accept();\r
127                             case KeyCodes.KEY_ESCAPE:\r
128                               closeDialog();\r
129                               break;\r
130                           }\r
131           }\r
132 \r
133 \r
134 \r
135         public void selectTab(int _tab) {\r
136                 inner.selectTab(_tab);\r
137         }\r
138 \r
139 \r
140         /**\r
141          * Enables IE selection prevention and hides the dialog\r
142          * (we disable the prevention on creation of the dialog)\r
143          */\r
144         public void closeDialog() {\r
145                 GSS.preventIESelection();\r
146                 hide();\r
147         }\r
148 \r
149 }\r