All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.talk2object.plum.interaction.viewbuilder.AnnotationViewBuilder Maven / Gradle / Ivy

There is a newer version: 0.1.22
Show newest version
package com.talk2object.plum.interaction.viewbuilder;

import com.talk2object.plum.interaction.application.PlumApplicationContextUtils;
import com.talk2object.plum.interaction.interaction.ActionBuilder;
import com.talk2object.plum.interaction.interaction.SortedActions;
import com.talk2object.plum.interaction.model.Application;
import com.talk2object.plum.interaction.rich.field.ViewMode;
import com.talk2object.plum.interaction.view.annotation.View;
import com.talk2object.plum.interaction.viewgeneneration.AbstractViewBuilder;
import com.talk2object.plum.interaction.viewgeneneration.BuilderMatchPolicy;
import com.talk2object.plum.interaction.viewgeneneration.ViewBuildContext;
import com.talk2object.plum.interaction.viewgeneneration.ViewClass;
import com.talk2object.plum.interaction.viewgeneneration.ViewDetail;
import com.talk2object.plum.interaction.viewgeneneration.ViewFactory;
import com.talk2object.plum.view._abstract.components.containers.window.MainWindow;
import com.talk2object.plum.view._abstract.components.menu.MenuBar;
import com.talk2object.plum.view._abstract.window.UIComponent;

/**
 * This is a view builder for any model which has {@link View} annotation.
 * 
 * @author jack
 *
 */
public class AnnotationViewBuilder extends AbstractViewBuilder {

	@Override
	public UIComponent build(Object model, String viewClass,
			ViewBuildContext parameterObject, UIComponent parentView) {
		Class modelClass = model.getClass();
		com.talk2object.plum.interaction.view.annotation.View viewAnnotation = (com.talk2object.plum.interaction.view.annotation.View) modelClass
				.getAnnotation(com.talk2object.plum.interaction.view.annotation.View.class);
		Class viewJavaClass = viewAnnotation.clazz();

		MainWindow view = null;
		Application app = (Application) model;
		try {
			view = (MainWindow) viewJavaClass.newInstance();
			view.setModel(model);

			view.setTitle(app.getTitle());

			// create menu from app object

			ActionBuilder actionBuilder = new ActionBuilder(model);
			SortedActions actions = actionBuilder.build();

			ViewFactory viewFactory = PlumApplicationContextUtils
					.getViewFactory();
			 
			UIComponent menuItems = viewFactory.create(actions,
					BuilderMatchPolicy.EXACT, ViewClass.MENU.compatibleExpr(),
					ViewBuildContext.DEFAULT, null);

			MenuBar menuBar = new MenuBar();
			menuBar.addChildComponent(menuItems, true);
			view.getMenuPanel().addChild(menuBar);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		// req.getSession().setAttribute(SESSION_KEY_VIEW, view);
		return view;
	}

	@Override
	public Class getModelClass() {

		return null;
	}

	public static boolean hasViewAnnotation(Class modelClass) {

		com.talk2object.plum.interaction.view.annotation.View viewAnnotation = (com.talk2object.plum.interaction.view.annotation.View) modelClass
				.getAnnotation(com.talk2object.plum.interaction.view.annotation.View.class);
		return viewAnnotation != null;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy