Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / AbstractPropertiesDialog.java @ fc0fa492

History | View | Annotate | Download (4.6 kB)

1
/*
2
 * Copyright 2007, 2008, 2009, 2010 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
6
 * GSS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GSS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
package gr.ebs.gss.client;
20

    
21
import gr.ebs.gss.client.rest.GetCommand;
22
import gr.ebs.gss.client.rest.resource.TagsResource;
23

    
24
import java.util.List;
25

    
26
import com.google.gwt.core.client.GWT;
27
import com.google.gwt.dom.client.NativeEvent;
28
import com.google.gwt.event.dom.client.ClickEvent;
29
import com.google.gwt.event.dom.client.ClickHandler;
30
import com.google.gwt.event.dom.client.KeyCodes;
31
import com.google.gwt.user.client.DeferredCommand;
32
import com.google.gwt.user.client.Event.NativePreviewEvent;
33
import com.google.gwt.user.client.ui.Anchor;
34
import com.google.gwt.user.client.ui.DialogBox;
35
import com.google.gwt.user.client.ui.FlowPanel;
36
import com.google.gwt.user.client.ui.Label;
37
import com.google.gwt.user.client.ui.TabPanel;
38
import com.google.gwt.user.client.ui.TextBox;
39

    
40
/**
41
 * Abstract class, parent of all 'File properties' dialog boxes.
42
 *
43
 * @author droutsis
44
 */
45
public abstract class AbstractPropertiesDialog extends DialogBox {
46

    
47
        protected static final String MULTIPLE_VALUES_TEXT = "(Multiple values)";
48

    
49
        /**
50
         * Text box with the tags associated with the file
51
         */
52
        protected TextBox tags = new TextBox();
53

    
54
        protected String initialTagText;
55

    
56
        /**
57
         * A FlowPanel with all user tags
58
         */
59
        protected FlowPanel allTagsContent;
60

    
61

    
62
        protected TabPanel inner = null;
63

    
64
        /**
65
         * The widget's constructor.
66
         *
67
         */
68
        public AbstractPropertiesDialog() {
69

    
70
                // Enable IE selection for the dialog (must disable it upon closing it)
71
                GSS.enableIESelection();
72

    
73
                setAnimationEnabled(true);
74

    
75
        }
76
        /**
77
         * Retrieves all user tags from the server and updates the FlowPanel
78
         *
79
         * @param userId
80
         */
81
        protected void updateTags() {
82
                GetCommand<TagsResource> tc = new GetCommand<TagsResource>(TagsResource.class, GSS.get().getCurrentUserResource().getTagsPath(),null) {
83

    
84
                        @Override
85
                        public void onComplete() {
86
                                allTagsContent.clear();
87
                                TagsResource tagr = getResult();
88
                                List<String> userTags = tagr.getTags();
89
                                Anchor tag = null;
90
                                for(String usrTag : userTags){
91
                                        tag = new Anchor(usrTag.toString(), false);
92
                                        tag.addStyleName("gss-tag");
93
                                        allTagsContent.add(tag);
94
                                        Label separator = new Label(", ");
95
                                        separator.addStyleName("gss-tag");
96
                                        allTagsContent.add(separator);
97
                                        tag.addClickHandler( new ClickHandler() {
98

    
99
                                                @Override
100
                                                public void onClick(ClickEvent event) {
101
                                                        String existing = tags.getText();
102
                                                        if (MULTIPLE_VALUES_TEXT.equals(existing)) existing = "";
103
                                                        String newTag = ((Anchor) event.getSource()).getText().trim();
104
                                                        // insert the new tag only if it is not in the list
105
                                                        // already
106
                                                        if (existing.indexOf(newTag) == -1 && !existing.trim().endsWith(newTag))
107
                                                                tags.setText(existing.trim()
108
                                                                                        + (existing.length() > 0 ? ", " : "")
109
                                                                                        + newTag);
110
                                                }
111
                                        });
112
                                }
113
                        }
114

    
115
                        @Override
116
                        public void onError(Throwable t) {
117
                                GWT.log("", t);
118
                                GSS.get().displayError("Unable to fetch user tags");
119
                        }
120
                };
121
                DeferredCommand.addCommand(tc);
122

    
123
        }
124

    
125
        /**
126
         * Accepts any change and updates the file
127
         *
128
         */
129
        protected abstract void accept();
130

    
131
        @Override
132
        @SuppressWarnings("fallthrough")
133
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
134
            super.onPreviewNativeEvent(preview);
135

    
136
            NativeEvent evt = preview.getNativeEvent();
137
            if (evt.getType().equals("keydown"))
138
                        // Use the popup's key preview hooks to close the dialog when either
139
                          // enter or escape is pressed.
140
                          switch (evt.getKeyCode()) {
141
                            case KeyCodes.KEY_ENTER:
142
                                    accept();
143
                            case KeyCodes.KEY_ESCAPE:
144
                              closeDialog();
145
                              break;
146
                          }
147
          }
148

    
149

    
150

    
151
        public void selectTab(int _tab) {
152
                inner.selectTab(_tab);
153
        }
154

    
155

    
156
        /**
157
         * Enables IE selection prevention and hides the dialog
158
         * (we disable the prevention on creation of the dialog)
159
         */
160
        public void closeDialog() {
161
                GSS.preventIESelection();
162
                hide();
163
        }
164

    
165
}