X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.nattable%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fnattable%2Foverride%2FCopyDataToClipboardSerializer.java;fp=bundles%2Forg.simantics.browsing.ui.nattable%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fnattable%2Foverride%2FCopyDataToClipboardSerializer.java;h=b4eda7d00e6f38184a83fb6fe14c884054bd4d90;hp=0000000000000000000000000000000000000000;hb=96bb7ef9cbe42d82eb58306d8f9b62392cc29ba8;hpb=ae5bb63c5c88f6569518fed2a24df86fbd0570ff diff --git a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/override/CopyDataToClipboardSerializer.java b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/override/CopyDataToClipboardSerializer.java new file mode 100644 index 000000000..b4eda7d00 --- /dev/null +++ b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/override/CopyDataToClipboardSerializer.java @@ -0,0 +1,92 @@ +/******************************************************************************* + * Copyright (c) 2012 Original authors and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Original authors and others - initial API and implementation + ******************************************************************************/ +package org.simantics.browsing.ui.nattable.override; + +import org.eclipse.nebula.widgets.nattable.copy.command.CopyDataToClipboardCommand; +import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell; +import org.eclipse.nebula.widgets.nattable.serializing.ISerializer; +import org.eclipse.swt.dnd.Clipboard; +import org.eclipse.swt.dnd.TextTransfer; +import org.eclipse.swt.dnd.Transfer; +import org.eclipse.swt.widgets.Display; + +/** + * CopyDataToClipboardSerializer implementation that limits the amount of data that is copied. + * Copying too much data will result in OutOfMemoryError. + * + * @see org.eclipse.nebula.widgets.nattable.copy.serializing.CopyDataToClipboardSerializer + * + * @author MLMARKO + * + */ +public class CopyDataToClipboardSerializer implements ISerializer { + + public static final int COPY_THRESHOLD = 500000; // threshold for preventing OOM. + + private final ILayerCell[][] copiedCells; + private final CopyDataToClipboardCommand command; + + public CopyDataToClipboardSerializer(ILayerCell[][] copiedCells, + CopyDataToClipboardCommand command) { + this.copiedCells = copiedCells; + this.command = command; + } + + @Override + public void serialize() { + final String cellDelimeter = this.command.getCellDelimeter(); + final String rowDelimeter = this.command.getRowDelimeter(); + + final TextTransfer textTransfer = TextTransfer.getInstance(); + final StringBuilder textData = new StringBuilder(); + int count = 0; + for (ILayerCell[] cells : copiedCells) { + count+= cells.length; + } + if (count <= COPY_THRESHOLD) { + int currentRow = 0; + for (ILayerCell[] cells : this.copiedCells) { + int currentCell = 0; + for (ILayerCell cell : cells) { + final String delimeter = ++currentCell < cells.length ? cellDelimeter + : ""; //$NON-NLS-1$ + if (cell != null) { + textData.append(getTextForCell(cell) + delimeter); + } else { + textData.append(delimeter); + } + } + if (++currentRow < this.copiedCells.length) { + textData.append(rowDelimeter); + } + } + } else { + textData.append("Too many cells copied (" + count + ")"); + } + if (textData.length() > 0) { + final Clipboard clipboard = new Clipboard(Display.getDefault()); + try { + clipboard.setContents(new Object[] { textData.toString() }, + new Transfer[] { textTransfer }); + } finally { + clipboard.dispose(); + } + } + } + + protected String getTextForCell(ILayerCell cell) { + return String.valueOf(cell.getDataValue()); + } + + final protected CopyDataToClipboardCommand getCommand() { + return this.command; + } +}