Creating Application-Level Windows dynamically

Hi!

Toolkit 5 reference manual says that application-level windows can even be created dynamically based on URLs. The actual chapter on that functionality is unfinished. Could someone explain what does this functionality actually mean and some examples when it would be useful?

In my application, I have a button that lets user the open the application in a new browser window, so the user can look at some reports in another window while she is editing something in the other. I managed to create this by just following the instructions in the reference guide.

Cheers,

Kaitsu

Hi,

By dynamic creation based on URLs, the manual refers to the technique of creating a window in
getWindow()
.

    public Window getWindow(String name) {
        if (name.equals("anotherpage")) {
            if (anotherpage == null) {
                // Create the window object on the fly.
                anotherpage = new Window("Yet Another Page");
                anotherpage.addComponent(new Label("This is a yet another window."));
                
                // We must add the window to the application, it is not done
                // automatically
                addWindow(anotherpage);
            }
            return anotherpage;
        }
        
        return super.getWindow(name);
    }

Now if your application base URL is http://localhost:8080/tk5/windowtest/ and you call it with http://localhost:8080/tk5/windowtest/anotherpage/, the
getWindow()
will create the window object dynamically when you load the page.

This may not be so useful if you have a few static window names, as then you can create them statically in a regular way. This is more useful for creating separate windows for, for example, email messages (or some other items), for which a user could open rather many windows, and it’s not sensible to create all Window objects before they want to be displayed.

Another technique is to create the Window objects when handling click events, but that does not allow bookmarking, etc.

Below is a more complete example that shows how to create windows based on their URLs. Here the Window objects are stored in a Map instead of a member variable, so their number is open.

Notice that the window names may not contain slash/.


package com.itmill.toolkit.tests.book;

import java.util.HashMap;

import com.itmill.toolkit.Application;
import com.itmill.toolkit.ui.*;
import com.itmill.toolkit.ui.Button.ClickEvent;
import com.itmill.toolkit.terminal.*;

public class WindowTestApplication extends Application {
    Window anotherpage = null;
    HashMap<String, Window> windows = new HashMap<String, Window>();

    public void init() {
        final Window main = new Window ("Window Test Application");
        setMainWindow(main);
        setTheme("tests-magi");
        
        /* Create a new window. */
        final Window mywindow = new Window("Second Window");
        
        /* Manually set the name of the window. */
        mywindow.setName("mywindow");
        
        /* Add some content to the window. */
        mywindow.addComponent(new Label("This is a second window."));

        /* Add the window to the application. */
        addWindow(mywindow);

        /* Add a button to open a new window. */
        main.addComponent(new Button("Click to open new window",
                          new Button.ClickListener() { 
            public void buttonClick(ClickEvent event) {
                // Open the window.
                main.open(new ExternalResource(mywindow.getURL()), "_new");
            }      
        }));        
        
        /* Add a link to the second window. */
        Link link = new Link("Click to open second window",
                             new ExternalResource(mywindow.getURL()));
        link.setTargetName("_new");
        main.addComponent(link);
        
        // Add the link manually inside a Label.
        main.addComponent(new Label("Second window: <a href='"
                                    + mywindow.getURL() + "' target='_new'>click to open</a>",
                          Label.CONTENT_XHTML));
        main.addComponent(new Label("The second window can be accessed through URL: "
                                    + mywindow.getURL()));

        // Add links to windows that do not yet exist, but are created dynamically
        // when the URL is called.
        main.addComponent(new Label("URLs to open item windows:"));
        final String[] items = new String[]
 {"mercury", "venus", "earth", "mars",
                "jupiter", "saturn", "uranus", "neptune"};
        for (String item : items) {
            // We do not create window objects here, but just links to the windows
            String windowUrl = getURL() + "planet-" + item;
            main.addComponent(new Label("A window about '"+item+"': <a href='" +
                    windowUrl + "' target='_new'>"+ windowUrl +"</a>",
                    Label.CONTENT_XHTML));
        }
    }
    
    public Window getWindow(String name) {
        if (name.startsWith("planet-")) {
            String planetName = name.substring("planet-".length());
            if (! windows.containsKey(planetName)) {
                // Create the window object on the fly.
                Window newWindow = new Window("Yet Another Page");
                newWindow.addComponent(new Label("This window contains details about "+planetName+"."));
                windows.put(planetName, newWindow);
                
                // We must add the window to the application, it is not done
                // automatically
                addWindow(newWindow);
            }
            return windows.get(planetName);
        }
        
        return super.getWindow(name);
    }
}