Skip to content

Package: ECPModelView$1

ECPModelView$1

nameinstructionbranchcomplexitylinemethod
doubleClick(DoubleClickEvent)
M: 38 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 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-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 javax.annotation.PostConstruct;
18: import javax.inject.Inject;
19: import javax.inject.Named;
20:
21: import org.eclipse.e4.core.di.annotations.Optional;
22: import org.eclipse.e4.ui.di.Focus;
23: import org.eclipse.e4.ui.model.application.ui.basic.MPart;
24: import org.eclipse.e4.ui.model.application.ui.menu.MToolBarElement;
25: import org.eclipse.e4.ui.model.application.ui.menu.MToolItem;
26: import org.eclipse.e4.ui.services.EMenuService;
27: import org.eclipse.e4.ui.services.IServiceConstants;
28: import org.eclipse.e4.ui.workbench.modeling.EPartService;
29: import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
30: import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
31: import org.eclipse.emf.ecore.EObject;
32: import org.eclipse.emf.ecp.core.ECPProject;
33: import org.eclipse.emf.ecp.core.util.ECPContainer;
34: import org.eclipse.emf.ecp.core.util.ECPUtil;
35: import org.eclipse.emf.ecp.internal.ui.model.ModelContentProvider;
36: import org.eclipse.emf.ecp.spi.ui.util.ECPHandlerHelper;
37: import org.eclipse.emf.ecp.ui.common.ECPViewerFactory;
38: import org.eclipse.emf.ecp.ui.e4.editor.ECPE4Editor;
39: import org.eclipse.jface.viewers.DoubleClickEvent;
40: import org.eclipse.jface.viewers.IDoubleClickListener;
41: import org.eclipse.jface.viewers.ISelection;
42: import org.eclipse.jface.viewers.ISelectionChangedListener;
43: import org.eclipse.jface.viewers.IStructuredSelection;
44: import org.eclipse.jface.viewers.SelectionChangedEvent;
45: import org.eclipse.jface.viewers.StructuredSelection;
46: import org.eclipse.jface.viewers.TreeViewer;
47: import org.eclipse.swt.widgets.Composite;
48:
49: /**
50: * Because {@link EMenuService} is still internal.
51: */
52: /**
53: * Model Explorer View Part.
54: *
55: * @author Jonas
56: *
57: */
58: public class ECPModelView {
59:
60:         /**
61:          * Constant for the setting whether the model explorer is linked with the editor.
62:          */
63:         public static final String P_LINK_WITH_EDITOR = "linkWithEditor"; //$NON-NLS-1$
64:         private static final String POPUPMENU_NAVIGATOR = "org.eclipse.emf.ecp.e4.application.popupmenu.navigator"; //$NON-NLS-1$
65:         private TreeViewer modelExplorerTree;
66:         private ModelContentProvider contentProvider;
67:
68:         @Inject
69:         private EPartService partService;
70:
71:         private MPart modelViewPart;
72:
73:         /**
74:          * Creates the model explorer view.
75:          *
76:          * @param composite the parent {@link Composite}
77:          * @param menuService the menu service to register the context menu
78:          * @param selectionService the selection service to publish the selection of the tree viewer.
79:          * @param part the current {@link MPart}
80:          */
81:         @PostConstruct
82:         public void create(Composite composite, EMenuService menuService, final ESelectionService selectionService,
83:                 MPart part) {
84:                 modelViewPart = part;
85:                 if (modelViewPart.getPersistedState().containsKey(P_LINK_WITH_EDITOR)) {
86:                         for (final MToolBarElement toolbarElement : part.getToolbar().getChildren()) {
87:                                 // TODO better way?
88:                                 if (toolbarElement.getElementId().endsWith("link")) {
89:                                         ((MToolItem) toolbarElement).setSelected(Boolean.valueOf(modelViewPart.getPersistedState().get(
90:                                                 P_LINK_WITH_EDITOR)));
91:                                 }
92:                         }
93:                 }
94:                 modelExplorerTree = ECPViewerFactory.createModelExplorerViewer(composite, true, null);
95:                 menuService.registerContextMenu(modelExplorerTree.getTree(),
96:                         POPUPMENU_NAVIGATOR);
97:                 contentProvider = (ModelContentProvider) modelExplorerTree.getContentProvider();
98:                 modelExplorerTree.addDoubleClickListener(new IDoubleClickListener() {
99:
100:                         @Override
101:                         public void doubleClick(DoubleClickEvent event) {
102:•                                if (event.getSelection() instanceof IStructuredSelection) {
103:                                         final IStructuredSelection structuredSelection = (IStructuredSelection) event.getSelection();
104:                                         final Object firstElement = structuredSelection.getFirstElement();
105:
106:•                                        if (firstElement instanceof ECPProject) {
107:                                                 final ECPProject project = (ECPProject) firstElement;
108:•                                                if (!project.isOpen()) {
109:                                                         project.open();
110:                                                 }
111:•                                        } else if (firstElement instanceof EObject) {
112:                                                 final ECPContainer context = ECPUtil.getModelContext(contentProvider,
113:                                                         structuredSelection.toArray());
114:                                                 ECPHandlerHelper.openModelElement(firstElement, (ECPProject) context);
115:
116:                                         }
117:                                 }
118:                         }
119:                 });
120:                 modelExplorerTree.addSelectionChangedListener(new ISelectionChangedListener() {
121:
122:                         @Override
123:                         public void selectionChanged(SelectionChangedEvent event) {
124:                                 final ISelection selection = event.getSelection();
125:                                 if (IStructuredSelection.class.isInstance(selection)) {
126:                                         final IStructuredSelection structuredSelection = (IStructuredSelection) selection;
127:                                         if (structuredSelection != null && !structuredSelection.isEmpty()) {
128:                                                 if (structuredSelection.size() == 1) {
129:                                                         selectionService
130:                                                                 .setSelection(structuredSelection.getFirstElement());
131:                                                         activateCorrectPart(structuredSelection.getFirstElement());
132:                                                 } else {
133:                                                         selectionService
134:                                                                 .setSelection(structuredSelection.toList());
135:                                                 }
136:                                         }
137:                                 }
138:                         }
139:                 });
140:         }
141:
142:         private void activateCorrectPart(Object firstSelection) {
143:                 if (!Boolean.valueOf(modelViewPart.getPersistedState().get(P_LINK_WITH_EDITOR))) {
144:                         return;
145:                 }
146:                 MPart partToHighlite = null;
147:                 for (final MPart part : partService.getParts()) {
148:                         if (part.getContext().get(ECPE4Editor.INPUT) == firstSelection) {
149:                                 partToHighlite = part;
150:                                 break;
151:                         }
152:                 }
153:                 if (partToHighlite == null) {
154:                         return;
155:                 }
156:                 partService.showPart(partToHighlite, PartState.ACTIVATE);
157:         }
158:
159:         /**
160:          * Sets the currently active part. Used to update the selection in case the model explorer is linked to editors.
161:          *
162:          * @param part the active {@link MPart}
163:          */
164:         @Inject
165:         public void setActivePart(@Named(IServiceConstants.ACTIVE_PART) @Optional MPart part) {
166:                 if (modelExplorerTree == null) {
167:                         return;
168:                 }
169:                 if (part == null) {
170:                         return;
171:                 }
172:                 if (modelViewPart == null || !Boolean.valueOf(modelViewPart.getPersistedState().get(P_LINK_WITH_EDITOR))) {
173:                         return;
174:                 }
175:                 final Object partInput = part.getContext().get(ECPE4Editor.INPUT);
176:                 if (partInput == null) {
177:                         return;
178:                 }
179:                 modelExplorerTree.setSelection(new StructuredSelection(partInput));
180:         }
181:
182:         /**
183:          * Sets the focus to the tree.
184:          */
185:         @Focus
186:         public void setFocus() {
187:                 modelExplorerTree.getTree().setFocus();
188:         }
189:
190: }