Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / AbstractPropertiesDialog.java @ 50326f36

History | View | Annotate | Download (5.5 kB)

1 ab1eb3f8 Christos Stathis
/*
2 58777026 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 58777026 Christos Stathis
 *
4 58777026 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 58777026 Christos Stathis
 * without modification, are permitted provided that the following
6 58777026 Christos Stathis
 * conditions are met:
7 58777026 Christos Stathis
 *
8 58777026 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 58777026 Christos Stathis
 *      disclaimer.
11 58777026 Christos Stathis
 *
12 58777026 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 58777026 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 58777026 Christos Stathis
 *      provided with the distribution.
16 58777026 Christos Stathis
 *
17 58777026 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 58777026 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 58777026 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 58777026 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 58777026 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 58777026 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 58777026 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 58777026 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 58777026 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 58777026 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 58777026 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 58777026 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 58777026 Christos Stathis
 *
30 58777026 Christos Stathis
 * The views and conclusions contained in the software and
31 58777026 Christos Stathis
 * documentation are those of the authors and should not be
32 58777026 Christos Stathis
 * interpreted as representing official policies, either expressed
33 58777026 Christos Stathis
 * or implied, of GRNET S.A.
34 ab1eb3f8 Christos Stathis
 */
35 ab1eb3f8 Christos Stathis
package gr.grnet.pithos.web.client;
36 ab1eb3f8 Christos Stathis
37 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.GetCommand;
38 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.TagsResource;
39 ab1eb3f8 Christos Stathis
40 ab1eb3f8 Christos Stathis
import java.util.List;
41 ab1eb3f8 Christos Stathis
42 ab1eb3f8 Christos Stathis
import com.google.gwt.core.client.GWT;
43 ab1eb3f8 Christos Stathis
import com.google.gwt.dom.client.NativeEvent;
44 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
45 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
46 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.KeyCodes;
47 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.DeferredCommand;
48 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.Event.NativePreviewEvent;
49 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.Anchor;
50 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.DialogBox;
51 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.FlowPanel;
52 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.Label;
53 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.TabPanel;
54 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.TextBox;
55 ab1eb3f8 Christos Stathis
56 ab1eb3f8 Christos Stathis
/**
57 ab1eb3f8 Christos Stathis
 * Abstract class, parent of all 'File properties' dialog boxes.
58 ab1eb3f8 Christos Stathis
 *
59 ab1eb3f8 Christos Stathis
 */
