package com.mockturtlesolutions.jpHtools.maintenance;
//Copyright (C) 2005 Daniel P. Dougherty
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public
//License as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//General Public License for more details.
//
//You should have received a copy of the GNU General Public
//License along with this program; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//$Id: $
import javax.swing.SwingUtilities;
import javax.swing.JProgressBar;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.Box;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Container;
import java.awt.Color;
import java.awt.event.*;
import java.awt.Dimension;
import com.mockturtlesolutions.jpHtools.database.*;
import com.mockturtlesolutions.snifflib.guitools.components.*;
import com.mockturtlesolutions.snifflib.reposconfig.database.*;
import com.mockturtlesolutions.snifflib.reposconfig.graphical.ReposConfigFrame;
import com.mockturtlesolutions.snifflib.graphics.*;
import com.mockturtlesolutions.snifflib.datatypes.DblMatrix;
import com.mockturtlesolutions.snifflib.datatypes.DataSet;
public class jpHtoolsTransferFrame extends JFrame
{
private JButton srcRepos;
private JButton destRepos;
private JButton expSearch;
private JButton buffSearch;
private Container contentPane;
private ActionListener okcancelListener;
private JButton okButton;
private JButton closeButton;
private JButton cancelButton;
private ReposConfigFrame srcEditor;
private ReposConfigFrame destEditor;
private FindNameDialog expFind;
private FindNameDialog buffFind;
protected pHtoolsConfig Config;
private pHtoolsConnectivity Connection;
protected pHtoolsPrefs Prefs;
private IconServer iconServer;
private String[] selectedBuffers;
private String[] selectedExperiments;
private JProgressBar progressBar;
private Object lock;
private boolean shouldStop;
private Thread transferThread;
private Color emptyColor;
private Color loadedColor;
private JButton clearExpSearch;
private JButton clearBuffSearch;
public jpHtoolsTransferFrame()
{
super("Transfer data between repositories...");
this.setSize(400,200);
this.contentPane = this.getContentPane();
this.contentPane.setLayout(new BoxLayout(this.contentPane,BoxLayout.Y_AXIS));
this.iconServer = new PhtoolsIconServer();
this.Config = new pHtoolsConfig();
this.Config.initialize();
this.Prefs = new pHtoolsPrefs();
this.Prefs.initialize();
this.Connection = new pHtoolsConnectivity(this.Config);
this.srcEditor = new ReposConfigFrame(this.Config);
this.srcEditor.setVisible(false);
this.srcEditor.addSelectListener(new SrcSelectListener());
this.destEditor = new ReposConfigFrame(this.Config);
this.destEditor.setVisible(false);
this.destEditor.addSelectListener(new DestSelectListener());
this.destEditor.addCloseListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
destEditor.setVisible(false);
}
});
this.srcEditor.addCloseListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
srcEditor.setVisible(false);
}
});
Box reposBox = Box.createHorizontalBox();
this.progressBar = new JProgressBar();
this.progressBar.setStringPainted(true);
this.lock = new Object();
this.shouldStop = false;
this.transferThread = null;
JLabel srcLabel = new JLabel("Source");
JLabel destLabel = new JLabel("Destination");
this.srcRepos = new JButton("default");
this.destRepos = new JButton("default");
this.srcRepos.addActionListener(new ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
selectedExperiments = null;
selectedBuffers = null;
srcEditor.setVisible(true);
}
}
);
this.destRepos.addActionListener(new ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
destEditor.setVisible(true);
}
}
);
srcLabel.setLabelFor(this.srcRepos);
destLabel.setLabelFor(this.destRepos);
Box srcBox = Box.createVerticalBox();
srcBox.add(srcLabel);
srcBox.add(this.srcRepos);
Box destBox = Box.createVerticalBox();
destBox.add(destLabel);
destBox.add(this.destRepos);
reposBox.add(srcBox);
reposBox.add(destBox);
Box transferBox = Box.createVerticalBox();
JLabel transLabel = new JLabel("Transfer");
this.expSearch = new JButton("Experiments");
this.buffSearch = new JButton("Buffer");
this.clearExpSearch = new JButton("Clear");
this.clearBuffSearch = new JButton("Clear");
clearExpSearch.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
selectedExperiments = null;
clearExpSearch.setBackground(emptyColor);
}
}
);
clearBuffSearch.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
selectedBuffers = null;
clearBuffSearch.setBackground(emptyColor);
}
}
);
this.emptyColor = clearExpSearch.getBackground();
this.loadedColor = Color.GRAY;
this.buffSearch.setSize(this.expSearch.getSize());
this.buffSearch.setMaximumSize(this.expSearch.getMaximumSize());
this.buffSearch.setPreferredSize(this.expSearch.getPreferredSize());
this.expFind = new FindNameDialog(this.Config,this.iconServer,FindNameDialog.EXPERIMENT_SEARCH);
this.buffFind = new FindNameDialog(this.Config,this.iconServer,FindNameDialog.BUFFER_SEARCH);
this.expFind.setVisible(false);
this.expFind.setMaxSelections(-1); //Arbitrary number of selections.
this.buffFind.setVisible(false);
this.buffFind.setMaxSelections(-1); //Arbitrary number of selections.
this.expFind.addOkListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
selectedExperiments = (String[])expFind.getSelectedNames();
if (selectedExperiments.length > 0)
{
clearExpSearch.setBackground(loadedColor);
}
//System.out.println("Setting visible off");
expFind.setVisible(false);
}
}
);
this.expFind.addCancelListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (selectedExperiments.length > 0)
{
clearExpSearch.setBackground(loadedColor);
}
//System.out.println("Setting visible off");
expFind.setVisible(false);
}
}
);
this.buffFind.addOkListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
selectedBuffers = (String[])buffFind.getSelectedNames();
if (selectedBuffers.length > 0)
{
clearBuffSearch.setBackground(loadedColor);
}
//System.out.println("Setting visible off");
buffFind.setVisible(false);
}
}
);
this.buffFind.addCancelListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
return;
}
}
);
this.expSearch.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String restrict_repos = srcRepos.getText();
expFind.restrictToRepository(restrict_repos);
expFind.setVisible(true);
}
}
);
this.buffSearch.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String restrict_repos = srcRepos.getText();
buffFind.restrictToRepository(restrict_repos);
buffFind.setVisible(true);
}
}
);
transferBox.add(transLabel);
Box expBox = Box.createHorizontalBox();
expBox.add(this.expSearch);
expBox.add(clearExpSearch);
Box buffBox = Box.createHorizontalBox();
buffBox.add(this.buffSearch);
buffBox.add(clearBuffSearch);
transferBox.add(expBox);
transferBox.add(buffBox);
Box aBox = Box.createHorizontalBox();
aBox.add(Box.createHorizontalGlue());
aBox.add(reposBox);
aBox.add(Box.createHorizontalGlue());
Box bBox = Box.createHorizontalBox();
bBox.add(Box.createHorizontalGlue());
bBox.add(transferBox);
bBox.add(Box.createHorizontalGlue());
progressBar.setIndeterminate(false);
this.add(aBox);
this.add(Box.createVerticalGlue());
this.add(bBox);
this.add(Box.createVerticalGlue());
this.closeButton = new JButton("Close");
closeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//nameText.setText(name);
//System.out.println("Cancel pushed");
stop_Transfers();
if (okcancelListener != null)
{
//System.out.println("Listener not null");
progressBar.setIndeterminate(false);
okcancelListener.actionPerformed(e);
}
//setVisible(false);
}
}
);
this.okButton = new JButton("Go!");
this.cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//nameText.setText(name);
//System.out.println("Cancel pushed");
stop_Transfers();
if (okcancelListener != null)
{
//System.out.println("Listener not null");
progressBar.setIndeterminate(false);
okcancelListener.actionPerformed(e);
}
//setVisible(false);
}
}
);
okButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
start_Transfers();
// String src = srcRepos.getText();
// String dest = destRepos.getText();
// ExperimentStorage srcExperiment;
// ExperimentStorage destExperiment;
// BufferStorage srcBuffer;
// BufferStorage destBuffer;
// String name;
//
// //Do the transfers!!
//
// try
// {
// if ((selectedExperiments!=null) && (selectedExperiments.length>0))
// {
//
//
// progressBar.setMinimum(0);
// progressBar.setMaximum(selectedExperiments.length);
// //progressBar.setStringPainted(true);
//
//
// ProgressUpdater progressRunner = new ProgressUpdater();
//
//
// for (int i=0;i<selectedExperiments.length;i++)
// {
// name = selectedExperiments[i];
// //System.out.println("Getting exp on src");
// srcExperiment = Connection.getExperiment(src,name);
// //System.out.println("Got exp on src");
// //System.out.println("Creating exp on dest");
// Connection.createExperiment(dest,name);
// //System.out.println("Created exp on dest");
// //System.out.println("Getting exp on dest");
// destExperiment = Connection.getExperiment(dest,name);
// //System.out.println("Got exp on dest");
//
// //System.out.println("Transfering storage");
// destExperiment.transferStorage(srcExperiment);
// //System.out.println("Finished transfering experiment:"+name);
//
//
//
//
// }
//
// }
//
//
// if ((selectedBuffers!=null) && (selectedBuffers.length>0))
// {
// for (int i=0;i<selectedBuffers.length;i++)
// {
// //System.out.println("Here "+selectedBuffers[i]);
// name = selectedBuffers[i];
// srcBuffer = Connection.getBuffer(src,name);
// Connection.createBuffer(dest,name);
// //System.out.println("OK till here");
// destBuffer = Connection.getBuffer(dest,name);
// //System.out.println("OK till here too");
// if (destBuffer == null)
// {
// System.out.println("destBuffer null");
// }
// if (srcBuffer == null)
// {
// System.out.println("srcBuffer null");
// }
// destBuffer.transferStorage(srcBuffer);
// //System.out.println("Done");
// }
// }
// }
// catch (JpHtoolsDatabaseException er)
// {
// showError("Problem performing transfer. "+er.getMessage());
// }
//
// if (okcancelListener != null)
// {
// okcancelListener.actionPerformed(e);
// }
//setVisible(false);
}
}
);
Box controlBox = Box.createHorizontalBox();
controlBox.add(Box.createHorizontalGlue());
controlBox.add(this.okButton);
controlBox.add(this.closeButton);
controlBox.add(this.cancelButton);
controlBox.add(Box.createHorizontalGlue());
//Add the progressBar.
this.add(Box.createVerticalGlue());
this.add(this.progressBar);
this.add(controlBox);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.pack();
this.setVisible(false);
}
public void start_Transfers()
{
if (this.transferThread == null)
{
this.transferThread = new TransferThread();
this.shouldStop = false;
this.transferThread.start();
}
}
public void stop_Transfers()
{
synchronized(lock)
{
shouldStop = true;
lock.notify();
}
}
private class TransferThread extends Thread
{
public void run ()
{
Runnable runner = new Runnable()
{
public void run()
{
int value = progressBar.getValue();
value++;
progressBar.setIndeterminate(false);
progressBar.setValue(value);
//progressTextField.setText (""+value);
}
};
String src = srcRepos.getText();
String dest = destRepos.getText();
ExperimentStorage srcExperiment;
ExperimentStorage destExperiment;
BufferStorage srcBuffer;
BufferStorage destBuffer;
String name;
//Do the transfers!!
try
{
progressBar.setMinimum(0);
int num_transfers = 0;
if (selectedExperiments!=null)
{
num_transfers = num_transfers + selectedExperiments.length;
}
if (selectedBuffers!=null)
{
num_transfers = num_transfers + selectedBuffers.length;
}
if (num_transfers <= 0)
{
return;
}
progressBar.setMaximum(num_transfers);
progressBar.setStringPainted(true);
progressBar.setIndeterminate(true);
if ((selectedExperiments!=null) && (selectedExperiments.length>0))
{
//ProgressUpdater progressRunner = new ProgressUpdater();
for (int i=0;i<selectedExperiments.length;i++)
{
name = selectedExperiments[i];
System.out.println("Getting exp on src");
srcExperiment = Connection.getExperiment(src,name);
System.out.println("Got exp on src");
System.out.println("Creating exp on dest");
Connection.createExperiment(dest,name);
System.out.println("Created exp on dest");
System.out.println("Getting exp on dest");
destExperiment = Connection.getExperiment(dest,name);
System.out.println("Got exp on dest");
System.out.println("Transfering storage");
destExperiment.transferStorage(srcExperiment);
System.out.println("Finished transfering experiment:"+name);
try
{
SwingUtilities.invokeAndWait(runner);
}
catch (java.lang.reflect.InvocationTargetException e)
{
break;
}
catch (InterruptedException e)
{
// Ignore Exception
}
synchronized(lock)
{
if (shouldStop)
{
break;
}
try
{
lock.wait(100);
}
catch (InterruptedException e)
{
// Ignore Exception
}
}
}
}
if ((selectedBuffers!=null) && (selectedBuffers.length>0))
{
for (int i=0;i<selectedBuffers.length;i++)
{
//System.out.println("Here "+selectedBuffers[i]);
name = selectedBuffers[i];
srcBuffer = Connection.getBuffer(src,name);
Connection.createBuffer(dest,name);
//System.out.println("OK till here");
destBuffer = Connection.getBuffer(dest,name);
//System.out.println("OK till here too");
if (destBuffer == null)
{
System.out.println("destBuffer null");
}
if (srcBuffer == null)
{
System.out.println("srcBuffer null");
}
destBuffer.transferStorage(srcBuffer);
try
{
SwingUtilities.invokeAndWait(runner);
}
catch (java.lang.reflect.InvocationTargetException e)
{
break;
}
catch (InterruptedException e)
{
// Ignore Exception
}
synchronized(lock)
{
if (shouldStop)
{
break;
}
try
{
lock.wait(100);
}
catch (InterruptedException e)
{
// Ignore Exception
}
}
//System.out.println("Done");
}
}
}
catch (Exception err)
{
throw new RuntimeException("Problem performing transfer.",err);
}
//progressBar.setIndeterminate(false);
progressBar.setValue(0);
// try
// {
// SwingUtilities.invokeAndWait(runner);
// }
// catch (java.lang.reflect.InvocationTargetException e)
// {
// //break;
// }
// catch (InterruptedException e)
// {
// // Ignore Exception
// }
//
// synchronized(lock)
// {
// if (shouldStop)
// {
// //break;
// }
//
// try
// {
// lock.wait(100);
// }
// catch (InterruptedException e)
// {
// // Ignore Exception
// }
// }
transferThread = null;
}
}
private class SrcSelectListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
srcRepos.setText(srcEditor.getCurrentRepository());
srcEditor.setVisible(false);
}
}
private class DestSelectListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
destRepos.setText(destEditor.getCurrentRepository());
destEditor.setVisible(false);
}
}
public void setOkCancelListener(ActionListener listen)
{
this.okcancelListener = listen;
}
public void showError(String msg)
{ JOptionPane dialog = new JOptionPane();
JTextArea area = new JTextArea(msg);
JScrollPane jsp = new JScrollPane(area);
jsp.setPreferredSize(new Dimension(500,200));
dialog.showMessageDialog(this,jsp,"Error",JOptionPane.ERROR_MESSAGE);
}
}