Skip to content

Package: CutCopyPasteListener$1

CutCopyPasteListener$1

nameinstructionbranchcomplexitylinemethod
createCommand(Collection)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 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: * Johannes Faltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.spi.table.ui.rcp;
15:
16: import java.util.Collection;
17:
18: import org.eclipse.emf.common.command.Command;
19: import org.eclipse.emf.common.command.UnexecutableCommand;
20: import org.eclipse.emf.ecore.EObject;
21: import org.eclipse.emf.ecore.EStructuralFeature;
22: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
23: import org.eclipse.emf.edit.command.CutToClipboardCommand;
24: import org.eclipse.emf.edit.command.PasteFromClipboardCommand;
25: import org.eclipse.emf.edit.domain.EditingDomain;
26: import org.eclipse.emf.edit.ui.action.CopyAction;
27: import org.eclipse.emf.edit.ui.action.CutAction;
28: import org.eclipse.emf.edit.ui.action.PasteAction;
29: import org.eclipse.jface.viewers.ISelectionChangedListener;
30: import org.eclipse.jface.viewers.IStructuredSelection;
31: import org.eclipse.jface.viewers.SelectionChangedEvent;
32: import org.eclipse.jface.viewers.StructuredSelection;
33: import org.eclipse.jface.viewers.TableViewer;
34: import org.eclipse.swt.SWT;
35: import org.eclipse.swt.events.KeyEvent;
36: import org.eclipse.swt.events.KeyListener;
37: import org.eclipse.ui.actions.BaseSelectionListenerAction;
38:
39: /**
40: * On creation this listener registers itself on a given {@link TableViewer} and register the according EMF
41: * Cut/Copy/Paste-Actions. They are triggered using keybindings.
42: *
43: * @author Johannes Faltermeier
44: *
45: */
46: public class CutCopyPasteListener implements KeyListener, ISelectionChangedListener {
47:
48:         private final CutAction cutAction;
49:         private final CopyAction copyAction;
50:         private final PasteAction pasteAction;
51:
52:         private EObject parent;
53:         private EStructuralFeature feature;
54:
55:         /**
56:          * Constructs this listener.
57:          *
58:          * @param tableViewer the {@link TableViewer}
59:          * @param editingDomain the {@link EditingDomain} (contains the used clipboard)
60:          * @param setting the parent EObject on which the paste will be performed
61:          */
62:         public CutCopyPasteListener(TableViewer tableViewer, EditingDomain editingDomain, Setting setting) {
63:                 parent = setting.getEObject();
64:                 feature = setting.getEStructuralFeature();
65:                 cutAction = new CutAction(editingDomain) {
66:                         @Override
67:                         public Command createCommand(Collection<?> selection) {
68:                                 return CutToClipboardCommand.create(domain, parent, feature, selection);
69:                         }
70:                 };
71:                 copyAction = new CopyAction(editingDomain);
72:                 pasteAction = new PasteAction(editingDomain) {
73:                         @Override
74:                         public Command createCommand(Collection<?> selection) {
75:                                 if (selection.size() == 1) {
76:                                         return PasteFromClipboardCommand.create(domain, parent, feature);
77:                                 }
78:                                 return UnexecutableCommand.INSTANCE;
79:                         }
80:                 };
81:                 tableViewer.getTable().addKeyListener(this);
82:                 tableViewer.addSelectionChangedListener(this);
83:         }
84:
85:         @Override
86:         public void selectionChanged(SelectionChangedEvent event) {
87:                 final IStructuredSelection currentSelection = event.getSelection() instanceof IStructuredSelection
88:                         ? (IStructuredSelection) event.getSelection()
89:                         : new StructuredSelection();
90:                 cutAction.selectionChanged(currentSelection);
91:                 copyAction.selectionChanged(currentSelection);
92:         }
93:
94:         @Override
95:         public void keyPressed(KeyEvent e) {
96:                 /* no op */
97:         }
98:
99:         @Override
100:         public void keyReleased(KeyEvent e) {
101:                 if (isActivated(e, SWT.CTRL, 'x')) {
102:                         execute(cutAction);
103:                 } else if (isActivated(e, SWT.CTRL, 'c')) {
104:                         execute(copyAction);
105:                 } else if (isActivated(e, SWT.CTRL, 'v')) {
106:                         /*
107:                          * set selection to parent to recheck enabled state. This works only, because keybindings can be used any
108:                          * time. If we would also want a menu, we need to handle this differently.
109:                          */
110:                         pasteAction.selectionChanged(new StructuredSelection(parent));
111:                         execute(pasteAction);
112:                 }
113:         }
114:
115:         private static void execute(BaseSelectionListenerAction delegatedAction) {
116:                 if (!delegatedAction.isEnabled()) {
117:                         return;
118:                 }
119:                 delegatedAction.run();
120:         }
121:
122:         private static boolean isActivated(KeyEvent event, int swtMask, char c) {
123:                 return (event.stateMask & swtMask) == swtMask && event.keyCode == c;
124:         }
125:
126:         /**
127:          * Called to notify this listener that the domain model has changed.
128:          *
129:          * @param setting the new table setting
130:          */
131:         public void rootDomainModelChanged(Setting setting) {
132:                 parent = setting.getEObject();
133:                 feature = setting.getEStructuralFeature();
134:         }
135:
136: }