WindowedDemos.java
/* 
 * Copyright 2008 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;

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

import com.itmill.toolkit.terminal.ExternalResource;
import com.itmill.toolkit.ui.Button;
import com.itmill.toolkit.ui.Embedded;
import com.itmill.toolkit.ui.Window;
import com.itmill.toolkit.ui.Button.ClickEvent;

/**
 * Embeds other demos in windows using an ExternalResource ("application in
 * application").
 
 @author IT Mill Ltd.
 @see com.itmill.toolkit.ui.Window
 */
public class WindowedDemos extends com.itmill.toolkit.Application {

    // keeps track of created windows
    private final HashMap windows = new HashMap();

    // mapping demo name to URL
    private static final HashMap servlets = new HashMap();
    static {
        servlets.put("Caching demo""CachingDemo/");
        servlets.put("Calculator""Calc/");
        servlets.put("Calendar demo""CalendarDemo/");
        servlets.put("Select demo""SelectDemo/");
        servlets.put("Table demo""TableDemo/");
        servlets.put("Browser demo""BrowserDemo/");
        servlets.put("Notification demo""NotificationDemo/");
    }

    public void init() {

        // Create new window for the application and give the window a visible.
        final Window main = new Window("IT Mill Toolkit 5 Windowed Demos");
        // set as main window
        setMainWindow(main);

        // Create menu window.
        final Window menu = new Window("Select demo");
        menu.setWidth(200);
        menu.setHeight(400);
        main.addWindow(menu)// add to layout

        // Create a menu button for each demo
        for (final Iterator it = servlets.keySet().iterator(); it.hasNext();) {
            final String name = (Stringit.next();
            final Button b = new Button(name, new Button.ClickListener() {
                public void buttonClick(ClickEvent event) {
                    show(event.getButton().getCaption());
                }

            });
            b.setStyleName("link");
            menu.addComponent(b);
        }

    }

    /**
     * Shows the specified demo in a separate window. Creates a new window if
     * the demo has not been shown already, re-uses old window otherwise.
     
     @param demoName
     *                the name of the demo to be shown
     */
    private void show(String demoName) {
        Window w = (Windowwindows.get(demoName);
        if (w == null) {
            w = new Window(demoName);
            w.setWidth(520);
            w.setHeight(500);
            w.setPositionX(202);
            w.getLayout().setSizeFull();
            w.getLayout().setMargin(false);
            windows.put(demoName, w);
            Embedded emb = new Embedded();
            emb.setType(Embedded.TYPE_BROWSER);
            emb
                    .setSource(new ExternalResource((Stringservlets
                            .get(demoName)));
            emb.setSizeFull();
            w.addComponent(emb);
        }
        getMainWindow().addWindow(w);
    }

}