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