60 ab1eb3f8 Christos Stathis
public abstract class AbstractPropertiesDialog extends DialogBox {
61 ab1eb3f8 Christos Stathis
62 ab1eb3f8 Christos Stathis
        protected static final String MULTIPLE_VALUES_TEXT = "(Multiple values)";
63 ab1eb3f8 Christos Stathis
64 ab1eb3f8 Christos Stathis
        /**
65 ab1eb3f8 Christos Stathis
         * Text box with the tags associated with the file
66 ab1eb3f8 Christos Stathis
         */
67 ab1eb3f8 Christos Stathis
        protected TextBox tags = new TextBox();
68 ab1eb3f8 Christos Stathis
69 ab1eb3f8 Christos Stathis
        protected String initialTagText;
70 ab1eb3f8 Christos Stathis
71 ab1eb3f8 Christos Stathis
        /**
72 ab1eb3f8 Christos Stathis
         * A FlowPanel with all user tags
73 ab1eb3f8 Christos Stathis
         */
74 ab1eb3f8 Christos Stathis
        protected FlowPanel allTagsContent;
75 ab1eb3f8 Christos Stathis
76 ab1eb3f8 Christos Stathis
77 ab1eb3f8 Christos Stathis
        protected TabPanel inner = null;
78 ab1eb3f8 Christos Stathis
79 ab1eb3f8 Christos Stathis
        /**
80 ab1eb3f8 Christos Stathis
         * The widget's constructor.
81 ab1eb3f8 Christos Stathis
         *
82 ab1eb3f8 Christos Stathis
         */
83 ab1eb3f8 Christos Stathis
        public AbstractPropertiesDialog() {
84 ab1eb3f8 Christos Stathis
85 ab1eb3f8 Christos Stathis
                // Enable IE selection for the dialog (must disable it upon closing it)
86 ab1eb3f8 Christos Stathis
                GSS.enableIESelection();
87 ab1eb3f8 Christos Stathis
88 ab1eb3f8 Christos Stathis
                setAnimationEnabled(true);
89 ab1eb3f8 Christos Stathis
90 ab1eb3f8 Christos Stathis
        }
91 ab1eb3f8 Christos Stathis
        /**
92 ab1eb3f8 Christos Stathis
         * Retrieves all user tags from the server and updates the FlowPanel
93 ab1eb3f8 Christos Stathis
         *
94 ab1eb3f8 Christos Stathis
         * @param userId
95 ab1eb3f8 Christos Stathis
         */
96 ab1eb3f8 Christos Stathis
        protected void updateTags() {
97 ab1eb3f8 Christos Stathis
                GetCommand<TagsResource> tc = new GetCommand<TagsResource>(TagsResource.class, GSS.get().getCurrentUserResource().getTagsPath(),null) {
98 ab1eb3f8 Christos Stathis
99 ab1eb3f8 Christos Stathis
                        @Override
100 ab1eb3f8 Christos Stathis
                        public void onComplete() {
101 ab1eb3f8 Christos Stathis
                                allTagsContent.clear();
102 ab1eb3f8 Christos Stathis
                                TagsResource tagr = getResult();
103 ab1eb3f8 Christos Stathis
                                List<String> userTags = tagr.getTags();
104 ab1eb3f8 Christos Stathis
                                Anchor tag = null;
105 ab1eb3f8 Christos Stathis
                                for(String usrTag : userTags){
106 ab1eb3f8 Christos Stathis
                                        tag = new Anchor(usrTag.toString(), false);
107 ab1eb3f8 Christos Stathis
                                        tag.addStyleName("pithos-tag");
108 ab1eb3f8 Christos Stathis
                                        allTagsContent.add(tag);
109 ab1eb3f8 Christos Stathis
                                        Label separator = new Label(", ");
110 ab1eb3f8 Christos Stathis
                                        separator.addStyleName("pithos-tag");
111 ab1eb3f8 Christos Stathis
                                        allTagsContent.add(separator);
112 ab1eb3f8 Christos Stathis
                                        tag.addClickHandler( new ClickHandler() {
113 ab1eb3f8 Christos Stathis
114 ab1eb3f8 Christos Stathis
                                                @Override
115 ab1eb3f8 Christos Stathis
                                                public void onClick(ClickEvent event) {
116 ab1eb3f8 Christos Stathis
                                                        String existing = tags.getText();
117 ab1eb3f8 Christos Stathis
                                                        if (MULTIPLE_VALUES_TEXT.equals(existing)) existing = "";
118 ab1eb3f8 Christos Stathis
                                                        String newTag = ((Anchor) event.getSource()).getText().trim();
119 ab1eb3f8 Christos Stathis
                                                        // insert the new tag only if it is not in the list
120 ab1eb3f8 Christos Stathis
                                                        // already
121 ab1eb3f8 Christos Stathis
                                                        if (existing.indexOf(newTag) == -1 && !existing.trim().endsWith(newTag))
122 ab1eb3f8 Christos Stathis
                                                                tags.setText(existing.trim()
123 ab1eb3f8 Christos Stathis
                                                                                        + (existing.length() > 0 ? ", " : "")
124 ab1eb3f8 Christos Stathis
                                                                                        + newTag);
125 ab1eb3f8 Christos Stathis
                                                }
126 ab1eb3f8 Christos Stathis
                                        });
127 ab1eb3f8 Christos Stathis
                                }
128 ab1eb3f8 Christos Stathis
                        }
129 ab1eb3f8 Christos Stathis
130 ab1eb3f8 Christos Stathis
                        @Override
131 ab1eb3f8 Christos Stathis
                        public void onError(Throwable t) {
132 ab1eb3f8 Christos Stathis
                                GWT.log("", t);
133 ab1eb3f8 Christos Stathis
                                GSS.get().displayError("Unable to fetch user tags");
134 ab1eb3f8 Christos Stathis
                        }
135 ab1eb3f8 Christos Stathis
                };
136 ab1eb3f8 Christos Stathis
                DeferredCommand.addCommand(tc);
137 ab1eb3f8 Christos Stathis
138 ab1eb3f8 Christos Stathis
        }
139 ab1eb3f8 Christos Stathis
140 ab1eb3f8 Christos Stathis
        /**
141 ab1eb3f8 Christos Stathis
         * Accepts any change and updates the file
142 ab1eb3f8 Christos Stathis
         *
143 ab1eb3f8 Christos Stathis
         */
144 ab1eb3f8 Christos Stathis
        protected abstract void accept();
145 ab1eb3f8 Christos Stathis
146 ab1eb3f8 Christos Stathis
        @Override
147 ab1eb3f8 Christos Stathis
        @SuppressWarnings("fallthrough")
148 ab1eb3f8 Christos Stathis
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
149 ab1eb3f8 Christos Stathis
            super.onPreviewNativeEvent(preview);
150 ab1eb3f8 Christos Stathis
151 ab1eb3f8 Christos Stathis
            NativeEvent evt = preview.getNativeEvent();
152 ab1eb3f8 Christos Stathis
            if (evt.getType().equals("keydown"))
153 ab1eb3f8 Christos Stathis
                        // Use the popup's key preview hooks to close the dialog when either
154 ab1eb3f8 Christos Stathis
                          // enter or escape is pressed.
155 ab1eb3f8 Christos Stathis
                          switch (evt.getKeyCode()) {
156 ab1eb3f8 Christos Stathis
                            case KeyCodes.KEY_ENTER:
157 ab1eb3f8 Christos Stathis
                                    accept();
158 ab1eb3f8 Christos Stathis
                            case KeyCodes.KEY_ESCAPE:
159 ab1eb3f8 Christos Stathis
                              closeDialog();
160 ab1eb3f8 Christos Stathis
                              break;
161 ab1eb3f8 Christos Stathis
                          }
162 ab1eb3f8 Christos Stathis
          }
163 ab1eb3f8 Christos Stathis
164 ab1eb3f8 Christos Stathis
165 ab1eb3f8 Christos Stathis
166 ab1eb3f8 Christos Stathis
        public void selectTab(int _tab) {
167 ab1eb3f8 Christos Stathis
                inner.selectTab(_tab);
168 ab1eb3f8 Christos Stathis
        }
169 ab1eb3f8 Christos Stathis
170 ab1eb3f8 Christos Stathis
171 ab1eb3f8 Christos Stathis
        /**
172 ab1eb3f8 Christos Stathis
         * Enables IE selection prevention and hides the dialog
173 ab1eb3f8 Christos Stathis
         * (we disable the prevention on creation of the dialog)
174 ab1eb3f8 Christos Stathis
         */
175 ab1eb3f8 Christos Stathis
        public void closeDialog() {
176 ab1eb3f8 Christos Stathis
                GSS.preventIESelection();
177 ab1eb3f8 Christos Stathis
                hide();
178 ab1eb3f8 Christos Stathis
        }
179 ab1eb3f8 Christos Stathis
}