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