Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.1 kB)

1
/*
2
 * Copyright 2011-2012 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.FileFolderResource;
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.ArrayList;
47
import java.util.Iterator;
48
import java.util.List;
49

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

    
65
public class TagTreeViewModel implements TreeViewModel {
66

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

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

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

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

    
87
    protected SingleSelectionModel<Tag> selectionModel;
88

    
89
    protected Pithos app;
90

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

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

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

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

    
131
    public Tag getSelection() {
132
        return selectionModel.getSelectedObject();
133
    }
134

    
135
    public void updateTag(Tag tag) {
136
        fetchTag(tag);
137
    }
138

    
139
    public void fetchTag(Tag t) {
140
        AccountResource account = app.getAccount();
141
        Iterator<Folder> iter = account.getContainers().iterator();
142
        fetchTag(iter, t, new ArrayList<FileFolderResource>());
143
    }
144

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

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

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

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