LoginForm Window

Hi Guys,

I have 2 separate questions. The main purpose is to build application with autorization module. I have implemented the appfoundation for this purpose, and it works correctly.

The problems which I have, right now, are connected with layout / application structure.

[b]

  1. First Problem
    [/b]

I have the main class in my webapp with the init() method called GrafikMakerUI, which extends UI class. Also in this class I build the layout. De facto layout is divided into 3 sections:
a) header
b) content
c) footer

In header I have logo, menubar and the userPanel. For each components I have separate methods to build them.

So there is method: buildUserPanel(), which checks if in the session there is no User object. If there is not it prints the two buttons: RegisterButton and LoginButton.

The LoginButton has the ClickListener() which, when is called, run the method buildLoginView():


	private void buildLoginView(){
		
		LoginPanel loginPanel = new LoginPanel(); 
		
		UI.getCurrent().addWindow(loginPanel);
		
	}

As you can see on the example above, I have the external class LoginPanel which contains:


public class LoginPanel extends Window {

	private Window loginModal;

	public LoginPanel() {
		super("Wymagana autoryzacja");
		buildLoginPanel();
		setContent(loginModal);
	}

	private void buildLoginPanel() {

		........

		final Button signin = new Button("Zaloguj");
		........

		signin.addClickListener(new ClickListener() {

			private static final long serialVersionUID = 1L;

			@SuppressWarnings({ "deprecation" })
			public void buttonClick(ClickEvent event) {
				try {
					AuthenticationUtil.authenticate(username.getValue(),
							password.getValue());
					loginModal.close();
                                        ...................
                                      [i]
[b]
Method for webapp refresh
[/b]
[/i]
                                        ....................
				} catch (InvalidCredentialsException e) {
					if (errorLabels.getComponentCount() > 0) {
						// Remove the previous error message
						errorLabels.removeComponent(errorLabels.getComponent(0));
					}
					Label error = new Label(
							"Nieprawidłowa nazwa użytkownika i/lub hasło",
							ContentMode.HTML);
					error.addStyleName("error");

					errorLabels.setSizeFull();
					errorLabels.addComponent(error);
					errorLabels.setComponentAlignment(error,
							Alignment.MIDDLE_CENTER);
					username.focus();

				} catch (AccountLockedException e) {
					if (errorLabels.getComponentCount() > 0) {
						// Remove the previous error message
						errorLabels.removeComponent(errorLabels.getComponent(0));
					}
					Label error = new Label(
							"Konto zostało zablokowane, prosimy o kontakt z administratorem",
							ContentMode.HTML);
					error.addStyleName("error");
					// Add animation
					errorLabels.setSizeFull();
					errorLabels.addComponent(error);
					errorLabels.setComponentAlignment(error,
							Alignment.MIDDLE_CENTER);
					username.focus();
				}
			}
		});

		signin.addShortcutListener(enter);
		loginModal.addShortcutListener(esc);
		loginPanel.addComponent(fields);
		loginPanel.addComponent(errorLabels);
		loginModal.setContent(loginPanel);
	}
}

The first problem which I am fighting with is the error:


Caused by: java.lang.IllegalArgumentException: A Window can only be added to a UI using UI.addWindow(Window window)
	at com.vaadin.ui.Window.setParent(Window.java:139)
	at com.vaadin.ui.AbstractSingleComponentContainer.setContent(AbstractSingleComponentContainer.java:137)
	at com.example.grafikmaker.UI.LoginPanel.<init>(LoginPanel.java:34)
	at com.example.grafikmaker.UI.GrafikMakerUI.buildLoginView(GrafikMakerUI.java:251)
	at com.example.grafikmaker.UI.GrafikMakerUI.access$0(GrafikMakerUI.java:249)
	at com.example.grafikmaker.UI.GrafikMakerUI$1.buttonClick(GrafikMakerUI.java:206)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
	... 32 more

I don’t know, why do I get it. Class LoginPanel extends Window class, so there should be no problem to add this component to the UI.

Additionally I can say, that I have tried cast LoginPanel to Window by creating LoginPanel in this way:


	private void buildLoginView(){
		
		Window loginPanel = (Window) new LoginPanel(); 
		
		UI.getCurrent().addWindow(loginPanel);
		
	}

and it didn’t work;

Of course, one from the possible solutions (which is not satisfying for me) is to make (within LoginPanel class) other method which will have public modifier and Window object as result. After that, it will be possible to call this method and receive the desired object (Window with the login form) . But for me it is not “nice” solution. I would like to have Class which constructor gives me what I want.


2. The second problem
is still related to the LoginWindow problem. Precisely my question is: how, in Vaadin 7, is it possible to refresh application (reload page) from an external class. In my case from LoginPanel class (I have selected the code above, where - after the autorization - refreshing should take place). I have searched the web and I found only one good article (form vaadin wiki: https://vaadin.com/wiki/-/wiki/Main/Authenticating+Vaadin-based+applications/pop_up ) but, the method (open()) , which is showed there is based on Vaadin 6, and it can’t be used in vaadin in version 7. Do you have any suggestions here ?

Thank you in advance

  1. In LoginPanel you have private field loginModal of type Window. You then try to set it as a content of LoginPanel Window, which is not allowed.

  2. Instead of thinking how to refresh browser page you should instead just update the UI with new content calling UI#setContent(Component)