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.io.*;
import jas2.swingstudio.*;

public class UpdatePluginsUI extends JASDialog
{
	private static JFrame frame;
	private PluginManager pluginmanager;
	private PluginManagerUI pluginmanagerui;
	private PluginProperties[] plugins;
	private JList pluginslist;
	private JLabel name;
	private JLabel author;
	private JLabel currVersion;
	private JLabel latestVersion;
	private JLabel requires;
	private JLabel path;
	private JTextArea description;
	private JButton update;
	private Thread updateguithread;
	private Container c;
	
	public UpdatePluginsUI(PluginManager pm, PluginManagerUI pmui)
	{
		super(frame, "Update Plugins", true,JASDialog.OK_BUTTON |  JASDialog.CANCEL_BUTTON | JASDialog.HELP_BUTTON);
	    pluginmanager = pm;
		pluginmanagerui = pmui;
	    showUpdatePluginsUI();
	}

    
    private void showUpdatePluginsUI(){
		super.setHelpTopic("userInterface.pluginmanager");
		c = getContentPane();
		c.add(BorderLayout.NORTH,new JLabel("New Versions of the following plugins are available:"));

		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(7,1));
		labelBox.add(new JLabel("Name: ",SwingConstants.RIGHT));
		labelBox.add(new JLabel("Author: ",SwingConstants.RIGHT));
		labelBox.add(new JLabel("Current Version: ",SwingConstants.RIGHT));
		labelBox.add(new JLabel("Latest Version: ",SwingConstants.RIGHT));
		labelBox.add(new JLabel("Requires: ",SwingConstants.RIGHT));
		labelBox.add(new JLabel("Install Path:",SwingConstants.RIGHT));
		labelBox.add(new JLabel("Description",SwingConstants.RIGHT));
		panel2.add(BorderLayout.WEST,labelBox);

		JPanel valueBox = new JPanel(new GridLayout(7,1));
		valueBox.add(name = new JLabel());
		valueBox.add(author = new JLabel());
		valueBox.add(currVersion = new JLabel());
		valueBox.add(latestVersion = new JLabel());
		valueBox.add(requires = new JLabel());
		valueBox.add(path = 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);
		panel2.add(BorderLayout.SOUTH,new JScrollPane(description));

		panel.add(BorderLayout.SOUTH,panel2);

		c.add(BorderLayout.CENTER,panel);

		JPanel buttons = new JPanel();

		update = new JButton("Update selected plugins");
		update.setEnabled(false);
		getRootPane().setDefaultButton(update);
		update.addActionListener(new ActionHandler());
		buttons.add(update);

		
		c.add(BorderLayout.SOUTH,buttons);

		updateList();
		show();
	}
	
	private void updateList(){
		Vector vectplugins = pluginmanager.getOutDatedPluginList();
		plugins = new PluginProperties[vectplugins.size()];
		vectplugins.copyInto(plugins);
		
		if(plugins !=null)pluginslist.setListData(plugins);
		pack();
	}

	
	class UpdateGUIThread extends Thread{
		private Object[] selected;
		
		UpdateGUIThread(Object[] plugins){
			selected = plugins;
		}
		
		public void run(){
			PluginDownloadProgress pdlp = new PluginDownloadProgress(selected,"update");
			SwingUtilities.invokeLater(new Runnable()
			{	
				public void run()
				{
					pluginmanagerui.updateTree();
					updateList();
					show();
				}
				
			});
		}
	        
	}
	/**Check for file write access and minimum jas version - may return null
	 */
	private Object[] checkSelectedPlugins(){
		Object[] selected = pluginslist.getSelectedValues();
		Vector okselected = new Vector();
		VersionNumberCompare vnc = new VersionNumberCompare();
		PluginProperties dummyplug;		
		File checkwrite;
		for(int i = 0; i < selected.length; i++)
		{
			dummyplug =(PluginProperties) selected[i];
			checkwrite = new File(dummyplug.path);
			if(!checkwrite.canWrite()){
				String[] options = {"OK"};	
				int button = JOptionPane.showOptionDialog(c,"No write access to "+checkwrite.getAbsolutePath()+". "+dummyplug.name+" cannot be updated." , "Updating Plugins", JOptionPane.DEFAULT_OPTION , JOptionPane.ERROR_MESSAGE,null,options,options[0]);
			}else{
				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 , "Updating 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;
		
	}
	
	class ActionHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent evt)
		{
			if(evt.getSource() == update){
				Object[] okselected =checkSelectedPlugins();
				if(okselected != null){
					updateguithread = new UpdateGUIThread(okselected);
					updateguithread.start();
				}
			}
			
		}
	}
		
	class ListHandler implements ListSelectionListener
	{
		public void valueChanged(ListSelectionEvent evt)
		{
			int index = pluginslist.getSelectedIndex();
			Object selected = pluginslist.getSelectedValue();
			if(selected instanceof PluginProperties)
			{
				update.setEnabled(true);

				PluginProperties plugin = (PluginProperties)selected;
				name.setText(plugin.name);
				author.setText(plugin.author);
				currVersion.setText(plugin.currversion);
				latestVersion.setText(plugin.latestversion);
				requires.setText(plugin.requires);
				path.setText(plugin.path);
				description.setText(plugin.description);
			}
			else
			{
				update.setEnabled(false);

				name.setText(null);
				author.setText(null);
				currVersion.setText(null);
				latestVersion.setText(null);
				requires.setText(null);
				path.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);
			}else{
			    setText(value.toString());
			 
			}
			
			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(updateguithread != null)
		{
			updateguithread.stop();
			updateguithread = null;
		}

		dispose();
	}
			
	protected void enableHelp(JASState state)
	{
		state.setEnabled(true);
	}

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


}

 

Ads help maintain this website.