001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.gui.clipboard;
025    
026    import java.awt.datatransfer.Clipboard;
027    import java.awt.datatransfer.ClipboardOwner;
028    import java.awt.datatransfer.DataFlavor;
029    import java.awt.datatransfer.Transferable;
030    import java.awt.datatransfer.UnsupportedFlavorException;
031    import java.io.IOException;
032    import java.io.Serializable;
033    
034    import org.ujmp.core.Matrix;
035    import org.ujmp.core.enums.FileFormat;
036    import org.ujmp.core.exceptions.MatrixException;
037    
038    public class MatrixSelection implements Transferable, ClipboardOwner, Serializable {
039            private static final long serialVersionUID = -8462961141636462510L;
040    
041            public static final int STRING = 0;
042    
043            public static final int IMAGE = 1;
044    
045            public static final int MATRIX = 2;
046    
047            // private static final DataFlavor[] flavors = { DataFlavor.stringFlavor,
048            // new DataFlavor("image/jpeg", "JPG Image"),
049            // MatrixFlavor.matrixFlavor };
050    
051            private static final DataFlavor[] flavors = { DataFlavor.stringFlavor };
052    
053            private String stringData = null;
054    
055            public MatrixSelection(Matrix matrix) throws MatrixException, IOException {
056                    stringData = matrix.exportToString(FileFormat.CSV);
057            }
058    
059            public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
060                    if (flavor.equals(flavors[STRING])) {
061                            return stringData;
062                    } else {
063                            throw new UnsupportedFlavorException(flavor);
064                    }
065            }
066    
067            public DataFlavor[] getTransferDataFlavors() {
068                    return flavors.clone();
069            }
070    
071            public boolean isDataFlavorSupported(DataFlavor flavor) {
072                    for (int i = 0; i < flavors.length; i++) {
073                            if (flavor.equals(flavors[i])) {
074                                    return true;
075                            }
076                    }
077                    return false;
078            }
079    
080            public void lostOwnership(Clipboard clipboard, Transferable contents) {
081            }
082    
083    }