package jas2.util.pluginmanager;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.text.*;
import jas2.util.*;
import java.net.*;
import java.io.*;
import jas2.swingstudio.*;

public class InstallPluginsUI extends JASDialog
{
    private static JFrame frame;
    private PluginProperties[] plugins;
    private PluginManager pluginmanager;
	private PluginManagerUI pluginmanagerui;
	private JList pluginslist;
	private JLabel name;
	private JLabel author;
	private JLabel version;
	private JLabel requires;
	private JTextArea description;
	private JRadioButton installUser;
	private JRadioButton installSystem;
	private JButton install;
	private Thread installguithread;
	private Container c;
    private boolean accessdirs;
	
	public InstallPluginsUI(PluginManager pm, PluginManagerUI pmui){
	    super(frame, "Install Plugins", true,JASDialog.OK_BUTTON |  JASDialog.CANCEL_BUTTON | JASDialog.HELP_BUTTON);
	    pluginmanager =pm;
		pluginmanagerui = pmui;
	    showInstallUI();
	}
	    
	private void showInstallUI(){
		super.setHelpTopic("userInterface.pluginmanager");
		c = getContentPane();
		c.removeAll();
		c.setLayout(new BorderLayout());
		c.add(BorderLayout.NORTH,new JLabel("The following plugins are available for installation"));

		JPanel panel = new JPanel(new BorderLayout());
		
		pluginslist = new JList();
		pluginslist.setCellRenderer(new Renderer());
		pluginslist.setVisibleRowCount(8);
		pluginslist.addListSelectionListener(new ListHandler());
		panel.add(BorderLayout.CENTER,new JScrollPane(pluginslist));

		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("Requires: ",SwingConstants.RIGHT));
		labelBox.add(new JLabel("Description ",SwingConstants.RIGHT));
		panel2.add(BorderLayout.WEST,labelBox);

		JPanel valueBox = new JPanel(new GridLayout(5,1));
		valueBox.add(name = new JLabel());
		valueBox.add(author = new JLabel());
		valueBox.add(version = new JLabel());
		valueBox.add(requires = new JLabel());
		valueBox.add(Box.createGlue());
		panel2.add(BorderLayout.CENTER,valueBox);

		description = new JTextArea(6,30);
		description.setEditable(false);
		description.setLineWrap(true);
		description.setWrapStyleWord(true);
		
		JPanel panel3 = new JPanel(new BorderLayout());
		panel3.add(BorderLayout.NORTH,new JScrollPane(description));
		ButtonGroup grp = new ButtonGroup();
		installUser = new JRadioButton();
				
		installUser.setText("Install in user directory: " +pluginmanager.getUserDirectory());
		grp.add(installUser);
		panel3.add(BorderLayout.CENTER,installUser);

		installSystem = new JRadioButton("Install in system directory: "+pluginmanager.getSystemDirectory());//,args));
		grp.add(installSystem);
		panel3.add(BorderLayout.SOUTH,installSystem);
		
		accessdirs = checkFileAccess();
						
		install = new JButton("Install selected plugins");
		install.setEnabled(false);
		getRootPane().setDefaultButton(install);
		install.addActionListener(new ActionHandler());
		Box box = new Box(BoxLayout.X_AXIS);
		box.add(Box.createGlue());
		box.add(install);
		box.add(Box.createGlue());
		
		panel2.add(BorderLayout.SOUTH,panel3);
		
		panel.add(BorderLayout.SOUTH,panel2);

		c.add(BorderLayout.CENTER,panel);

		c.add(BorderLayout.SOUTH,box);
			
