summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/demo/coverflow/CoverflowApplication.java
blob: 941c808b09611cf1a2bb783aff2ba29a5d24faa4 (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
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.itmill.toolkit.demo.coverflow;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.itmill.toolkit.data.Property;
import com.itmill.toolkit.terminal.Resource;
import com.itmill.toolkit.terminal.ThemeResource;
import com.itmill.toolkit.ui.Alignment;
import com.itmill.toolkit.ui.Button;
import com.itmill.toolkit.ui.Embedded;
import com.itmill.toolkit.ui.Label;
import com.itmill.toolkit.ui.Panel;
import com.itmill.toolkit.ui.VerticalLayout;
import com.itmill.toolkit.ui.Window;
import com.itmill.toolkit.ui.Button.ClickEvent;

public class CoverflowApplication extends com.itmill.toolkit.Application {

    Coverflow covers = new Coverflow();

    public void init() {

        setMainWindow(new Window("Coverflow", createMainLayout()));

        setTheme("coverflow");

        addSlidesToCoverflow();
    }

    private VerticalLayout createMainLayout() {

        // Initialize coverflow component
        covers.setHeight("150px");
        covers.setWidth("100%");
        covers.setBackgroundColor(0, 0, 0, 100, 100, 100);

        // Initialize visible slide viewer
        Panel slidePanel = new Panel();
        slidePanel.setStyleName(Panel.STYLE_LIGHT);
        slidePanel.setSizeFull();
        final Embedded visibleSlide = new Embedded();
        visibleSlide.setHeight("480px");
        slidePanel.addComponent(visibleSlide);
        ((VerticalLayout) slidePanel.getLayout()).setComponentAlignment(
                visibleSlide, "center");

        // Listen to coverflow changes as change slides when needed
        covers.addListener(new Property.ValueChangeListener() {
            public void valueChange(Property.ValueChangeEvent event) {
                visibleSlide.setSource((Resource) covers.getValue());
            }
        });

        // Show sources button
        Button showSrc = new Button("Show source", new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                Window srcWindow = new Window("Source code");
                srcWindow.setWidth("700px");
                srcWindow.setHeight("500px");
                Label l = new Label(getSourceCodeForThisClass(),
                        Label.CONTENT_XHTML);
                srcWindow.addComponent(l);
                getMainWindow().addWindow(srcWindow);
            }
        });
        showSrc.setStyleName(Button.STYLE_LINK);
        // Initialize main layout
        VerticalLayout layout = new VerticalLayout();
        layout.addComponent(showSrc);
        layout.setComponentAlignment(showSrc, Alignment.TOP_RIGHT);
        layout.addComponent(slidePanel);
        layout.addComponent(covers);
        layout.setExpandRatio(slidePanel, 1);
        layout.setSizeFull();

        return layout;
    }

    private String getSourceCodeForThisClass() {
        String code = "Could not find source-file";
        try {
            InputStream is = this.getClass().getResource(
                    "CoverflowApplication.html").openStream();
            BufferedReader r = new BufferedReader(new InputStreamReader(is));
            StringBuffer buf = new StringBuffer();
            String line;
            while ((line = r.readLine()) != null) {
                buf.append(line);
            }
            code = buf.toString();
        } catch (IOException ignored) {
        }
        return code;
    }

    private void addSlidesToCoverflow() {
        for (int i = 0; i < 20; i++) {
            String head = "images/";
            String tail = "slideshow-example.0" + ((i < 10) ? "0" : "") + i
                    + ".jpg";
            ThemeResource slide = new ThemeResource(head + tail);
            covers.addItem(slide);
            covers.setItemIcon(slide,
                    new ThemeResource(head + "thumbs/" + tail));
        }
    }
}