Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.1 kB)

1
/*
2
 * Copyright 2007, 2008, 2009 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.Iterator;
25
import java.util.List;
26

    
27
import com.google.gwt.core.client.GWT;
28
import com.google.gwt.user.client.DeferredCommand;
29
import com.google.gwt.user.client.ui.Button;
30
import com.google.gwt.user.client.ui.ClickListener;
31
import com.google.gwt.user.client.ui.DialogBox;
32
import com.google.gwt.user.client.ui.FlowPanel;
33
import com.google.gwt.user.client.ui.KeyboardListener;
34
import com.google.gwt.user.client.ui.TabPanel;
35
import com.google.gwt.user.client.ui.TextBox;
36
import com.google.gwt.user.client.ui.Widget;
37

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

    
45
        protected static final String MULTIPLE_VALUES_TEXT = "(Multiple values)";
46

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

    
52
        protected String initialTagText;
53

    
54
        /**
55
         * A FlowPanel with all user tags
56
         */
57
        protected FlowPanel allTagsContent;
58

    
59

    
60
        protected TabPanel inner = null;
61

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

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

    
71
                setAnimationEnabled(true);
72

    
73
        }
74

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

    
83
                        @Override
84
                        public void onComplete() {
85
                                allTagsContent.clear();
86
                                TagsResource tagr = getResult();
87
                                List<String> userTags = tagr.getTags();
88
                                Iterator t = userTags.iterator();
89
                                while (t.hasNext()) {
90
                                        final Button tag = new Button((String) t.next(), new ClickListener() {
91

    
92
                                                public void onClick(Widget sender) {
93
                                                        String existing = tags.getText();
94
                                                        if (MULTIPLE_VALUES_TEXT.equals(existing)) existing = "";
95
                                                        String newTag = ((Button) sender).getText().trim();
96
                                                        // insert the new tag only if it is not in the list
97
                                                        // already
98
                                                        if (existing.indexOf(newTag + ",") == -1 && !existing.trim().endsWith(newTag))
99
                                                                tags.setText(existing.trim() + (existing.length() > 0 ? ", " : "") + newTag);
100
                                                }
101
                                        });
102
                                        allTagsContent.add(tag);
103
                                }
104
                        }
105

    
106
                        @Override
107
                        public void onError(Throwable t) {
108
                                GWT.log("", t);
109
                                GSS.get().displayError("Unable to fetch user tags");
110
                        }
111
                };
112
                DeferredCommand.addCommand(tc);
113

    
114
        }
115

    
116
        /**
117
         * Accepts any change and updates the file
118
         *
119
         */
120
        protected abstract void accept();
121

    
122
        @Override
123
        @SuppressWarnings("fallthrough")
124
        public boolean onKeyDownPreview(char key, int modifiers) {
125
                // Use the popup's key preview hooks to close the dialog when either
126
                // enter or escape is pressed.
127
                switch (key) {
128
                        case KeyboardListener.KEY_ENTER:
129
                                accept();
130
                        case KeyboardListener.KEY_ESCAPE:
131
                                closeDialog();
132
                                break;
133
                }
134

    
135
                return true;
136
        }
137

    
138

    
139
        public void selectTab(int _tab) {
140
                inner.selectTab(_tab);
141
        }
142

    
143

    
144
        /**
145
         * Enables IE selection prevention and hides the dialog
146
         * (we disable the prevention on creation of the dialog)
147
         */
148
        public void closeDialog() {
149
                GSS.preventIESelection();
150
                hide();
151
        }
152

    
153
}