Window problem in vaadin

Hi sir ,
I have been creating one vaadin application . I have application class , Login window , Homewindow.

First i set login window as the mainwindow in application class. When user enter correct username and password , i changed the mainwindow as Homewindow.

What the problem here is that when i refresh the browser page the window automatically replaced by Login window . I have used auto refresher in my Homewindow .

It will cause any problem .

Please give some suggestion to solve this.

Thank you.

I built an entire Vaadin app in a single file to show switching between two windows in Vaadin 6.7.6. I have one window for login, and another window for content.

Create a new Vaadin project, replace with this code, change the name of class and package at top, and run it. I added many calls to System.out.println to help see “beneath the covers”.

Not sure what you mean by “when i refresh the browser page”. Normally neither the programmer nor user wants to cause any refresh/reload of the browser page while running a Vaadin app. Perhaps you can clarify your situation.

Hope this helps you.

package com.example.acmeinvoices;

import com.vaadin.Application;
import com.vaadin.ui.*;

/**
 * Example app in Vaadin 6.7.6 to demonstrate switching between two Windows.
 * One Window is for login, the other Window is for content.
 * Many System.out.println calls to help see "beneath the covers".
 * 
 * © 2012 Basil Bourque. This source code may be used freely forever by anyone taking complete responsibility for doing so.
 * 
 * @author Basil Bourque
 *
 */
public class AcmeinvoicesApplication extends Application implements Application.UserChangeListener {

    private Window contentWindow = null;
    private Window loginWindow = null;

    @Override
    public void init() {
        System.out.println( "DEBUG - Vaadin Application init method running. Now: " + new java.util.Date() );

        // Build content window.
        this.contentWindow = new Window( "See My Content" );
        Label contentLabel = new Label( "Content goes here. " + new java.util.Date() );
        this.contentWindow.addComponent( contentLabel );
        Button logoutButton = new Button( "Logout" );
        logoutButton.addListener( new Button.ClickListener() {

            @Override
            public void buttonClick( Button.ClickEvent event ) {
                System.out.println( "DEBUG - User clicked Logout button." );
                event.getButton().getApplication().setUser( null );
            }
        } );
        this.contentWindow.addComponent( logoutButton );

        // Build login window.
        this.loginWindow = new Window( "Login" );
        Label loginLabel = new Label( "Login stuff goes here. " + new java.util.Date() );
        this.loginWindow.addComponent( loginLabel );
        Label loginLabel2 = new Label( "We let anybody into this app. Just click the 'Login' button. " );
        this.loginWindow.addComponent( loginLabel2 );
        Button loginButton = new Button( "Login" );
        // Add a listener to the button. When user clicks, we set the app's "User" object.
        // We can set any kind of object, any subclass of Object, including String.
        // Setting the User object automatically fires an event. Other objects may listen for that event.
        loginButton.addListener( new Button.ClickListener() {

            @Override
            public void buttonClick( Button.ClickEvent event ) {
                System.out.println( "DEBUG - User clicked Login button." );
                event.getButton().getApplication().setUser( "anonymous user " + new java.util.Date() );
            }
        } );
        this.loginWindow.addComponent( loginButton );

        this.addListener( this ); // Listen for UserChangeEvent.

        setMainWindow( this.loginWindow );
    }

    /**
     * Respond to the user's successful login. Switch out the login window for the main content window.
     * 
     * @see com.vaadin.Application.UserChangeListener#applicationUserChanged(com.vaadin.Application.UserChangeEvent)
     */
    @Override
    public void applicationUserChanged( UserChangeEvent event ) {
        Window oldWindow = this.getMainWindow();
        if ( event.getNewUser() == null ) { // User must have done a logout.
            System.out.println( "DEBUG - applicationUserChanged method. Null user set. " );
            // this.setMainWindow( this.loginWindow );
            this.close(); // Restarts the app by default. Or goes to a static web page if you called setLogoutURL().
            // For more info, see: https://vaadin.com/book/-/page/application.close.html
        } else { // Valid user login.
            System.out.println( "DEBUG - applicationUserChanged method. Showing content window. User: "
                    + event.getNewUser() );
            this.setMainWindow( this.contentWindow );
        }
        this.removeWindow( oldWindow ); // Must be called to actually make the old window disappear, and newly set one appear. Counter-intuitive but necessary.
    }

}

Sir ,

     I have used  the Application.UserChangeListener as per your example code. You are correct  that neither developer nor user don't  want to refresh
     the page while application is  running . Actually Login window and Home window both are seperate class which extends window . I created the object 
     for that classes in Application class and set the mainWindow . Still the problem is while refreshing Home window is replaced by Login window.
     Following code is used in my application.

     [code]

//This is my login action in my LoginWindow class
@Override
public void buttonClick(Button.ClickEvent event)
{

  if(event.getSource() == btnlogin)
  {
       uname = txtusername.getValue().toString();
       pword = txtpwd.getValue().toString();
       try
       {
            BWLogin instance = BWLogin.getInstance ();
            instance.authenticate((String)uname, (String)pword);
            System.out.println( "DEBUG - User clicked Login button." );
            event.getButton().getApplication().setUser( "anonymous user " + new java.util.Date() );
            URL urL = BWLogin.getInstance ().getURL ();
            open ( new ExternalResource (urL));
         }
         catch ( Exception e )
         {
             showNotification ( e.toString ());
         }
  }

}

// This is my logout action in the Application class

public void buttonClick(ClickEvent event)
{
if(event.getSource() == btnlout)
{
System.out.println( “DEBUG - User clicked Logout button.” );
event.getButton().getApplication().setUser( null );
getMainWindow().getApplication().close();
}
}

[/indent]

[/code]

What do you mean by that phrase? Exactly what is the user or the computer doing?

Is the user hitting the web browser’s “Reload” or “Refresh” button to reload the entire web page?

Do you mean when the browser window redraws, such as when being revealed after some other window appeared on top?

Also, Nicolas Fränkel
suggests
that there may be bugs or bad side-effects when switching the
Window
in a Vaadin 6 app. He suggests keeping the single original Window, and then swap out the content of that Window. You can call the Window’s
setContent
method to replace the current/default Layout in that Window. Or you can add and remove components to that current/default Layout.

–Basil Bourque

\

Sir ,
You are correct that when user hitting the web browser’s"Reload" or “Refresh” button to reload the entire web page Home window replaced by Login window.
Now i have changed the code like what you said above . I have only one mainwindow and i changed the Login window and Home Window as customcomponents.(Not exends window)
I changed the view by using setContent method. But still when “Reload” or “Refresh” Home page is changed to Login page. I have one doubt also that I used autorefresher add on in
Home page .It Could cause this problem ? .

 Please give some solution.

Thank you