AppFoundation - View Module

I’m having trouble getting started with the AppFoundation View Module. Below is the code I’ve put together. The code (shown below) compiles and runs, but brings up a blank window in my browser. The logging output is the following:


JTest starting...
BinRoot : /app/JART/JART/bin
DataRoot: /app/JART/data/JART
DB Url     : jdbc:mysql://localhost:9999/dbname
Login View called

After the LoginView constructor is called, I don’t see any more print statements, so the view is not being activated. Can someone show me what I am missing? What do I need to do to put the text fields and buttons up on the screen?

A couple of things I noticed:

  1. In MainView, I changed the activate() routine to take a parameter of AbstractView<?> - and then Eclipse forced me to create another routine activate(View v). Why?

  2. What is the difference between calling activateView() and just calling activate()? Is activate() supposed to be called by the user? Or only by the library (internally)?

Thanks,

nbc


File::JtestApplication.java


package com.verisign.jart;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.vaadin.appfoundation.view.ViewHandler;

import com.vaadin.Application;
import com.vaadin.ui.*;
import com.verisign.common.rrd.db.Ctl_DB_Base;
import com.verisign.common.utils.VRSNLogger;
import com.verisign.common.vaadin.window.VRSN_Application;
//import com.verisign.jart.db.dbManager_JART;

public class JtestApplication extends VRSN_Application {
	private static final long serialVersionUID = 1L;
	private static final VRSNLogger Logger = VRSNLogger.getLogger(JtestApplication.class);
	private static String sJART_DataProperties = null;
	private String dbPassword = null;
	private String dbURL = null;
	private String dbUser = null;

	public static String getsJART_DataProperties() {
		return sJART_DataProperties;
	}

	@Override
	public void init() {
		Logger.info("JTest starting...");
		
		getContext().addTransactionListener(new ViewHandler(this));
 
		// Initialize configuration properties
		initProperties();
		
		// Initialize JART specific properties, including database, RRD Host etc.
		initJARTProperties();

		Window mainWindow = new Window("Jtest Application");
		setMainWindow(mainWindow);

		mainWindow.addComponent(new MainView(this));
	}

	private void initProperties(){
		InputStream is=null;
		try{
			is = Application.class.getClassLoader().getResourceAsStream("resources/config.properties");
		}
		catch(Exception e){
			Logger.error("Error loading 'config.properties' properties", e);
			return;
		}

		if (is!=null){
			try{
				Properties props=new Properties();
				props.load(is);
				binRoot = props.getProperty("bin.rootdir");
				dataRoot = props.getProperty("data.rootdir");

				Logger.info("BinRoot : " + binRoot);
				Logger.info("DataRoot: " + dataRoot);

                sJART_DataProperties = dataRoot + "/../JART_DataProperties.txt";
				logDir = dataRoot + "/log";
			}
			catch (IOException e){
				Logger.error("Error reading properties 'config.properties' ", e);
			}
		}
	}

	private void initJARTProperties(){
		InputStream is=null;

		try{
			is = new FileInputStream(getsJART_DataProperties());
		}
		catch(Exception e){
			Logger.error("Error loading " + getsJART_DataProperties(), e);
			return;
		}

		if (is!=null){
			try{
				Properties props=new Properties();
				props.load(is);

				dbUser = props.getProperty("db.username");
				dbPassword = props.getProperty("db.password");
				dbURL = props.getProperty("db.url");
				Ctl_DB_Base.setDBProperties(dbUser, dbPassword, dbURL);
			}
			catch (IOException e){
				Logger.error("Error reading properties " + getsJART_DataProperties(), e);
			}
		}
	}
}

===========
File::MainView.java

package com.verisign.jart;


import org.vaadin.appfoundation.view.AbstractView;
import org.vaadin.appfoundation.view.View;
import org.vaadin.appfoundation.view.ViewContainer;
import org.vaadin.appfoundation.view.ViewHandler;

import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.verisign.common.utils.VRSNLogger;

public class MainView extends HorizontalLayout implements ViewContainer {
	private static final long serialVersionUID = 1L;
	private static final VRSNLogger Logger = VRSNLogger.getLogger(MainView.class);
	private JtestApplication myApp = null;

	public JtestApplication getMyApp(){
		return(myApp);
	}

	private AbstractView<?> mainView = null;

