Skip to content

Package: GridCopyKeyListener

GridCopyKeyListener

nameinstructionbranchcomplexitylinemethod
GridCopyKeyListener(Display)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
copySelectionToClipboard(Grid)
M: 30 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getSelectionAsText(Grid)
M: 122 C: 0
0%
M: 20 C: 0
0%
M: 11 C: 0
0%
M: 31 C: 0
0%
M: 1 C: 0
0%
getText(Grid, int, int)
M: 28 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
initializeTableSelection(Grid, Point[], int, int, int, int)
M: 40 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
isTriggerActive()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
keyPressed(KeyEvent)
M: 15 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
keyReleased(KeyEvent)
M: 11 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
setTriggerActive(boolean)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2016 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Alexandra Buzila - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.internal.table.nebula.grid;
15:
16: import java.util.Map;
17:
18: import org.eclipse.emf.ecp.edit.spi.swt.table.ECPCellEditor;
19: import org.eclipse.nebula.widgets.grid.Grid;
20: import org.eclipse.nebula.widgets.grid.GridItem;
21: import org.eclipse.swt.SWT;
22: import org.eclipse.swt.dnd.Clipboard;
23: import org.eclipse.swt.dnd.TextTransfer;
24: import org.eclipse.swt.events.KeyEvent;
25: import org.eclipse.swt.events.KeyListener;
26: import org.eclipse.swt.graphics.Point;
27: import org.eclipse.swt.widgets.Display;
28:
29: /**
30: * {@link KeyListener} for the copy action on a {@link Grid} control.
31: *
32: * @author Alexandra Buzila
33: * @since 1.10
34: *
35: */
36: public class GridCopyKeyListener implements KeyListener {
37:         private final Clipboard clipboard;
38:         private boolean triggerActive;
39:
40:         /**
41:          * Constructor.
42:          *
43:          * @param display the {@link Display} on which to allocate this command's {@link Clipboard}.
44:          */
45:         public GridCopyKeyListener(Display display) {
46:                 clipboard = new Clipboard(display);
47:         }
48:
49:         @Override
50:         public void keyPressed(KeyEvent e) {
51:•                setTriggerActive((e.stateMask & SWT.CTRL) != 0 && e.keyCode == 'c');
52:         }
53:
54:         @Override
55:         public void keyReleased(KeyEvent e) {
56:•                if (isTriggerActive()) {
57:                         final Grid grid = (Grid) e.widget;
58:                         copySelectionToClipboard(grid);
59:                 }
60:         }
61:
62:         /**
63:          * Copies the table selection of the {@link Grid} as a formatted string (if a selection exists).
64:          *
65:          * @param grid the {@link Grid} control.
66:          */
67:         public void copySelectionToClipboard(Grid grid) {
68:                 final String selectionText = getSelectionAsText(grid);
69:•                if (selectionText == null || selectionText.isEmpty()) {
70:                         return;
71:                 }
72:                 final String[] data = { selectionText };
73:                 final TextTransfer[] dataTypes = { TextTransfer.getInstance() };
74:
75:                 clipboard.setContents(data, dataTypes);
76:         }
77:
78:         /**
79:          * Returns the table selection of the {@link Grid} as a formatted string.
80:          *
81:          * @param grid the {@link Grid} control
82:          * @return the selection
83:          */
84:         public String getSelectionAsText(Grid grid) {
85:                 final Point[] cellSelection = grid.getCellSelection();
86:                 final StringBuilder selection = new StringBuilder();
87:                 int minRow = Integer.MAX_VALUE;
88:                 int minColumn = Integer.MAX_VALUE;
89:                 int maxRow = Integer.MIN_VALUE;
90:                 int maxColumn = Integer.MIN_VALUE;
91:•                for (final Point point : cellSelection) {
92:                         final int row = point.y;
93:                         final int col = point.x;
94:•                        if (row < minRow) {
95:                                 minRow = row;
96:                         }
97:•                        if (row > maxRow) {
98:                                 maxRow = row;
99:                         }
100:•                        if (col < minColumn) {
101:                                 minColumn = col;
102:                         }
103:•                        if (col > maxColumn) {
104:                                 maxColumn = col;
105:                         }
106:                 }
107:                 final int columnSize = maxColumn - minColumn + 1;
108:                 final int rowSize = maxRow - minRow + 1;
109:                 final String[][] tableSelection = initializeTableSelection(grid, cellSelection, columnSize, rowSize, minColumn,
110:                         minRow);
111:•                for (int i = 0; i < rowSize; i++) {
112:•                        for (int j = 0; j < columnSize; j++) {
113:                                 final String text = tableSelection[j][i];
114:•                                if (j != 0) {
115:                                         selection.append('\t');
116:                                 }
117:•                                if (text != null) {
118:                                         selection.append(text);
119:                                 }
120:                         }
121:•                        if (i != rowSize - 1) {
122:                                 selection.append('\n');
123:                         }
124:                 }
125:                 return selection.toString();
126:         }
127:
128:         private String[][] initializeTableSelection(Grid grid, Point[] cellSelection, int columnSize, int rowSize,
129:                 int minColumn, int minRow) {
130:                 final String[][] tableSelection = new String[columnSize][rowSize];
131:•                for (int i = 0; i < cellSelection.length; i++) {
132:                         final int column = cellSelection[i].x;
133:                         final int row = cellSelection[i].y;
134:                         final String text = getText(grid, column, row);
135:                         tableSelection[column - minColumn][row - minRow] = text;
136:                 }
137:                 return tableSelection;
138:         }
139:
140:         private String getText(Grid grid, int column, int row) {
141:                 final GridItem item = grid.getItem(row);
142:                 @SuppressWarnings("unchecked")
143:                 final Map<Integer, String> copyAlternative = (Map<Integer, String>) item
144:                         .getData(ECPCellEditor.COPY_STRING_ALTERNATIVE);
145:•                if (copyAlternative != null && copyAlternative.containsKey(column)) {
146:                         return copyAlternative.get(column);
147:                 }
148:                 final String text = item.getText(column);
149:                 return text;
150:         }
151:
152:         /**
153:          * @return <code>true</code> if copy was triggered in key pressed
154:          */
155:         protected boolean isTriggerActive() {
156:                 return triggerActive;
157:         }
158:
159:         /**
160:          * May be called from {@link #keyPressed(KeyEvent)} to indicated whether this triggers the action.
161:          *
162:          * @param triggerActive <code>true</code> if key release should perform the action, <code>false</code> otherwise
163:          */
164:         protected void setTriggerActive(boolean triggerActive) {
165:                 this.triggerActive = triggerActive;
166:         }
167: }