Package: ConvertVdmPpToVdmRtCommandHandler

ConvertVdmPpToVdmRtCommandHandler

nameinstructionbranchcomplexitylinemethod
ConvertVdmPpToVdmRtCommandHandler()
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: 43 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
rename(File)
M: 57 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.vdmrt.ui
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.vdmrt.ui.handlers;
23:
24: import java.io.File;
25: import java.util.Arrays;
26: import java.util.List;
27: import java.util.Vector;
28:
29: import org.eclipse.core.commands.AbstractHandler;
30: import org.eclipse.core.commands.ExecutionEvent;
31: import org.eclipse.core.commands.ExecutionException;
32: import org.eclipse.core.commands.IHandler;
33: import org.eclipse.core.internal.resources.ProjectDescription;
34: import org.eclipse.core.resources.IProject;
35: import org.eclipse.core.resources.IProjectDescription;
36: import org.eclipse.core.resources.IResource;
37: import org.eclipse.core.resources.ResourcesPlugin;
38: import org.eclipse.core.runtime.IProgressMonitor;
39: import org.eclipse.core.runtime.IStatus;
40: import org.eclipse.core.runtime.Status;
41: import org.eclipse.core.runtime.jobs.Job;
42: import org.eclipse.jface.viewers.IStructuredSelection;
43: import org.eclipse.ui.handlers.HandlerUtil;
44: import org.overture.ide.vdmpp.core.IVdmPpCoreConstants;
45: import org.overture.ide.vdmrt.core.IVdmRtCoreConstants;
46: import org.overture.ide.vdmrt.core.VdmRtCorePlugin;
47:
48: @SuppressWarnings("restriction")
49: public class ConvertVdmPpToVdmRtCommandHandler extends AbstractHandler
50:                 implements IHandler
51: {
52:
53:         public Object execute(ExecutionEvent event) throws ExecutionException
54:         {
55:                 try
56:                 {
57:                         IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelectionChecked(event);
58:                         final IProject project = (IProject) selection.getFirstElement();
59:
60:•                        if (project == null)
61:                         {
62:                                 return null;
63:                         }
64:                         
65:•                        if (project != null)
66:                         {
67:•                                if (project.hasNature(IVdmPpCoreConstants.NATURE))
68:                                 {
69:                                         Job copyProject = new Job("Converting VDM-PP project to VDM-RT...")
70:                                         {
71:                                                 
72:                                                 @Override
73:                                                 protected IStatus run(IProgressMonitor monitor)
74:                                                 {
75:                                                         try
76:                                                         {
77:                                                                 monitor.beginTask("Creating new project", IProgressMonitor.UNKNOWN);
78:                                                                 IProjectDescription description = new ProjectDescription();
79:                                                                 description.setName(project.getName() + "_VDM_RT");
80:                                                                 description.setBuildSpec(project.getDescription().getBuildSpec());
81:                                                                 List<String> natures = new Vector<String>();
82:                                                                 natures.addAll(Arrays.asList(project.getDescription().getNatureIds()));
83:
84:                                                                 if (natures.contains(IVdmPpCoreConstants.NATURE))
85:                                                                 {
86:                                                                         natures.remove(IVdmPpCoreConstants.NATURE);
87:                                                                 }
88:                                                                 monitor.beginTask("Adding VDM-RT nature to project", IProgressMonitor.UNKNOWN);
89:                                                                 natures.add(IVdmRtCoreConstants.NATURE);
90:                                                                 String[] n = new String[natures.size()];
91:                                                                 natures.toArray(n);
92:                                                                 description.setNatureIds(n);
93:
94:                                                                 monitor.beginTask("Copying files", IProgressMonitor.UNKNOWN);
95:                                                                 project.copy(description, true, monitor);
96:
97:                                                                 IProject newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(description.getName());
98:                                                                 if (newProject != null)
99:                                                                 {
100:                                                                         rename(newProject.getLocation().toFile());
101:                                                                         newProject.refreshLocal(IResource.DEPTH_INFINITE, monitor);
102:                                                                 }
103:                                                                 return Status.OK_STATUS;
104:                                                         } catch (Exception ex)
105:                                                         {
106:                                                                 return new Status(IStatus.ERROR, VdmRtCorePlugin.PLUGIN_ID, "Error converting VDM-PP project to VDM-RT",ex);
107:                                                         }
108:                                                 }
109:                                         };
110:                                         copyProject.schedule();
111:                                 }
112:                                 
113:                         }
114:                 } catch (Exception ex)
115:                 {
116:                         System.err.println(ex.getMessage() + ex.getStackTrace());
117:                 }
118:                 return null;
119:
120:         }
121:
122:         public static void rename(File file)
123:         {
124:•                if (file.isFile() && file.getName().toLowerCase().endsWith("vdmpp"))
125:                 {
126:                         String sourcePath = file.getAbsolutePath();
127:                         String newPath = sourcePath.substring(0, sourcePath.length() - 5)
128:                                         + "vdmrt";
129:                         if (file.renameTo(new File(newPath)))
130:                         {
131:
132:                         }
133:
134:                 }
135:•                if (file.isDirectory())
136:                 {
137:•                        for (File member : file.listFiles())
138:                         {
139:                                 rename(member);
140:                         }
141:                 }
142:         }
143: }