Package: DotHandler

DotHandler

nameinstructionbranchcomplexitylinemethod
DotHandler()
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: 132 C: 0
0%
M: 18 C: 0
0%
M: 10 C: 0
0%
M: 30 C: 0
0%
M: 1 C: 0
0%
getDotPath()
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
makeImage(File, INode, String, File)
M: 25 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * Developer Utilities for Overture
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.developerutils.dot;
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.commands.IHandler;
30: import org.eclipse.core.resources.IContainer;
31: import org.eclipse.core.resources.IProject;
32: import org.eclipse.core.resources.IResource;
33: import org.eclipse.core.runtime.CoreException;
34: import org.eclipse.jface.dialogs.MessageDialog;
35: import org.eclipse.jface.preference.IPreferenceStore;
36: import org.eclipse.jface.viewers.ISelection;
37: import org.eclipse.jface.viewers.IStructuredSelection;
38: import org.eclipse.ui.handlers.HandlerUtil;
39: import org.overture.ast.definitions.SClassDefinition;
40: import org.overture.ast.modules.AModuleModules;
41: import org.overture.ast.node.INode;
42: import org.overture.ast.preview.DotGraphVisitor;
43: import org.overture.ast.preview.GraphViz;
44: import org.overture.ast.preview.GraphViz.GraphVizException;
45: import org.overture.ide.core.IVdmModel;
46: import org.overture.ide.core.resources.IVdmProject;
47: import org.overture.ide.plugins.developerutils.IOvertureDeveloperUtils;
48: import org.overture.ide.plugins.developerutils.OvertureDeveliperUtilsPlugin;
49: import org.overture.ide.ui.utility.VdmTypeCheckerUi;
50:
51: public class DotHandler extends AbstractHandler implements IHandler
52: {
53:
54:         public Object execute(ExecutionEvent event) throws ExecutionException
55:         {
56:                 ISelection selection = HandlerUtil.getCurrentSelection(event);
57:
58:•                if (selection instanceof IStructuredSelection)
59:                 {
60:
61:                         final IContainer c = (IContainer) ((IStructuredSelection) selection).getFirstElement();
62:
63:                         final IProject project = c.getProject();
64:                         IVdmProject p = (IVdmProject) project.getAdapter(IVdmProject.class);
65:•                        if (p != null)
66:                         {
67:                                 final IVdmModel model = p.getModel();
68:•                                if (!model.isParseCorrect())
69:                                 {
70:                                         return null;
71:                                         //return new Status(Status.ERROR, IPoviewerConstants.PLUGIN_ID, "Project contains parse errors");
72:                                 }
73:
74:•                                if (model == null || !model.isTypeCorrect())
75:                                 {
76:                                         VdmTypeCheckerUi.typeCheck(HandlerUtil.getActiveShell(event), p);
77:                                 }
78:•                                for (INode node : p.getModel().getRootElementList())
79:                                 {
80:                                         String name = "out";
81:•                                        if (node instanceof SClassDefinition)
82:                                         {
83:                                                 name = ((SClassDefinition) node).getName().getName();
84:•                                        } else if (node instanceof AModuleModules)
85:                                         {
86:                                                 name = ((AModuleModules) node).getName().getName();
87:                                         }
88:                                         name += ".svg";
89:                                         File generated = p.getModelBuildPath().getOutput().getLocation().toFile();
90:                                         generated.mkdirs();
91:                                         try
92:                                         {
93:                                                 File dotpath = getDotPath();
94:•                                                if(!dotpath.exists())
95:                                                 {
96:                                                         MessageDialog.openError(HandlerUtil.getActiveShell(event), "Dot path not valid", "Could not find the following path: \""+dotpath.toString()+"\". Check that the path is set under Preferences->VDM->Dot DOT Path.");
97:                                                         return null;
98:                                                 }
99:                                                 makeImage(dotpath, node, "svg", new File(generated, name));
100:                                         } catch (Exception e)
101:                                         {
102:                                                 // TODO Auto-generated catch block
103:                                                 e.printStackTrace();
104:                                         }
105:                                 }
106:
107:                         }
108:                         try
109:                         {
110:                                 project.refreshLocal(IResource.DEPTH_INFINITE, null);
111:                         } catch (CoreException e)
112:                         {
113:                         }
114:                 }
115:                 return null;
116:         }
117:
118:         private File getDotPath()
119:         {
120:                 IPreferenceStore store = OvertureDeveliperUtilsPlugin.getDefault().getPreferenceStore();
121:                 return new File(store.getString(IOvertureDeveloperUtils.DOT_PATH_PREFERENCE));
122:         }
123:
124:         public static void makeImage(File dotPath, INode node, String type,
125:                         File output) throws GraphVizException
126:         {
127:                 DotGraphVisitor visitor = new DotGraphVisitor();
128:                 try
129:                 {
130:                         node.apply(visitor, null);
131:                 } catch (Throwable e)
132:                 {
133:                         // Ignore
134:                 }
135:                 GraphViz gv = new GraphViz(dotPath);
136:                 gv.writeGraphToFile(gv.getGraph(visitor.getResultString(), type), output);
137:         }
138: }