	public MainView(JtestApplication app){
		setSizeFull();

		setMargin(true);
		setSpacing(true);
		myApp = app;

		ViewHandler.addView(LoginView.class, this);

		ViewHandler.activateView(new LoginView(new VerticalLayout(), myApp));
	}

	public void activate(AbstractView<?> view) {
		Logger.info("MainView:: Activate");
		if(mainView == null){
			addComponent(view);
		}
		else{
			replaceComponent(mainView, view);
		}
		mainView = view;
	}

	public void deactivate(View view) {
		Logger.info("MainView:: Deactivate");
	}

	public void activate(View view) {
		Logger.info("Should not call this...");
	}

}

=============
File:: LoginView.java 
package com.verisign.jart;


import org.vaadin.appfoundation.view.AbstractView;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.verisign.common.utils.VRSNLogger;

public class LoginView extends AbstractView<VerticalLayout> implements ClickListener {
	private static final long serialVersionUID = 1L;
	private static final VRSNLogger Logger = VRSNLogger.getLogger(LoginView.class);
	private JtestApplication myApp = null;
	private TextField userName = null;
	private PasswordField passWord = null;
	private Button loginButton = null;
	
	public JtestApplication getApp(){
		return(myApp);
	}
	
	protected LoginView(VerticalLayout layout, JtestApplication app) {
		super(layout);
		Logger.info("Login View called");
		myApp = app;
		
		userName = new TextField("UserName:");
		userName.setNullRepresentation("");
		
		passWord = new PasswordField("Password:");
		passWord.setNullRepresentation("");
		
		loginButton = new Button("Login");
		
		layout.addComponent(userName);
		layout.addComponent(passWord);
		layout.addComponent(loginButton);
	}

	public void buttonClick(ClickEvent event) {
		Logger.info("11111");
	}

	public void activated(Object... params) {
		Logger.info("22222");
	}

	public void deactivated(Object... params) {
		Logger.info("33333");
	}
}

Hi

The problem you are experiencing is due the parameters you give the activateView -method. Let’s take a closer look. You register the view like this

ViewHandler.addView(LoginView.class, this);

…and then the next line you try to active the view like this

ViewHandler.activateView(new LoginView(new VerticalLayout(), myApp));

In other words, you’re trying to activate the view using a view instance, not the view id. What you should be doing, is calling

ViewHandler.activateView(LoginView.class);

However, this will not work because the ViewHandler now expects the LoginView to contain a default constructor, which it doesn’t. Luckily, it is really easy to create. Your current constructor looks like this

protected LoginView(VerticalLayout layout, JtestApplication app) {
    super(layout);
    ....
}

There is no need for you to pass either of those two arguments to the constructor, since the vertical layout you can create within the constructor itself and the application instance you can get through calling getApplication() after the view has been attached. So, the default constructor could look like this

public LoginView() {
    super(new VerticalLayout());
    ....
}

If you really need to pass some parameters to the constructor, then you need to implement your custom
ViewFactory
(you can extend the
DefaultViewFactory
) and then assign your factory by calling
ViewHandler.setDefaultViewFactory(yourFactory)
.

  • Kim

I think I understand now - that makes sense. I got one example working and I think the rest should fall into place.

Thanks very much!

nbc

Hi Kim,

I think I have the view part working - I did create a new view factory for one of my views and it seems to work. Now I would like to be able to incorporate a URI into the system, and I can’t seem to make that work. My first view is the login page which you get to by entering a URL like:


http://mysystem/myprogram

but if the user types:


http://mysystem/myprogram#playlist/foobar

Then I would like to skip the login view, log the user in as ‘anonymous’, and go directly to my ‘Playlist’ view. I have several problems with that at the moment. Any help you could give me, or pointers to some examples would be a great help… The problems I have are these:

  1. Right now, it seems that I have to activate the login view in my application init() routine. If I don’t do that, how do I load that page?

  2. I tried creating a URI handler, but it won’t pick up the arguments (#playlist with arg = foobar) inside of the init() routine - it looks like that gets called after init() - but by that time, I’ve already loaded the login view. I could try loading the login page in the URI handler, but does that get called if there is no URI?

I found another thread that was discussing argument processing, and I think you mentioned that the user should try using your appfoundation add-on - particularly the ‘view’ library. That drove me to get that addon running, (and I like it - thanks for that!) but I’m still not clear on the ‘correct’ way to use it to pull in URL arguments or URI fragments. Some exposition would help me greatly…

Thanks in advance,

nbc