package org.docear.plugin.bibtex.actions;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.Collection;
import org.freeplane.core.ui.AFreeplaneAction;
import org.freeplane.core.ui.EnabledAction;
import org.freeplane.features.map.NodeModel;
import org.freeplane.features.mode.Controller;
@EnabledAction(checkOnNodeChange=true)
public class CopyCiteKeyToClipboard extends AFreeplaneAction{
public final static String KEY = "CopyCiteKeyToClipboardAction";
/**
*
*/
private static final long serialVersionUID = 1L;
public CopyCiteKeyToClipboard() {
super(KEY);
}
public void actionPerformed(ActionEvent e) {
Collection<NodeModel> nodes = Controller.getCurrentModeController().getMapController().getSelectedNodes();
String strBuffer = CopyBibtexToClipboard.serializeStringSet(CopyBibtexToClipboard.getKeySet(nodes));
final String citeKeys = "\\cite{"+strBuffer+"}";
Transferable content = new Transferable() {
public boolean isDataFlavorSupported(DataFlavor flavor) {
return DataFlavor.stringFlavor.equals(flavor);
}
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{DataFlavor.stringFlavor};
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if(DataFlavor.stringFlavor.equals(flavor)) {
return citeKeys;
}
throw new UnsupportedFlavorException(flavor);
}
};
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(content, null);
}
public void setEnabled() {
setEnabled(true);
}
}