package org.cytoscape.task.internal.export;

/*
 * #%L
 * Cytoscape Core Task Impl (core-task-impl)
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2006 - 2013 The Cytoscape Consortium
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.SwingUtilities;

import org.apache.commons.io.FilenameUtils;
import org.cytoscape.io.CyFileFilter;
import org.cytoscape.io.write.CyWriter;
import org.cytoscape.io.write.CyWriterFactory;
import org.cytoscape.io.write.CyWriterManager;
import org.cytoscape.work.ContainsTunables;
import org.cytoscape.work.Tunable;
import org.cytoscape.work.TunableValidator;
import org.cytoscape.work.swing.RequestsUIHelper;
import org.cytoscape.work.swing.TunableUIHelper;
import org.cytoscape.work.util.ListChangeListener;
import org.cytoscape.work.util.ListSelection;
import org.cytoscape.work.util.ListSingleSelection;

/**
 * An abstract utility implementation of a Task that writes a user defined file
 * to a file type determined by a provided writer manager.
 * This class is meant to be extended for specific file types such that the appropriate
 * {@link org.cytoscape.io.write.CyWriter} can be identified.
 */
public abstract class TunableAbstractCyWriter<S extends CyWriterFactory, T extends CyWriterManager<S>> extends
		AbstractCyWriter<S, T> implements TunableValidator, RequestsUIHelper {

	/**
	 * The list of file type options generated by the file types available from
	 * the CyWriterManager. This field should not be set directly, but rather
	 * handled by the {@link org.cytoscape.work.Tunable} processing.
	 */
	@Tunable(description = "Select the export file format:", gravity = 0.0)
	public ListSingleSelection<String> options;

	@ContainsTunables
	public CyWriter writer = null;
	
	@Override
	public CyWriter getWriter() {
		return writer;
	}

	@Override
	protected final String getExportFileFormat() {
		return options.getSelectedValue();
	}
	
	protected TunableUIHelper helper = null;

	/**
	 * @param writerManager
	 *            The CyWriterManager to be used to determine which
	 *            {@link org.cytoscape.io.write.CyWriter} to be used to write
	 *            the file chosen by the user.
	 */
	public TunableAbstractCyWriter(T writerManager) {
		super(writerManager);
		final List<String> availableFormats = new ArrayList<>(getFileFilterDescriptions());
		options = new ListSingleSelection<String>(availableFormats);
		options.addListener(new ListChangeListener<String>() {
			@Override
			public void selectionChanged(ListSelection<String> source) {
				try {
					writer = getWriter(getFileFilter(getExportFileFormat()));
					if(helper != null)
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								helper.refresh(TunableAbstractCyWriter.this);
							}
						});
						
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void listChanged(ListSelection<String> source) {
			}
		});
	}

	@Override
	public ValidationState getValidationState(final Appendable msg) {
		if (getExportFileFormat() == null) {
			try {
				msg.append("Select a file type.");
			} catch (final Exception e) {
				/* Intentionally empty. */
			}

			return ValidationState.INVALID;
		}

		if (outputFile == null) {
			try {
				msg.append("Enter the file address.");
			} catch (final Exception e) {
				/* Intentionally empty. */
			}

			return ValidationState.INVALID;
		}

		boolean isFileChanged = false;

		// Make sure we have the right extension, if not, then force it:
		if (!fileExtensionIsOk(outputFile)) {
			outputFile = addOrReplaceExtension(outputFile);
			isFileChanged = true;
		}
		
		if (isFileChanged && outputFile.exists()) {
			try {
				msg.append("File already exists, are you sure you want to overwrite it?");
			} catch (final Exception e) {
				/* Intentionally empty. */
			}

			return ValidationState.REQUEST_CONFIRMATION;
		} else {
			return ValidationState.OK;
		}
	}

	protected final boolean fileExtensionIsOk(final File file) {
		final String exportFileFormat = getExportFileFormat();
		
		if (exportFileFormat == null)
			return true;

		final CyFileFilter filter = getFileFilter(exportFileFormat);
		
		if (filter == null)
			return true;

		return filter.getExtensions().contains(FilenameUtils.getExtension(file.getName()));
	}

	protected final File addOrReplaceExtension(final File file) {
		final CyFileFilter filter = getFileFilter(getExportFileFormat());
		
		if (filter == null)
			return file;

		final Iterator<String> extensions = filter.getExtensions().iterator();
		
		if (!extensions.hasNext())
			return file;

		final String filterExtension = extensions.next();
		String fileName = file.getAbsolutePath();
		final String fileExtension = FilenameUtils.getExtension(fileName);
		
		if (!filterExtension.trim().equals(fileExtension.trim()))
			fileName += "." + filterExtension;
		
		return new File(fileName);
	}
	
	@Override
	public void setUIHelper(TunableUIHelper helper) {
		this.helper = helper;
	}
}

@@ -3347,16 +3347,98 @@
 at()));%0A
+%09%09%09%09%09if(outputFile != null)%0A%09%09%09%09%09%09outputFile = addOrReplaceExtension(outputFile);%0A
 %09%09%09%09%09if(

Add or replace file extension when a new file filter is selected.