Skip to content

Package: DynamicCreateChildrenElementsMenuContribution

DynamicCreateChildrenElementsMenuContribution

nameinstructionbranchcomplexitylinemethod
DynamicCreateChildrenElementsMenuContribution()
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%
aboutToShow(List)
M: 127 C: 0
0%
M: 22 C: 0
0%
M: 12 C: 0
0%
M: 34 C: 0
0%
M: 1 C: 0
0%
getSelectedObjects()
M: 43 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 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: * Eugen Neufeld - initial API and implementation
13: *
14: *******************************************************************************/
15: package org.eclipse.emf.ecp.ui.e4.view;
16:
17: import java.util.ArrayList;
18: import java.util.Arrays;
19: import java.util.Collection;
20: import java.util.List;
21:
22: import javax.inject.Inject;
23:
24: import org.eclipse.e4.core.di.annotations.Execute;
25: import org.eclipse.e4.ui.di.AboutToShow;
26: import org.eclipse.e4.ui.model.application.ui.menu.MDirectMenuItem;
27: import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
28: import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
29: import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
30: import org.eclipse.emf.ecore.EObject;
31: import org.eclipse.emf.ecp.common.spi.ChildrenDescriptorCollector;
32: import org.eclipse.emf.ecp.core.ECPProject;
33: import org.eclipse.emf.ecp.core.util.ECPUtil;
34: import org.eclipse.emf.ecp.spi.ui.util.ECPHandlerHelper;
35: import org.eclipse.emf.edit.command.CommandActionDelegate;
36: import org.eclipse.emf.edit.command.CommandParameter;
37: import org.eclipse.emf.edit.domain.EditingDomain;
38: import org.eclipse.emf.edit.provider.ComposedImage;
39: import org.eclipse.emf.edit.ui.action.CreateChildAction;
40: import org.eclipse.jface.viewers.ISelection;
41: import org.eclipse.jface.viewers.IStructuredSelection;
42: import org.eclipse.jface.viewers.StructuredSelection;
43:
44: /**
45: * @author krusche
46: *
47: */
48: public class DynamicCreateChildrenElementsMenuContribution {
49:         @Inject
50:         private ESelectionService selectionService;
51:
52:         private List<?> getSelectedObjects() {
53:                 final Object sel = selectionService.getSelection();
54:•                if (sel != null) {
55:•                        if (sel instanceof Collection) {
56:                                 final Collection<?> col = (Collection<?>) sel;
57:                                 return new ArrayList<Object>(col);
58:•                        } else if (sel instanceof Object[]) {
59:                                 return Arrays.asList((Object[]) sel);
60:•                        } else if (sel instanceof IStructuredSelection) {
61:                                 final IStructuredSelection ssel = (IStructuredSelection) sel;
62:                                 return ssel.toList();
63:                         } else {
64:                                 return Arrays.asList(sel);
65:                         }
66:                 }
67:
68:                 return null;
69:         }
70:
71:         /**
72:          * Adds the child create items to the popup menue.
73:          *
74:          * @param items the list to add the items to
75:          */
76:         @AboutToShow
77:         public void aboutToShow(List<MMenuElement> items) {
78:                 final List<?> selectedObjects = getSelectedObjects();
79:•                if (selectedObjects == null || selectedObjects.isEmpty()) {
80:                         return;
81:                 }
82:                 final Object[] elements = selectedObjects.toArray();
83:
84:•                if (elements.length == 1 && elements[0] instanceof EObject) {
85:                         final EObject eObject = (EObject) elements[0];
86:                         final ECPProject project = ECPUtil.getECPProjectManager()
87:                                 .getProject(eObject);
88:                         final EditingDomain domain = project.getEditingDomain();
89:                         final Collection<?> childDescriptors = new ChildrenDescriptorCollector()
90:                                 .getDescriptors(eObject);
91:
92:•                        for (final Object childDescriptor : childDescriptors) {
93:                                 final CommandParameter cp = (CommandParameter) childDescriptor;
94:•                                if (cp.getEReference() == null) {
95:                                         continue;
96:                                 }
97:•                                if (!cp.getEReference().isMany()
98:•                                        && eObject.eIsSet(cp.getEStructuralFeature())) {
99:                                         continue;
100:•                                } else if (cp.getEReference().isMany()
101:•                                        && cp.getEReference().getUpperBound() != -1
102:                                         && cp.getEReference().getUpperBound() <= ((List<?>) eObject
103:•                                                .eGet(cp.getEReference())).size()) {
104:                                         continue;
105:                                 }
106:
107:                                 final CustomCreateChildAction createChildAction = new CustomCreateChildAction(
108:                                         domain, new StructuredSelection(eObject),
109:                                         childDescriptor, cp, project);
110:
111:                                 final MDirectMenuItem dynamicItem = MMenuFactory.INSTANCE
112:                                         .createDirectMenuItem();
113:
114:                                 dynamicItem.setLabel(createChildAction.getText());
115:                                 dynamicItem.setTooltip(createChildAction.getToolTipText());
116:                                 dynamicItem.setObject(new Object() {
117:                                         @Execute
118:                                         public void execute(MDirectMenuItem me) {
119:                                                 createChildAction.run();
120:                                         }
121:                                 });
122:
123:                                 dynamicItem.setIconURI(createChildAction.getImageURIString());
124:                                 items.add(dynamicItem);
125:                         }
126:                 }
127:         }
128:
129:         /**
130:          * Action to create a new {@link EObject} as child.
131:          *
132:          * @author Jonas
133:          *
134:          */
135:         private final class CustomCreateChildAction extends CreateChildAction {
136:
137:                 private CustomCreateChildAction(EditingDomain editingDomain,
138:                         ISelection selection, Object descriptor, CommandParameter cp,
139:                         ECPProject project) {
140:                         super(editingDomain, selection, descriptor);
141:                         this.cp = cp;
142:                         this.project = project;
143:                 }
144:
145:                 private final CommandParameter cp;
146:
147:                 private final ECPProject project;
148:
149:                 @Override
150:                 public void run() {
151:                         super.run();
152:                         ECPHandlerHelper.openModelElement(cp.getEValue(), project);
153:                 }
154:
155:                 private String getImageURIString() {
156:                         final CommandActionDelegate commandActionDelegate = (CommandActionDelegate) command;
157:                         final Object image = commandActionDelegate.getImage();
158:                         if (ComposedImage.class.isInstance(image)) {
159:                                 final ComposedImage composedImage = (ComposedImage) image;
160:                                 final Object subImage = composedImage.getImages().get(0);
161:                                 return subImage.toString();
162:                         }
163:                         return image.toString();
164:                 }
165:
166:         }
167: }