jadex.base.gui.componenttree.ComponentTreeNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-tools-base-swing Show documentation
Show all versions of jadex-tools-base-swing Show documentation
GUI-dependent classes for jadex-tools-base.
package jadex.base.gui.componenttree;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import jadex.base.SRemoteGui;
import jadex.base.gui.CMSUpdateHandler;
import jadex.base.gui.asynctree.AbstractSwingTreeNode;
import jadex.base.gui.asynctree.AsyncSwingTreeModel;
import jadex.base.gui.asynctree.ISwingTreeNode;
import jadex.base.gui.asynctree.ITreeNode;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IExternalAccess;
import jadex.bridge.nonfunctional.INFPropertyMetaInfo;
import jadex.bridge.nonfunctional.SNFPropertyProvider;
import jadex.bridge.service.IServiceIdentifier;
import jadex.bridge.service.ProvidedServiceInfo;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.types.cms.ICMSComponentListener;
import jadex.bridge.service.types.cms.IComponentDescription;
import jadex.bridge.service.types.cms.IComponentManagementService;
import jadex.commons.SReflect;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;
import jadex.commons.gui.CombiIcon;
import jadex.commons.gui.SGUI;
import jadex.commons.gui.future.SwingResultListener;
/**
* Node object representing a service container.
*/
public class ComponentTreeNode extends AbstractSwingTreeNode implements IActiveComponentTreeNode
{
//-------- constants --------
/**
* The image icons.
*/
public static final UIDefaults icons = new UIDefaults(new Object[]
{
"overlay_check", SGUI.makeIcon(ComponentTreeNode.class, "/jadex/base/gui/images/overlay_check.png"),
// "overlay_busy", SGUI.makeIcon(ComponentTreeNode.class, "/jadex/base/gui/images/overlay_busy.png")
"overlay_busy", SGUI.makeIcon(ComponentTreeNode.class, "/jadex/base/gui/images/overlay_clock.png"),
"overlay_system", SGUI.makeIcon(ComponentTreeNode.class, "/jadex/base/gui/images/overlay_system.png")
});
//-------- attributes --------
/** The component description. */
protected IComponentDescription desc;
/** The component management service. */
protected final IComponentManagementService cms;
/** The platform access. */
protected IExternalAccess access;
/** The icon cache. */
protected final ComponentIconCache iconcache;
/** The properties component (if any). */
protected ComponentProperties propcomp;
/** The cms listener (if any). */
protected ICMSComponentListener cmslistener;
/** The component id for listening (if any). */
protected IComponentIdentifier listenercid;
/** Flag indicating a broken node (e.g. children could not be searched due to network problems). */
protected boolean broken;
/** Flag indicating a busy node (e.g. update in progress). */
protected boolean busy;
//-------- constructors --------
/**
* Create a new service container node.
*/
public ComponentTreeNode(ISwingTreeNode parent, AsyncSwingTreeModel model, JTree tree, IComponentDescription desc,
IComponentManagementService cms, ComponentIconCache iconcache, IExternalAccess access)
{
super(parent, model, tree);
assert desc!=null;
// System.out.println("node: "+getClass()+" "+desc.getName()+" "+desc.getType());
this.desc = desc;
this.cms = cms;
this.iconcache = iconcache;
this.access = access;
model.registerNode(this);
// Add CMS listener for platform node.
if(desc.getName().getParent()==null)
{
addCMSListener(desc.getName());
}
}
//-------- AbstractComponentTreeNode methods --------
/**
* Get the id used for lookup.
*/
public Object getId()
{
return desc.getName();
}
/**
* Get the icon as byte[] for a node.
*/
public byte[] getIcon()
{
return null;
}
/**
* Get the icon for a node.
*/
public Icon getSwingIcon()
{
Icon icon = iconcache.getIcon(desc.getType(), this, getModel());
if(busy)
{
icon = icon!=null ? new CombiIcon(new Icon[]{icon, icons.getIcon("overlay_busy")}) : icons.getIcon("overlay_busy");
}
else if(broken)
{
icon = icon!=null ? new CombiIcon(new Icon[]{icon, icons.getIcon("overlay_check")}) : icons.getIcon("overlay_check");
}
if(desc.isSystemComponent())
{
icon = icon!=null ? new CombiIcon(new Icon[]{icon, icons.getIcon("overlay_system")}) : icons.getIcon("overlay_system");
}
return icon;
}
/**
* Get tooltip text.
*/
public String getTooltipText()
{
return desc.toString();
}
/**
* Refresh the node.
* @param recurse Recursively refresh subnodes, if true.
*/
public void refresh(final boolean recurse)
{
// System.out.println("CTN refresh: "+getId());
busy = true;
getModel().fireNodeChanged(ComponentTreeNode.this);
cms.getComponentDescription(desc.getName())
.addResultListener(new SwingResultListener(new IResultListener()
{
public void resultAvailable(IComponentDescription result)
{
ComponentTreeNode.this.desc = (IComponentDescription)result;
broken = false;
busy = false;
getModel().fireNodeChanged(ComponentTreeNode.this);
ComponentTreeNode.super.refresh(recurse);
}
public void exceptionOccurred(Exception exception)
{
broken = true;
busy = false;
getModel().fireNodeChanged(ComponentTreeNode.this);
}
}));
}
/**
* Asynchronously search for children.
* Should call setChildren() once children are found.
*/
protected void searchChildren()
{
busy = true;
getModel().fireNodeChanged(ComponentTreeNode.this);
// if(getComponentIdentifier().getName().indexOf("Garbage")!=-1)
// System.out.println("searchChildren: "+getId());
searchChildren(cms, getComponentIdentifier())
.addResultListener(new IResultListener>()
{
public void resultAvailable(List result)
{
broken = false;
busy = false;
getModel().fireNodeChanged(ComponentTreeNode.this);
setChildren(result);
}
public void exceptionOccurred(Exception exception)
{
broken = true;
busy = false;
getModel().fireNodeChanged(ComponentTreeNode.this);
List res = Collections.emptyList();
setChildren(res);
}
});
}
//-------- methods --------
/**
* Create a new component node.
*/
public ISwingTreeNode createComponentNode(final IComponentDescription desc)
{
ISwingTreeNode node = getModel().getNode(desc.getName());
if(node==null)
{
boolean proxy = "jadex.platform.service.remote.Proxy".equals(desc.getModelName());
if(proxy)
{
// Only create proxy nodes for local proxy components to avoid infinite nesting.
if(((IActiveComponentTreeNode)getModel().getRoot()).getComponentIdentifier().getName().equals(desc.getName().getPlatformName()))
{
// System.out.println("proxy for: "+desc.getName()+" from: "+getDescription().getName());
node = new ProxyComponentTreeNode(this, getModel(), getTree(), desc, cms, iconcache, access);
}
else
{
// System.out.println("creating pseudo: "+desc.getName()+" from: "+getDescription().getName());
node = new PseudoProxyComponentTreeNode(this, getModel(), getTree(), desc, cms, iconcache, access);
}
}
else
{
node = new ComponentTreeNode(this, getModel(), getTree(), desc, cms, iconcache, access);
}
}
return node;
}
/**
* Create a string representation.
*/
public String toString()
{
return desc.getName().getLocalName();
}
/**
* Get the component description.
*/
public IComponentDescription getDescription()
{
return desc;
}
/**
* Get the component id.
*/
public IComponentIdentifier getComponentIdentifier()
{
return desc!=null? desc.getName(): null;
}
/**
* Set the component description.
*/
public void setDescription(IComponentDescription desc)
{
this.desc = desc;
if(propcomp!=null)
{
propcomp.setDescription(desc);
propcomp.repaint();
}
}
/**
* True, if the node has properties that can be displayed.
*/
public boolean hasProperties()
{
return true;
}
/**
* Get or create a component displaying the node properties.
* Only to be called if hasProperties() is true;
*/
public JComponent getPropertiesComponent()
{
// Refresh to update cid addresses later.
refresh(false);
if(propcomp==null)
{
propcomp = new ComponentProperties();
}
propcomp.setDescription(desc);
// System.out.println(desc.getName()+" "+getClass().getName());
return propcomp;
}
/**
* Asynchronously search for children.
*/
protected IFuture> searchChildren(final IComponentManagementService cms, final IComponentIdentifier cid)
{
final Future> ret = new Future>();
final List children = new ArrayList();
final boolean ready[] = new boolean[2]; // 0: children, 1: services;
// if(ComponentTreeNode.this.toString().indexOf("Hunter")!=-1)
// System.out.println("searchChildren 1: "+this);
cms.getChildrenDescriptions(cid).addResultListener(new SwingResultListener(new IResultListener()
{
public void resultAvailable(final IComponentDescription[] achildren)
{
// if(ComponentTreeNode.this.toString().indexOf("Hunter")!=-1)
// System.out.println("searchChildren 2: "+ComponentTreeNode.this+" "+achildren.length);
// final IComponentDescription[] achildren = (IComponentDescription[])result;
Arrays.sort(achildren, new java.util.Comparator()
{
public int compare(IComponentDescription o1, IComponentDescription o2)
{
return o1.getName().getName().toLowerCase().compareTo(o2.getName().getName().toLowerCase());
}
});
for(int i=0; i(new IResultListener()
{
public void resultAvailable(IComponentIdentifier root)
{
cms.getExternalAccess(root)
.addResultListener(new SwingResultListener(new IResultListener()
{
public void resultAvailable(final IExternalAccess rootea)
{
cms.getExternalAccess(cid)
.addResultListener(new SwingResultListener(new IResultListener()
{
public void resultAvailable(final IExternalAccess ea)
{
// System.out.println("search childs: "+ea);
SNFPropertyProvider.getNFPropertyNames(ea)
// ((INFPropertyProvider)ea.getExternalComponentFeature(INFPropertyComponentFeature.class)).getNFPropertyNames()
.addResultListener(new SwingResultListener(new IResultListener()
{
public void resultAvailable(String[] names)
{
if(names!=null && names.length>0)
{
NFPropertyContainerNode cn = (NFPropertyContainerNode)getModel().getNode(getId()+NFPropertyContainerNode.NAME);
if(cn==null)
cn = new NFPropertyContainerNode(null, null, ComponentTreeNode.this, getModel(), getTree(), ea, null, null, null);
children.add(0, cn);
cont(ea);
// final NFPropertyContainerNode node = cn;
//
// final List results = new ArrayList();
// Iterator it = SReflect.getIterator(names);
// createNFPropertyNodes(it, results, ea, rootea, cn).addResultListener(new IResultListener()
// {
// public void resultAvailable(Void result)
// {
// Collections.sort(results, new java.util.Comparator()
// {
// public int compare(ISwingTreeNode t1, ISwingTreeNode t2)
// {
// String si1 = ((NFPropertyNode)t1).getMetaInfo().getName();
// String si2 = ((NFPropertyNode)t2).getMetaInfo().getName();
// return si1.compareTo(si2);
// }
// });
//
// node.setChildren(results);
// cont(ea, node, results);
// }
//
// public void exceptionOccurred(Exception exception)
// {
// cont(ea, null, null);
// }
// });
}
else
{
cont(ea);
}
}
public void exceptionOccurred(Exception exception)
{
cont(ea);
}
}));
}
public void cont(final IExternalAccess ea)
{
SRemoteGui.getServiceInfos(ea)
.addResultListener(new SwingResultListener
© 2015 - 2025 Weber Informatics LLC | Privacy Policy