Directory

← Back

Navigator

Navigator is an easy to use view manager that supports lazy initialization, bookmarking and multiple browser windows.

Author

Rating

With Navigator component, you can easily add the following features to your Vaadin application:

  • Navigation between view using almost any UI component
  • Bookmarking different views
  • Bookmarking references to data within a view (for example ticket number on ticket view)
  • Confirming navigation from a view if user have not saved data
  • Lazy initialization - view implementations are instantiated when they are navigated to

See the code-example below.

Sample code

package example;

import org.vaadin.navigator.Navigator;
import org.vaadin.navigator.Navigator.NavigableApplication;

import com.vaadin.Application;
import com.vaadin.ui.Label;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.MenuBar.MenuItem;

@SuppressWarnings("serial")
public class SimpleExample extends Application implements NavigableApplication {

    @Override
    public void init() {
        setMainWindow(createNewWindow());
    }

    @Override
    public Window getWindow(String name) {
        // Use navigator to manage multiple browser windows
        return Navigator.getWindow(this, name, super.getWindow(name));
    }

    public Window createNewWindow() {
        final Navigator navigator = new Navigator();
        MenuBar menu = new MenuBar();
        VerticalLayout layout = new VerticalLayout();
        Window w = new Window("Navigator example", layout);

        w.addComponent(menu);
        w.addComponent(navigator);
        layout.setMargin(false);
        layout.setSpacing(true);
        layout.setSizeFull();
        layout.setExpandRatio(navigator, 1.0f);
        menu.setWidth("100%");

        // Wire up the navigation
        for (final Class viewClass : new Class[] { Dashboard.class,
                Editor.class, Ticket.class, MockupView.class }) {
            navigator.addView(viewClass.getSimpleName(), viewClass);
            menu.addItem(viewClass.getSimpleName(), new MenuBar.Command() {

                public void menuSelected(MenuItem selectedItem) {
                    navigator.navigateTo(viewClass);
                }
            });
        }

        return w;
    }

    static public class MockupView extends Label implements Navigator.View {

        public String getWarningForNavigatingFrom() {
            return "You did not click save button, are you sure you want to navigate away?";
        }

        public void init(Navigator navigator, Application application) {
            setValue("This is just a mockup of a view");
        }

        public void navigateTo(String requestedDataId) {
            // This is called each time the view is navigated to
            // Requested data id can be passed in URL
        }
    }
}

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

  • Compiled with Vaadin 6.5.0
  • Added listener for view changes
Released
2011-02-08
Maturity
EXPERIMENTAL
License
Apache License 2.0

Compatibility

Framework
Vaadin 6.0+
Vaadin 6.1+ in 0.1
Browser
Browser Independent
Online