aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/demo/coverflow/gwt/client/ui/VCoverflow.java
blob: 35a7118fb0f2f33b2a6a79e18053f8e7ee5f4772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.vaadin.demo.coverflow.gwt.client.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.Paintable;
import com.vaadin.terminal.gwt.client.UIDL;

public class VCoverflow extends Composite implements Paintable {
    private String uidlId;
    protected ApplicationConnection client;
    private ArrayList coverList = new ArrayList();

    private Object _selected;
    private boolean flashInited = false;
    private HTML flash;
    private boolean scrollbarVisibility = true;
    private String backgroundGradientStart;
    private String backgroundGradientEnd;
    private boolean colorChanged = false;
    private boolean sbVisibilityChanged = false;
    private HashMap keyMap = new HashMap();

    /**
     * Constructor
     */
    public VCoverflow() {
        flash = new HTML();

        initWidget(flash);
    }

    /**
     * This method accepts parses the uidl sent by the server
     * 
     * @param UIDL
     *            uidl
     * @param ApplicationConnection
     *            client
     */
    public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
        // Store variables
        uidlId = uidl.getId();
        this.client = client;
        String tempColor;

        if (client.updateComponent(this, uidl, true)) {
            return;
        }

        // Has the scrollbar's visibility status changed?
        if (uidl.hasAttribute("scrollbarVisibility")) {
            boolean tempVis = uidl.getBooleanAttribute("scrollbarVisibility");
            if (scrollbarVisibility != tempVis) {
                scrollbarVisibility = tempVis;
                sbVisibilityChanged = true;
            }
        }

        // Has the start gradient changed?
        if (uidl.hasAttribute("backgroundGradientStart")) {
            tempColor = uidl.getStringAttribute("backgroundGradientStart")
                    .toString();
            if (tempColor != backgroundGradientStart) {
                backgroundGradientStart = tempColor;
                colorChanged = true;
            }
        }

        // Has the end gradient changed?
        if (uidl.hasAttribute("backgroundGradientEnd")) {
            tempColor = uidl.getStringAttribute("backgroundGradientEnd")
                    .toString();
            if (tempColor != backgroundGradientEnd) {
                backgroundGradientEnd = tempColor;
                colorChanged = true;
            }
        }

        final UIDL images = uidl.getChildUIDL(0);

        // Check which covers should be removed. This array list contains all
        // current
        // covers. We remove from this list all covers which are sent with the
        // repainted
        // uidl. All remaining covers in this list should be "old" ones and are
        // should
        // be deleted.

        ArrayList newList = new ArrayList();

        // Iterate through all option elements
        for (final Iterator i = images.getChildIterator(); i.hasNext();) {
            final UIDL imgUidl = (UIDL) i.next();

            // Make sure all required attributes exist
            if (imgUidl.hasAttribute("caption") && imgUidl.hasAttribute("key")
                    && imgUidl.hasAttribute("icon")) {
                HashMap set = new HashMap();

                // Update the key map
                keyMap.put(imgUidl.getStringAttribute("caption"), imgUidl
                        .getStringAttribute("key"));

                // Get information

                set.put("icon", client.translateVaadinUri(imgUidl
                        .getStringAttribute("icon")));
                set.put("caption", imgUidl.getStringAttribute("caption"));

                newList.add(set);

                // Is the current cover selected?
                if (imgUidl.hasAttribute("selected")) {
                    _selected = imgUidl.getStringAttribute("caption");
                }
            }
        }

        // Deleted items
        ArrayList intersectList = new ArrayList();
        intersectList.addAll(coverList);
        intersectList.removeAll(newList);

        if (flashInited) {
            for (int i = 0; i < intersectList.size(); i++) {
                HashMap cover = (HashMap) intersectList.get(i);
                removeCover(uidlId, cover.get("caption").toString());
            }
        }

        // Added items
        intersectList = new ArrayList();
        intersectList.addAll(newList);
        intersectList.removeAll(coverList);

        if (flashInited) {
            for (int i = 0; i < intersectList.size(); i++) {
                HashMap cover = (HashMap) intersectList.get(i);
                addCover(uidlId, cover.get("caption").toString(), cover.get(
                        "icon").toString());
            }
        }

        coverList = newList;

        // Has the flash been initialized?
        if (!flashInited) {
            colorChanged = false;
            setFlash();
            initializeMethods(uidlId);
        }

        // Inform flash of the selected cover
        if (_selected != null && flashInited) {
            selectCover(uidlId, _selected.toString());
        }

        if (colorChanged && flashInited) {
            setBackgroundColor(uidlId, backgroundGradientStart,
                    backgroundGradientEnd);
            colorChanged = false;
        }

