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

jadex.base.gui.config.ClasspathPanel Maven / Gradle / Ivy

There is a newer version: 4.0.267
Show newest version
package jadex.base.gui.config;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import jadex.commons.Properties;
import jadex.commons.Property;
import jadex.commons.SReflect;
import jadex.commons.SUtil;
import jadex.commons.gui.PropertiesPanel;

/**
 *  Show/edit the class path or the used maven artifacts
 */
public class ClasspathPanel	extends JPanel
{
	//-------- attributes --------
	
	/** The table model. */
	protected DefaultTableModel	model;
	
	/** The current class loader. */
	protected ClassLoader	classloader;
	
	//-------- constructors --------
	
	/**
	 *  Create a class path panel.
	 */
	public ClasspathPanel()
	{
		super(new BorderLayout());
		
		this.model	= new DefaultTableModel(new String[]{"Entries"}, 0)
		{
			public boolean isCellEditable(int row, int column)
			{
				return false;
			}
		};
		final JTable	classpath = new JTable(model);
		classpath.setDefaultRenderer(Object.class, new DefaultTableCellRenderer()
		{
			public Component getTableCellRendererComponent(JTable table, Object value, boolean selected, boolean focus, int row, int column)
			{
				
				JComponent	ret	= (JComponent)super.getTableCellRendererComponent(table, ((File)value).getName(), selected, focus, row, column);
				ret.setToolTipText(((File)value).getAbsolutePath());
				return ret;
			}
		});
		
		PropertiesPanel	props	= new PropertiesPanel("Classpath");
		props.addFullLineComponent("classpath", new JScrollPane(classpath), 1);
		JButton[]	buts	= props.createButtons("buts", new String[]{"Add...", "Remove..."}, 0);
		
		this.add(props, BorderLayout.CENTER);
		
		final JFileChooser	fc	= new JFileChooser();
		fc.setMultiSelectionEnabled(true);
		fc.setFileFilter(new FileFilter()
		{
			public boolean accept(File file)
			{
				return file.isDirectory() || file.getName().endsWith(".jar");
			}

			public String getDescription()
			{
				return "Classpath entry (.jar file or bin or classes directory)";
			}
		});

		buts[0].addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				int	ok	= fc.showOpenDialog(ClasspathPanel.this);
				if(ok==JFileChooser.APPROVE_OPTION)
				{
					File[]	files 	= fc.getSelectedFiles();
					for(int i=0; i0)
				{
					for(int i=rows.length-1; i>=0; i--)
					{
						model.removeRow(rows[i]);
					}
					classloader	= null;
				}
			}
		});

	}

	/**
	 * 
	 */
	public Class[] scanForFactories()
	{
		List>	ret	= new ArrayList>();
		String[]	facs	= scanForFiles(new String[]{"Factory.class"});
		try
		{
			Class	type	= SReflect.findClass("jadex.bridge.service.types.factory.IComponentFactory", null, getClassLoader());
			
			for(int i=0; i	fac	= SReflect.findClass0(clname, null, getClassLoader());
				if(fac!=null && !fac.isInterface() && SReflect.isSupertype(type, fac))
				{
					ret.add(fac);
				}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return ret.toArray(new Class[ret.size()]);
	}

	/**
	 * 
	 */
	public ClassLoader getClassLoader()
	{
		if(classloader==null)
		{
			List	urls	= new ArrayList();
			for(int i=0; i	ret	= new ArrayList();
		try
		{
			for(int i=0; i e=jar.entries(); e.hasMoreElements(); )
					{
						JarEntry	je	= e.nextElement();
						for(int j=0; j=0; i--)
		{
			model.removeRow(i);
		}
		Property[]	entries	= props.getProperties("ENTRY");
		for(int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy