001 /*
002 * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>.
003 *
004 * This file is part of
005 * CleanSheets - a spreadsheet application for the Java platform.
006 *
007 * CleanSheets is free software; you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published by
009 * the Free Software Foundation; either version 2 of the License, or
010 * (at your option) any later version.
011 *
012 * CleanSheets is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with CleanSheets; if not, write to the Free Software
019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020 */
021 package csheets.ui.sheet;
022
023 import java.awt.datatransfer.Clipboard;
024 import java.awt.datatransfer.ClipboardOwner;
025 import java.awt.datatransfer.DataFlavor;
026 import java.awt.datatransfer.Transferable;
027 import java.awt.datatransfer.UnsupportedFlavorException;
028
029 import csheets.core.Cell;
030
031 /**
032 * A transferable for transferring a range of cells. Cells are transferred
033 * either as objects locally within the Java VM, or exported as strings.
034 * @author Einar Pehrson
035 */
036 public class CellTransferable implements Transferable, ClipboardOwner {
037
038 /** The data flavor for local transfer of cells */
039 public static final DataFlavor LOCAL_CELL_FLAVOR
040 = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + ";class=" +
041 Cell.class.getName(), "CleanSheets Cell Range (Local)");
042
043 /** The data flavor for serialized transfer of cells */
044 public static final DataFlavor SERIAL_CELL_FLAVOR
045 = new DataFlavor(Cell.class, "CleanSheets Cell Range (Serialized)");
046
047 /** The cells to transfer */
048 private Cell[][] cells;
049
050 /**
051 * Creates a new cell transferable.
052 * @param cells the cells to transfer
053 */
054 public CellTransferable(Cell[][] cells) {
055 this.cells = cells;
056 }
057
058 public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
059 if (LOCAL_CELL_FLAVOR.equals(flavor))
060 // Exports as cell array
061 return cells;
062 else if (DataFlavor.stringFlavor.equals(flavor)) {
063 // Exports as string
064 String data = "";
065 for (Cell[] row : cells) {
066 for (int column = 0; column < row.length; column++) {
067 data += row[column].getValue().toString();
068 if (column != row.length - 1)
069 data += "\t";
070 }
071 data += "\n";
072 }
073 return data;
074 } else
075 throw new UnsupportedFlavorException(flavor);
076 }
077
078 public DataFlavor[] getTransferDataFlavors() {
079 return new DataFlavor[] {LOCAL_CELL_FLAVOR, DataFlavor.stringFlavor};
080 }
081
082 public boolean isDataFlavorSupported(DataFlavor flavor) {
083 return LOCAL_CELL_FLAVOR.equals(flavor)
084 || DataFlavor.stringFlavor.equals(flavor);
085 }
086
087 public void lostOwnership(Clipboard clipboard, Transferable contents) {}
088 }