Skip to content

Package: TreeMasterDetailMenuListener

TreeMasterDetailMenuListener

nameinstructionbranchcomplexitylinemethod
TreeMasterDetailMenuListener(ChildrenDescriptorCollector, MenuManager, TreeViewer, EditingDomain, Collection, CreateElementCallback, DeleteActionBuilder)
M: 0 C: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
addDeleteActionToContextMenu(EditingDomain, IMenuManager, IStructuredSelection)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
createCreateChildAction(EditingDomain, EObject, CommandParameter)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
fillContextMenu(IMenuManager, Collection, EditingDomain, EObject)
M: 14 C: 51
78%
M: 6 C: 12
67%
M: 5 C: 5
50%
M: 5 C: 10
67%
M: 0 C: 1
100%
filterDescriptor(CommandParameter)
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%
initializeRightClickActions()
M: 1 C: 20
95%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 4
80%
M: 0 C: 1
100%
menuAboutToShow(IMenuManager)
M: 2 C: 109
98%
M: 6 C: 10
63%
M: 6 C: 3
33%
M: 2 C: 23
92%
M: 0 C: 1
100%

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: * Clemens Elflein - initial API and implementation
13: * Johannes Faltermeier - moved to dedicated class
14: ******************************************************************************/
15: package org.eclipse.emfforms.spi.swt.treemasterdetail;
16:
17: import java.util.ArrayList;
18: import java.util.Collection;
19: import java.util.List;
20:
21: import org.eclipse.emf.ecore.EObject;
22: import org.eclipse.emf.ecp.common.spi.ChildrenDescriptorCollector;
23: import org.eclipse.emf.edit.command.CommandParameter;
24: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
25: import org.eclipse.emf.edit.domain.EditingDomain;
26: import org.eclipse.emfforms.spi.swt.treemasterdetail.actions.MasterDetailAction;
27: import org.eclipse.emfforms.spi.swt.treemasterdetail.util.CreateChildAction;
28: import org.eclipse.emfforms.spi.swt.treemasterdetail.util.CreateElementCallback;
29: import org.eclipse.jface.action.Action;
30: import org.eclipse.jface.action.IAction;
31: import org.eclipse.jface.action.IMenuListener;
32: import org.eclipse.jface.action.IMenuManager;
33: import org.eclipse.jface.action.MenuManager;
34: import org.eclipse.jface.action.Separator;
35: import org.eclipse.jface.resource.ImageDescriptor;
36: import org.eclipse.jface.viewers.IStructuredSelection;
37: import org.eclipse.jface.viewers.TreeViewer;
38: import org.osgi.framework.FrameworkUtil;
39:
40: /**
41: * {@link IMenuListener Menu listener} added on the tree master detail.
42: *
43: * @author Clemens Elflein
44: *
45: */
46: public class TreeMasterDetailMenuListener implements IMenuListener {
47:
48:         private final Collection<MasterDetailAction> rightClickActions;
49:         private final ChildrenDescriptorCollector childrenDescriptorCollector;
50:         private final MenuManager menuMgr;
51:         private final TreeViewer treeViewer;
52:         private final EditingDomain editingDomain;
53:         private final CreateElementCallback createElementCallback;
54:         private final DeleteActionBuilder deleteActionBuilder;
55:
56:         /**
57:          * Default constructor.
58:          *
59:          * @param childrenDescriptorCollector the child description collector
60:          * @param menuMgr the menu manager
61:          * @param treeViewer the treeviewer
62:          * @param editingDomain the editing domain
63:          * @param rightClickActions the right click actions to show
64:          * @param createElementCallback the create element callback
65:          * @param deleteActionBuilder the delete action builder
66:          * @since 1.8
67:          */
68:         public TreeMasterDetailMenuListener(
69:                 ChildrenDescriptorCollector childrenDescriptorCollector,
70:                 MenuManager menuMgr,
71:                 TreeViewer treeViewer,
72:                 EditingDomain editingDomain,
73:                 Collection<MasterDetailAction> rightClickActions,
74:                 CreateElementCallback createElementCallback,
75:                 DeleteActionBuilder deleteActionBuilder) {
76:
77:                 this.childrenDescriptorCollector = childrenDescriptorCollector;
78:                 this.menuMgr = menuMgr;
79:                 this.treeViewer = treeViewer;
80:                 this.editingDomain = editingDomain;
81:                 this.rightClickActions = rightClickActions;
82:                 this.createElementCallback = createElementCallback;
83:                 this.deleteActionBuilder = deleteActionBuilder;
84:                 initializeRightClickActions();
85:         }
86:
87:         /**
88:          * The rightClickActions need to be initialized in order for the key bindings to work, even if the context menu was
89:          * not yet shown.
90:          */
91:         private void initializeRightClickActions() {
92:•                if (rightClickActions == null) {
93:                         return;
94:                 }
95:•                for (final MasterDetailAction action : rightClickActions) {
96:                         action.setTreeViewer(treeViewer);
97:                 }
98:         }
99:
100:         @Override
101:         public void menuAboutToShow(IMenuManager manager) {
102:•                if (treeViewer.getSelection().isEmpty()) {
103:                         return;
104:                 }
105:•                if (treeViewer.getSelection() instanceof IStructuredSelection) {
106:                         final IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
107:
108:•                        if (selection.size() == 1 && selection.getFirstElement() instanceof EObject) {
109:                                 final EObject eObject = (EObject) selection.getFirstElement();
110:                                 final EditingDomain domain = AdapterFactoryEditingDomain.getEditingDomainFor(eObject);
111:•                                if (domain == null) {
112:                                         return;
113:                                 }
114:                                 final Collection<?> descriptors = childrenDescriptorCollector.getDescriptors(eObject);
115:                                 fillContextMenu(manager, descriptors, editingDomain, eObject);
116:                         }
117:                         manager.add(new Separator());
118:                         addDeleteActionToContextMenu(editingDomain, menuMgr, selection);
119:
120:•                        if (rightClickActions != null) {
121:                                 // JFACE API
122:                                 @SuppressWarnings("unchecked")
123:                                 final List<Object> list = new ArrayList<Object>(selection.toList());
124:
125:•                                for (final MasterDetailAction menuAction : rightClickActions) {
126:•                                        if (menuAction.shouldShow(list)) {
127:                                                 menuAction.setTreeViewer(treeViewer);
128:                                                 final Action newAction = new Action() {
129:                                                         @Override
130:                                                         public void run() {
131:                                                                 super.run();
132:                                                                 menuAction.execute(list);
133:                                                         }
134:                                                 };
135:
136:                                                 newAction.setImageDescriptor(ImageDescriptor.createFromURL(FrameworkUtil.getBundle(
137:                                                         menuAction.getClass())
138:                                                         .getResource(menuAction.getImagePath())));
139:                                                 newAction.setText(menuAction.getLabel());
140:
141:                                                 manager.add(newAction);
142:                                         }
143:                                 }
144:
145:                         }
146:                 }
147:         }
148:
149:         /**
150:          * Fill context menu.
151:          *
152:          * @param manager The menu manager responsible for the context menu
153:          * @param descriptors The menu items to be added
154:          * @param domain The editing domain of the current EObject
155:          * @param eObject The model element
156:          */
157:         private void fillContextMenu(IMenuManager manager, Collection<?> descriptors, final EditingDomain domain,
158:                 final EObject eObject) {
159:•                for (final Object descriptor : descriptors) {
160:
161:•                        if (!CommandParameter.class.isInstance(descriptor)) {
162:                                 continue;
163:                         }
164:                         final CommandParameter cp = (CommandParameter) descriptor;
165:•                        if (cp.getEReference() == null) {
166:                                 continue;
167:                         }
168:•                        if (filterDescriptor(cp)) {
169:                                 continue;
170:                         }
171:•                        if (!cp.getEReference().isMany() && eObject.eIsSet(cp.getEStructuralFeature())) {
172:                                 continue;
173:•                        } else if (cp.getEReference().isMany() && cp.getEReference().getUpperBound() != -1
174:•                                && cp.getEReference().getUpperBound() <= ((List<?>) eObject.eGet(cp.getEReference())).size()) {
175:                                 continue;
176:                         }
177:
178:                         manager.add(createCreateChildAction(domain, eObject, cp));
179:                 }
180:
181:         }
182:
183:         /**
184:          * Creates a new child creation action based on the given {@link CommandParameter}. Subclasses might override this
185:          * to customize the action creation (e.g. an action's label).
186:          *
187:          * @param domain The {@link EditingDomain} the element is be created with
188:          * @param eObject The parent object the element is added to
189:          * @param cp The {@link CommandParameter} creating the element when executed
190:          * @return The action creating a new child when ran
191:          * @since 1.20
192:          */
193:         protected IAction createCreateChildAction(final EditingDomain domain, final EObject eObject,
194:                 final CommandParameter cp) {
195:                 return new CreateChildAction(eObject, domain, treeViewer, cp, createElementCallback);
196:         }
197:
198:         /**
199:          * Allows to prevent adding a create child action for the given {@link CommandParameter}.
200:          *
201:          * @param cp the descriptor
202:          * @return <code>true</code> if action should be filtered (=not created), <code>false</code> otherwise
203:          * @since 1.8
204:          */
205:         protected boolean filterDescriptor(CommandParameter cp) {
206:                 return false;
207:         }
208:
209:         /**
210:          * Adds the delete action to context menu.
211:          *
212:          * @param editingDomain the editing domain
213:          * @param manager the manager
214:          * @param selection the selection
215:          */
216:         private void addDeleteActionToContextMenu(final EditingDomain editingDomain, final IMenuManager manager,
217:                 final IStructuredSelection selection) {
218:                 final Action deleteAction = deleteActionBuilder.createDeleteAction(selection, editingDomain);
219:                 manager.add(deleteAction);
220:         }
221: }