/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License");  you may not use this file except in 
 * compliance with the License.  You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is Protege-2000.
 *
 * The Initial Developer of the Original Code is Stanford University. Portions
 * created by Stanford University are Copyright (C) 2007.  All Rights Reserved.
 *
 * Protege was developed by Stanford Medical Informatics
 * (http://www.smi.stanford.edu) at the Stanford University School of Medicine
 * with support from the National Library of Medicine, the National Science
 * Foundation, and the Defense Advanced Research Projects Agency.  Current
 * information about Protege can be obtained at http://protege.stanford.edu.
 *
 */

package edu.stanford.smi.protege.util;

import java.awt.datatransfer.*;
import java.util.*;

import edu.stanford.smi.protege.model.*;

/**
 * Transferable object for a collection of Frames.  This is used for drag and drop.  Note that frames are not
 * serializable so we have to use "java local object mime type".
 * 
 * @author    Ray Fergerson <fergerson@smi.stanford.edu>
 */
public class TransferableCollection implements Transferable {
    private static final Collection flavors = new ArrayList();
    private static DataFlavor collectionFlavor;
    private ArrayList frames;

    static {
        try {
            String collectionClassname = ArrayList.class.getName();
            String collectionFlavorName = DataFlavor.javaJVMLocalObjectMimeType + "; class=" + collectionClassname;
            collectionFlavor = new DataFlavor(collectionFlavorName);

            flavors.add(collectionFlavor);
            flavors.add(DataFlavor.stringFlavor);
        } catch (Exception e) {
            Log.getLogger().warning(Log.toString(e));
        }
    }

    public TransferableCollection(Collection frames) {
        this.frames = new ArrayList(frames);
    }

    public static DataFlavor getCollectionFlavor() {
        return collectionFlavor;
    }

    public Object getTransferData(DataFlavor flavor) {
        Object o;
        if (flavor.equals(collectionFlavor)) {
            o = getCollectionTransferData();
        } else if (flavor.equals(DataFlavor.stringFlavor)) {
            o = getStringTransferData();
        } else {
            o = null;
        }
        return o;
    }

    public Object getCollectionTransferData() {
        return new ArrayList(frames);
    }

    public Object getStringTransferData() {
        StringBuffer buffer = new StringBuffer();
        Iterator i = frames.iterator();
        while (i.hasNext()) {
            Frame frame = (Frame) i.next();
            String name = frame.getName();
            buffer.append(name);
            if (i.hasNext()) {
                buffer.append("\n");
            }
        }
        return buffer.toString();
    }

    public DataFlavor[] getTransferDataFlavors() {
        return (DataFlavor[]) flavors.toArray(new DataFlavor[flavors.size()]);
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return flavors.contains(flavor);
    }
}
