Skip to content

Package: PreviewWorkspaceViewProvider

PreviewWorkspaceViewProvider

nameinstructionbranchcomplexitylinemethod
PreviewWorkspaceViewProvider()
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
addViewModel(IPath)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
addViewResourceChangeListener()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
canProvideViewModel(EObject, VViewModelProperties)
M: 0 C: 23
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 4
100%
M: 0 C: 1
100%
dispose()
M: 0 C: 8
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 3
100%
M: 0 C: 1
100%
loadView(IPath)
M: 20 C: 27
57%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 4 C: 7
64%
M: 0 C: 1
100%
provideViewModel(EObject, VViewModelProperties)
M: 2 C: 21
91%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 1 C: 3
75%
M: 0 C: 1
100%
removeViewModel(IPath)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
unloadView(IPath)
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%

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: * Eugen Neufeld - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.model.preview.common;
15:
16: import java.io.IOException;
17: import java.util.LinkedHashMap;
18: import java.util.Map;
19:
20: import org.eclipse.core.resources.IResourceChangeEvent;
21: import org.eclipse.core.resources.IResourceChangeListener;
22: import org.eclipse.core.resources.IResourceDelta;
23: import org.eclipse.core.resources.ResourcesPlugin;
24: import org.eclipse.core.runtime.IPath;
25: import org.eclipse.emf.common.util.URI;
26: import org.eclipse.emf.ecore.EObject;
27: import org.eclipse.emf.ecore.resource.Resource;
28: import org.eclipse.emf.ecore.resource.ResourceSet;
29: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
30: import org.eclipse.emf.ecp.view.spi.model.VView;
31: import org.eclipse.emf.ecp.view.spi.model.VViewModelProperties;
32: import org.eclipse.emf.ecp.view.spi.provider.IViewProvider;
33: import org.osgi.service.component.annotations.Component;
34: import org.osgi.service.component.annotations.Deactivate;
35:
36: /**
37: * The IViewProvider provides views from the workspace.
38: *
39: * @author Eugen Neufeld
40: *
41: */
42: @Component(service = { IViewProvider.class, PreviewWorkspaceViewProvider.class })
43: public class PreviewWorkspaceViewProvider implements IViewProvider {
44:
45:         private final Map<IPath, VView> trackedPaths = new LinkedHashMap<IPath, VView>();
46:         private IResourceChangeListener viewResourceChangeListener;
47:
48:         /** Constructor. */
49:         public PreviewWorkspaceViewProvider() {
50:                 addViewResourceChangeListener();
51:         }
52:
53:         private void addViewResourceChangeListener() {
54:                 viewResourceChangeListener = new IResourceChangeListener() {
55:                         @Override
56:                         public void resourceChanged(IResourceChangeEvent event) {
57:                                 if (trackedPaths.isEmpty()) {
58:                                         return;
59:                                 }
60:                                 IResourceDelta delta = event.getDelta();
61:                                 if (delta != null) {
62:                                         while (delta.getAffectedChildren().length != 0) {
63:                                                 delta = delta.getAffectedChildren()[0];
64:                                         }
65:                                         for (final IPath path : trackedPaths.keySet()) {
66:                                                 if (delta.getResource().getFullPath().equals(path)) {
67:                                                         // reload view
68:                                                         final VView view = loadView(path);
69:                                                         trackedPaths.put(path, view);
70:                                                 }
71:                                         }
72:                                 }
73:                         }
74:                 };
75:                 ResourcesPlugin.getWorkspace().addResourceChangeListener(viewResourceChangeListener);
76:         }
77:
78:         /**
79:          * Add a new view model path to the list of available views in the preview.
80:          *
81:          * @param path The {@link IPath} to load
82:          */
83:         public void addViewModel(final IPath path) {
84:                 final VView view = loadView(path);
85:                 trackedPaths.put(path, view);
86:         }
87:
88:         private VView loadView(IPath path) {
89:                 final ResourceSet resourceSet = new ResourceSetImpl();
90:                 final Resource resource = resourceSet
91:                         .createResource(URI.createPlatformResourceURI(path.toOSString(), true));
92:                 try {
93:                         resource.load(null);
94:                         final EObject eObject = resource.getContents().get(0);
95:•                        if (!VView.class.isInstance(eObject)) {
96:                                 throw new IllegalArgumentException(
97:                                         "The provided path " + path.toString() + " doesn't contain a VView."); //$NON-NLS-1$//$NON-NLS-2$
98:                         }
99:                         return (VView) eObject;
100:                 } catch (final IOException ex) {
101:                         throw new IllegalArgumentException(ex);
102:                 }
103:         }
104:
105:         /**
106:          * Remove a view model path from the list of available views in the preview.
107:          *
108:          * @param path The {@link IPath} to remove
109:          */
110:         public void removeViewModel(IPath path) {
111:                 unloadView(path);
112:                 trackedPaths.remove(path);
113:         }
114:
115:         private void unloadView(IPath path) {
116:                 final VView view = trackedPaths.get(path);
117:                 view.eResource().unload();
118:         }
119:
120:         @Override
121:         public double canProvideViewModel(EObject eObject, VViewModelProperties properties) {
122:•                for (final VView view : trackedPaths.values()) {
123:•                        if (view.getRootEClass().equals(eObject.eClass())) {
124:                                 return 10;
125:                         }
126:                 }
127:                 return NOT_APPLICABLE;
128:         }
129:
130:         @Override
131:         public VView provideViewModel(EObject eObject, VViewModelProperties properties) {
132:•                for (final VView view : trackedPaths.values()) {
133:•                        if (view.getRootEClass().equals(eObject.eClass())) {
134:                                 return view;
135:                         }
136:                 }
137:                 return null;
138:         }
139:
140:         /** Clean the used resources. */
141:         @Deactivate
142:         public void dispose() {
143:•                if (viewResourceChangeListener != null) {
144:                         ResourcesPlugin.getWorkspace().removeResourceChangeListener(viewResourceChangeListener);
145:                 }
146:         }
147:
148: }