Skip to content

Package: GenerateEcoreEditorJavaCodeAction

GenerateEcoreEditorJavaCodeAction

nameinstructionbranchcomplexitylinemethod
GenerateEcoreEditorJavaCodeAction()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
canExecute(Object)
M: 2 C: 13
87%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 2
67%
M: 0 C: 1
100%
getAction(Object, ISelectionProvider)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getEPackage(ResourceSet)
M: 2 C: 29
94%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 1 C: 5
83%
M: 0 C: 1
100%
getGenModel(ResourceSet)
M: 2 C: 38
95%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 8
89%
M: 0 C: 1
100%
getGenModelFile(IContainer, ResourceSet, EPackage)
M: 1 C: 73
99%
M: 6 C: 8
57%
M: 6 C: 2
25%
M: 1 C: 11
92%
M: 0 C: 1
100%
getGenModelFiles(IContainer)
M: 1 C: 33
97%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2017 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: * Alexandra Buzila - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.editor.genmodel.toolbaractions;
15:
16: import java.util.ArrayList;
17: import java.util.List;
18:
19: import org.eclipse.core.resources.IContainer;
20: import org.eclipse.core.resources.IFile;
21: import org.eclipse.core.resources.IResource;
22: import org.eclipse.core.resources.IWorkspaceRoot;
23: import org.eclipse.core.resources.ResourcesPlugin;
24: import org.eclipse.core.runtime.CoreException;
25: import org.eclipse.core.runtime.Path;
26: import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
27: import org.eclipse.emf.codegen.ecore.genmodel.GenPackage;
28: import org.eclipse.emf.common.util.URI;
29: import org.eclipse.emf.ecore.EPackage;
30: import org.eclipse.emf.ecore.resource.Resource;
31: import org.eclipse.emf.ecore.resource.ResourceSet;
32: import org.eclipse.emfforms.internal.editor.genmodel.Messages;
33: import org.eclipse.jface.action.Action;
34: import org.eclipse.jface.dialogs.MessageDialog;
35: import org.eclipse.jface.viewers.ISelectionProvider;
36: import org.eclipse.swt.widgets.Display;
37:
38: /**
39: * The ToolbarAction allowing the User to generate Java code from the EcoreEditor.
40: *
41: * @author Alexandra Buzila
42: *
43: */
44: public class GenerateEcoreEditorJavaCodeAction extends GenerateJavaCodeAction {
45:
46:         @Override
47:         public boolean canExecute(Object object) {
48:•                if (!ResourceSet.class.isInstance(object)) {
49:                         return false;
50:                 }
51:                 // We can execute our Action only if the ResourceSet contains an EPackage
52:•                return getEPackage((ResourceSet) object) != null;
53:
54:         }
55:
56:         @Override
57:         public Action getAction(Object currentObject, ISelectionProvider selectionProvider) {
58:                 final ResourceSet resourceSet = (ResourceSet) currentObject;
59:                 return new CreateEcoreJavaCodeAction(resourceSet, selectionProvider);
60:         }
61:
62:         @Override
63:         protected GenModel getGenModel(ResourceSet resourceSet) {
64:                 final EPackage ePackage = getEPackage(resourceSet);
65:•                if (ePackage == null) {
66:                         return null;
67:                 }
68:                 final URI resourceURI = ePackage.eResource().getURI();
69:                 final URI uri = ePackage.eResource().getResourceSet().getURIConverter().normalize(resourceURI);
70:                 final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
71:                 final IFile file = root.getFile(new Path(uri.toPlatformString(true)));
72:                 final IContainer parent = file.getParent();
73:                 return getGenModelFile(parent, resourceSet, ePackage);
74:         }
75:
76:         private GenModel getGenModelFile(IContainer parent, ResourceSet resourceSet, EPackage ePackage) {
77:•                for (final IResource resource : getGenModelFiles(parent)) {
78:                         final Resource emfResource = resourceSet
79:                                 .getResource(URI.createPlatformResourceURI(resource.getFullPath().toString(), true), true);
80:•                        if (emfResource.getContents().isEmpty() || !GenModel.class.isInstance(emfResource.getContents().get(0))) {
81:                                 continue;
82:                         }
83:                         final GenModel genModel = (GenModel) emfResource.getContents().get(0);
84:•                        for (final GenPackage genPackage : genModel.getGenPackages()) {
85:•                                if (ePackage.getNsURI() != null && genPackage.getEcorePackage() != null
86:•                                        && ePackage.getNsURI().equals(genPackage.getEcorePackage().getNsURI())) {
87:                                         resourceSet.getResources().remove(emfResource);
88:                                         return genModel;
89:                                 }
90:                         }
91:                 }
92:                 return null;
93:         }
94:
95:         private List<IResource> getGenModelFiles(IContainer parent) {
96:                 final List<IResource> genModelFiles = new ArrayList<IResource>();
97:                 try {
98:•                        for (final IResource member : parent.members()) {
99:•                                if (member.getFileExtension().equals("genmodel")) { //$NON-NLS-1$
100:                                         genModelFiles.add(member);
101:                                 }
102:                         }
103:                 } catch (final CoreException ex) {
104:                         // ignore
105:                 }
106:                 return genModelFiles;
107:         }
108:
109:         /**
110:          * Returns the first {@link EPackage} object found in the first resource of the given {@link ResourceSet}.
111:          *
112:          * @param resourceSet the {@link ResourceSet} to check
113:          * @return the {@link EPackage} or <code>null</code> if none was found
114:          */
115:         protected EPackage getEPackage(ResourceSet resourceSet) {
116:•                if (resourceSet.getResources().isEmpty()) {
117:                         return null;
118:                 }
119:                 final Resource topResource = resourceSet.getResources().get(0);
120:•                if (!topResource.getContents().isEmpty() && EPackage.class.isInstance(topResource.getContents().get(0))) {
121:                         return (EPackage) topResource.getContents().get(0);
122:                 }
123:                 return null;
124:         }
125:
126:         /**
127:          * ToolbarAction to generate Java Code from the EcoreEditor.
128:          */
129:         class CreateEcoreJavaCodeAction extends CreateJavaCodeAction {
130:
131:                 private final ResourceSet resourceSet;
132:
133:                 /**
134:                  * Constructor.
135:                  *
136:                  * @param resourceSet the {@link ResourceSet} containing the genModel file
137:                  * @param selectionProvider the {@link ISelectionProvider}
138:                  */
139:                 CreateEcoreJavaCodeAction(ResourceSet resourceSet, ISelectionProvider selectionProvider) {
140:                         super(selectionProvider);
141:                         this.resourceSet = resourceSet;
142:                 }
143:
144:                 /**
145:                  * Constructor.
146:                  *
147:                  * @param resourceSet the {@link ResourceSet} containing the genModel file
148:                  * @param text the string used as the text for the action, or null if there is no text
149:                  * @param types the project types
150:                  * @param selectionProvider the {@link ISelectionProvider}
151:                  */
152:                 CreateEcoreJavaCodeAction(ResourceSet resourceSet, String text, Object[] types,
153:                         ISelectionProvider selectionProvider) {
154:                         super(text, types, selectionProvider);
155:                         this.resourceSet = resourceSet;
156:                 }
157:
158:                 @Override
159:                 public void run() {
160:                         final GenModel genModel = GenerateEcoreEditorJavaCodeAction.this.getGenModel(resourceSet);
161:                         if (genModel == null) {
162:                                 showNoGenModelDialog();
163:                                 return;
164:                         }
165:                         setGenModel(genModel);
166:                         super.run();
167:                 }
168:
169:                 @Override
170:                 protected Action getJavaCodeAction(String text, Object[] types) {
171:                         return new CreateEcoreJavaCodeAction(resourceSet, text, types, getSelectionProvider());
172:                 }
173:
174:                 private void showNoGenModelDialog() {
175:                         MessageDialog.openConfirm(Display.getCurrent().getActiveShell(),
176:                                 Messages.GenerateEcoreEditorJavaCodeAction_noGenModelDialogTitle,
177:                                 Messages.GenerateEcoreEditorJavaCodeAction_noGenModelDialogMessage);
178:                 }
179:
180:         }
181: }