Package: Uml2VdmCommand

Uml2VdmCommand

nameinstructionbranchcomplexitylinemethod
Uml2VdmCommand()
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%
execute(ExecutionEvent)
M: 45 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * UML2 Translator
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.plugins.uml2.commands;
23:
24: import java.io.File;
25:
26: import org.eclipse.core.commands.AbstractHandler;
27: import org.eclipse.core.commands.ExecutionEvent;
28: import org.eclipse.core.commands.ExecutionException;
29: import org.eclipse.core.resources.IFile;
30: import org.eclipse.core.resources.IResource;
31: import org.eclipse.core.runtime.CoreException;
32: import org.eclipse.core.runtime.IProgressMonitor;
33: import org.eclipse.core.runtime.IStatus;
34: import org.eclipse.core.runtime.NullProgressMonitor;
35: import org.eclipse.core.runtime.Status;
36: import org.eclipse.core.runtime.content.IContentType;
37: import org.eclipse.core.runtime.jobs.Job;
38: import org.eclipse.emf.common.util.URI;
39: import org.eclipse.jface.viewers.ISelection;
40: import org.eclipse.jface.viewers.IStructuredSelection;
41: import org.eclipse.swt.widgets.Display;
42: import org.eclipse.ui.handlers.HandlerUtil;
43: import org.overture.ide.core.resources.IVdmProject;
44: import org.overture.ide.plugins.uml2.Activator;
45: import org.overture.ide.plugins.uml2.uml2vdm.Uml2Vdm;
46:
47: public class Uml2VdmCommand extends AbstractHandler
48: {
49:
50:         public Object execute(ExecutionEvent event) throws ExecutionException
51:         {
52:
53:                 ISelection selection = HandlerUtil.getCurrentSelection(event);
54:
55:•                if (selection instanceof IStructuredSelection)
56:                 {
57:                         IStructuredSelection structuredSelection = (IStructuredSelection) selection;
58:
59:                         Object firstElement = structuredSelection.getFirstElement();
60:•                        if (firstElement instanceof IFile)
61:                         {
62:                                 final IFile iFile = (IFile) firstElement;
63:                                 java.net.URI absolutePath = iFile.getLocationURI();
64:                                 final URI uri = URI.createFileURI(absolutePath.getPath());
65:                                 final Uml2Vdm uml2vdm = new Uml2Vdm();
66:                                 final Display display = Display.getCurrent();
67:                                 Job convert = new Job("Importing UML")
68:                                 {
69:
70:                                         @Override
71:                                         protected IStatus run(IProgressMonitor progress)
72:                                         {
73:                                                 progress.beginTask("Importing", 100);
74:                                                 progress.worked(5);
75:
76:                                                 IVdmProject p = (IVdmProject) iFile.getProject().getAdapter(IVdmProject.class);
77:                                                 String extension = null;
78:                                                 if (p != null)
79:                                                 {
80:                                                         for (IContentType ct : p.getContentTypeIds())
81:                                                         {
82:                                                                 if (!ct.getId().contains(".external."))
83:                                                                 {
84:                                                                         extension = ct.getFileSpecs(IContentType.FILE_EXTENSION_SPEC)[0];
85:                                                                         break;
86:                                                                 }
87:                                                         }
88:
89:                                                 }
90:
91:                                                 if (!uml2vdm.initialize(uri, extension))
92:                                                 {
93:                                                         return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed importing .uml file. Maybe it doesnt have the EMF UML format");
94:                                                 }
95:
96:                                                 progress.worked(40);
97:                                                 display.asyncExec(new Runnable()
98:                                                 {
99:                                                         public void run()
100:                                                         {
101:                                                                 uml2vdm.convert(new File(iFile.getProject().getLocation().toFile(), "uml_import"));
102:
103:                                                                 try
104:                                                                 {
105:                                                                         iFile.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
106:                                                                 } catch (CoreException e)
107:                                                                 {
108:                                                                         // ignore
109:                                                                 }
110:                                                         }
111:                                                 });
112:
113:                                                 progress.worked(50);
114:
115:                                                 progress.worked(5);
116:                                                 progress.done();
117:
118:                                                 return Status.OK_STATUS;
119:                                         }
120:                                 };
121:                                 convert.schedule();
122:                         }
123:
124:                 }
125:
126:                 return null;
127:         }
128:
129: }