package jas2.bean;

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.util.*;
import jas2.hist.*;
import jas2.swingstudio.*;
import jas2.job.HistogramAccess;

/**
 * Returns the DataSource of some tree item.
 */
public class TreeDataSourceProvider implements HasDataSource 
{
	public DataSource getDataSource(String param) throws IllegalArgumentException
	{
		TreeModel model = JavaAnalysisStudio.getApp().getTreeModel();
		TreeAdaptorSite node = findNode(model,param);
		Object source = node.getAssociatedObject();
		if (source instanceof DataSource) return ((DataSource)source);
      if (source instanceof HistogramAccess) return ((HistogramAccess) source).getData();
		throw new IllegalArgumentException("Path did not lead to a DataSource "+source.getClass());
	}
	protected TreeAdaptorSite findNode(TreeModel model, String path)
	throws IllegalArgumentException 
	{
		TreeAdaptorSite node = (TreeAdaptorSite)model.getRoot();
		StringTokenizer tokenizer = new StringTokenizer(path,"/");
		while (tokenizer.hasMoreTokens()) 
		{
			TreeAdaptorSite newNode = null;
			String token = tokenizer.nextToken();
			// Be nice to Babar, first try a case sensitive search, 
			// but if that fails to find anything try again with a 
			// case insensitive search
			Enumeration children = node.children();
			while (children.hasMoreElements()) 
			{
				TreeAdaptorSite child = (TreeAdaptorSite)children.nextElement();
				if (child.getName().equals(token)) 
				{
				    newNode = child;
					break;
				}
			}
			if (newNode == null) 
			{
				children = node.children();
				while (children.hasMoreElements()) 
				{
					TreeAdaptorSite child = (TreeAdaptorSite)children.nextElement();
					if (child.getName().equalsIgnoreCase(token)) 
					{
					    newNode = child;
						break;
					}
				}
				if (newNode == null) 
				{
					throw new IllegalArgumentException("Node "+token+" in "+path+" not found");
				}
			}
			node = newNode;
		}
		return node;
	}
}

 

Ads help maintain this website.