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.DataFlavor;
024    import java.awt.datatransfer.Transferable;
025    import java.awt.datatransfer.UnsupportedFlavorException;
026    import java.io.IOException;
027    
028    import javax.swing.JComponent;
029    import javax.swing.TransferHandler;
030    
031    import csheets.core.Cell;
032    import csheets.core.formula.compiler.FormulaCompilationException;
033    
034    /**
035     * The transfer handler used to transfer ranges of cells.
036     * @author Einar Pehrson
037     */
038    @SuppressWarnings("serial")
039    public class CellTransferHandler extends TransferHandler {
040    
041            /** The table from which cells were copied or moved */
042            private SpreadsheetTable sourceTable;
043    
044            /** The action that was taken on export, i.e. either COPY or MOVE */
045            private int exportAction = -1;
046    
047            /**
048             * Creates a new cell transfer handler.
049             */
050            public CellTransferHandler() {}
051    
052            protected Transferable createTransferable(JComponent c) {
053                    if (c instanceof SpreadsheetTable) {
054                            this.sourceTable = (SpreadsheetTable)c;
055                            return new CellTransferable(sourceTable.getSelectedCells());
056                    }
057                    return null;
058            }
059    
060            public boolean importData(JComponent c, Transferable t) {
061                    if (canImport(c, t.getTransferDataFlavors())) {
062                            // Fetches destination
063                            SpreadsheetTable table = (SpreadsheetTable)c;
064                            int activeColumn = table.getColumnModel().getSelectionModel().getAnchorSelectionIndex();
065                            int activeRow = table.getSelectionModel().getAnchorSelectionIndex();
066    
067                            if (t.isDataFlavorSupported(CellTransferable.LOCAL_CELL_FLAVOR)) {
068                                    // Fetches transfer data from cell transferable
069                                    Cell[][] range = null;
070                                    try {
071                                            range = (Cell[][])t.getTransferData(CellTransferable.LOCAL_CELL_FLAVOR);
072                                    } catch (UnsupportedFlavorException e) {
073                                            return false;
074                                    } catch (IOException e) {
075                                            return false;
076                                    }
077    
078                                    // Pastes data
079                                    for (int row = 0; row < range.length; row++)
080                                            for (int column = 0; column < range[row].length; column++) {
081                                                    // Fetches source and destination cell
082                                                    Cell sourceCell = range[row][column];
083                                                    Cell destCell = table.getSpreadsheet().getCell(activeColumn + column, activeRow + row);
084            
085                                                    // Performs action and updates table
086                                                    if (exportAction == COPY)
087                                                            destCell.copyFrom(sourceCell);
088                                                    else if (exportAction == MOVE || exportAction == -1)
089                                                            destCell.moveFrom(sourceCell);
090                                            }
091                                    return true;
092                            } else if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
093                                    // Fetches transfer data from string transferable
094                                    String data = null;
095                                    try {
096                                            data = (String)t.getTransferData(DataFlavor.stringFlavor);
097                                    } catch (UnsupportedFlavorException e) {
098                                            return false;
099                                    } catch (IOException e) {
100                                            return false;
101                                    }
102    
103                                    if (data != null) {
104                                            // Pastes data
105                                            String[] rows = data.split("\n");
106                                            for (int row = 0; row < rows.length; row++) {
107                                                    String[] rowData = rows[row].split("\t");
108                                                    for (int column = 0; column < rowData.length; column++) {
109                                                            Cell destCell = table.getSpreadsheet().getCell(
110                                                                    activeColumn + column, activeRow + row);
111                                                            try {
112                                                                    destCell.setContent(rowData[column]);
113                                                            } catch (FormulaCompilationException e) {}
114                                                    }
115                                            }
116                                    }
117                            }
118                    }
119                    return false;
120            }
121    
122            public boolean canImport(JComponent c, DataFlavor[] flavors) {
123                    boolean hasValidFlavor = false;
124                    for (DataFlavor flavor : flavors)
125                            if (flavor.equals(CellTransferable.LOCAL_CELL_FLAVOR)
126                                    || flavor.equals(DataFlavor.stringFlavor)) {
127                                    hasValidFlavor = true;
128                                    break;
129                            }
130                    return c instanceof SpreadsheetTable && hasValidFlavor;
131            }
132    
133            public int getSourceActions(JComponent c) {
134                    return COPY; // COPY_OR_MOVE;
135            }
136    
137            protected void exportDone(JComponent c, Transferable data, int action) {
138                    this.exportAction = action;
139            }
140    }