]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/CreateShapeHandler.java
Allow simulation stop handling in Simantics/Sequences
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / CreateShapeHandler.java
1 package org.simantics.modeling.ui.actions;
2
3 import java.awt.geom.Point2D;
4
5 import org.eclipse.core.commands.AbstractHandler;
6 import org.eclipse.core.commands.ExecutionEvent;
7 import org.eclipse.core.commands.ExecutionException;
8 import org.eclipse.ui.IEditorPart;
9 import org.eclipse.ui.IWorkbenchPart;
10 import org.eclipse.ui.handlers.HandlerUtil;
11 import org.eclipse.ui.part.MultiPageEditorPart;
12 import org.simantics.Simantics;
13 import org.simantics.databoard.Bindings;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.common.ResourceArray;
18 import org.simantics.db.common.request.WriteRequest;
19 import org.simantics.db.common.utils.OrderedSetUtils;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.request.Read;
22 import org.simantics.diagram.stubs.DiagramResource;
23 import org.simantics.diagram.stubs.G2DResource;
24 import org.simantics.diagram.synchronization.graph.AddElement;
25 import org.simantics.g2d.canvas.ICanvasContext;
26 import org.simantics.g2d.participant.MouseUtil;
27 import org.simantics.g2d.participant.MouseUtil.MouseInfo;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.layer0.utils.direct.GraphUtils;
30 import org.simantics.modeling.ModelingResources;
31 import org.simantics.structural.stubs.StructuralResource2;
32 import org.simantics.ui.workbench.IResourceEditorInput;
33 import org.simantics.ui.workbench.ResourceEditorInput;
34
35 /**
36  * Base class for shape creation handlers of the symbol editor.
37  * 
38  * @author Tuukka Lehtonen
39  */
40 public abstract class CreateShapeHandler extends AbstractHandler {
41
42     public abstract String getDefaultElementData();
43
44     public Object addElement(final double mposX, final double mposY, final Resource composite) {
45         final String data = getDefaultElementData();
46         Simantics.getSession().asyncRequest(new WriteRequest() {
47             @Override
48             public void perform(WriteGraph g) throws DatabaseException {
49                 Layer0 L0 = Layer0.getInstance(g);
50                 DiagramResource dr = DiagramResource.getInstance(g);
51                 G2DResource g2d = G2DResource.getInstance(g);
52                 Resource svgElement = GraphUtils.create(g,
53                         L0.InstanceOf, dr.SVGElement,
54                         g2d.HasSVGDocument, data);
55                 g.addLiteral(svgElement, dr.HasTransform, dr.IsTransformOf,
56                         g2d.Transform,
57                         new double[] {1.0, 0.0, 0.0, 1.0, mposX, mposY},
58                         Bindings.DOUBLE_ARRAY);
59                 OrderedSetUtils.add(g, composite, svgElement);
60                 g.claim(composite, L0.ConsistsOf, svgElement);
61                 AddElement.claimFreshElementName(g, composite, svgElement);
62             }
63         });
64
65         return null;
66     }
67
68     @Override
69     public Object execute(ExecutionEvent event) throws ExecutionException {
70         IWorkbenchPart ap = HandlerUtil.getActivePart(event);
71         IEditorPart viewer = null;
72         if (ap instanceof MultiPageEditorPart) {
73             MultiPageEditorPart rfe = (MultiPageEditorPart) ap;
74             IResourceEditorInput in = (ResourceEditorInput) rfe.getEditorInput();
75             final ResourceArray ra = in.getResourceArray();
76             ResourceArray symbolEditorInput;
77             try {
78                 symbolEditorInput = Simantics.getSession().syncRequest(new Read<ResourceArray>() {
79                     @Override
80                     public ResourceArray perform(ReadGraph graph) throws DatabaseException {
81                         StructuralResource2 sr = StructuralResource2.getInstance(graph);
82                         ModelingResources mr = ModelingResources.getInstance(graph);
83                         Resource symbol = graph.getPossibleObject(ra.resources[0], mr.ComponentTypeToSymbol);
84                         Resource definedBy = symbol != null ? graph.getPossibleObject(symbol, sr.IsDefinedBy) : null;
85                         ResourceArray result = definedBy != null ? new ResourceArray(definedBy) : ResourceArray.EMPTY;
86                         return result;
87                     }
88                 });
89
90                 IEditorPart[] eps = rfe.findEditors(new ResourceEditorInput("org.simantics.modeling.ui.symbolEditor", symbolEditorInput)); //$NON-NLS-1$
91                 if (eps.length == 0) {
92                     System.out.println("symbol editor part not found from multi page editor part: " + ap); //$NON-NLS-1$
93                     return null;
94                 }
95                 viewer = eps[0];
96             } catch (DatabaseException e) {
97                 e.printStackTrace();
98                 return null;
99             }
100         } else if (ap instanceof IEditorPart) {
101             viewer = (IEditorPart) ap;
102         } else {
103             return null;
104         }
105         ICanvasContext ctx = (ICanvasContext) viewer.getAdapter(ICanvasContext.class);
106         if (ctx == null) {
107             System.out.println("No canvas context"); //$NON-NLS-1$
108             return null;
109         }
110         MouseInfo minfo = ctx.getSingleItem(MouseUtil.class).getMousePressedInfo(0);
111         if(minfo == null) {
112             System.out.println("No mouse info"); //$NON-NLS-1$
113             return null;
114         }
115         final Point2D mpos = minfo.canvasPosition;
116         IResourceEditorInput input = (IResourceEditorInput)viewer.getEditorInput();
117         Resource composite = input.getResource();
118
119         return addElement(mpos.getX(), mpos.getY(), composite);
120     }
121
122 }