Problems with Appfoundation

I just wanted to test the view module of appfoundation and dont get it to work.

I copied the code from this example: http://code.google.com/p/vaadin-appfoundation/wiki/View

public class MainView extends CustomComponent implements ViewContainer {

  private VerticalLayout layout = new VerticalLayout();
  private View currentView;

  public MainView() {
    setCompositionRoot(layout);
    // Register child views
    ViewHandler.addView(YourView.class, this);

    // Set the default view
    currentView = ViewHandler.getViewItem(YourView.class).getView();
    layout.addComponent((Component) currentView);
  }

    @Override
    public void activate(View view) {
        if (!(view instanceof Component)) {
            throw new IllegalArgumentException("View must be a component");
        }
        layout.replaceComponent((Component) currentView, (Component) view);
        currentView = view;
    }

    @Override
    public void deactivate(View view) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
public class MyApplication extends Application {

    @Override
    public void init() {
        ViewHandler.initialize(this);

        Window mainWindow = new Window("MyApplication");
        setMainWindow(mainWindow);

        mainWindow.addComponent(new Label("click"));

        Button b1 = new Button("blubb");
        b1.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                getMainWindow().showNotification("button clicked");
                ViewHandler.activateView(YourView.class, this);
            }
        });
        
        mainWindow.addComponent(b1);
    }
}
public class YourView extends AbstractView<VerticalLayout> implements
        ClickListener {

    private TextField comments;
    private Button sendCommentBtn;

    public YourView() {
        super(new VerticalLayout());
        comments = new TextField();
        comments.setCaption("Your comments");
        // content is the layout you provided in the super's constructor
        getContent().addComponent(comments);

        sendCommentBtn = new Button("Send", this);
        getContent().addComponent(sendCommentBtn);
    }

    @Override
    public void activated(Object... params) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void deactivated(Object... params) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void buttonClick(ClickEvent event) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

if the button is clicked, the shownotification appears, but the view “yourclass” not.

dont know the problem :frowning:

Hi

There are a couple problems with your code. First of all, you have created a MainView which is responsible of registering the views and handling the view changes. Even though you’ve implemented this class, you are never using it anywhere, thus, no views are registered. You need to add the MainView to your main window in order for it to work, so add this at the end of your application’s init method

mainWindow.addComponent(new MainView());

After you’ve added this line to your init method, everything should work. However, your MainView sets YourView as its default view, so you will see the YourView immediately in your application. To better demonstrate the actual view changing, you should remove the setting of the default view and modify the activate() method in MainView so that it can handle situations where currentView is null.


    public MainView() {
        setCompositionRoot(layout);
        // Register child views
        ViewHandler.addView(YourView.class, this);
    }

    public void activate(View view) {
        if (!(view instanceof Component)) {
            throw new IllegalArgumentException("View must be a component");
        }
        if (currentView == null) {
            layout.addComponent((Component) view);
        } else {
            layout.replaceComponent((Component) currentView, (Component) view);
        }
        currentView = view;
    }

Another minor problem is that you are throwing an UnsupportedOperationException in the view’s activated() method. Although this does not hinder the view from becoming activated, it will show you a red warning sign in your “blubb” button. Just leave the method unimplemented rather than throwing an exception.