package edu.wustl.cab2b.client.ui.searchDataWizard;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JScrollPane;

import edu.wustl.cab2b.client.ui.controls.Cab2bButton;
import edu.wustl.cab2b.client.ui.controls.Cab2bLabel;
import edu.wustl.cab2b.client.ui.controls.Cab2bPanel;
import edu.wustl.cab2b.client.ui.mainframe.MainFrame;
import edu.wustl.cab2b.client.ui.mainframe.NewWelcomePanel;
import edu.wustl.cab2b.client.ui.util.WindowUtilities;
import edu.wustl.cab2b.common.domain.DCQL;

/**
 * This class displays the DCQL Panel which shows the XML format of DCQL along with function of saving the XML file 
 * 
 * @author gaurav_mehta
 */
public class ShowDCQLPanel extends Cab2bPanel {

    /* JDialog in which the Panel is displayed */
    private JDialog dialog;

    // Cab2bLabel in which the entire XML is 
    final private Cab2bLabel xmlTextPane = new Cab2bLabel();

    // Cab2bPanel for showing Success and failure messages
    final Cab2bPanel messagePanel = new Cab2bPanel();

    /**
     * @param dcql 
     */
    public ShowDCQLPanel(DCQL dcql) {
        initGUI(dcql);
    }

    private void initGUI(DCQL dcql) {
        String xmlText = new XmlParser().parseXml(dcql.getDcqlQuery());

        xmlTextPane.setText(xmlText);
        xmlTextPane.setBackground(Color.WHITE);

        Cab2bPanel xmlPanel = new Cab2bPanel();
        xmlPanel.add(xmlTextPane);
        xmlPanel.setBackground(Color.WHITE);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.getViewport().add(xmlPanel);
        scrollPane.getViewport().setBackground(Color.WHITE);

        Cab2bPanel xmlNavigationPanel = new Cab2bPanel();
        Cab2bButton exportButton = new Cab2bButton("Export");
        Cab2bButton cancelButton = new Cab2bButton("Cancel");

        // Action Listener for Export Button
        exportButton.addActionListener(new ExportButtonListner());

        // Action Listener for Cancel Button
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                dialog.dispose();
            }
        });

        Cab2bPanel buttonPanel = new Cab2bPanel();
        FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT);
        buttonPanel.setLayout(flowLayout);
        buttonPanel.add(exportButton);
        buttonPanel.add(cancelButton);
        buttonPanel.setBackground(new Color(240, 240, 240));

        xmlNavigationPanel.add("br left", messagePanel);
        xmlNavigationPanel.add("hfill", buttonPanel);
        xmlNavigationPanel.setPreferredSize(new Dimension(880, 50));
        xmlNavigationPanel.setBackground(new Color(240, 240, 240));

        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);
        add(xmlNavigationPanel, BorderLayout.SOUTH);
    }

    /**
     * JDialog for showing DCQL XML Details Panel
     * 
     * @return
     */
    public JDialog showInDialog() {
        Dimension dimension = MainFrame.getScreenDimesion();
        dialog = WindowUtilities.setInDialog(NewWelcomePanel.getMainFrame(), this, "DCQL Xml", new Dimension(
                (int) (dimension.width * 0.77), (int) (dimension.height * 0.65)), true, false);
        dialog.setVisible(true);

        return dialog;
    }
    
    private boolean writeFile(File file, String dataString) {
        try {
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
            out.print(dataString);
            out.flush();
            out.close();
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    /**
     * Action listener class for Export Button. It saves the XML file to user defined location in user defined format
     */
    class ExportButtonListner implements ActionListener {
        public void actionPerformed(ActionEvent actionEvent) {
            JFileChooser fileChooser = new JFileChooser();

            // A call to JFileChooser's ShowSaveDialog PopUp
            fileChooser.showSaveDialog(NewWelcomePanel.getMainFrame());

            File file = fileChooser.getSelectedFile();
            // Function call for writing the File and saving it
            boolean saveReturnValue = writeFile(file, xmlTextPane.getText());
            if (saveReturnValue == true) {
                Cab2bLabel successResultLabel = new Cab2bLabel("File Saved Successfully");
                successResultLabel.setForeground(Color.GREEN);
                messagePanel.add(successResultLabel);
                messagePanel.repaint();
            } else {
                Cab2bLabel failureResultLabel = new Cab2bLabel("File Could not be Saved");
                failureResultLabel.setForeground(Color.RED);
                messagePanel.add(failureResultLabel);
                messagePanel.repaint();

            }
        }
    }
}

@@ -1371,24 +1371,62 @@
 b2bPanel();%0D
+%0A    %0D%0A    private String dcqlString;%0D
 %0A%0D%0A    /**%0D%0A
@@ -1486,32 +1486,80 @@
 l(DCQL dcql) %7B%0D%0A
+        this.dcqlString = dcql.getDcqlQuery();%0D%0A
         initGUI(
@@ -1558,20 +1558,16 @@
 initGUI(
-dcql
 );%0D%0A    
@@ -1596,25 +1596,16 @@
 initGUI(
-DCQL dcql
 ) %7B%0D%0A   
@@ -1655,31 +1655,22 @@
 Xml(dcql
-.getDcqlQuery()
+String
 );%0D%0A%0D%0A  
@@ -4338,24 +4338,31 @@
 Listener %7B%0D%0A
+    %09%0D%0A
         publ
@@ -4791,29 +4791,18 @@
 le, 
-xmlTextPane.getText()
+dcqlString
 );%0D%0A

Fix for Bug # 10563