		updateList();
		show();
		
	}
	/**Returns false if no write access to user and systemes directories
	 */
	
	private boolean checkFileAccess(){
		boolean syscheck=false;
		boolean usercheck=false;
		File system = new File(pluginmanager.getSystemDirectory());
		File user = new File(pluginmanager.getUserDirectory());
		// extension directories created in run()
		// Note we cannot use getParentFile() since it does not exist in JDK1.1
		if(!system.exists())system = new File(system.getParent());
		if(!user.exists())user = new File(user.getParent());
		
		if(user.canWrite())usercheck = true;
		if(system.canWrite())syscheck = true;
		
		if(syscheck & usercheck){
			installSystem.setSelected(true);
			return true;
		}else if(usercheck){
			installSystem.setEnabled(false);
			installUser.setSelected(true);
			return true;
		}else if (syscheck){
			installUser.setEnabled(false);
			installSystem.setSelected(true);
			return true;
		}else{
			installUser.setEnabled(false);
			installSystem.setEnabled(false);
			return false;
		}
	}
	
	private void updateList(){
		Vector vectplugins = pluginmanager.getNotInstalledPluginList();
		plugins = new PluginProperties[vectplugins.size()];
		vectplugins.copyInto(plugins);
		
		if(plugins != null) pluginslist.setListData(plugins);
		pack();
		
	}
	
	class InstallGUIThread extends Thread{
		PluginDownloadProgress pdlp;
		Object[] selected;
		String path;
		
		InstallGUIThread(Object[] plugins){
			selected = plugins;
		}
		
		public void run(){
			path = makeInstallDirectories();
			pdlp = new PluginDownloadProgress( selected, path);
			
			SwingUtilities.invokeLater(new Runnable()
			{	
				public void run()
				{
					pluginmanagerui.updateTree();
					updateList();
					show();
				}
				
			});
		}
	        
	}
	
	//check for min jas version requirement
	private Object[] checkSelectedPlugins(){
		Object[] selected = pluginslist.getSelectedValues();
		Vector okselected = new Vector();
		VersionNumberCompare vnc = new VersionNumberCompare();
		PluginProperties dummyplug;		
		for(int i = 0; i < selected.length; i++)
		{
			dummyplug =(PluginProperties) selected[i];
			try{
				int versionresults = vnc.compareVersions(JavaAnalysisStudio.getVersionNumber(),dummyplug.minJASversion,false);//string version issue
			
				if(versionresults == vnc.UPDATEVERSION){
					String[] options = {"OK"};	
					String updatemessage =dummyplug.name+" requires JAS version "+dummyplug.minJASversion +" or greater. Latest JAS version available at www-sldnt.slac.stanford.edu/jas.";
					int versionbutton = JOptionPane.showOptionDialog(c,updatemessage , "Installing Plugins", JOptionPane.DEFAULT_OPTION , JOptionPane.ERROR_MESSAGE,null,options,options[0]);				
				}else if(versionresults != vnc.UPDATEVERSION & versionresults != vnc.VERSIONCHECKERROR ){
					okselected.addElement(dummyplug);
				}
			}catch(VersionNumberCompare.VersionCompareException vne){
				//do nothing
			}
			
		}
		
		if(okselected.size()>0){
			Object[] returnok = new Object[okselected.size()];
			okselected.copyInto(returnok);
			return returnok;
		}else return null;
	}
	
	private String makeInstallDirectories(){
		String path;
		if(installUser.isSelected()){
			path = pluginmanager.getUserDirectory();
			File installdir = new File(path);
			if(!(installdir.isDirectory()))installdir.mkdir();
			return path;
		}else{
			path = pluginmanager.getSystemDirectory();
			File installdir = new File(path);
			if(!(installdir.isDirectory()))installdir.mkdir();
			return path;
		}
	}
	
	class ActionHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent evt)
		{
			if(evt.getSource() == install){
				if(!accessdirs){//dont have access to user or system dirs
					String[] options = new String[]{"OK"};
					int notify = JOptionPane.showOptionDialog(c,"You do not have write access to an install directory.", "Installing Plugins", JOptionPane.DEFAULT_OPTION , JOptionPane.ERROR_MESSAGE,null,options,options[0]);
				}else{
					Object[] okselected =checkSelectedPlugins();
					if(okselected != null){
						installguithread = new InstallGUIThread(okselected);
						installguithread.start();
					}
				}
				
				
			}
			
				
				
		}
	}

	class ListHandler implements ListSelectionListener
	{
		public void valueChanged(ListSelectionEvent evt)
		{
			Object selected = pluginslist.getSelectedValue();
			if(selected instanceof PluginProperties)
			{
				install.setEnabled(true);

				PluginProperties plugin = (PluginProperties) selected;
				name.setText(plugin.name);
				author.setText(plugin.author);
				version.setText(plugin.latestversion);
				requires.setText(plugin.requires);
				description.setText(plugin.description);
				
			}
			else
			{
				install.setEnabled(false);

				name.setText(null);
				author.setText(null);
				version.setText(null);
				requires.setText(null);
				description.setText(null);
			}
		}
	}
        
    class Renderer extends JLabel implements ListCellRenderer
	{
		Renderer()
		{
			setOpaque(true);
			setFont(UIManager.getFont("Tree.font"));
		}

		public Component getListCellRendererComponent(JList list,
			Object value, int index, boolean isSelected, boolean cellHasFocus)
		{
			
			if(value instanceof PluginProperties){
			    setText(((PluginProperties) value).name);
			}
			
			if (isSelected)
			{
				setBackground(UIManager.getColor(
					"List.selectionBackground"));
				setForeground(UIManager.getColor(
					"List.selectionForeground"));
			}
			else
			{
				setBackground(list.getBackground());
				setForeground(list.getForeground());
			}
       
			return this;
		}
        
		
	}
    
		
	
    public void onOK(){
		if(installguithread != null)
		{
			installguithread.stop();
			installguithread = null;
		}
		dispose();
	}
			
	protected void enableHelp(JASState state)
	{
		state.setEnabled(true);
	}

	public void onCancel(){
		if(installguithread != null)
		{
			installguithread.stop();
			installguithread = null;
		}
		dispose();
	}
				
	
	

}

 

Ads help maintain this website.