1 package org.simantics.modeling.ui.actions;
3 import java.awt.geom.Point2D;
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;
36 * Base class for shape creation handlers of the symbol editor.
38 * @author Tuukka Lehtonen
40 public abstract class CreateShapeHandler extends AbstractHandler {
42 public abstract String getDefaultElementData();
44 public Object addElement(final double mposX, final double mposY, final Resource composite) {
45 final String data = getDefaultElementData();
46 Simantics.getSession().asyncRequest(new WriteRequest() {
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,
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);
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;
78 symbolEditorInput = Simantics.getSession().syncRequest(new Read<ResourceArray>() {
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;
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$
96 } catch (DatabaseException e) {
100 } else if (ap instanceof IEditorPart) {
101 viewer = (IEditorPart) ap;
105 ICanvasContext ctx = (ICanvasContext) viewer.getAdapter(ICanvasContext.class);
107 System.out.println("No canvas context"); //$NON-NLS-1$
110 MouseInfo minfo = ctx.getSingleItem(MouseUtil.class).getMousePressedInfo(0);
112 System.out.println("No mouse info"); //$NON-NLS-1$
115 final Point2D mpos = minfo.canvasPosition;
116 IResourceEditorInput input = (IResourceEditorInput)viewer.getEditorInput();
117 Resource composite = input.getResource();
119 return addElement(mpos.getX(), mpos.getY(), composite);