Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / tagtree / TagTreeViewModel.java @ 3f8622d4

History | View | Annotate | Download (7.2 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35

    
36
package gr.grnet.pithos.web.client.tagtree;
37

    
38
import gr.grnet.pithos.web.client.Pithos;
39
import gr.grnet.pithos.web.client.foldertree.AccountResource;
40
import gr.grnet.pithos.web.client.foldertree.File;
41
import gr.grnet.pithos.web.client.foldertree.Folder;
42
import gr.grnet.pithos.web.client.rest.GetRequest;
43
import gr.grnet.pithos.web.client.rest.RestException;
44
import gr.grnet.pithos.web.client.tagtree.TagTreeView.Templates;
45

    
46
import java.util.Iterator;
47
import java.util.LinkedHashSet;
48
import java.util.List;
49
import java.util.Set;
50

    
51
import com.google.gwt.cell.client.AbstractCell;
52
import com.google.gwt.cell.client.Cell;
53
import com.google.gwt.cell.client.TextCell;
54
import com.google.gwt.core.client.GWT;
55
import com.google.gwt.core.client.Scheduler;
56
import com.google.gwt.event.dom.client.ContextMenuEvent;
57
import com.google.gwt.http.client.Response;
58
import com.google.gwt.safehtml.shared.SafeHtml;
59
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
60
import com.google.gwt.text.shared.SafeHtmlRenderer;
61
import com.google.gwt.user.client.ui.AbstractImagePrototype;
62
import com.google.gwt.view.client.ListDataProvider;
63
import com.google.gwt.view.client.SingleSelectionModel;
64
import com.google.gwt.view.client.TreeViewModel;
65

    
66
public class TagTreeViewModel implements TreeViewModel {
67

    
68
    private Cell<Tag> tagCell = new AbstractCell<Tag>(ContextMenuEvent.getType().getName()) {
69

    
70
       @Override
71
        public void render(@SuppressWarnings("unused") Context context, Tag tag, SafeHtmlBuilder safeHtmlBuilder) {
72
            String html = AbstractImagePrototype.create(TagTreeView.images.tag()).getHTML();
73
            safeHtmlBuilder.appendHtmlConstant(html);
74
            safeHtmlBuilder.append(Templates.INSTANCE.nameSpan(tag.getName()));
75
        }
76

    
77
        @Override
78
        public void onBrowserEvent(@SuppressWarnings("unused") Context context, @SuppressWarnings("unused") com.google.gwt.dom.client.Element parent, Tag tag, com.google.gwt.dom.client.NativeEvent event, @SuppressWarnings("unused") com.google.gwt.cell.client.ValueUpdater<Tag> valueUpdater) {
79
            if (event.getType().equals(com.google.gwt.event.dom.client.ContextMenuEvent.getType().getName())) {
80
                TagTreeViewModel.this.selectionModel.setSelected(tag, true);
81
            }
82
        }
83
    };
84

    
85
    private ListDataProvider<String> rootDataProvider = new ListDataProvider<String>();
86
    private ListDataProvider<Tag> tagDataProvider = new ListDataProvider<Tag>();
87

    
88
    protected SingleSelectionModel<Tag> selectionModel;
89

    
90
    protected Pithos app;
91

    
92
    public TagTreeViewModel(Pithos _app, SingleSelectionModel<Tag> selectionModel) {
93
        app = _app;
94
        this.selectionModel = selectionModel;
95
    }
96

    
97
    @Override
98
    public <T> NodeInfo<?> getNodeInfo(T value) {
99
        if (value == null) {
100
            return new DefaultNodeInfo<String>(rootDataProvider, new TextCell(new SafeHtmlRenderer<String>() {
101
                @Override
102
                public SafeHtml render(String object) {
103
                    SafeHtmlBuilder builder = new SafeHtmlBuilder();
104
                    render(object, builder);
105
                    return builder.toSafeHtml();
106
                }
107

    
108
                @Override
109
                public void render(String object, SafeHtmlBuilder builder) {
110
                    String html = AbstractImagePrototype.create(TagTreeView.images.tag()).getHTML();
111
                    builder.appendHtmlConstant(html);
112
                    builder.append(Templates.INSTANCE.nameSpan(object));
113
                }
114
            }), null, null);
115
        }
116
        else if (value instanceof String) {
117
            //fetchAllTags();
118
            return new DefaultNodeInfo<Tag>(tagDataProvider, tagCell, selectionModel, null);
119
        }
120
        else
121
            return null;
122
    }
123

    
124
    @Override
125
    public boolean isLeaf(Object o) {
126
        if (o == null)
127
            return false;
128
        if (o instanceof String)
129
            return tagDataProvider.getList().isEmpty();
130
                return true;
131
    }
132

    
133
    public Tag getSelection() {
134
        return selectionModel.getSelectedObject();
135
    }
136

    
137
    public void updateTag(Tag tag) {
138
        fetchTag(tag);
139
    }
140

    
141
    public void fetchTag(Tag t) {
142
        AccountResource account = app.getAccount();
143
        Iterator<Folder> iter = account.getContainers().iterator();
144
        fetchTag(iter, t, new LinkedHashSet<File>());
145
    }
146

    
147
    protected void fetchTag(final Iterator<Folder> iter, final Tag t, final Set<File> files) {
148
        if (iter.hasNext()) {
149
            Folder f = iter.next();
150
            String path = f.getUri() + "?format=json&meta=" + t.getName();
151
            GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), app.getUsername(), path) {
152
                @Override
153
                public void onSuccess(Folder _result) {
154
                    files.addAll(_result.getFiles());
155
                    fetchTag(iter, t, files);
156
                }
157

    
158
                @Override
159
                public void onError(Throwable th) {
160
                    GWT.log("Error getting folder", th);
161
                                        app.setError(th);
162
                    if (th instanceof RestException)
163
                        app.displayError("Error getting folder: " + ((RestException) th).getHttpStatusText());
164
                    else
165
                        app.displayError("System error fetching folder: " + th.getMessage());
166
                }
167

    
168
                                @Override
169
                                protected void onUnauthorized(Response response) {
170
                                        app.sessionExpired();
171
                                }
172
            };
173
            getFolder.setHeader("X-Auth-Token", app.getToken());
174
            Scheduler.get().scheduleDeferred(getFolder);
175
        }
176
        else {
177
            app.showFiles(files);
178
        }
179
    }
180

    
181
    public void initialize(List<Tag> allTags) {
182
        tagDataProvider.getList().addAll(allTags);
183
        rootDataProvider.getList().add("Tags");
184
    }
185
}