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

jadex.base.gui.asynctree.AsyncTreeCellRenderer Maven / Gradle / Ivy

There is a newer version: 2.4
Show newest version
package jadex.base.gui.asynctree;

import jadex.commons.gui.CombiIcon;

import java.awt.Component;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultTreeCellRenderer;


/**
 * Renderer for component tree cells.
 */
public class AsyncTreeCellRenderer extends DefaultTreeCellRenderer
{
	// -------- TreeCellRenderer interface --------

	/**
	 * Get the cell renderer for a node.
	 */
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean selected, boolean expanded, boolean leaf, int row,
			boolean hasFocus)
	{
		assert SwingUtilities.isEventDispatchThread();
		
		// Change icons depending on node type.
		ITreeNode node = (ITreeNode)value;
		Icon icon = node.getIcon();
		String tooltip = node.getTooltipText();
		// Add overlays to icon (if any).
		if(tree.getModel() instanceof AsyncTreeModel)
		{
			List icons = null;
			INodeHandler[] handlers = ((AsyncTreeModel)tree.getModel()).getNodeHandlers();
			for(int i = 0; handlers != null && i < handlers.length; i++)
			{
				Icon overlay = handlers[i].getOverlay(node);
				if(overlay != null)
				{
					if(icons == null)
					{
						icons = new ArrayList();
						if(icon != null)
						{
							// Base icon.
							icons.add(icon);
						}
					}
					icons.add(overlay);
				}
			}
			if(icons != null)
			{
				icon = new CombiIcon((Icon[])icons.toArray(new Icon[icons
						.size()]));
			}
		}
		if(icon != null)
		{
			setOpenIcon(icon);
			setClosedIcon(icon);
			setLeafIcon(icon);
		}
		else
		{
			setOpenIcon(getDefaultOpenIcon());
			setClosedIcon(getDefaultClosedIcon());
			setLeafIcon(getDefaultLeafIcon());
		}
		if(tooltip!=null)
		{
			setToolTipText(tooltip);
		}
		
		JComponent comp = (JComponent)super.getTreeCellRendererComponent(tree,
			getLabel(node), selected, expanded, leaf, row, hasFocus);

		return comp;
	}

	/**
	 *  Overwritten to clear background behind icon too.
	 *  Required for semi-transparent icons.
	 */
	public void paint(Graphics g)
	{
		if(getBackground()!=null)
		{
			g.setColor(getBackground());
			g.fillRect(0, 0, getWidth(), getHeight());
		}
		super.paint(g);
	}
	
	/**
	 *  Get the label for a node.
	 *  May be overridden by sub classes.
	 */
	protected String	getLabel(ITreeNode node)
	{
		return node.toString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy