Skip to content

Package: KeybindedMasterDetailAction

KeybindedMasterDetailAction

nameinstructionbranchcomplexitylinemethod
KeybindedMasterDetailAction()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
executeOnKeyPressed(ISelection)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getCurrentSelection()
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%
isActivated(KeyEvent, int, char)
M: 6 C: 8
57%
M: 3 C: 1
25%
M: 2 C: 1
33%
M: 0 C: 1
100%
M: 0 C: 1
100%
isExecuteOnKeyPressed(KeyEvent)
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isExecuteOnKeyRelease()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
keyPressed(KeyEvent)
M: 4 C: 10
71%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%
keyReleased(KeyEvent)
M: 4 C: 7
64%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%
selectionChanged(SelectionChangedEvent)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
setExecuteOnKeyRelease(boolean)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
setTreeViewer(TreeViewer)
M: 0 C: 26
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 8
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 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: * Stefan Dirix - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.spi.swt.treemasterdetail.actions;
15:
16: import org.eclipse.jface.viewers.ISelection;
17: import org.eclipse.jface.viewers.ISelectionChangedListener;
18: import org.eclipse.jface.viewers.SelectionChangedEvent;
19: import org.eclipse.jface.viewers.TreeViewer;
20: import org.eclipse.swt.events.KeyEvent;
21: import org.eclipse.swt.events.KeyListener;
22:
23: /**
24: * Extended {@link MasterDetailAction} to help introduce keybindings.
25: *
26: * @author Stefan Dirix
27: * @since 1.8
28: *
29: */
30: public abstract class KeybindedMasterDetailAction extends MasterDetailAction
31:         implements KeyListener, ISelectionChangedListener {
32:
33:         private TreeViewer registeredTreeViewer;
34:         private ISelection currentSelection;
35:         private boolean executeOnKeyRelease;
36:
37:         /**
38:          * {@inheritDoc}
39:          *
40:          * @see org.eclipse.emfforms.spi.swt.treemasterdetail.actions.MasterDetailAction#setTreeViewer(org.eclipse.jface.viewers.TreeViewer)
41:          */
42:         @Override
43:         public void setTreeViewer(TreeViewer treeviewer) {
44:                 super.setTreeViewer(treeviewer);
45:•                if (registeredTreeViewer != null) {
46:                         registeredTreeViewer.getTree().removeKeyListener(this);
47:                         registeredTreeViewer.removeSelectionChangedListener(this);
48:                 }
49:                 treeviewer.getTree().addKeyListener(this);
50:                 treeviewer.addSelectionChangedListener(this);
51:                 registeredTreeViewer = treeviewer;
52:         }
53:
54:         /**
55:          * {@inheritDoc}
56:          *
57:          * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
58:          */
59:         @Override
60:         public void selectionChanged(SelectionChangedEvent event) {
61:                 currentSelection = event.getSelection();
62:         }
63:
64:         /**
65:          * Returns the current {@link ISelection}.
66:          *
67:          * @return
68:          *                 The current {@link ISelection} if there is one, {@code null} otherwise.
69:          */
70:         protected ISelection getCurrentSelection() {
71:                 return currentSelection;
72:         }
73:
74:         /**
75:          * {@inheritDoc}
76:          *
77:          * @see org.eclipse.swt.events.KeyListener#keyPressed(org.eclipse.swt.events.KeyEvent)
78:          */
79:         @Override
80:         public void keyPressed(KeyEvent event) {
81:                 setExecuteOnKeyRelease(isExecuteOnKeyRelease(event));
82:•                if (isExecuteOnKeyPressed(event)) {
83:                         executeOnKeyPressed(currentSelection);
84:                 }
85:         }
86:
87:         /**
88:          * Determines if the {@link #executeOnKeyPressed(ISelection)} method shall be executed.
89:          *
90:          * @param event
91:          * The {@link KeyEvent} which triggers this method.
92:          * @return
93:          *                 {@code true} if {@link #executeOnKeyPressed(ISelection)} method shall be called, {@code false} otherwise.
94:          */
95:         protected boolean isExecuteOnKeyPressed(KeyEvent event) {
96:                 return false;
97:         }
98:
99:         /**
100:          * This method is triggered by {@link #isExecuteOnKeyPressed(KeyEvent)} when a {@link KeyEvent} is triggered by a
101:          * key press.
102:          *
103:          * @param currentSelection
104:          * The current {@link ISelection}.
105:          */
106:         protected void executeOnKeyPressed(ISelection currentSelection) {
107:                 // no op
108:         }
109:
110:         /**
111:          * {@inheritDoc}
112:          *
113:          * @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent)
114:          */
115:         @Override
116:         public void keyReleased(KeyEvent event) {
117:•                if (isExecuteOnKeyRelease()) {
118:                         executeOnKeyRelease(currentSelection);
119:                 }
120:                 setExecuteOnKeyRelease(false);
121:         }
122:
123:         /**
124:          * Determines if the {@link #executeOnKeyRelease(ISelection)} method shall be executed.
125:          *
126:          * @param event
127:          * The {@link KeyEvent} on Keypress. This allows to react on key combinations even on key release
128:          * @return
129:          *                 {@code true} if {@link #executeOnKeyRelease(ISelection)} method shall be called, {@code false} otherwise.
130:          */
131:         protected abstract boolean isExecuteOnKeyRelease(KeyEvent event);
132:
133:         /**
134:          * This method is triggered by {@link #isExecuteOnKeyRelease(KeyEvent)} when a {@link KeyEvent} is triggered by a
135:          * key release.
136:          *
137:          * @param currentSelection
138:          * The current {@link ISelection}.
139:          */
140:         protected abstract void executeOnKeyRelease(ISelection currentSelection);
141:
142:         /**
143:          * Determines if the keys indicated by the SWT {@code swtMask} and {@code c} are active.
144:          *
145:          * @param event
146:          * The {@link KeyEvent} to check.
147:          * @param swtMask
148:          * SWT key event mask, e.g. {@link org.eclipse.swt.SWT.CTRL SWT.CTRL},
149:          * {@link org.eclipse.swt.SWT.ALT SWT.ALT} etc.
150:          * @param c
151:          * The additional pressed char. Use {@link KeyEvent#keyCode} if you only want to check for
152:          * {@code swtMask}.
153:          * @return
154:          *                 {@code true} if the keys indicated by {@code swtMask} and {@code c} are active, {@code false} otherwise.
155:          */
156:         protected static boolean isActivated(KeyEvent event, int swtMask, char c) {
157:•                return (event.stateMask & swtMask) == swtMask && event.keyCode == c;
158:         }
159:
160:         /**
161:          * @return the executeOnKeyRelease
162:          * @since 1.14
163:          */
164:         protected boolean isExecuteOnKeyRelease() {
165:                 return executeOnKeyRelease;
166:         }
167:
168:         /**
169:          * @param executeOnKeyRelease the executeOnKeyRelease to set
170:          * @since 1.14
171:          */
172:         protected void setExecuteOnKeyRelease(boolean executeOnKeyRelease) {
173:                 this.executeOnKeyRelease = executeOnKeyRelease;
174:         }
175: }