summaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/LayoutDemo.java
blob: 275ccf4d5d3fa5dbc123ace722d7d0a32f14cb8a (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
/* 
 * Copyright 2011 Vaadin Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.vaadin.tests;

import com.vaadin.server.ClassResource;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Component;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.UI.LegacyWindow;
import com.vaadin.ui.VerticalLayout;

/**
 * This example demonstrates layouts. Layouts are populated with sample Vaadin
 * UI components.
 * 
 * @author Vaadin Ltd.
 * @since 4.0.0
 * 
 */
public class LayoutDemo extends com.vaadin.LegacyApplication {

    /**
     * Initialize Application. Demo components are added to main window.
     */
    @Override
    public void init() {
        final LegacyWindow mainWindow = new LegacyWindow("Layout demo");
        setMainWindow(mainWindow);

        //
        // Create horizontal ordered layout
        //
        final HorizontalLayout layoutA = new HorizontalLayout();
        // Add 4 panels
        fillLayout(layoutA, 4);

        //
        // Create vertical ordered layout
        //
        final VerticalLayout layoutB = new VerticalLayout();
        // Add 4 panels
        fillLayout(layoutB, 4);

        //
        // Create grid layout
        //
        final GridLayout layoutG = new GridLayout(4, 4);
        // Add 16 panels components
        fillLayout(layoutG, 16);

        //
        // Create grid layout
        //
        final GridLayout layoutG2 = new GridLayout(4, 4);
        // Add 4 panels with absolute coordinates (diagonally)
        layoutG2.addComponent(getExampleComponent("x=0, y=0"), 0, 0);
        layoutG2.addComponent(getExampleComponent("x=1, y=1"), 1, 1);
        layoutG2.addComponent(getExampleComponent("x=2, y=2"), 2, 2);
        layoutG2.addComponent(getExampleComponent("x=3, y=3"), 3, 3);
        // Add 4 pictures with absolute coordinates (diagonally)
        layoutG2.addComponent(getExamplePicture("x=3, y=0"), 3, 0);
        layoutG2.addComponent(getExamplePicture("x=2, y=1"), 2, 1);
        layoutG2.addComponent(getExamplePicture("x=1, y=2"), 1, 2);
        layoutG2.addComponent(getExamplePicture("x=0, y=3"), 0, 3);

        //
        // Create TabSheet
        //
        final TabSheet tabsheet = new TabSheet();
        tabsheet.setCaption("Tabsheet, above layouts are added to this component");
        tabsheet.addTab(layoutA, "Horizontal ordered layout", null);
        tabsheet.addTab(layoutB, "Vertical ordered layout", null);
        tabsheet.addTab(layoutG, "First grid layout", null);
        tabsheet.addTab(layoutG2, "Second grid layout", null);

        //
        // Add demo layouts to main window
        //
        mainWindow.addComponent(new Label(
                "<h3>Horizontal ordered layout</h3>Added four components.",
                ContentMode.HTML));
        mainWindow.addComponent(layoutA);
        mainWindow.addComponent(new Label(
                "<br /><h3>Vertical ordered layout</h3>Added four components.",
                ContentMode.HTML));
        mainWindow.addComponent(layoutB);
        mainWindow.addComponent(new Label(
                "<br /><h3>Grid Layout (4 x 4)</h3>Added 16 components.",
                ContentMode.HTML));
        mainWindow.addComponent(layoutG);
        mainWindow.addComponent(new Label("<br /><h3>Grid Layout (4 x 4)</h3>"
                + "Added four panels and four embedded components "
                + "diagonally with absolute coordinates.", ContentMode.HTML));
        mainWindow.addComponent(layoutG2);
        mainWindow.addComponent(new Label(
                "<br /><h3>TabSheet</h3>Added above layouts as tabs.",
                ContentMode.HTML));
        mainWindow.addComponent(tabsheet);

    }

    private Component getExamplePicture(String caption) {
        // loads image from package com.vaadin.demo
        final ClassResource cr = new ClassResource("m-bullet-blue.gif");
        final Embedded em = new Embedded("Embedded " + caption, cr);
        em.setWidth("170px");
        return em;
    }

    private Component getExampleComponent(String caption) {
        final Panel panel = new Panel();
        panel.setCaption("Panel component " + caption);
        panel.addComponent(new Label(
                "Panel is a container for other components, by default it draws a frame around it's "
                        + "extremities and may have a caption to clarify the nature of the contained components' purpose."
                        + " Panel contains an layout where the actual contained components are added, "
                        + "this layout may be switched on the fly.",
                ContentMode.HTML));
        panel.setWidth("222px");
        return panel;
    }

    /**
     * Add multiple demo component to given layout.
     * 
     * @param layout
     *            where components are added
     * @param numberOfComponents
     *            to add
     */
    private void fillLayout(Layout layout, int numberOfComponents) {
        for (int i = 1; i <= numberOfComponents; i++) {
            layout.addComponent(getExampleComponent(Integer.toString(i)));
        }
    }

}