]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/override/CopyDataToClipboardSerializer.java
Sync git svn branch with SVN repository r33144.
[simantics/platform.git] / bundles / org.simantics.browsing.ui.nattable / src / org / simantics / browsing / ui / nattable / override / CopyDataToClipboardSerializer.java
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
7  *
8  * Contributors:
9  *     Original authors and others - initial API and implementation
10  ******************************************************************************/
11 package org.simantics.browsing.ui.nattable.override;
12
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;
20
21 /**
22  * CopyDataToClipboardSerializer implementation that limits the amount of data that is copied.
23  * Copying too much data will result in OutOfMemoryError.
24  * 
25  * @see org.eclipse.nebula.widgets.nattable.copy.serializing.CopyDataToClipboardSerializer
26  * 
27  * @author MLMARKO
28  *
29  */
30 public class CopyDataToClipboardSerializer implements ISerializer {
31         
32         public static final int COPY_THRESHOLD = 500000; // threshold for preventing OOM.
33
34     private final ILayerCell[][] copiedCells;
35     private final CopyDataToClipboardCommand command;
36
37     public CopyDataToClipboardSerializer(ILayerCell[][] copiedCells,
38             CopyDataToClipboardCommand command) {
39         this.copiedCells = copiedCells;
40         this.command = command;
41     }
42
43     @Override
44     public void serialize() {
45         final String cellDelimeter = this.command.getCellDelimeter();
46         final String rowDelimeter = this.command.getRowDelimeter();
47
48         final TextTransfer textTransfer = TextTransfer.getInstance();
49         final StringBuilder textData = new StringBuilder();
50         int count = 0;
51         for (ILayerCell[] cells : copiedCells) {
52                 count+= cells.length;
53         }
54         if (count <= COPY_THRESHOLD) {
55                 int currentRow = 0;
56                 for (ILayerCell[] cells : this.copiedCells) {
57                     int currentCell = 0;
58                     for (ILayerCell cell : cells) {
59                         final String delimeter = ++currentCell < cells.length ? cellDelimeter
60                                 : ""; //$NON-NLS-1$
61                         if (cell != null) {
62                             textData.append(getTextForCell(cell) + delimeter);
63                         } else {
64                             textData.append(delimeter);
65                         }
66                     }
67                     if (++currentRow < this.copiedCells.length) {
68                         textData.append(rowDelimeter);
69                     }
70                 }
71         } else {
72                 textData.append("Too many cells copied (" + count + ")");
73         }
74         if (textData.length() > 0) {
75             final Clipboard clipboard = new Clipboard(Display.getDefault());
76             try {
77                 clipboard.setContents(new Object[] { textData.toString() },
78                         new Transfer[] { textTransfer });
79             } finally {
80                 clipboard.dispose();
81             }
82         }
83     }
84
85     protected String getTextForCell(ILayerCell cell) {
86         return String.valueOf(cell.getDataValue());
87     }
88
89     final protected CopyDataToClipboardCommand getCommand() {
90         return this.command;
91     }
92 }