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

com.talk2object.plum.interaction.generic.SubViewAction Maven / Gradle / Ivy

package com.talk2object.plum.interaction.generic;

import org.apache.log4j.Logger;

import com.talk2object.plum.interaction.AbstractSingleMethodInteraction;
import com.talk2object.plum.interaction.annotation.Action;
import com.talk2object.plum.interaction.application.PlumApplicationContextUtils;
import com.talk2object.plum.interaction.session.SessionContext;
import com.talk2object.plum.interaction.session.SessionContextUtils;
import com.talk2object.plum.interaction.view.component.containers.window.FormView;
import com.talk2object.plum.view.component.Component;
import com.talk2object.plum.view.component.containers.Panel;
import com.talk2object.plum.view.component.containers.window.WindowManager;
import com.talk2object.plum.view.context.ViewSessionContextUtils;
import com.talk2object.plum.viewgeneration.classexpr.ClassExpressions;

/**
 * 
 * @author jack
 *
 */
public class SubViewAction extends AbstractSingleMethodInteraction {
	private FormView subView;

	public SubViewAction(Object model) {
		super(model);
	}

	public SubViewAction(Object model, String methodName) {
		super(model, methodName);

	}

	private static Logger logger = Logger.getLogger(SubViewAction.class);

	@Override
	public void start() {
		Action actionAnnotation = method.getAnnotation(Action.class);

		try {
			Object result = method.invoke(model);

			if (result == null)
				throw new RuntimeException("subView is null.");

			com.talk2object.plum.viewgeneration.ViewFactory viewFactory = PlumApplicationContextUtils
					.getViewFactory();

			Component subView = null;
			if (result instanceof Component)
				subView = (Component) result;
			else {
				if (actionAnnotation.modelClasses().length == 0) {
					subView = viewFactory.create(result, ClassExpressions
							.many(actionAnnotation.viewClasses()));
				} else {
					subView = viewFactory.create(result, ClassExpressions
							.many(actionAnnotation.modelClasses()),
							ClassExpressions.many(actionAnnotation
									.viewClasses()));
				}

				if (subView == null)
					throw new RuntimeException(
							"failed to create view for modeclass:"
									+ result.getClass().getSimpleName());
			}

			// inject subview into content panel
			// of main window.
			SessionContext sessionContext = SessionContextUtils
					.getCurrentSessionContext();
			WindowManager windowManager = ViewSessionContextUtils
					.getWindowManager(sessionContext);
			Panel body = windowManager.getMainWindow().getBodyPanel();
			body.removeAllChildren();

			body.addChild(subView);

		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy