ClientCachingExample.java
/* 
 * Copyright 2009 IT Mill 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.itmill.toolkit.demo.featurebrowser;

import com.itmill.toolkit.terminal.PaintException;
import com.itmill.toolkit.terminal.PaintTarget;
import com.itmill.toolkit.ui.CustomComponent;
import com.itmill.toolkit.ui.Label;
import com.itmill.toolkit.ui.Layout;
import com.itmill.toolkit.ui.TabSheet;
import com.itmill.toolkit.ui.VerticalLayout;

/**
 * This example is a (simple) demonstration of client-side caching. The content
 * in one tab is intentionally made very slow to produce server-side. When the
 * user changes to this tab for the first time, there will be a 3 second wait
 * before the content shows up, but the second time it shows up immediately
 * since the content has not changed and is cached client-side.
 
 @author IT Mill Ltd.
 */
public class ClientCachingExample extends CustomComponent {

    private static final String msg = "This example is a (simple) demonstration of client-side caching."
            " The content in one tab is intentionally made very slow to"
            " 'produce' server-side. When you changes to this tab for the"
            " first time, there will be a 3 second wait before the content"
            " shows up, but the second time it shows up immediately since the"
            " content has not changed and is cached client-side.";

    public ClientCachingExample() {

        final VerticalLayout main = new VerticalLayout();
        main.setMargin(true);
        setCompositionRoot(main);

        main.addComponent(new Label(msg));

        final TabSheet ts = new TabSheet();
        main.addComponent(ts);

        Layout layout = new VerticalLayout();
        layout.setMargin(true);
        Label l = new Label("This is a normal label, quick to render.");
        l.setCaption("A normal label");
        layout.addComponent(l);

        ts.addTab(layout, "Normal"null);

        layout = new VerticalLayout();
        layout.setMargin(true);
        l = new Label("Slow label - until cached client side.") {
            @Override
            public void paintContent(PaintTarget targetthrows PaintException {
                try {
                    Thread.sleep(3000);
                catch (final Exception e) {
                    // IGNORED
                }
                super.paintContent(target);
            }

        };
        l.setCaption("A slow label");
        layout.addComponent(l);
        ts.addTab(layout, "Slow"null);

    }
}