
/**
 * DownloadDialog.java
 * A dialog that allows user download the spectral database from over the network 
 * Created: July 11, 2006
 *
 * @author Nathan Stevens
 * @version 0.1 7/11/2006
 */
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.net.*;
import java.io.*;

public class DownloadDialog extends JDialog implements ActionListener {
  private FRETViewPlugin fretView = null; // The fret view plugin
  private JTextField urlTF = null; // the url of the database file
  private JLabel statusLB = null; // the status of the download
  private String url_string = null; // the url to download from
  
  // default method not used
  public DownloadDialog() {}
  
  public DownloadDialog(FRETViewPlugin fretView) {
    super(fretView, "Database Download",  false);
    this.fretView = fretView;
    
    // handles clossing of this plugin
		addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        setVisible(false);
      }
		});
    
    // set the layout to border layer
    getContentPane().setLayout(new BorderLayout());
    
    // some gui stuff
    JPanel panel;
    JLabel label;
    JButton button;
    GridBagLayout gridbag;
    GridBagConstraints c;
    
    // add the mainPanel
    JPanel panel_A = new JPanel();
    gridbag = new GridBagLayout();
    c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL; 
    panel_A.setLayout(gridbag);
    
    // first row
    label = new JLabel("DB File URL : ");
    c.gridx = 0;
    c.gridy = 0;
    gridbag.setConstraints(label, c);
    panel_A.add(label);
    
    // load the url if it is loaded from the properties file
    url_string = fretView.properties.getProperty("dburl", "http://turmac13.chem.columbia.edu/fretview/test.dal");
    
    urlTF = new JTextField(url_string, 30);
    c.gridx = 1;
    c.gridy = 0;
    c.gridwidth = 2;
    gridbag.setConstraints(urlTF, c);
    panel_A.add(urlTF);
    
    c.gridwidth = 1;
    
    // second row
    label = new JLabel("Status : ");
    c.gridx = 0;
    c.gridy = 1;
    gridbag.setConstraints(label, c);
    panel_A.add(label);
    
    statusLB = new JLabel("n/a");
    statusLB.setForeground(Color.blue);
    c.gridx = 1;
    c.gridy = 1;
    c.weightx = 1.0f; // request any additional space
    gridbag.setConstraints(statusLB, c);
    panel_A.add(statusLB);
    
    c.weightx = 0.0f; // reset to old value
    
    button = new JButton("OK");
    button.setActionCommand("ok");
		button.addActionListener(this);
    c.gridx = 2;
    c.gridy = 1;
    gridbag.setConstraints(button, c);
    panel_A.add(button);
    
    getContentPane().add(panel_A, "Center");
  }
  
  // handle any action events
  public void actionPerformed(ActionEvent ae) {
		String cmd = ae.getActionCommand();
		
		if(cmd.equals("close")) {
			setVisible(false);
		}
    else if(cmd.equals("ok")) {
      downloadDatabase();
    }
    else {
      System.err.println("Action Command not handeled in class DownloadDialog ...");
      System.err.println(cmd);
    }
  }
  
  // Method to actually download the database file
  private void downloadDatabase() {
    try {
      statusLB.setText("Dowloading ...");
      URL url = new URL(urlTF.getText());
      URLConnection urlConn = url.openConnection();
      
      ObjectInputStream in = new ObjectInputStream(urlConn.getInputStream());
      Object object = in.readObject();
      in.close();
      
      DonorAcceptorData daData = (DonorAcceptorData)object;
      
      // load the database now
      fretView.setDonorAcceptorList(daData);
      
      // alert the user the file has finished downloading
      statusLB.setText("Done ...");
      
      // save this url for future use
      saveDatabaseURL();
    }
    catch(MalformedURLException e) {
      statusLB.setText("Bad URL");
    }
    catch(IOException e) {
      statusLB.setText("Connection Error");
    }
    catch(ClassNotFoundException e) {
      statusLB.setText("Currupted Data");
    }
  }
  
  // Method to see whether to save the url downlload location
  private void saveDatabaseURL() {
    fretView.properties.setProperty("dburl", urlTF.getText());
    fretView.storeProperties();
  }
} //--end class Download Dialog
