| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 class CopyPasteManager { | |
| 6 | |
| 7 // Anchor cell of the current selection | |
| 8 CellLocation _selectionAnchor; | |
| 9 // Current selection in paste string format | |
| 10 Map<CellLocation, String> _selectionContents; | |
| 11 // Opposite corner cell of the current selection | |
| 12 CellLocation _selectionCorner; | |
| 13 // Height of the selection | |
| 14 int _selectionHeight; | |
| 15 // The selection manager | |
| 16 SelectionManager _selectionManager; | |
| 17 // Current selection styles (as copies) | |
| 18 Map<CellLocation, Style> _selectionStyles; | |
| 19 // Width of the selection | |
| 20 int _selectionWidth; | |
| 21 // Spreadsheet containing the selection | |
| 22 Spreadsheet _spreadsheet; | |
| 23 | |
| 24 CopyPasteManager(this._selectionManager, this._spreadsheet) { } | |
| 25 | |
| 26 /** | |
| 27 * Clear the cell contents within the selection, leaving styles intact. | |
| 28 */ | |
| 29 void clearSelection() { | |
| 30 _selectionManager.applyToSelectedCells( | |
| 31 (CellLocation location) { | |
| 32 // TODO: should styles be cleared? | |
| 33 _spreadsheet.clearCell(location.rowCol); | |
| 34 }); | |
| 35 } | |
| 36 | |
| 37 /** | |
| 38 * Copy the current selection contents and styles into a local copy buffer. | |
| 39 */ | |
| 40 void copySelection() { | |
| 41 if (_selectionManager.isSelectionEmpty()) { | |
| 42 return; | |
| 43 } | |
| 44 | |
| 45 _selectionWidth = _selectionManager.selectionCorner.col - _selectionManager.
selectedCell.col; | |
| 46 _selectionHeight = _selectionManager.selectionCorner.row - _selectionManager
.selectedCell.row; | |
| 47 _selectionContents = new Map<CellLocation, String>(); | |
| 48 _selectionStyles = new Map<CellLocation, Style>(); | |
| 49 _selectionManager.applyToSelectedCells( | |
| 50 (CellLocation location) { | |
| 51 Cell cell = location.getCell(); | |
| 52 if (cell != null) { | |
| 53 _selectionContents[location] = cell.getPasteContent(); | |
| 54 _selectionStyles[location] = cell.style; | |
| 55 } else { | |
| 56 _selectionContents[location] = null; | |
| 57 _selectionStyles[location] = null; | |
| 58 } | |
| 59 }); | |
| 60 if (_selectionContents.isEmpty()) { | |
| 61 // Empty -> null | |
| 62 _selectionContents = null; | |
| 63 _selectionStyles = null; | |
| 64 _selectionAnchor = null; | |
| 65 _selectionCorner = null; | |
| 66 } else { | |
| 67 _selectionAnchor = _selectionManager.selectedCell; | |
| 68 _selectionCorner = _selectionManager.selectionCorner; | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 /** | |
| 73 * Return [:true:] if the local copy buffer is non-empty. | |
| 74 */ | |
| 75 bool hasCopiedData() => _selectionContents != null; | |
| 76 | |
| 77 /** | |
| 78 * Paste data from the local copy buffer into the spreadsheet, starting at the | |
| 79 * [SelectionManager]'s selected cell. | |
| 80 */ | |
| 81 void pasteSelection() { | |
| 82 if (_selectionContents == null) { | |
| 83 return; | |
| 84 } | |
| 85 // TODO: copy in chunks (when source is not a single row/col) | |
| 86 CellLocation pasteAnchor = _selectionManager.selectedCell; | |
| 87 CellLocation pasteCorner = _selectionManager.selectionCorner; | |
| 88 int dx1 = pasteAnchor.col - _selectionAnchor.col; | |
| 89 int dy1 = pasteAnchor.row - _selectionAnchor.row; | |
| 90 int dx2 = pasteCorner.col - _selectionAnchor.col; | |
| 91 int dy2 = pasteCorner.row - _selectionAnchor.row; | |
| 92 int ystep = _sign(dy2 - dy1); | |
| 93 int xstep = _sign(dx2 - dx1); | |
| 94 if (_selectionCorner.row != _selectionAnchor.row) { | |
| 95 dy2 = dy1; | |
| 96 } | |
| 97 if (_selectionCorner.col != _selectionAnchor.col) { | |
| 98 dx2 = dx1; | |
| 99 } | |
| 100 _selectionContents.forEach((CellLocation location, String contents) { | |
| 101 int dy = dy1; | |
| 102 while (true) { | |
| 103 int dx = dx1; | |
| 104 while (true) { | |
| 105 CellLocation newLocation = new CellLocation(pasteAnchor.spreadsheet, | |
| 106 new RowCol(location.row + dy, location.col + dx)); | |
| 107 if (contents != null && | |
| 108 newLocation.row >= 1 && | |
| 109 newLocation.col >= 1) { | |
| 110 newLocation.setCellFromContentString(contents); | |
| 111 newLocation.spreadsheet.setCellStyle(newLocation.rowCol, _selectionS
tyles[location]); | |
| 112 } | |
| 113 if (dx == dx2) { | |
| 114 break; | |
| 115 } | |
| 116 dx += xstep; | |
| 117 } | |
| 118 if (dy == dy2) { | |
| 119 break; | |
| 120 } | |
| 121 dy += ystep; | |
| 122 } | |
| 123 }); | |
| 124 } | |
| 125 | |
| 126 int _sign(int x) { | |
| 127 if (x < 0) { | |
| 128 return -1; | |
| 129 } else if (x > 0) { | |
| 130 return 1; | |
| 131 } | |
| 132 return 0; | |
| 133 } | |
| 134 } | |
| OLD | NEW |