package jas2.swingstudio;
import jas2.util.*;
import jas2.util.gui.*;
import jas2.loader.*;
import java.util.*;
import java.io.*;
import javax.swing.*;

public class JASSourcePath extends UserClassPath
{
	private JASSourcePath()
	{
		this(Application.getApplication().getUserProperties().getBoolean("SourcePathPanel.enabled",false));
	}
	private JASSourcePath(boolean enabled)
	{
		// if enabled is false the JASSourcePath is always equivalent to JASClassPath
		super("JAS_SOURCEPATH");
		this.enabled = enabled;
	}
	public Enumeration elements()
	{
		if (enabled) return super.elements();
		else return JASClassPath.create().elements();
	}
	/**
	 * Create a (unique) instance of JASSourcePath
	 */
	public static JASSourcePath create()
	{
		return singleton;
	}
	/**
	 * Find a file by searching in the source path
	 */
	public File findFile(String fileName)
	{
		Enumeration e = elements();
		while (e.hasMoreElements())
		{
			final File dir = (File) e.nextElement();
			final File f = new File(dir,fileName);
			if (f.exists()) return f;
		}
		return null; // not found
	}
	boolean isEnabled()
	{
		return enabled;
	}
	void setEnabled(boolean value)
	{
		Application.getApplication().getUserProperties().setBoolean("SourcePathPanel.enabled",value);
		enabled = value;
		if (enabled) 
		{
			Vector v = new Vector();
			Enumeration e = JASClassPath.create().elements();
			while (e.hasMoreElements()) v.addElement(e.nextElement());
			setContents(v);
		}
	}
	private boolean enabled;
	private static JASSourcePath singleton = new JASSourcePath();
}
class SourcePathPanel extends JASAddRemovePanel
{
	SourcePathPanel(UserClassPath p)
	{
		super(p,"Look for source files in these directories:",
				"Class path",null,new ClassPathListCellRenderer());
	}

	protected Object add(String addText)
	{
		String dir = Application.getApplication().getUserProperties().getProperty("SourcePathPanel.dir");
		final JDirectoryChooser choose = new JDirectoryChooser(dir);
		choose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		choose.setDialogTitle("Select Directory to Add");
		
		int rc = choose.showDialog(this);
		if (rc != choose.APPROVE_OPTION) return null;
		File f = choose.getSelectedFile();
		Application.getApplication().getUserProperties().put("SourcePathPanel.dir",f.toString());	
		return f;		
	}
}

 

Ads help maintain this website.