package editor.gui.ccp.handler;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.function.Predicate;

import javax.swing.TransferHandler;

import editor.collection.deck.CategorySpec;
import editor.gui.ccp.data.CategoryTransferData;
import editor.gui.ccp.data.DataFlavors;

@SuppressWarnings("serial")
public class CategoryImportHandler extends TransferHandler implements ImportHandler
{
    private final Predicate<CategorySpec> contains;
    private final Predicate<CategorySpec> add;

    public CategoryImportHandler(Predicate<CategorySpec> c, Predicate<CategorySpec> a)
    {
        contains = c;
        add = a;
    }

    @Override
    public DataFlavor supportedFlavor()
    {
        return DataFlavors.categoryFlavor;
    }

    @Override
    public boolean canImport(TransferSupport supp)
    {
        if (supp.isDrop())
            return false;
        else if (supp.isDataFlavorSupported(DataFlavors.categoryFlavor))
        {
            try
            {
                CategoryTransferData data = (CategoryTransferData)supp.getTransferable().getTransferData(DataFlavors.categoryFlavor);
                return !contains.test(data.spec);
            }
            catch (UnsupportedFlavorException | IOException e)
            {
                return false;
            }
        }
        else
            return false;
    }

    @Override
    public boolean importData(TransferSupport supp)
    {
        try
        {
            if (!canImport(supp))
                return false;
            else
                return add.test(((CategoryTransferData)supp.getTransferable().getTransferData(DataFlavors.categoryFlavor)).spec);
        }
        catch (UnsupportedFlavorException | IOException e)
        {
            return false;
        }
    }
}
@@ -361,16 +361,92 @@
 avors;%0A%0A
+/**%0A * Importer for category specifications.%0A * %0A * @author Alec Roelke%0A */%0A
 @Suppres
@@ -559,103 +559,557 @@
     
-private final Predicate%3CCategorySpec%3E contains;%0A    private final Predicate%3CCategorySpec%3E add;%0A
+/** Function specifying how to tell if the deck contains the category already. */%0A    private final Predicate%3CCategorySpec%3E contains;%0A    /** Function specifying how to add the category. */%0A    private final Predicate%3CCategorySpec%3E add;%0A%0A    /**%0A     * Create a new category import handler.%0A     * %0A     * @param c function specifying whether or not the source component contains the%0A     * category, if applicable (should just return false if it's not applicable)%0A     * @param a function specifying how to add the category to the component%0A     */
 %0A   

Add documentation to category import handler