package org.openlca.app.wizards.io;

import java.io.File;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.IImportWizard;
import org.eclipse.ui.IWorkbench;
import org.openlca.app.M;
import org.openlca.app.db.Database;
import org.openlca.app.navigation.Navigator;
import org.openlca.app.rcp.images.Icon;
import org.openlca.core.database.IDatabase;
import org.openlca.io.simapro.csv.input.SimaProCsvImport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimaProCsvImportWizard extends Wizard implements IImportWizard {

	private Logger log = LoggerFactory.getLogger(getClass());
	private FileImportPage importPage;

	public SimaProCsvImportWizard() {
		setNeedsProgressMonitor(true);
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		setWindowTitle(M.SimaProCSVImport);
		setDefaultPageImageDescriptor(Icon.IMPORT_ZIP_WIZARD
				.descriptor());
	}

	@Override
	public boolean performFinish() {
		File[] files = importPage.getFiles();
		IDatabase database = Database.get();
		if (files == null || files.length == 0 || database == null)
			return false;
		final SimaProCsvImport importer = new SimaProCsvImport(database,
				files[0]);
		try {
			Database.getIndexUpdater().beginTransaction();
			getContainer().run(true, true, new IRunnableWithProgress() {
				@Override
				public void run(final IProgressMonitor monitor)
						throws InvocationTargetException, InterruptedException {
					monitor.beginTask(M.Import, IProgressMonitor.UNKNOWN);
					ImportHandler handler = new ImportHandler(monitor);
					handler.run(importer);
				}
			});
			Navigator.refresh();
		} catch (Exception e) {
			log.error("SimaPro CSV import failed", e);
		} finally {
			Database.getIndexUpdater().endTransaction();			
		}
		return true;
	}

	@Override
	public void addPages() {
		importPage = new FileImportPage(new String[] { "csv" }, false);
		addPage(importPage);
	}

}

@@ -55,167 +55,57 @@
 le;%0A
-import java.lang.reflect.InvocationTargetException;%0A%0Aimport org.eclipse.core.runtime.IProgressMonitor;%0Aimport org.eclipse.jface.operation.IRunnableWithProgress
+%0Aimport org.eclipse.core.runtime.IProgressMonitor
 ;%0Aim
@@ -1122,23 +1122,17 @@
 tabase d
-atabase
+b
  = Datab
@@ -1187,23 +1187,17 @@
 = 0 %7C%7C d
-atabase
+b
  == null
@@ -1217,22 +1217,16 @@
 alse;%0A%09%09
-final 
 SimaProC
@@ -1271,29 +1271,16 @@
 rt(d
-atabase,%0A%09%09%09%09
+b, 
 files
-%5B0%5D
 );%0A%09
@@ -1374,168 +1374,21 @@
 ue, 
-new IRunnableWithProgress() %7B%0A%09%09%09%09@Override%0A%09%09%09%09public void run(final IProgressMonitor monitor)%0A%09%09%09%09%09%09throws InvocationTargetException, InterruptedException
+monitor -%3E
  %7B%0A
-%09
 %09%09%09%09
@@ -1446,17 +1446,16 @@
 N);%0A%09%09%09%09
-%09
 ImportHa
@@ -1498,17 +1498,16 @@
 nitor);%0A
-%09
 %09%09%09%09hand
@@ -1529,14 +1529,8 @@
 r);%0A
-%09%09%09%09%7D%0A
 %09%09%09%7D
@@ -1689,19 +1689,16 @@
 ction();
-%09%09%09
 %0A%09%09%7D%0A%09%09r
@@ -1808,20 +1808,19 @@
 csv%22 %7D, 
-fals
+tru
 e);%0A%09%09ad

support multiple files in SimaPro CSV import