Skip to content

Package: ViewEditorActionBarContributor

ViewEditorActionBarContributor

nameinstructionbranchcomplexitylinemethod
ViewEditorActionBarContributor()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
createDeleteAction()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getService(Class)
M: 2 C: 9
82%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 2
100%
M: 0 C: 1
100%
getViewModelContext()
M: 2 C: 9
82%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2019 Christian W. Damus 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: * Christian W. Damus - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.ide.editor.view;
15:
16: import java.util.Collection;
17:
18: import org.eclipse.emf.ecp.edit.spi.DeleteService;
19: import org.eclipse.emf.ecp.edit.spi.ConditionalDeleteService;
20: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
21: import org.eclipse.emf.edit.ui.action.DeleteAction;
22: import org.eclipse.emf.edit.ui.action.EditingDomainActionBarContributor;
23: import org.eclipse.jface.viewers.IStructuredSelection;
24:
25: /**
26: * Action-bar contributor for the View Model Editor.
27: *
28: * @since 1.20
29: */
30: public class ViewEditorActionBarContributor extends EditingDomainActionBarContributor {
31:
32:         /**
33:          * Initializes me.
34:          */
35:         public ViewEditorActionBarContributor() {
36:                 super();
37:         }
38:
39:         @Override
40:         protected DeleteAction createDeleteAction() {
41:                 return new DeleteServiceAction();
42:         }
43:
44:         /**
45:          * Obtain a service of the given type from the {@linkplain #getViewModelContext() view model context}, if any.
46:          *
47:          * @param serviceType the type of service to obtain
48:          * @return the service, or {@code null} if none can be obtained
49:          *
50:          * @param <T> the type of service to obtain
51:          */
52:         protected <T> T getService(Class<T> serviceType) {
53:                 final ViewModelContext context = getViewModelContext();
54:•                return context == null ? null : context.getService(serviceType);
55:         }
56:
57:         /**
58:          * Obtain the editor's view model context.
59:          *
60:          * @return the view model context, or {@code null} if the editor has none
61:          */
62:         protected ViewModelContext getViewModelContext() {
63:•                return activeEditor == null ? null : activeEditor.getAdapter(ViewModelContext.class);
64:         }
65:
66:         //
67:         // Nested types
68:         //
69:
70:         /**
71:          * Custom delete action that delegates to the {@link DeleteService}, if it's available.
72:          */
73:         private class DeleteServiceAction extends DeleteAction {
74:
75:                 DeleteServiceAction() {
76:                         super();
77:                 }
78:
79:                 @Override
80:                 public void run() {
81:                         final DeleteService deleteService = getService(DeleteService.class);
82:                         if (deleteService == null) {
83:                                 super.run();
84:                         } else {
85:                                 @SuppressWarnings("unchecked")
86:                                 final Collection<Object> selection = getStructuredSelection().toList();
87:                                 // The delete service executes commands on the stack
88:                                 deleteService.deleteElements(selection);
89:                         }
90:                 }
91:
92:                 @Override
93:                 public boolean updateSelection(IStructuredSelection selection) {
94:                         final ConditionalDeleteService deleteService = ConditionalDeleteService.getDeleteService(getViewModelContext());
95:
96:                         return !selection.isEmpty() && deleteService.canDelete(selection.toList());
97:                 }
98:
99:         }
100: }