        if (sbVisibilityChanged && flashInited) {
            toggleScrollbarVisibility(uidlId, scrollbarVisibility);
            sbVisibilityChanged = false;
        }

    }

    /**
     * Inform the server which cover is selected
     * 
     * @param String
     *            coverKey
     */
    public void setCover(String coverId) {
        if (uidlId == null || client == null) {
            return;
        }

        client.updateVariable(uidlId, "selected", new String[] { keyMap.get(
                coverId).toString() }, true);
    }

    /**
     * Initialize the native javascript functions needed for the flash <-> GWT
     * communication
     * 
     * @param String
     *            id
     */
    public native void initializeMethods(String id) /*-{
                var app = this;
                
                if($wnd.vaadin.coverflow == null)
                	var coverflow = [];
                else
                	var coverflow = $wnd.vaadin.coverflow;
                	
                coverflow['getCovers_' + id] = function() {                
                   	app.@com.vaadin.demo.coverflow.gwt.client.ui.VCoverflow::getCovers()();
                };   
                
               	coverflow['setCurrent_' + id] = function(selected) {
                   	app.@com.vaadin.demo.coverflow.gwt.client.ui.VCoverflow::setCover(Ljava/lang/String;)(selected);
                };
                
                $wnd.vaadin.coverflow = coverflow;
            }-*/;

    /**
     * This function sends all covers to the flash. We cannot do this directly
     * in the updateFromUIDL method, because we cannot be sure if the flash has
     * been loaded into the browser. The flash will call for this method when
     * it's ready.
     */
    public void getCovers() {
        // Loop through all stored coves
        for (int i = 0; i < coverList.size(); i++) {
            HashMap set = (HashMap) coverList.get(i);

            try {
                // Add the cover
                addCover(uidlId, set.get("caption").toString(), set.get("icon")
                        .toString());
            } catch (Exception e) {
                // Do not add covers lacking obligatory data
            }
        }
        // The flash calls for this method, therefore we can be sure that the
        // flash has been loaded
        // into the browser.
        flashInited = true;

        // Set selected cover
        if (_selected != null) {
            selectCover(uidlId, _selected.toString());
        }
    }

    /**
     * This function is a native javascript function which adds covers to the
     * actual flash. This method works as a bridge between GWT and flash.
     * 
     * @param id
     * @param key
     * @param caption
     * @param icon
     */
    public native void addCover(String id, String caption, String icon) /*-{   
                try {
                	    $doc['fxcoverflow' + id].addCover(caption.toString(), icon.toString());
                    }
                    catch(e) {
                        $wnd.alert(e.message);
                    }   
                     	
                }-*/;

    /**
     * This function tells the flash which cover should be selected.
     * 
     * @param id
     * @param key
     */
    public native void selectCover(String id, String key) /*-{    
             	$doc["fxcoverflow" + id].selectCover(key.toString());
             }-*/;

    public native void setBackgroundColor(String id, String startGradient,
            String endGradient) /*-{    	
                	$doc["fxcoverflow" + id].setBackgroundColor("0x" + startGradient.toString(), "0x" + endGradient.toString());
            }-*/;

    public native void toggleScrollbarVisibility(String id, boolean visibility) /*-{    	
             	$doc["fxcoverflow" + id].toggleScrollbarVisibility(visibility);
             }-*/;

    public native void removeCover(String id, String key) /*-{    	
             	$doc["fxcoverflow" + id].removeCover(key);
             }-*/;

    /**
     * Set the HTML coding of the flash movie. This isn't done until the
     * updateFromUIDL method is called for the first time. The reason is that we
     * use an id from the UIDL to uniquely identify all instances of this
     * widget.
     */
    private void setFlash() {
        String html = "<object id=\"fxcoverflow"
                + uidlId
                + "\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"100%\""
                + " height=\"100%\" codebase=\"http://fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\">"
                + "<param name=\"movie\" value=\""
                + GWT.getModuleBaseURL()
                + "coverflowflash.swf\">"
                + "<param name=\"quality\" value=\"high\">"
                + "<param name=\"flashVars\" value=\"pid="
                + uidlId
                + "&sbVis="
                + scrollbarVisibility
                + "&bgS=0x"
                + backgroundGradientStart
                + "&bgE=0x"
                + backgroundGradientEnd
                + "\" />"
                + "<embed name=\"fxcoverflow"
                + uidlId
                + "\" flashVars=\"pid="
                + uidlId
                + "&sbVis="
                + scrollbarVisibility
                + "&bgS=0x"
                + backgroundGradientStart
                + "&bgE=0x"
                + backgroundGradientEnd
                + "\" src=\""
                + GWT.getModuleBaseURL()
                + "coverflowflash.swf\" width=\"100%\" height=\"100%\" "
                + "quality=\"high\" "
                + "pluginspage=\"http://www.adobe.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">"
                + "</embed>" + "</object>";
        flash.setHTML(html);
    }
}