package jas2.util.pluginmanager;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.Vector;
import java.net.*;
import jas2.util.*;



public class PluginManagerUI extends JASDialog implements ActionListener{
	private Vector loadedplugins;
	private JTree tree;
	private JLabel path;
	private JLabel name;
	private JLabel author;
	private JLabel currversion;
	private JButton remove;
	private JButton update;
	private JButton install;
	private PluginManager pluginmanager;
	private JTextArea textarea;
	private DefaultMutableTreeNode loadedtree;
	private JOptionPane updatepane;
	private Container c;
	
	public PluginManagerUI(PluginManager pm, Frame frame){
		super(frame, "Plugin Manager", true,JASDialog.OK_BUTTON | JASDialog.CANCEL_BUTTON | JASDialog.HELP_BUTTON);
		pluginmanager =pm;
	}
	
	public void showManagerGUI(){
		super.setHelpTopic("userInterface.pluginmanager");
		c = getContentPane();
		c.removeAll();
		c.setLayout(new BorderLayout());
		
		JPanel panel = new JPanel(new BorderLayout());
		tree = new JTree();
		tree.setCellRenderer(new Renderer());
		tree.setRootVisible(false);
		tree.setVisibleRowCount(8);
		tree.putClientProperty("JTree.lineStyle", "Angled");
		tree.getSelectionModel().addTreeSelectionListener(new TreeHandler());
		JScrollPane treescroll = new JScrollPane(tree);
		panel.add(BorderLayout.CENTER,treescroll);
						
		JPanel panel2 = new JPanel(new BorderLayout());
		JPanel labelBox = new JPanel(new GridLayout(5,1));
		labelBox.add(new JLabel("Name: ", SwingConstants.RIGHT));
		labelBox.add(new JLabel("Author: ", SwingConstants.RIGHT));
		labelBox.add(new JLabel("Version: ", SwingConstants.RIGHT));
		labelBox.add(new JLabel("Install path: ", SwingConstants.RIGHT ));
		labelBox.add(new JLabel("Description ", SwingConstants.RIGHT));
		panel2.add(BorderLayout.WEST,labelBox);

		JPanel panel3 = new JPanel(new BorderLayout()); 
		textarea = new JTextArea(6,30);
		textarea.setEditable(false);
		textarea.setLineWrap(true);
		textarea.setWrapStyleWord(true);
		JScrollPane editorscroll = new JScrollPane(textarea);
		panel3.add(BorderLayout.NORTH,textarea);
				
		JPanel valueBox = new JPanel(new GridLayout(5,1));
		valueBox.add(name = new JLabel());
		valueBox.add(author = new JLabel());
		valueBox.add(currversion = new JLabel());
		valueBox.add(path = new JLabel());
		panel2.add(BorderLayout.CENTER,valueBox);
		panel2.add(BorderLayout.SOUTH,panel3);

		panel.add(BorderLayout.SOUTH,panel2);
		c.add(BorderLayout.CENTER,panel);

		JPanel buttons = new JPanel();
        remove = new JButton("Remove selected plugins");
		remove.addActionListener(this);
		remove.setActionCommand("remove");
		buttons.add(remove);
		update = new JButton("Update installed plugins");
		update.addActionListener(this);
		update.setActionCommand("update");
		buttons.add(update);
		install = new JButton("Install a plugin");
		install.addActionListener(this);
		install.setActionCommand("install");
		buttons.add(install);

		c.add(BorderLayout.SOUTH,buttons);

		updateTree();
		show();
		
	}
	
	protected void updateTree()
	{
		
		DefaultMutableTreeNode treeRoot = new DefaultMutableTreeNode();
		DefaultTreeModel treeModel = new DefaultTreeModel(treeRoot, true);

		loadedplugins = pluginmanager.getLoadedPluginList();
	    loadedtree = new DefaultMutableTreeNode("Loaded Plugins", true);
		treeModel.insertNodeInto(loadedtree, treeRoot,0);
		DefaultMutableTreeNode node =null;
		
		if(loadedplugins != null){
			for(int i = 0; i < loadedplugins.size(); i++)
			{
				node = new DefaultMutableTreeNode(loadedplugins.elementAt(i),false);
				treeModel.insertNodeInto(node,loadedtree,0);
			}
		}

		treeRoot.insert(loadedtree,0);

		tree.setModel(treeModel);
		for(int i = 0; i < tree.getRowCount(); i++)
			tree.expandRow(i);

		remove.setEnabled(false);
		if(pluginmanager.getNotInstalledPluginList().size() >0)install.setEnabled(true);
		else install.setEnabled(false);
		if(pluginmanager.getOutDatedPluginList().size() >0)update.setEnabled(true);
		else update.setEnabled(false);
		path.setText(null);
		name.setText(null);
		author.setText(null);
		currversion.setText(null);
			
		pack();
		
	}

	
	
	class TreeHandler implements TreeSelectionListener
	{
		public void valueChanged(TreeSelectionEvent evt)
		{
			TreePath selection = evt.getPath();
			DefaultMutableTreeNode node;
						
			
			if(selection == null)
			{
				node = null;
			}
			else
			{
				node = (DefaultMutableTreeNode)
					selection.getLastPathComponent();
			}

			String pathstr;
			String namestr;
			String authorstr;
			String versionstr;
            String description;
			
            
			if(node != null && node.isLeaf() && node.getUserObject() instanceof PluginProperties)
			{
				PluginProperties plugprops = (PluginProperties)node.getUserObject();
				pathstr = plugprops.path;
				namestr = plugprops.name;
				authorstr = plugprops.author;
				versionstr = plugprops.currversion;
				description = plugprops.description;
				
			}
			else
			{
				pathstr = namestr = authorstr = versionstr = "";
			    description = "Information describing the use of the available plugins will be displayed here.";
         	}

			path.setText(pathstr);
			name.setText(namestr);
			author.setText(authorstr);
			currversion.setText(versionstr);
					
			node = null;
			TreePath[] checkselection = tree.getSelectionPaths();
			if(checkselection != null){
			
checkloop :    for(int i=0;i 

Ads help maintain this website.