/*
 * CopyAction.java  $Revision: 1.5 $  $Date: 2007/10/03 04:39:39 $
 *
 * Copyright (C) 2007 Meldcraft Corporation
 *
 * This file is part of Scaffold.
 * 
 * Scaffold 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 3 of the License, or
 * (at your option) any later version.
 * 
 * Scaffold 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Scaffold.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.meldcraft.application.swing.action;

import java.awt.Toolkit;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;

import javax.swing.Action;
import javax.swing.KeyStroke;

import com.meldcraft.application.ApplicationElementContext;

/**
 * @version $Revision: 1.5 $ $Date: 2007/10/03 04:39:39 $
 * @author Rob Richards
 */
public class CopyAction extends AbstractContextCheckingAction
{
// Static variables
    public static final String      COPY_DATA_KEY = "CopyAction.CopyData";

// Member variables
    private Object      mCopyValue = null;
    private Object      mLock = new Object();

// Constructors and initialization
    public CopyAction(ApplicationElementContext context)
    {
        super(context);
        putValue(Action.NAME, "Copy");
        putValue(Action.MNEMONIC_KEY, new Integer('C'));
        putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control C"));
        putValue(Action.SHORT_DESCRIPTION, "Copy");
        putValue(Action.LONG_DESCRIPTION, "Copy");
    }

// Interface implemenations
    public void actionPerformed(ActionEvent e)
    {
        Object val = getCopyValue();
        if (val instanceof Transferable)
        {
            Transferable trans = (Transferable)val;
            ClipboardOwner owner = null;
            if (val instanceof ClipboardOwner)
            {
                owner = (ClipboardOwner)val;
            }

            Toolkit.getDefaultToolkit().
                getSystemClipboard().setContents(trans, owner);
        }
    }

// Public methods
// Protected methods
    protected boolean canEnableForValue(Object key, Object value)
    {
        boolean ret = false;
        setCopyValue(value);
        if ((value instanceof Transferable) &&
            (((Transferable)value).getTransferDataFlavors() != null))
        {
            ret = true;
        }
        return ret;
    }

    protected Object getCopyValue()
    {
        synchronized(mLock)
        {
            return mCopyValue;
        }
    }

    protected void setCopyValue(Object value)
    {
        synchronized(mLock)
        {
            mCopyValue = value;
        }
    }

// Private methods
// Inner classes
}
