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

jadex.bdi.planlib.DefaultBDIViewerPanel Maven / Gradle / Ivy

Go to download

The Jadex applib BDI package contain ready to use functionalities for BDI agents mostly in form of modules called capabilities.

There is a newer version: 2.4
Show newest version
package jadex.bdi.planlib;

import jadex.base.gui.componentviewer.AbstractComponentViewerPanel;
import jadex.base.gui.componentviewer.IAbstractViewerPanel;
import jadex.base.gui.componentviewer.IComponentViewerPanel;
import jadex.base.gui.plugin.IControlCenter;
import jadex.bdi.runtime.IBDIInternalAccess;
import jadex.bdi.runtime.ICapability;
import jadex.bridge.IComponentStep;
import jadex.bridge.IExternalAccess;
import jadex.bridge.IInternalAccess;
import jadex.commons.SReflect;
import jadex.commons.future.CounterResultListener;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.transformation.annotations.Classname;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 *  Default panel for viewing BDI agents that include viewable capabilities. 
 */
public class DefaultBDIViewerPanel extends AbstractComponentViewerPanel
{
	//-------- constants --------
	
	/** The constant for the agent optional viewerclass. */
	public static final String PROPERTY_AGENTVIEWERCLASS = "bdiviewerpanel.agentviewerclass";
	
	/** The constant for the agent optional viewerclass. */
	public static final String PROPERTY_INCLUDESUBCAPABILITIES = "bdiviewerpanel.includesubcapabilities";
	
	//-------- attributes --------
	
	/** The panel. */
	protected JPanel panel;
	
	/** The classloader for the component. */
	protected ClassLoader cl;
	
	//-------- methods --------
	
	/**
	 *  Called once to initialize the panel.
	 *  Called on the swing thread.
	 *  @param jcc	The jcc.
	 * 	@param component The component.
	 */
	public IFuture init(final IControlCenter jcc, final IExternalAccess component)
	{
		final Future ret = new Future();
		
		this.panel = new JPanel(new BorderLayout());
		
		// Init interface is asynchronous but super implementation is not.
		IFuture	fut	= super.init(jcc, component);
		assert fut.isDone();
		
		jcc.getClassLoader(component.getModel().getResourceIdentifier())
			.addResultListener(new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(ClassLoader result)
			{
				cl	= result;
				component.scheduleStep(new IComponentStep()
				{
					@Classname("createPanels")
					public IFuture execute(IInternalAccess ia)
					{
						final IBDIInternalAccess scope	= (IBDIInternalAccess)ia;
						String[] subcapnames = (String[])scope.getModel().getProperty(PROPERTY_INCLUDESUBCAPABILITIES, cl);
						if(subcapnames==null)
						{
							subcapnames = (String[])scope.getSubcapabilityNames();
						}
						createPanels(scope, subcapnames, ret);
					
						return IFuture.DONE;
					}
				}).addResultListener(new DelegationResultListener(ret));
			}
		});
		
		return ret;
	}
	
	/**
	 *  Create the panels.
	 */
	protected void createPanels(IBDIInternalAccess scope, String[] subcapnames, Future ret)
	{
		final List panels = new ArrayList();
		
		final CounterResultListener lis = new CounterResultListener(
			subcapnames.length+1, true, new DelegationResultListener(ret)
		{
			public void customResultAvailable(Void result)
			{
//				if(subpanels.size()==1)
//				{
//					Object[] tmp = (Object[])subpanels.get(0);
//					add(((IComponentViewerPanel)tmp[1]).getComponent(), BorderLayout.CENTER);
//				}
//				else if(subpanels.size()>1)
				{
					JTabbedPane tp = new JTabbedPane();
					for(int i=0; i clazz	= SReflect.classForName(clname, scope.getClassLoader());
				IComponentViewerPanel panel = (IComponentViewerPanel)clazz.newInstance();
				panels.add(new Object[]{"agent", panel});
				panel.init(jcc, getActiveComponent()).addResultListener(lis);
			}
			catch(Exception e)
			{
				lis.exceptionOccurred(e);
			}
		}
		else
		{
			lis.exceptionOccurred(new RuntimeException("No viewerclass: "+clname));
		}
		
		// Capability panels.
		if(subcapnames!=null)
		{
			for(int i=0; i clazz = clid instanceof Class? (Class)clid: clid instanceof String? SReflect.classForName0((String)clid, subcap.getClassLoader()): null;
				try
				{
					IComponentViewerPanel panel = (IComponentViewerPanel)clazz.newInstance();
					panels.add(new Object[]{subcapnames[i], panel});
					panel.init(jcc, subcap.getExternalAccess()).addResultListener(lis);
				}
				catch(Exception e)
				{
					lis.exceptionOccurred(e);
				}
			}
		}
	}
	
	/**
	 *  The id used for mapping properties.
	 * /
	public String getId()
	{
		return "default_bdi_viewer_panel";
	}*/

	/**
	 *  The component to be shown in the gui.
	 *  @return	The component to be displayed.
	 */
	public JComponent getComponent()
	{
		return panel;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy