/*
 * COPYRIGHT (c) 2010 by Institute of Computer Science, 
 * Foundation for Research and Technology - Hellas
 * Contact: 
 *      POBox 1385, Heraklio Crete, GR-700 13 GREECE
 *      Tel:+30-2810-391632
 *      Fax: +30-2810-391638
 *      E-mail: isl@ics.forth.gr
 *      http://www.ics.forth.gr/isl/cci.html
 * 
 *  This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 
 * Unported License (http://creativecommons.org/licenses/by-sa/3.0/)
*/

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

public class GUI extends JFrame implements ActionListener{
	JTextField inputFolder = new JTextField(20);
	JTextField outputFolder = new JTextField(13);
	JTextArea statusText = new JTextArea("",5,30);//size of status area
	JButton inBrowse = new JButton("Browse");
	JButton outBrowse = new JButton("Browse");
	JButton convert = new JButton("Convert");
	JPanel inPanel = new JPanel();
	JPanel outPanel = new JPanel();
	JPanel statusPanel = new JPanel();
	JFileChooser fcin = new JFileChooser();
	JFileChooser fcout = new JFileChooser();
	File inputdir;
	File outputdir;
	String[] files;
	String outputText;
	Convertor convertor;
	
    // private Desktop desktop;
    // private Desktop.Action action = Desktop.Action.OPEN;
	
	public GUI(){
		super("CIDOC CRM: XML-to-RDF");
		setLayout(new BorderLayout());
		inPanel.setBorder(BorderFactory.createTitledBorder("XML Input File/Folder"));
		outPanel.setBorder(BorderFactory.createTitledBorder("RDF Output Folder"));
		statusPanel.setBorder(BorderFactory.createTitledBorder("Conversion Status"));
		add(inPanel,BorderLayout.NORTH);
		add(outPanel,BorderLayout.CENTER);
		add(convert,BorderLayout.EAST);
		add(statusPanel,BorderLayout.SOUTH);
		inputFolder.addActionListener(this);
		inputFolder.setActionCommand("selectedInFolder");
		outputFolder.addActionListener(this);
		outputFolder.setActionCommand("selectedOutFolder");
		statusText.setBounds(20, 20, 20, 20);
		statusText.setText("Select Input folder.\nIf no output is specified same folder will be used");
		inBrowse.addActionListener(this);
		inBrowse.setActionCommand("pressedInBrowse");
		outBrowse.addActionListener(this);
		outBrowse.setActionCommand("pressedOutBrowse");
		convert.addActionListener(this);
		convert.setActionCommand("Convert");
		inPanel.add(inputFolder);
		inPanel.add(inBrowse);
		outPanel.add(outputFolder);
		outPanel.add(outBrowse);
		//statusPanel.add(statusText);
		fcin.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		FileNameExtensionFilter filter = new FileNameExtensionFilter("XML Files", "xml");
		fcin.setFileFilter(filter);
		fcout.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		outputText="";

		statusPanel.add(new JScrollPane(statusText));
		
		convertor = new Convertor();
	}
	
	public static void showGUI(){
		GUI gui = new GUI();
		gui.pack();
		gui.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent event){
		if("selectedInFolder".equals(event.getActionCommand())){
			
		}
		if("selectedOutFolder".equals(event.getActionCommand())){
			
		}
		if("pressedInBrowse".equals(event.getActionCommand())){
            int returnVal = fcin.showOpenDialog(GUI.this);
            if (returnVal == JFileChooser.APPROVE_OPTION){
            	inputdir = fcin.getSelectedFile();
            	files = inputdir.list(new MyFileNameFilter());
                inputFolder.setText(inputdir.getAbsolutePath());
            }
            this.pack();
		}
		if("pressedOutBrowse".equals(event.getActionCommand())){
            int returnVal = fcout.showOpenDialog(GUI.this);
            if (returnVal == JFileChooser.APPROVE_OPTION){
            	outputdir = fcout.getSelectedFile();
                outputFolder.setText(outputdir.getAbsolutePath());
            }
            this.pack();			
		}
		if("Convert".equals(event.getActionCommand())){
			//outputText="";//erasing last conversions
			if(inputdir==null){
				statusText.setText("Select input folder please!");
				return;
			}
			if(files==null){//If he chooses a single file
				try{
					if(outputdir==null)
						outputdir=inputdir.getParentFile();
	        		outputText+="Converting "+inputdir.getName()+"...";
	        		statusText.setText(outputText);
	        		if (Convertor.convert(inputdir.getName(),inputdir.getParentFile().getAbsolutePath(),outputdir.getAbsolutePath())) 
						outputText+="\nConversion completed with parsing errors...\nAll following conversions will be wrong.\nPlease restart!\n";
	        		else outputText+="\nConversion completed successfully\n";
	        		statusText.setText(outputText);
	        		this.pack();	
				}catch(ConversionCrashedException cce){
	        		outputText+="Conversion FAILED!\n";
	        		statusText.setText(outputText);
	        		this.pack();					
				}
			}else{//If he chooses a folder
				if(outputdir==null)
					outputdir=inputdir;
	        	for(int i=0;i<files.length;i++){
					try{
		        		outputText+="Converting "+files[i]+"...";
		        		statusText.setText(outputText);
		        		if (Convertor.convert(files[i],inputdir.getAbsolutePath(),outputdir.getAbsolutePath()))
							outputText+="\nConversion completed with parsing errors...\nAll following conversions will be wrong.\nPlease restart!\n";
		        		else outputText+="\nConversion completed successfully\n";
		        		statusText.setText(outputText);
		        		this.pack();
					}catch(ConversionCrashedException cce){
		        		outputText+="Conversion FAILED!\n";
		        		statusText.setText(outputText);
		        		this.pack();					
					}
	        	}
			}
    		outputText+="---------------\n";
    		statusText.setText(outputText);
		}
	}
	public static void main(String[] args){
		showGUI();
	}
	
}
