From e650c464b28377276ac010577703184eb5890a05 Mon Sep 17 00:00:00 2001 From: niemisto Date: Fri, 6 Nov 2009 15:05:51 +0000 Subject: [PATCH] git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@12943 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn.ui/.classpath | 7 + org.simantics.sysdyn.ui/.project | 28 ++++ .../.settings/org.eclipse.jdt.core.prefs | 8 + org.simantics.sysdyn.ui/META-INF/MANIFEST.MF | 18 +++ org.simantics.sysdyn.ui/adapters.xml | 32 ++++ org.simantics.sysdyn.ui/build.properties | 5 + org.simantics.sysdyn.ui/plugin.xml | 22 +++ .../org/simantics/sysdyn/ui/Activator.java | 50 +++++++ .../simantics/sysdyn/ui/SysdynResource.java | 72 +++++++++ .../ui/adapters/VariableClassFactory.java | 46 ++++++ .../ui/adapters/VariableElementWriter.java | 23 +++ .../ui/diagram/DiagramEditorFacade.java | 124 ++++++++++++++++ .../ui/diagram/DrawableElementClass.java | 137 ++++++++++++++++++ .../ui/diagram/SysdynDiagramEditor.java | 12 ++ .../sysdyn/ui/elements/Drawable.java | 34 +++++ .../sysdyn/ui/elements/IDrawable.java | 15 ++ .../sysdyn/ui/elements/ITransformable.java | 11 ++ .../ui/elements/PositionableDrawable.java | 40 +++++ .../sysdyn/ui/elements/TestDrawable.java | 36 +++++ 19 files changed, 720 insertions(+) create mode 100644 org.simantics.sysdyn.ui/.classpath create mode 100644 org.simantics.sysdyn.ui/.project create mode 100644 org.simantics.sysdyn.ui/.settings/org.eclipse.jdt.core.prefs create mode 100644 org.simantics.sysdyn.ui/META-INF/MANIFEST.MF create mode 100644 org.simantics.sysdyn.ui/adapters.xml create mode 100644 org.simantics.sysdyn.ui/build.properties create mode 100644 org.simantics.sysdyn.ui/plugin.xml create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/Activator.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/SysdynResource.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/adapters/VariableClassFactory.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/adapters/VariableElementWriter.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/diagram/DiagramEditorFacade.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/diagram/DrawableElementClass.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/diagram/SysdynDiagramEditor.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/Drawable.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/IDrawable.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/ITransformable.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/PositionableDrawable.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TestDrawable.java diff --git a/org.simantics.sysdyn.ui/.classpath b/org.simantics.sysdyn.ui/.classpath new file mode 100644 index 00000000..8a8f1668 --- /dev/null +++ b/org.simantics.sysdyn.ui/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/org.simantics.sysdyn.ui/.project b/org.simantics.sysdyn.ui/.project new file mode 100644 index 00000000..2ebd5691 --- /dev/null +++ b/org.simantics.sysdyn.ui/.project @@ -0,0 +1,28 @@ + + + org.simantics.sysdyn.ui + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/org.simantics.sysdyn.ui/.settings/org.eclipse.jdt.core.prefs b/org.simantics.sysdyn.ui/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..6d816954 --- /dev/null +++ b/org.simantics.sysdyn.ui/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,8 @@ +#Thu Nov 05 12:45:23 EET 2009 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF b/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF new file mode 100644 index 00000000..33131cad --- /dev/null +++ b/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF @@ -0,0 +1,18 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Ui +Bundle-SymbolicName: org.simantics.sysdyn.ui;singleton:=true +Bundle-Version: 1.0.0.qualifier +Bundle-Activator: org.simantics.sysdyn.ui.Activator +Require-Bundle: org.eclipse.ui, + org.eclipse.core.runtime, + org.simantics.ui;bundle-version="1.0.0", + org.simantics.diagram;bundle-version="1.0.0", + org.simantics.db;bundle-version="0.6.2", + org.simantics.db.common;bundle-version="0.6.2", + org.simantics.db.layer0;bundle-version="0.7.0", + org.simantics.g2d;bundle-version="1.0.0", + org.simantics.scenegraph;bundle-version="1.0.0", + org.simantics.layer0.utils;bundle-version="0.6.2" +Bundle-ActivationPolicy: lazy +Bundle-RequiredExecutionEnvironment: JavaSE-1.6 diff --git a/org.simantics.sysdyn.ui/adapters.xml b/org.simantics.sysdyn.ui/adapters.xml new file mode 100644 index 00000000..86c2aa36 --- /dev/null +++ b/org.simantics.sysdyn.ui/adapters.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/build.properties b/org.simantics.sysdyn.ui/build.properties new file mode 100644 index 00000000..6f20375d --- /dev/null +++ b/org.simantics.sysdyn.ui/build.properties @@ -0,0 +1,5 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + plugin.xml diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml new file mode 100644 index 00000000..cac6482e --- /dev/null +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/Activator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/Activator.java new file mode 100644 index 00000000..21342378 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/Activator.java @@ -0,0 +1,50 @@ +package org.simantics.sysdyn.ui; + +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; + +/** + * The activator class controls the plug-in life cycle + */ +public class Activator extends AbstractUIPlugin { + + // The plug-in ID + public static final String PLUGIN_ID = "org.simantics.sysdyn.ui"; + + // The shared instance + private static Activator plugin; + + /** + * The constructor + */ + public Activator() { + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) + */ + public void start(BundleContext context) throws Exception { + super.start(context); + plugin = this; + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) + */ + public void stop(BundleContext context) throws Exception { + plugin = null; + super.stop(context); + } + + /** + * Returns the shared instance + * + * @return the shared instance + */ + public static Activator getDefault() { + return plugin; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/SysdynResource.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/SysdynResource.java new file mode 100644 index 00000000..426b3220 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/SysdynResource.java @@ -0,0 +1,72 @@ +/******************************************************************************* + * Copyright (c) 2007- VTT Technical Research Centre of Finland. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui; + + +import org.simantics.db.Resource; +import org.simantics.db.ReadGraph; +import org.simantics.db.Session; +import org.simantics.db.exception.DatabaseException; + +public class SysdynResource { + + public final Resource HasX; + public final Resource HasY; + public final Resource SysdynModel; + public final Resource SysdynModelManager; + public final Resource SysdynModellingDomain; + public final Resource TestDiagram; + public final Resource Variable; + public final Resource VariableElement; + + public static class URIs { + public static final String HasX = "http://www.simantics.org/Sysdyn#HasX"; + public static final String HasY = "http://www.simantics.org/Sysdyn#HasY"; + public static final String SysdynModel = "http://www.simantics.org/Sysdyn#SysdynModel"; + public static final String SysdynModelManager = "http://www.simantics.org/Sysdyn#SysdynModelManager"; + public static final String SysdynModellingDomain = "http://www.simantics.org/Sysdyn#SysdynModellingDomain"; + public static final String TestDiagram = "http://www.simantics.org/Sysdyn#TestDiagram"; + public static final String Variable = "http://www.simantics.org/Sysdyn#Variable"; + public static final String VariableElement = "http://www.simantics.org/Sysdyn#VariableElement"; + } + + public static Resource getResourceOrNull(ReadGraph graph, String uri) { + try { + return graph.getResourceByURI(uri); + } catch(DatabaseException e) { + System.err.println(e.getMessage()); + return null; + } + } + + public SysdynResource(ReadGraph graph) { + HasX = getResourceOrNull(graph, URIs.HasX); + HasY = getResourceOrNull(graph, URIs.HasY); + SysdynModel = getResourceOrNull(graph, URIs.SysdynModel); + SysdynModelManager = getResourceOrNull(graph, URIs.SysdynModelManager); + SysdynModellingDomain = getResourceOrNull(graph, URIs.SysdynModellingDomain); + TestDiagram = getResourceOrNull(graph, URIs.TestDiagram); + Variable = getResourceOrNull(graph, URIs.Variable); + VariableElement = getResourceOrNull(graph, URIs.VariableElement); + } + + public static SysdynResource getInstance(ReadGraph graph) { + Session session = graph.getSession(); + SysdynResource ret = session.getService(SysdynResource.class); + if(ret == null) { + ret = new SysdynResource(graph); + session.registerService(SysdynResource.class, ret); + } + return ret; + } + +} + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/adapters/VariableClassFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/adapters/VariableClassFactory.java new file mode 100644 index 00000000..e45047f4 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/adapters/VariableClassFactory.java @@ -0,0 +1,46 @@ +package org.simantics.sysdyn.ui.adapters; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.adapter.ElementQuerySupport; +import org.simantics.diagram.adapter.GraphElementClassFactory; +import org.simantics.diagram.adapter.GraphElementFactory; +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.impl.Element; +import org.simantics.sysdyn.ui.SysdynResource; +import org.simantics.sysdyn.ui.diagram.DrawableElementClass; +import org.simantics.sysdyn.ui.elements.TestDrawable; + +public class VariableClassFactory implements GraphElementFactory, GraphElementClassFactory { + + @Override + public IElement spawn(ReadGraph g, Resource resource, + ElementQuerySupport support, IDiagram diagram) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(g); + + ElementClass elementClass = + DrawableElementClass.createElementClass(sr.VariableElement); + IElement element = Element.spawnNew(elementClass); + + TestDrawable drawable = new TestDrawable(); + drawable.moveTo( + (Double)g.getRelatedValue(resource, sr.HasX), + (Double)g.getRelatedValue(resource, sr.HasY) + ); + element.setHint(DrawableElementClass.KEY_CONTENT, drawable); + + //diagram.addElement(element); + return element; + } + + @Override + public ElementClass create(ReadGraph g, Resource elementType, + ElementQuerySupport support) throws DatabaseException { + return DrawableElementClass.createElementClass(elementType); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/adapters/VariableElementWriter.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/adapters/VariableElementWriter.java new file mode 100644 index 00000000..a671b6ba --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/adapters/VariableElementWriter.java @@ -0,0 +1,23 @@ +package org.simantics.sysdyn.ui.adapters; + +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.synchronization.graph.ElementWriter; +import org.simantics.g2d.element.IElement; + +public class VariableElementWriter implements ElementWriter { + + @Override + public void addToGraph(WriteGraph graph, IElement element, + Resource elementResource) throws DatabaseException { + System.out.println("addToGraph"); + } + + @Override + public void removeFromGraph(WriteGraph graph, Resource elementResource) + throws DatabaseException { + System.out.println("removeFromGraph"); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/diagram/DiagramEditorFacade.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/diagram/DiagramEditorFacade.java new file mode 100644 index 00000000..daf35821 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/diagram/DiagramEditorFacade.java @@ -0,0 +1,124 @@ +package org.simantics.sysdyn.ui.diagram; + +import java.awt.Color; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.simantics.db.ReadGraph; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.adapter.GraphToDiagramSynchronizer; +import org.simantics.diagram.adapter.IDiagramLoader; +import org.simantics.diagram.participant.SGFocusParticipant; +import org.simantics.g2d.canvas.Hints; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.canvas.impl.CanvasContext; +import org.simantics.g2d.chassis.SWTChassis; +import org.simantics.g2d.diagram.DiagramHints; +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.g2d.diagram.participant.CanvasBoundsParticipant; +import org.simantics.g2d.diagram.participant.DiagramParticipant; +import org.simantics.g2d.diagram.participant.ElementInteractor; +import org.simantics.g2d.diagram.participant.ElementPainter; +import org.simantics.g2d.diagram.participant.Selection; +import org.simantics.g2d.diagram.participant.pointertool.PointerInteractor; +import org.simantics.g2d.element.ElementClassProviders; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.impl.Element; +import org.simantics.g2d.participant.CanvasGrab; +import org.simantics.g2d.participant.KeyUtil; +import org.simantics.g2d.participant.MousePanZoomInteractor; +import org.simantics.g2d.participant.MouseUtil; +import org.simantics.g2d.participant.Notifications; +import org.simantics.g2d.participant.PanZoomRotateHandler; +import org.simantics.g2d.participant.PointerPainter; +import org.simantics.g2d.participant.RulerPainter; +import org.simantics.g2d.participant.SGTransformUtil; +import org.simantics.g2d.snap.GridSnapAdvisor; +import org.simantics.layer0.utils.ResourceArray; +import org.simantics.sysdyn.ui.elements.IDrawable; +import org.simantics.ui.SimanticsUI; +import org.simantics.ui.workbench.ResourceEditorPart; +import org.simantics.utils.datastructures.hints.IHintContext; +import org.simantics.utils.threads.AWTThread; + +public abstract class DiagramEditorFacade extends ResourceEditorPart { + + SWTChassis chassis; + IDiagram diagram; + IDiagramLoader loader; + + public DiagramEditorFacade() { + super(); + } + + @Override + public void reload(ReadGraph g) throws DatabaseException { + // TODO Auto-generated method stub + } + + @Override + public void createPartControl(Composite parent) { + chassis = new SWTChassis(parent, SWT.NONE); + chassis.syncPopulate(); + + final ICanvasContext context = new CanvasContext( + AWTThread.getThreadAccess()); + chassis.setCanvasContext(context); + + context.add(new SGTransformUtil()); + context.add(new MouseUtil()); + context.add(new KeyUtil()); + context.add(new PanZoomRotateHandler()); + context.add(new MousePanZoomInteractor()); + context.add(new RulerPainter()); + context.add(new CanvasGrab()); + context.add(new CanvasBoundsParticipant()); + context.add(new Notifications()); + context.add(new SGFocusParticipant(chassis)); + context.add(new DiagramParticipant()); + context.add(new ElementPainter()); + context.add(new PointerInteractor(true, true, true, false, true, false, null)); + context.add(new ElementInteractor()); + context.add(new Selection()); + + IHintContext h = context.getDefaultHintContext(); + h.setHint(PointerPainter.KEY_PAINT_POINTER, true); + + h.setHint(ElementPainter.KEY_SELECTION_FRAME_COLOR, Color.MAGENTA); + h.setHint(Hints.KEY_TOOL, Hints.POINTERTOOL); + + h.setHint(PanZoomRotateHandler.KEY_ZOOM_IN_LIMIT, 100000.0); + h.setHint(PanZoomRotateHandler.KEY_ZOOM_OUT_LIMIT, 10.0); + + h.setHint(DiagramHints.SNAP_ADVISOR, + new GridSnapAdvisor(10.0)); + + try { + SimanticsUI.getSession().syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + loader = new GraphToDiagramSynchronizer(graph, context, + ElementClassProviders.staticProvider(null)); + + diagram = loader.loadDiagram(graph, + getResourceInput().getResource(), + ResourceArray.EMPTY, + null); + } + + }); + h.setHint(DiagramHints.KEY_DIAGRAM, diagram); + } catch (DatabaseException e) { + e.printStackTrace(); + } + + } + + @Override + public void setFocus() { + chassis.setFocus(); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/diagram/DrawableElementClass.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/diagram/DrawableElementClass.java new file mode 100644 index 00000000..2851dccd --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/diagram/DrawableElementClass.java @@ -0,0 +1,137 @@ +package org.simantics.sysdyn.ui.diagram; + +import java.awt.Shape; +import java.awt.geom.AffineTransform; +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.util.Collection; + +import org.simantics.db.Resource; +import org.simantics.diagram.adapter.handler.ResourceAdapter; +import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy; +import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.InternalSize; +import org.simantics.g2d.element.handler.Move; +import org.simantics.g2d.element.handler.Outline; +import org.simantics.g2d.element.handler.Pick2; +import org.simantics.g2d.element.handler.SceneGraph; +import org.simantics.g2d.element.handler.Transform; +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.sysdyn.ui.elements.IDrawable; +import org.simantics.sysdyn.ui.elements.ITransformable; +import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; + +public class DrawableElementClass { + private DrawableElementClass() {} + + public static KeyOf KEY_CONTENT = new KeyOf(IDrawable.class, "CONTENT"); + public static AffineTransform IDENTITY_TRANSFORM = new AffineTransform(); + + public static WidgetHandler WIDGET_HANDLER_INSTANCE = new WidgetHandler(); + + private static class WidgetHandler implements Transform, SceneGraph, InternalSize, Move, Outline, Pick2 { + + private static final long serialVersionUID = 1722978969083481038L; + + @Override + public void setTransform(IElement e, AffineTransform at) { + IDrawable drawable = (IDrawable)e.getHint(KEY_CONTENT); + if(drawable instanceof ITransformable) { + drawable = ((ITransformable)drawable).setTransform(at); + e.setHint(KEY_CONTENT, drawable); + drawable.updateSG(); + } + } + + @Override + public AffineTransform getTransform(IElement e) { + IDrawable drawable = (IDrawable)e.getHint(KEY_CONTENT); + if(drawable instanceof ITransformable) + return ((ITransformable)drawable).getTransform(); + else + return IDENTITY_TRANSFORM; + } + + @Override + public void cleanup(IElement e) { + ((IDrawable)e.getHint(KEY_CONTENT)).cleanupSG(); + } + + @Override + public void init(IElement e, G2DParentNode parent) { + ((IDrawable)e.getHint(KEY_CONTENT)).initSG(parent); + } + + @Override + public Rectangle2D getBounds(IElement e, Rectangle2D size) { + if(size == null) + size = new Rectangle2D.Double(); + ((IDrawable)e.getHint(KEY_CONTENT)).getBounds(size); + return size; + } + + @Override + public Point2D getPosition(IElement e) { + IDrawable drawable = (IDrawable)e.getHint(KEY_CONTENT); + if(drawable instanceof ITransformable) + return ((ITransformable)drawable).getPosition(); + else + return new Point2D.Double(0.0, 0.0); + } + + @Override + public void moveTo(IElement e, double x, double y) { + IDrawable drawable = (IDrawable)e.getHint(KEY_CONTENT); + if(drawable instanceof ITransformable) { + drawable = ((ITransformable)drawable).moveTo(x, y); + e.setHint(KEY_CONTENT, drawable); + drawable.updateSG(); + } + } + + @Override + public Shape getElementShape(IElement e) { + return getBounds(e, null); + } + + @Override + public int pick(IElement e, Shape s, PickPolicy policy, + Collection result) { + if(pickTest(e, s, policy)) { + result.add(e); + return 1; + } + else + return 0; + } + + @Override + public boolean pickTest(IElement e, Shape s, PickPolicy policy) { + IDrawable drawable = (IDrawable)e.getHint(KEY_CONTENT); + switch(policy) { + case PICK_CONTAINED_OBJECTS: + return drawable.pickContained(s); + case PICK_INTERSECTING_OBJECTS: + return drawable.pickIntersecting(s); + default: + return false; + } + } + + } + + public static ElementClass createElementClass(final Resource resource) { + return ElementClass.compile( + WIDGET_HANDLER_INSTANCE, + new ResourceAdapter() { + + @Override + public Resource getResource() { + return resource; + } + + } + ); + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/diagram/SysdynDiagramEditor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/diagram/SysdynDiagramEditor.java new file mode 100644 index 00000000..614a4374 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/diagram/SysdynDiagramEditor.java @@ -0,0 +1,12 @@ +package org.simantics.sysdyn.ui.diagram; + +import org.simantics.sysdyn.ui.elements.TestDrawable; + + +public class SysdynDiagramEditor extends DiagramEditorFacade { + + public SysdynDiagramEditor() { + super(); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/Drawable.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/Drawable.java new file mode 100644 index 00000000..90ab9cd5 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/Drawable.java @@ -0,0 +1,34 @@ +package org.simantics.sysdyn.ui.elements; + +import java.awt.Shape; +import java.awt.geom.AffineTransform; + +import org.simantics.scenegraph.g2d.G2DParentNode; + +public abstract class Drawable implements IDrawable { + + public static final AffineTransform IDENTITY = new AffineTransform(); + + @Override + public void initSG(G2DParentNode parent) { + } + + @Override + public void cleanupSG() { + } + + @Override + public void updateSG() { + } + + @Override + public boolean pickContained(Shape s) { + return false; + } + + @Override + public boolean pickIntersecting(Shape s) { + return false; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/IDrawable.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/IDrawable.java new file mode 100644 index 00000000..d48a3c90 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/IDrawable.java @@ -0,0 +1,15 @@ +package org.simantics.sysdyn.ui.elements; + +import java.awt.Shape; +import java.awt.geom.Rectangle2D; + +import org.simantics.scenegraph.g2d.G2DParentNode; + +public interface IDrawable { + void getBounds(Rectangle2D size); + void initSG(G2DParentNode parent); + void updateSG(); + void cleanupSG(); + boolean pickIntersecting(Shape s); + boolean pickContained(Shape s); +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/ITransformable.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/ITransformable.java new file mode 100644 index 00000000..266b8b5c --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/ITransformable.java @@ -0,0 +1,11 @@ +package org.simantics.sysdyn.ui.elements; + +import java.awt.geom.AffineTransform; +import java.awt.geom.Point2D; + +public interface ITransformable extends IDrawable { + ITransformable setTransform(AffineTransform at); + AffineTransform getTransform(); + public ITransformable moveTo(double x, double y); + Point2D getPosition(); +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/PositionableDrawable.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/PositionableDrawable.java new file mode 100644 index 00000000..58d697e3 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/PositionableDrawable.java @@ -0,0 +1,40 @@ +package org.simantics.sysdyn.ui.elements; + +import java.awt.geom.AffineTransform; +import java.awt.geom.Point2D; + +public abstract class PositionableDrawable extends Drawable implements ITransformable, Cloneable { + protected double posX; + protected double posY; + + @Override + public Point2D getPosition() { + return new Point2D.Double(posX, posY); + } + @Override + public AffineTransform getTransform() { + return new AffineTransform(1.0, 0.0, 0.0, 1.0, posX, posY); + } + @Override + public ITransformable moveTo(double x, double y) { + try { + PositionableDrawable d = (PositionableDrawable)clone(); + d.posX = x; + d.posY = y; + return d; + } catch(CloneNotSupportedException e) { + throw new Error(e); + } + } + @Override + public ITransformable setTransform(AffineTransform at) { + try { + PositionableDrawable d = (PositionableDrawable)clone(); + d.posX = at.getTranslateX(); + d.posY = at.getTranslateY(); + return d; + } catch(CloneNotSupportedException e) { + throw new Error(e); + } + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TestDrawable.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TestDrawable.java new file mode 100644 index 00000000..5d35c5ec --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TestDrawable.java @@ -0,0 +1,36 @@ +package org.simantics.sysdyn.ui.elements; + +import java.awt.Shape; +import java.awt.geom.AffineTransform; +import java.awt.geom.Rectangle2D; + +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.scenegraph.g2d.nodes.ShapeNode; + + +public class TestDrawable extends PositionableDrawable { + + @Override + public void getBounds(Rectangle2D size) { + size.setFrame(-10.0, -10.0, 20.0, 20.0); + } + + ShapeNode node; + + @Override + public void initSG(G2DParentNode parent) { + node = parent.addNode(ShapeNode.class); + node.setShape(new Rectangle2D.Double(-10.0, -10.0, 20.0, 20.0)); + } + + @Override + public boolean pickContained(Shape s) { + return s.contains(-10.0, -10.0, 20.0, 20.0); + } + + @Override + public boolean pickIntersecting(Shape s) { + return s.intersects(-10.0, -10.0, 20.0, 20.0); + } + +} -- 2.47.1