1 /*******************************************************************************
2 * Copyright (c) 2012 Original authors and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Original authors and others - initial API and implementation
10 ******************************************************************************/
11 package org.simantics.browsing.ui.nattable.override;
13 import org.eclipse.nebula.widgets.nattable.copy.command.CopyDataToClipboardCommand;
14 import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
15 import org.eclipse.nebula.widgets.nattable.serializing.ISerializer;
16 import org.eclipse.swt.dnd.Clipboard;
17 import org.eclipse.swt.dnd.TextTransfer;
18 import org.eclipse.swt.dnd.Transfer;
19 import org.eclipse.swt.widgets.Display;
22 * CopyDataToClipboardSerializer implementation that limits the amount of data that is copied.
23 * Copying too much data will result in OutOfMemoryError.
25 * @see org.eclipse.nebula.widgets.nattable.copy.serializing.CopyDataToClipboardSerializer
30 public class CopyDataToClipboardSerializer implements ISerializer {
32 public static final int COPY_THRESHOLD = 500000; // threshold for preventing OOM.
34 private final ILayerCell[][] copiedCells;
35 private final CopyDataToClipboardCommand command;
37 public CopyDataToClipboardSerializer(ILayerCell[][] copiedCells,
38 CopyDataToClipboardCommand command) {
39 this.copiedCells = copiedCells;
40 this.command = command;
44 public void serialize() {
45 final String cellDelimeter = this.command.getCellDelimeter();
46 final String rowDelimeter = this.command.getRowDelimeter();
48 final TextTransfer textTransfer = TextTransfer.getInstance();
49 final StringBuilder textData = new StringBuilder();
51 for (ILayerCell[] cells : copiedCells) {
54 if (count <= COPY_THRESHOLD) {
56 for (ILayerCell[] cells : this.copiedCells) {
58 for (ILayerCell cell : cells) {
59 final String delimeter = ++currentCell < cells.length ? cellDelimeter
62 textData.append(getTextForCell(cell) + delimeter);
64 textData.append(delimeter);
67 if (++currentRow < this.copiedCells.length) {
68 textData.append(rowDelimeter);
72 textData.append("Too many cells copied (" + count + ")");
74 if (textData.length() > 0) {
75 final Clipboard clipboard = new Clipboard(Display.getDefault());
77 clipboard.setContents(new Object[] { textData.toString() },
78 new Transfer[] { textTransfer });
85 protected String getTextForCell(ILayerCell cell) {
86 return String.valueOf(cell.getDataValue());
89 final protected CopyDataToClipboardCommand getCommand() {