/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 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 Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.io;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.datatransfer.StringSelection;
import java.awt.Toolkit;

import java.io.IOException;

/*
 * Created on Apr 15, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author jeff
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ClipboardSupport {

	private static final String ENTRY_SEPARATOR = ",";
	
	public static String GetTabularStringData() {
		String htmlContents = GetClipboardHTML();
		if(htmlContents == null)
			return GetClipboardText();
		
		return HTMLImportExport.ConvertHTMLToTextTable(htmlContents);
	}
	
	public static String GetClipboardHTML() {
		// get the system clipboard
		Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		
		// get the contents on the clipboard in a 
		// transferable object
		Transferable clipboardContents = systemClipboard.getContents(null);
		
		// check if clipboard is empty
		if (clipboardContents == null) {
			return null;
		} else
			try {
				
				DataFlavor htmlFlavor = new DataFlavor("text/html;class=java.lang.String");
				
				if (clipboardContents.isDataFlavorSupported(htmlFlavor)) {
					// return text content
					String returnText =	(String)clipboardContents.getTransferData(htmlFlavor);
					return returnText;
				}
			} catch (ClassNotFoundException cnfe) {
				cnfe.printStackTrace();
			} catch (UnsupportedFlavorException ufe) {
				ufe.printStackTrace();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		return null;
	}
	
	public static String GetClipboardText() {
		// get the system clipboard
		Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		
		// get the contents on the clipboard in a 
		// transferable object
		Transferable clipboardContents = systemClipboard.getContents(null);
		
		// check if clipboard is empty
		if (clipboardContents == null) {
			return null;
		} else
			try {
				// see if DataFlavor of 
				// DataFlavor.stringFlavor is supported
				if (clipboardContents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
					// return text content
					String returnText =	(String)clipboardContents.getTransferData(DataFlavor.stringFlavor);
					return returnText;
				}
			} catch (UnsupportedFlavorException ufe) {
				ufe.printStackTrace();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		return null;
	}
	
	public static void putClipboard(String text, ClipboardOwner owner) {
		StringSelection stringSelection = new StringSelection(text);
	    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
	    clipboard.setContents(stringSelection, owner);
	}
	

}
