package uk.org.rightfield;
/*******************************************************************************
 * Copyright (c) 2009-2012, University of Manchester
 * 
 * Licensed under the New BSD License. 
 * Please see LICENSE file that is distributed with the source code
 ******************************************************************************/

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.prefs.Preferences;

import javax.swing.JFrame;

import org.apache.log4j.Logger;

import uk.ac.manchester.cs.owl.semspreadsheets.export.CSVExporter;
import uk.ac.manchester.cs.owl.semspreadsheets.export.Exporter;
import uk.ac.manchester.cs.owl.semspreadsheets.export.RDFExporter;
import uk.ac.manchester.cs.owl.semspreadsheets.model.WorkbookManager;
import uk.ac.manchester.cs.owl.semspreadsheets.ui.WorkbookFrame;

/**
 * @author Stuart Owen
 * @author Matthew Horridge
 */
public class RightField {

	private static Logger logger = Logger.getLogger(RightField.class);
	
    private static final String WINDOW_X_KEY = "orch.window.x";

    private static final String WINDOW_Y_KEY = "orch.window.y";

    private static final String WINDOW_WIDTH_KEY = "orch.window.width";

    private static final String WINDOW_HEIGHT_KEY = "orch.window.height";

    public static void main(String[] args) {
    	new RightField(args);
    }
    
    public RightField(String [] args) {
    	logger.debug("Intialised");
    	boolean cont=handleArguments(args);
    	
    	if (cont) {
    		startUp();
    	}
    }
    
    private boolean handleArguments(String [] args) {
    	RightFieldOptions options = new RightFieldOptions(args);
    	boolean cont=true;
    	if (options.isExport()) {
    		try {
    			export(options);
    			cont=false;
    		}
    		catch(Exception e) {
    			logger.error("Error attempting export",e);    			
    		}    		
    	}
    	return cont;
    }
    
    private void export(RightFieldOptions options) throws Exception {
    	logger.debug("Exporting "+options.getFilename());
    	File file = new File(options.getFilename());
    	Exporter exporter;
    	if (options.getExportFormat().equals("rdf")) {
    		exporter=new RDFExporter(file,options.getId());
    	}
    	else if (options.getExportFormat().equals("csv")) {
    		exporter=new CSVExporter(file);
    	}
    	else {
    		throw new Exception("Unrecognised export format");
    	}
    	exporter.export(System.out);
    	
    }
    
    private void startUp() {
    	logger.debug("Starting Up UI");
    	WorkbookManager manager = new WorkbookManager();    	
        final WorkbookFrame frame = new WorkbookFrame(manager);
        Preferences preferences = Preferences.userNodeForPackage(RightField.class);
        int x = preferences.getInt(WINDOW_X_KEY, 50);
        int y = preferences.getInt(WINDOW_Y_KEY, 50);
        int width = preferences.getInt(WINDOW_WIDTH_KEY, 800);
        int height = preferences.getInt(WINDOW_HEIGHT_KEY, 600);
        frame.setLocation(x, y);
        frame.setSize(width, height);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {
        	@Override
        	public void windowClosed(WindowEvent e) {
        		System.exit(0);
        	}
            @Override
            public void windowClosing(WindowEvent e) {            	
            	boolean close = frame.checkSavedState("Exit RightField");            	            	
				if (close) {
					Preferences preferences = Preferences.userNodeForPackage(RightField.class);
	                preferences.putInt(WINDOW_X_KEY, frame.getX());
	                preferences.putInt(WINDOW_Y_KEY, frame.getY());
	                preferences.putInt(WINDOW_WIDTH_KEY, frame.getWidth());
	                preferences.putInt(WINDOW_HEIGHT_KEY, frame.getHeight());
					frame.dispose();
				}                
            }
        });
    }
}

@@ -1547,24 +1547,45 @@
 p();%0A    %09%7D%0A
+    %09System.exit(0);%0A
     %7D%0A    %0A 
@@ -1750,24 +1750,42 @@
 Export()) %7B%0A
+    %09%09cont=false;%0A
     %09%09try %7B%0A
@@ -1811,27 +1811,15 @@
 ns);
-%0A
     %09%09%09
-cont=false;
 %0A   
@@ -1899,23 +1899,40 @@
 ort%22,e);
+%0A
     %09%09%09
+System.exit(-1);
 %0A    %09%09%7D
@@ -2497,17 +2497,16 @@
 em.out);
-%0A
     %09%0A  

exit with an error status if export fails, e.g. due to an invalid file format