]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Add initial version of regions to source control 19/2419/3
authorjsimomaa <jani.simomaa@gmail.com>
Mon, 5 Nov 2018 14:07:02 +0000 (16:07 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 5 Nov 2018 20:03:54 +0000 (20:03 +0000)
gitlab #14

Change-Id: I57884758ed59938fa61ef6e4139d9771183a7e21

32 files changed:
org.simantics.district.network.ontology/META-INF/MANIFEST.MF
org.simantics.district.region.ontology/.classpath [new file with mode: 0644]
org.simantics.district.region.ontology/.project [new file with mode: 0644]
org.simantics.district.region.ontology/META-INF/MANIFEST.MF [new file with mode: 0644]
org.simantics.district.region.ontology/build.properties [new file with mode: 0644]
org.simantics.district.region.ontology/graph.tg [new file with mode: 0644]
org.simantics.district.region.ontology/graph/DiagramRegions.pgraph [new file with mode: 0644]
org.simantics.district.region.ontology/src/org/simantics/district/region/ontology/Activator.java [new file with mode: 0644]
org.simantics.district.region.ontology/src/org/simantics/district/region/ontology/DiagramRegionsResource.java [new file with mode: 0644]
org.simantics.district.region.ui/.classpath [new file with mode: 0644]
org.simantics.district.region.ui/.project [new file with mode: 0644]
org.simantics.district.region.ui/META-INF/MANIFEST.MF [new file with mode: 0644]
org.simantics.district.region.ui/build.properties [new file with mode: 0644]
org.simantics.district.region.ui/fragment.e4xmi [new file with mode: 0644]
org.simantics.district.region.ui/plugin.xml [new file with mode: 0644]
org.simantics.district.region.ui/src/org/simantics/district/region/ui/Activator.java [new file with mode: 0644]
org.simantics.district.region.ui/src/org/simantics/district/region/ui/CreateRegionFromSelection.java [new file with mode: 0644]
org.simantics.district.region.ui/src/org/simantics/district/region/ui/DiagramRegionsTableUI.java [new file with mode: 0644]
org.simantics.district.region.ui/src/org/simantics/district/region/ui/DiagramRegionsTableView.java [new file with mode: 0644]
org.simantics.district.region.ui/src/org/simantics/district/region/ui/OpenDiagramRegionsTable.java [new file with mode: 0644]
org.simantics.district.region.ui/src/org/simantics/district/region/ui/handlers/RemoveRegionHandler.java [new file with mode: 0644]
org.simantics.district.region.ui/src/org/simantics/district/region/ui/handlers/ZoomToRegionHandler.java [new file with mode: 0644]
org.simantics.district.region/.classpath [new file with mode: 0644]
org.simantics.district.region/.project [new file with mode: 0644]
org.simantics.district.region/META-INF/MANIFEST.MF [new file with mode: 0644]
org.simantics.district.region/build.properties [new file with mode: 0644]
org.simantics.district.region/scl/Simantics/Diagram/Regions.scl [new file with mode: 0644]
org.simantics.district.region/src/org/simantics/district/region/Activator.java [new file with mode: 0644]
org.simantics.district.region/src/org/simantics/district/region/DiagramRegions.java [new file with mode: 0644]
org.simantics.district.regions.feature/.project [new file with mode: 0644]
org.simantics.district.regions.feature/build.properties [new file with mode: 0644]
org.simantics.district.regions.feature/feature.xml [new file with mode: 0644]

index 16df5e156eb986fe82cbf90050bd1fe68da41758..a4cb4e141a7edd2a477b148a2b85e0c2a69942e6 100644 (file)
@@ -11,6 +11,7 @@ Require-Bundle: org.simantics.layer0,
  org.simantics.image2.ontology;bundle-version="1.2.0",
  org.simantics.selectionview.ontology;bundle-version="1.2.0",
  org.simantics.selectionview.ui.ontology;bundle-version="1.1.0",
- org.simantics.g2d.ontology;bundle-version="1.1.0"
+ org.simantics.g2d.ontology;bundle-version="1.1.0",
+ org.simantics.layer0x.ontology;bundle-version="1.1.0"
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Export-Package: org.simantics.district.network.ontology
diff --git a/org.simantics.district.region.ontology/.classpath b/org.simantics.district.region.ontology/.classpath
new file mode 100644 (file)
index 0000000..eca7bdb
--- /dev/null
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
+       <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+       <classpathentry kind="src" path="src"/>
+       <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/org.simantics.district.region.ontology/.project b/org.simantics.district.region.ontology/.project
new file mode 100644 (file)
index 0000000..c549af0
--- /dev/null
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+       <name>org.simantics.district.region.ontology</name>
+       <comment></comment>
+       <projects>
+       </projects>
+       <buildSpec>
+               <buildCommand>
+                       <name>org.simantics.graph.builder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.jdt.core.javabuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.pde.ManifestBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.pde.SchemaBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+       </buildSpec>
+       <natures>
+               <nature>org.eclipse.pde.PluginNature</nature>
+               <nature>org.eclipse.jdt.core.javanature</nature>
+               <nature>org.simantics.graph.nature</nature>
+       </natures>
+</projectDescription>
diff --git a/org.simantics.district.region.ontology/META-INF/MANIFEST.MF b/org.simantics.district.region.ontology/META-INF/MANIFEST.MF
new file mode 100644 (file)
index 0000000..2ec31d7
--- /dev/null
@@ -0,0 +1,13 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Simantics Diagram Region Ontology
+Bundle-SymbolicName: org.simantics.district.region.ontology
+Bundle-Version: 1.0.0.qualifier
+Bundle-Activator: org.simantics.district.region.ontology.Activator
+Bundle-Vendor: Semantum Oy
+Require-Bundle: org.eclipse.core.runtime,
+ org.simantics.layer0,
+ org.simantics.diagram.ontology;bundle-version="2.2.0"
+Bundle-RequiredExecutionEnvironment: JavaSE-1.8
+Bundle-ActivationPolicy: lazy
+Export-Package: org.simantics.district.region.ontology
diff --git a/org.simantics.district.region.ontology/build.properties b/org.simantics.district.region.ontology/build.properties
new file mode 100644 (file)
index 0000000..e85b630
--- /dev/null
@@ -0,0 +1,5 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+               .,\
+               graph.tg
diff --git a/org.simantics.district.region.ontology/graph.tg b/org.simantics.district.region.ontology/graph.tg
new file mode 100644 (file)
index 0000000..2cc8f4c
Binary files /dev/null and b/org.simantics.district.region.ontology/graph.tg differ
diff --git a/org.simantics.district.region.ontology/graph/DiagramRegions.pgraph b/org.simantics.district.region.ontology/graph/DiagramRegions.pgraph
new file mode 100644 (file)
index 0000000..c7774db
--- /dev/null
@@ -0,0 +1,14 @@
+L0 = <http://www.simantics.org/Layer0-1.1>
+DIA = <http://www.simantics.org/Diagram-2.2>
+
+DIARegions = <http://www.simantics.org/DistrictDiagramRegions-1.0> : L0.Ontology
+    @L0.new
+    L0.HasResourceClass "org.simantics.district.region.ontology.DiagramRegionsResource"
+
+DIARegions.Region <T L0.Entity
+
+DIARegions.Region.area <R L0.HasProperty
+
+DIARegions.hasRegion <R L0.IsWeaklyRelatedTo
+    L0.InverseOf DIARegions.regionOf <R L0.IsWeaklyRelatedTo
+    L0.HasDomain DIA.Diagram
diff --git a/org.simantics.district.region.ontology/src/org/simantics/district/region/ontology/Activator.java b/org.simantics.district.region.ontology/src/org/simantics/district/region/ontology/Activator.java
new file mode 100644 (file)
index 0000000..85b64b7
--- /dev/null
@@ -0,0 +1,30 @@
+package org.simantics.district.region.ontology;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+public class Activator implements BundleActivator {
+
+       private static BundleContext context;
+
+       static BundleContext getContext() {
+               return context;
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
+        */
+       public void start(BundleContext bundleContext) throws Exception {
+               Activator.context = bundleContext;
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
+        */
+       public void stop(BundleContext bundleContext) throws Exception {
+               Activator.context = null;
+       }
+
+}
diff --git a/org.simantics.district.region.ontology/src/org/simantics/district/region/ontology/DiagramRegionsResource.java b/org.simantics.district.region.ontology/src/org/simantics/district/region/ontology/DiagramRegionsResource.java
new file mode 100644 (file)
index 0000000..2777f8a
--- /dev/null
@@ -0,0 +1,70 @@
+package org.simantics.district.region.ontology;
+
+import org.simantics.db.RequestProcessor;
+import org.simantics.db.Resource;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.request.Read;
+import org.simantics.db.Session;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.service.QueryControl;
+
+public class DiagramRegionsResource {
+    
+    public final Resource Region;
+    public final Resource Region_area;
+    public final Resource Region_area_Inverse;
+    public final Resource hasRegion;
+    public final Resource regionOf;
+        
+    public static class URIs {
+        public static final String Region = "http://www.simantics.org/DistrictDiagramRegions-1.0/Region";
+        public static final String Region_area = "http://www.simantics.org/DistrictDiagramRegions-1.0/Region/area";
+        public static final String Region_area_Inverse = "http://www.simantics.org/DistrictDiagramRegions-1.0/Region/area/Inverse";
+        public static final String hasRegion = "http://www.simantics.org/DistrictDiagramRegions-1.0/hasRegion";
+        public static final String regionOf = "http://www.simantics.org/DistrictDiagramRegions-1.0/regionOf";
+    }
+    
+    public static Resource getResourceOrNull(ReadGraph graph, String uri) {
+        try {
+            return graph.getResource(uri);
+        } catch(DatabaseException e) {
+            System.err.println(e.getMessage());
+            return null;
+        }
+    }
+    
+    public DiagramRegionsResource(ReadGraph graph) {
+        Region = getResourceOrNull(graph, URIs.Region);
+        Region_area = getResourceOrNull(graph, URIs.Region_area);
+        Region_area_Inverse = getResourceOrNull(graph, URIs.Region_area_Inverse);
+        hasRegion = getResourceOrNull(graph, URIs.hasRegion);
+        regionOf = getResourceOrNull(graph, URIs.regionOf);
+    }
+    
+    public static DiagramRegionsResource getInstance(ReadGraph graph) {
+        Session session = graph.getSession();
+        DiagramRegionsResource ret = session.peekService(DiagramRegionsResource.class);
+        if(ret == null) {
+            QueryControl qc = graph.getService(QueryControl.class);
+            ret = new DiagramRegionsResource(qc.getIndependentGraph(graph));
+            session.registerService(DiagramRegionsResource.class, ret);
+        }
+        return ret;
+    }
+    
+    public static DiagramRegionsResource getInstance(RequestProcessor session) throws DatabaseException {
+        DiagramRegionsResource ret = session.peekService(DiagramRegionsResource.class);
+        if(ret == null) {
+            ret = session.syncRequest(new Read<DiagramRegionsResource>() {
+                public DiagramRegionsResource perform(ReadGraph graph) throws DatabaseException {
+                    QueryControl qc = graph.getService(QueryControl.class);
+                    return new DiagramRegionsResource(qc.getIndependentGraph(graph));
+                }
+            });
+            session.registerService(DiagramRegionsResource.class, ret);
+        }
+        return ret;
+    }
+    
+}
+
diff --git a/org.simantics.district.region.ui/.classpath b/org.simantics.district.region.ui/.classpath
new file mode 100644 (file)
index 0000000..eca7bdb
--- /dev/null
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
+       <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+       <classpathentry kind="src" path="src"/>
+       <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/org.simantics.district.region.ui/.project b/org.simantics.district.region.ui/.project
new file mode 100644 (file)
index 0000000..2b5278c
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+       <name>org.simantics.district.region.ui</name>
+       <comment></comment>
+       <projects>
+       </projects>
+       <buildSpec>
+               <buildCommand>
+                       <name>org.eclipse.jdt.core.javabuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.pde.ManifestBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.pde.SchemaBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+       </buildSpec>
+       <natures>
+               <nature>org.eclipse.pde.PluginNature</nature>
+               <nature>org.eclipse.jdt.core.javanature</nature>
+       </natures>
+</projectDescription>
diff --git a/org.simantics.district.region.ui/META-INF/MANIFEST.MF b/org.simantics.district.region.ui/META-INF/MANIFEST.MF
new file mode 100644 (file)
index 0000000..1d45878
--- /dev/null
@@ -0,0 +1,23 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Simantics Diagram Region UI
+Bundle-SymbolicName: org.simantics.district.region.ui;singleton:=true
+Bundle-Version: 1.0.0.qualifier
+Bundle-Activator: org.simantics.district.region.ui.Activator
+Require-Bundle: org.eclipse.ui,
+ org.eclipse.core.runtime,
+ org.eclipse.e4.ui.di,
+ org.eclipse.e4.ui.model.workbench,
+ org.simantics,
+ org.simantics.district.region,
+ org.eclipse.e4.core.di.annotations,
+ org.simantics.ui,
+ org.eclipse.e4.ui.workbench,
+ org.slf4j.api,
+ org.simantics.g2d,
+ org.simantics.diagram,
+ org.simantics.modeling,
+ org.eclipse.e4.ui.services
+Bundle-RequiredExecutionEnvironment: JavaSE-1.8
+Bundle-ActivationPolicy: lazy
+Export-Package: org.simantics.district.region.ui.handlers
diff --git a/org.simantics.district.region.ui/build.properties b/org.simantics.district.region.ui/build.properties
new file mode 100644 (file)
index 0000000..55c54ef
--- /dev/null
@@ -0,0 +1,6 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+               .,\
+               fragment.e4xmi,\
+               plugin.xml
diff --git a/org.simantics.district.region.ui/fragment.e4xmi b/org.simantics.district.region.ui/fragment.e4xmi
new file mode 100644 (file)
index 0000000..76544f9
--- /dev/null
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="ASCII"?>
+<fragment:ModelFragments xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:commands="http://www.eclipse.org/ui/2010/UIModel/application/commands" xmlns:fragment="http://www.eclipse.org/ui/2010/UIModel/fragment" xmlns:menu="http://www.eclipse.org/ui/2010/UIModel/application/ui/menu" xmi:id="_BxaXACerEeWxCPrV0pAZQQ">
+  <fragments xsi:type="fragment:StringModelFragment" xmi:id="_n2jsIMq-EeeUz6Cs9kKeKg" featurename="categories" parentElementId="xpath:/"/>
+  <fragments xsi:type="fragment:StringModelFragment" xmi:id="_4fyr4MqVEeeUz6Cs9kKeKg" featurename="commands" parentElementId="xpath:/">
+    <elements xsi:type="commands:Command" xmi:id="_x1nG0H4WEei74IuRct85qQ" elementId="org.simantics.district.region.ui.commands.openDiagramRegionsTable" commandName="Open Diagram Regions Table"/>
+    <elements xsi:type="commands:Command" xmi:id="_TRDvIJVeEeiLv8HYhYbUCQ" elementId="org.simantics.district.region.ui.command.createRegionFromSelection" commandName="Create Region From Selection"/>
+    <elements xsi:type="commands:Command" xmi:id="_lohj8JWFEeiLv8HYhYbUCQ" elementId="org.simantics.district.region.ui.commands.zoomToRegion" commandName="Zoom To Region"/>
+    <elements xsi:type="commands:Command" xmi:id="_8J3YAJWHEeiLv8HYhYbUCQ" elementId="org.simantics.district.region.ui.command.removeRegion" commandName="Remove Region"/>
+  </fragments>
+  <fragments xsi:type="fragment:StringModelFragment" xmi:id="_1Ds_gMqVEeeUz6Cs9kKeKg" featurename="handlers" parentElementId="xpath:/">
+    <elements xsi:type="commands:Handler" xmi:id="_1G5RYH4WEei74IuRct85qQ" elementId="fi.apros.visualization.table.handlers.openPropertyTable" contributionURI="bundleclass://org.simantics.district.region.ui/org.simantics.district.region.ui.OpenDiagramRegionsTable" command="_x1nG0H4WEei74IuRct85qQ"/>
+    <elements xsi:type="commands:Handler" xmi:id="_WFPuAJVeEeiLv8HYhYbUCQ" elementId="org.simantics.district.region.ui.handler.createRegionFromSelection" contributionURI="bundleclass://org.simantics.district.region.ui/org.simantics.district.region.ui.CreateRegionFromSelection" command="_TRDvIJVeEeiLv8HYhYbUCQ"/>
+    <elements xsi:type="commands:Handler" xmi:id="_n-ShcJWFEeiLv8HYhYbUCQ" elementId="org.simantics.district.region.ui.handler.zoomToRegion" contributionURI="bundleclass://org.simantics.district.region.ui/org.simantics.district.region.ui.handlers.ZoomToRegionHandler" command="_lohj8JWFEeiLv8HYhYbUCQ"/>
+    <elements xsi:type="commands:Handler" xmi:id="_9o_BYJWHEeiLv8HYhYbUCQ" elementId="org.simantics.district.region.ui.handler.removeRegion" contributionURI="bundleclass://org.simantics.district.region.ui/org.simantics.district.region.ui.handlers.RemoveRegionHandler" command="_8J3YAJWHEeiLv8HYhYbUCQ"/>
+  </fragments>
+  <fragments xsi:type="fragment:StringModelFragment" xmi:id="_Fso08MrIEeeUz6Cs9kKeKg" featurename="toolBarContributions" parentElementId="xpath:/"/>
+  <fragments xsi:type="fragment:StringModelFragment" xmi:id="_WNB8EH4oEei74IuRct85qQ" featurename="trimContributions" parentElementId="xpath:/">
+    <elements xsi:type="menu:TrimContribution" xmi:id="_ZW7FMH4oEei74IuRct85qQ" elementId="fi.apros.visualization.table.trimcontribution.0" parentId="org.eclipse.ui.main.toolbar">
+      <children xsi:type="menu:ToolBar" xmi:id="_tRL0MH4oEei74IuRct85qQ" elementId="fi.apros.visualization.table.toolbar.0">
+        <children xsi:type="menu:HandledToolItem" xmi:id="_uWPXwH4oEei74IuRct85qQ" elementId="org.simantics.diagram.regions.table.handledtoolitem.openDiagramRegionsTable" label="Open Diagram Regions Table" iconURI="platform:/plugin/com.famfamfam.silk/icons/table_edit.png" command="_x1nG0H4WEei74IuRct85qQ"/>
+      </children>
+    </elements>
+  </fragments>
+  <fragments xsi:type="fragment:StringModelFragment" xmi:id="_YrKV8H4pEei74IuRct85qQ" featurename="menuContributions" parentElementId="xpath:/">
+    <elements xsi:type="menu:MenuContribution" xmi:id="_azWxgH4pEei74IuRct85qQ" elementId="fi.apros.visualization.table.menucontribution.0" parentId="popup:#AprosDiagramPopup">
+      <children xsi:type="menu:HandledMenuItem" xmi:id="_laBOMH4pEei74IuRct85qQ" elementId="org.simantics.diagram.regions.table.handledmenuitem.opendiagramregionstable" label="Open Diagram Regions Table" command="_x1nG0H4WEei74IuRct85qQ"/>
+    </elements>
+    <elements xsi:type="menu:MenuContribution" xmi:id="_a9GXALGoEeio951ihPslkA" elementId="org.simantics.district.region.ui.menucontribution.regionTableContextMenu" parentId="org.simantics.district.region.ui.contextMenu">
+      <children xsi:type="menu:HandledMenuItem" xmi:id="_eXZ6sLGoEeio951ihPslkA" elementId="org.simantics.district.region.ui.handledmenuitem.zoomtoregion" label="Zoom To Region" iconURI="platform:/plugin/com.famfamfam.silk/icons/zoom.png" command="_lohj8JWFEeiLv8HYhYbUCQ"/>
+      <children xsi:type="menu:HandledMenuItem" xmi:id="_iB2zMLGoEeio951ihPslkA" elementId="org.simantics.district.region.ui.handledmenuitem.removeregion" label="Remove Region" iconURI="platform:/plugin/com.famfamfam.silk/icons/layout_delete.png" command="_8J3YAJWHEeiLv8HYhYbUCQ"/>
+    </elements>
+  </fragments>
+</fragment:ModelFragments>
diff --git a/org.simantics.district.region.ui/plugin.xml b/org.simantics.district.region.ui/plugin.xml
new file mode 100644 (file)
index 0000000..3fba72d
--- /dev/null
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<?eclipse version="3.4"?>
+<plugin>
+   <extension
+         id="org.simantics.district.region.table.fragment"
+         point="org.eclipse.e4.workbench.model">
+      <fragment
+            apply="initial"
+            uri="fragment.e4xmi">
+      </fragment>
+   </extension>
+   <extension
+         point="org.eclipse.ui.views">
+      <e4view
+            class="org.simantics.district.region.ui.DiagramRegionsTableView"
+            icon="platform:/plugin/com.famfamfam.silk/icons/table_edit.png"
+            id="org.simantics.diagram.regions.table.diagramRegionsTable"
+            name="Diagram Regions Table"
+            restorable="true"
+            allowMultiple="false">
+      </e4view>
+   </extension>
+</plugin>
diff --git a/org.simantics.district.region.ui/src/org/simantics/district/region/ui/Activator.java b/org.simantics.district.region.ui/src/org/simantics/district/region/ui/Activator.java
new file mode 100644 (file)
index 0000000..501e6da
--- /dev/null
@@ -0,0 +1,50 @@
+package org.simantics.district.region.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.district.region.ui"; //$NON-NLS-1$
+
+       // 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.district.region.ui/src/org/simantics/district/region/ui/CreateRegionFromSelection.java b/org.simantics.district.region.ui/src/org/simantics/district/region/ui/CreateRegionFromSelection.java
new file mode 100644 (file)
index 0000000..33759e2
--- /dev/null
@@ -0,0 +1,84 @@
+package org.simantics.district.region.ui;
+
+import java.awt.geom.Rectangle2D;
+import java.util.Set;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.e4.core.di.annotations.CanExecute;
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
+import org.eclipse.jface.dialogs.InputDialog;
+import org.eclipse.jface.window.Window;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.progress.UIJob;
+import org.simantics.Simantics;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.request.Write;
+import org.simantics.diagram.ui.DiagramModelHints;
+import org.simantics.district.region.DiagramRegions;
+import org.simantics.g2d.canvas.ICanvasContext;
+import org.simantics.g2d.diagram.IDiagram;
+import org.simantics.g2d.diagram.participant.Selection;
+import org.simantics.g2d.element.ElementUtils;
+import org.simantics.g2d.element.IElement;
+import org.simantics.utils.ui.workbench.WorkbenchUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class CreateRegionFromSelection {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(CreateRegionFromSelection.class);
+
+    @CanExecute
+    public boolean canExecute(ESelectionService selectionService) {
+        return selectionService.getSelection() != null;
+    }
+
+    @Execute
+    public void createRegionFromSelection() {
+        IEditorPart activeEditor = WorkbenchUtils.getActiveEditor();
+        ICanvasContext context = activeEditor.getAdapter(ICanvasContext.class);
+        if (context != null) {
+            IDiagram diagram = activeEditor.getAdapter(IDiagram.class);
+            if (diagram != null) {
+                Resource diagramResource = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
+                Selection selection = context.getAtMostOneItemOfClass(Selection.class);
+                Set<IElement> allSelections = selection.getAllSelections();
+                Rectangle2D surroundingElementBoundsOnDiagram = ElementUtils.getSurroundingElementBoundsOnDiagram(allSelections);
+                double[] coordinates = DiagramRegions.shapeToCoordinates(surroundingElementBoundsOnDiagram);
+                
+                UIJob job = new UIJob("Creating new region for diagram") {
+                    
+                    @Override
+                    public IStatus runInUIThread(IProgressMonitor monitor) {
+                        InputDialog d = new InputDialog(getDisplay().getActiveShell(), "Enter label for region", "Enter region label", "", null);
+                        int status = d.open();
+                        if (status == Window.OK) {
+                            String label = d.getValue();
+                            Simantics.getSession().asyncRequest(new Write() {
+                                
+                                @Override
+                                public void perform(WriteGraph graph) throws DatabaseException {
+                                    DiagramRegions.createRegionForDiagram(graph, diagramResource, label, coordinates);
+                                }
+                            });
+                        } else {
+                            
+                        }
+                        return Status.OK_STATUS;
+                    }
+                };
+                job.setUser(true);
+                job.schedule();
+            } else {
+                LOGGER.warn("No diagram available for active editor {}", activeEditor);
+            }
+        } else {
+            LOGGER.warn("No CanvasContext available for active editor {}", activeEditor);
+        }
+    }
+}
diff --git a/org.simantics.district.region.ui/src/org/simantics/district/region/ui/DiagramRegionsTableUI.java b/org.simantics.district.region.ui/src/org/simantics/district/region/ui/DiagramRegionsTableUI.java
new file mode 100644 (file)
index 0000000..f4b6397
--- /dev/null
@@ -0,0 +1,170 @@
+package org.simantics.district.region.ui;
+
+import java.awt.geom.Rectangle2D;
+import java.text.DecimalFormat;
+import java.util.Collection;
+import java.util.Set;
+
+import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
+import org.eclipse.jface.viewers.ColumnLabelProvider;
+import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
+import org.eclipse.jface.viewers.DoubleClickEvent;
+import org.eclipse.jface.viewers.IDoubleClickListener;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.ITreeContentProvider;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.TreeSelection;
+import org.eclipse.jface.viewers.TreeViewer;
+import org.eclipse.jface.viewers.TreeViewerColumn;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.layout.FillLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Tree;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.common.procedure.adapter.SyncListenerAdapter;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.district.region.DiagramRegions;
+import org.simantics.district.region.DiagramRegions.DiagramRegion;
+import org.simantics.district.region.ui.handlers.ZoomToRegionHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DiagramRegionsTableUI extends Composite {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(DiagramRegionsTableUI.class);
+    
+    private TreeViewer viewer;
+    private TreeViewerColumn column1;
+    private TreeViewerColumn column2;
+
+    private ESelectionService selectionService;
+
+    public DiagramRegionsTableUI(ESelectionService selectionService, Composite parent, int style) {
+        super(parent, style);
+        this.selectionService = selectionService;
+        parent.setLayout(new FillLayout());
+//        GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
+//        GridLayoutFactory.fillDefaults().numColumns(1).applyTo(this);
+        this.setLayout(new FillLayout());
+        Simantics.getSession().asyncRequest(new DiagramRegions.DiagramRegionsRequest(), new SyncListenerAdapter<Collection<DiagramRegion>>() {
+
+            public void execute(ReadGraph graph, Collection<DiagramRegion> result) {
+                parent.getDisplay().asyncExec(() -> {
+                    viewer.setInput(result);
+                });
+            }
+            
+            @Override
+            public void exception(ReadGraph graph, Throwable t) throws DatabaseException {
+                LOGGER.error("Could not listen", t);
+            }
+
+            @Override
+            public boolean isDisposed() {
+                return DiagramRegionsTableUI.this.isDisposed();
+            }
+        });
+        
+        viewer = new TreeViewer(this, SWT.FULL_SELECTION);
+        viewer.addSelectionChangedListener(new ISelectionChangedListener() {
+            
+            @Override
+            public void selectionChanged(SelectionChangedEvent event) {
+                DiagramRegionsTableUI.this.selectionService.setSelection(event.getSelection());
+            }
+        });
+        viewer.addDoubleClickListener(new IDoubleClickListener() {
+            
+            @Override
+            public void doubleClick(DoubleClickEvent event) {
+                LOGGER.info("event " +  event);
+                TreeSelection selection = (TreeSelection) viewer.getSelection();
+                DiagramRegion region = (DiagramRegion) selection.getFirstElement();
+                ZoomToRegionHandler.zoomTo(viewer.getControl(), getDisplay(), region.getResource());
+            }
+        });
+        viewer.setContentProvider(new ITreeContentProvider() {
+            
+            @Override
+            public boolean hasChildren(Object element) {
+                return false;
+            }
+            
+            @Override
+            public Object getParent(Object element) {
+                return null;
+            }
+            
+            @Override
+            public Object[] getElements(Object inputElement) {
+                if (inputElement == null && (!(inputElement instanceof Collection)))
+                    return new Object[0];
+                Set<DiagramRegion> set = (Set) inputElement;
+                return set.toArray();
+            }
+            
+            @Override
+            public Object[] getChildren(Object parentElement) {
+                return null;
+            }
+            
+            @Override
+            public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+                ITreeContentProvider.super.inputChanged(viewer, oldInput, newInput);
+            }
+        });
+
+        ColumnViewerToolTipSupport.enableFor(viewer);
+
+        Tree table = viewer.getTree();
+        table.setHeaderVisible(true);
+        table.setLinesVisible(true);
+
+        column1 = new TreeViewerColumn(viewer, SWT.NONE);
+        column1.getColumn().setText("Region");
+        column1.getColumn().setWidth(200);
+        column1.getColumn().setResizable(true);
+        column1.setLabelProvider(new ColumnLabelProvider() {
+            @Override
+            public String getText(Object element) {
+                DiagramRegion region = (DiagramRegion) element;
+                return region.getLabel();
+            }
+            @Override
+            public Image getImage(Object element) {
+                return null;
+            }
+        });
+        
+        column2 = new TreeViewerColumn(viewer, SWT.NONE);
+        column2.getColumn().setText("Coordinates");
+        column2.getColumn().setWidth(200);
+        column2.getColumn().setResizable(true);
+        column2.setLabelProvider(new ColumnLabelProvider() {
+            @Override
+            public String getText(Object element) {
+                DiagramRegion region = (DiagramRegion) element;
+                Rectangle2D rect = region.getShape().getBounds2D();
+                DecimalFormat df = new DecimalFormat("0.00");
+                String x = df.format(rect.getX());
+                String y = df.format(rect.getY());
+                String width = df.format(rect.getWidth());
+                String height = df.format(rect.getHeight());
+                return "[x=" + x + ", y=" + y + ", width=" + width + ", height=" + height + "]";
+            }
+            @Override
+            public Image getImage(Object element) {
+                return null;
+            }
+        });
+
+    }
+
+    public Tree getTree() {
+        return viewer.getTree();
+    }
+
+}
diff --git a/org.simantics.district.region.ui/src/org/simantics/district/region/ui/DiagramRegionsTableView.java b/org.simantics.district.region.ui/src/org/simantics/district/region/ui/DiagramRegionsTableView.java
new file mode 100644 (file)
index 0000000..1fff18d
--- /dev/null
@@ -0,0 +1,83 @@
+package org.simantics.district.region.ui;
+
+import java.util.List;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+
+import org.eclipse.e4.ui.di.Focus;
+import org.eclipse.e4.ui.model.application.MApplication;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
+import org.eclipse.e4.ui.model.application.ui.menu.MHandledToolItem;
+import org.eclipse.e4.ui.model.application.ui.menu.MMenuContribution;
+import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
+import org.eclipse.e4.ui.model.application.ui.menu.MPopupMenu;
+import org.eclipse.e4.ui.model.application.ui.menu.MToolBar;
+import org.eclipse.e4.ui.services.EMenuService;
+import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
+import org.eclipse.swt.widgets.Composite;
+
+/**
+ * @author Jani Simomaa
+ */
+public class DiagramRegionsTableView {
+
+    DiagramRegionsTableUI ui;
+
+    /**
+     * See
+     * http://www.vogella.com/tutorials/EclipsePlugin/article.html#adding-eclipse-4-x-parts-to-eclipse-3-x-applications-via-the-code-org-eclipse-ui-views-code-extension-point
+     * 
+     * Cannot contribute toolbar items via fragment at this point
+     */
+    @Inject
+    public void init(MPart part, MApplication app) {
+        MHandledToolItem createHandledToolItem = MMenuFactory.INSTANCE.createHandledToolItem();
+        // Command is contributed via fragment
+        createHandledToolItem.setCommand(app.getCommand("org.simantics.district.region.ui.command.createRegionFromSelection"));
+        createHandledToolItem.setLabel("Create Region From Selection");
+        createHandledToolItem.setIconURI("platform:/plugin/com.famfamfam.silk/icons/table_add.png");
+        MToolBar toolBar = MMenuFactory.INSTANCE.createToolBar();
+        toolBar.setToBeRendered(true);
+        toolBar.getChildren().add(createHandledToolItem);
+        part.setToolbar(toolBar);
+        
+        
+        MPopupMenu createPopupMenu = MMenuFactory.INSTANCE.createPopupMenu();
+        createPopupMenu.setElementId(POPUP_ID);
+        createHandledToolItem.setContributorURI("org.simantics.district.region.ui.contextMenu");
+        
+        List<MMenuContribution> menuContributions = app.getMenuContributions();
+        for (MMenuContribution menuContribution : menuContributions) {
+            if ("org.simantics.district.region.ui.contextMenu".equals(menuContribution.getParentId())) {
+                // ok, add to menu
+                createPopupMenu.getChildren().addAll(menuContribution.getChildren());
+            }
+        }
+        
+        part.getMenus().add(createPopupMenu);
+    }
+
+    public static final String POPUP_ID = "org.simantics.district.region.ui.popup";
+
+    @PostConstruct
+    public void postConstruct(ESelectionService selectionService, Composite parent, EMenuService menuService) {
+        // System.out.println("construct MPTV");
+        this.ui = new DiagramRegionsTableUI(selectionService, parent, 0);
+        menuService.registerContextMenu(this.ui.getTree(), POPUP_ID);
+    }
+
+    @PreDestroy
+    public void dispose() {
+        // System.out.println("dispose MPTV");
+        ui.dispose();
+        ui = null;
+    }
+
+    @Focus
+    public void onFocus() {
+        ui.setFocus();
+    }
+
+}
\ No newline at end of file
diff --git a/org.simantics.district.region.ui/src/org/simantics/district/region/ui/OpenDiagramRegionsTable.java b/org.simantics.district.region.ui/src/org/simantics/district/region/ui/OpenDiagramRegionsTable.java
new file mode 100644 (file)
index 0000000..c434298
--- /dev/null
@@ -0,0 +1,19 @@
+package org.simantics.district.region.ui;
+
+import org.eclipse.e4.core.di.annotations.CanExecute;
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
+import org.simantics.ui.workbench.e4.E4WorkbenchUtils;
+
+public class OpenDiagramRegionsTable {
+
+    @CanExecute
+    public boolean canExecute(ESelectionService selectionService) {
+        return true;
+    }
+
+    @Execute
+    public void openPropertyTable(ESelectionService selectionService) {
+        E4WorkbenchUtils.openAndShowPart("org.simantics.diagram.regions.diagramRegionsTable");
+    }
+}
diff --git a/org.simantics.district.region.ui/src/org/simantics/district/region/ui/handlers/RemoveRegionHandler.java b/org.simantics.district.region.ui/src/org/simantics/district/region/ui/handlers/RemoveRegionHandler.java
new file mode 100644 (file)
index 0000000..46b11a2
--- /dev/null
@@ -0,0 +1,39 @@
+package org.simantics.district.region.ui.handlers;
+
+import javax.inject.Named;
+
+import org.eclipse.e4.core.di.annotations.CanExecute;
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
+import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.swt.widgets.Shell;
+import org.simantics.Simantics;
+import org.simantics.db.request.Write;
+import org.simantics.district.region.DiagramRegions;
+import org.simantics.district.region.DiagramRegions.DiagramRegion;
+import org.simantics.utils.ui.ISelectionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class RemoveRegionHandler {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(RemoveRegionHandler.class);
+    public static final String COMMAND_ID = "org.simantics.district.region.ui.command.removeRegion";
+    public static final String LABEL = "Remove Region";
+
+    @CanExecute
+    public boolean canExecute() {
+        return true;
+    }
+
+    @Execute
+    public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell activeShell,
+            @Named(IServiceConstants.ACTIVE_PART) MPart part,
+            @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
+        DiagramRegion region = ISelectionUtils.filterSingleSelection(selection, DiagramRegion.class);
+        Simantics.getSession().asyncRequest((Write) graph -> {
+            DiagramRegions.removeRegion(graph, region.getResource());
+        });
+    }
+}
diff --git a/org.simantics.district.region.ui/src/org/simantics/district/region/ui/handlers/ZoomToRegionHandler.java b/org.simantics.district.region.ui/src/org/simantics/district/region/ui/handlers/ZoomToRegionHandler.java
new file mode 100644 (file)
index 0000000..47290bd
--- /dev/null
@@ -0,0 +1,124 @@
+package org.simantics.district.region.ui.handlers;
+
+import java.awt.Shape;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import javax.inject.Named;
+
+import org.eclipse.e4.core.di.annotations.CanExecute;
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
+import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbenchPart;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.district.region.DiagramRegions;
+import org.simantics.district.region.DiagramRegions.DiagramRegion;
+import org.simantics.g2d.canvas.ICanvasContext;
+import org.simantics.g2d.diagram.IDiagram;
+import org.simantics.g2d.diagram.handler.PickRequest;
+import org.simantics.g2d.diagram.handler.impl.PickContextImpl;
+import org.simantics.g2d.element.ElementHints;
+import org.simantics.g2d.element.IElement;
+import org.simantics.modeling.actions.NavigateToTarget;
+import org.simantics.ui.workbench.action.DefaultActions;
+import org.simantics.utils.threads.ThreadUtils;
+import org.simantics.utils.ui.ISelectionUtils;
+import org.simantics.utils.ui.workbench.WorkbenchUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ZoomToRegionHandler {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(ZoomToRegionHandler.class);
+    public static final String COMMAND_ID = "org.simantics.district.region.ui.commands.zoomToRegion";
+    public static final String LABEL = "Zoom To Region";
+    
+    @CanExecute
+    public boolean canExecute() {
+        return true;
+    }
+    
+    @Execute
+    public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell activeShell,
+            @Named(IServiceConstants.ACTIVE_PART) MPart part,
+            @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
+        DiagramRegion region = ISelectionUtils.filterSingleSelection(selection, DiagramRegion.class);
+        zoomTo(part, activeShell.getDisplay(), region.getResource());
+    }
+
+    public static void zoomTo(MPart part, Display display, Resource region) {
+        Resource dia;
+        try {
+            dia = Simantics.getSession().syncRequest(new UniqueRead<Resource>() {
+                
+                @Override
+                public Resource perform(ReadGraph graph) throws DatabaseException {
+                    return DiagramRegions.getDiagramForRegion(graph, region);
+                }
+            });
+            
+            // get currentyl active workbench part
+            IWorkbenchPart beforePerformingDefaultAction = WorkbenchUtils.getActiveWorkbenchPart();
+            IEditorPart activeEditor = WorkbenchUtils.getActiveEditor();
+            LOGGER.info("activeWorkbenchPart {}", beforePerformingDefaultAction);
+            LOGGER.info("activeEditor {}", activeEditor);
+            DefaultActions.performDefaultAction((Control)part.getWidget(), dia);
+            Simantics.getSession().asyncRequest(new ReadRequest() {
+                
+                @Override
+                public void run(ReadGraph graph) throws DatabaseException {
+                    Shape shp = DiagramRegions.coordinatesToShape(graph, region);
+                    display.asyncExec(() -> {
+                        IWorkbenchPart afterPerformingDefaultAction = WorkbenchUtils.getActiveWorkbenchPart();
+                        LOGGER.info("afterActiveWorkbenchPart {}", afterPerformingDefaultAction);
+                        int tries = 0;
+                        while (Objects.equals(beforePerformingDefaultAction, afterPerformingDefaultAction) && tries <= 10) {
+                            try {
+                                Thread.sleep(150);
+                            } catch (InterruptedException e) {
+                                e.printStackTrace();
+                            }
+                            afterPerformingDefaultAction = WorkbenchUtils.getActiveEditor();
+                            LOGGER.info("afterActiveWorkbenchPart {}", afterPerformingDefaultAction);
+                            tries++;
+                        }
+                        LOGGER.info("afterActiveWorkbenchPart {}", afterPerformingDefaultAction);
+                        if (beforePerformingDefaultAction.equals(afterPerformingDefaultAction)) {
+                            LOGGER.warn("No can do");
+                        } else if (afterPerformingDefaultAction != null) {
+                            // Okay, get the IDiagram now
+                            IDiagram diagram = afterPerformingDefaultAction.getAdapter(IDiagram.class);
+                            if (diagram == null)
+                                LOGGER.warn("No can do - no IDiagram available");
+                            else {
+                                Set<IElement> elems = new HashSet<>();
+                                PickContextImpl.INSTANCE.pick(diagram, new PickRequest(shp), elems);
+                                ICanvasContext context = afterPerformingDefaultAction.getAdapter(ICanvasContext.class);
+                                List<Object> collect = elems.stream().map(el -> el.getHint(ElementHints.KEY_OBJECT)).collect(Collectors.toList());
+                                ThreadUtils.asyncExec(context.getThreadAccess(), NavigateToTarget.elementSelectorZoomer(context, collect, false));
+                            }
+                        } else {
+                            LOGGER.info("afterPerforming is null");
+                        }
+                    });
+                }
+            });
+        } catch (DatabaseException e) {
+            e.printStackTrace();
+        }
+    }
+}
diff --git a/org.simantics.district.region/.classpath b/org.simantics.district.region/.classpath
new file mode 100644 (file)
index 0000000..eca7bdb
--- /dev/null
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
+       <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+       <classpathentry kind="src" path="src"/>
+       <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/org.simantics.district.region/.project b/org.simantics.district.region/.project
new file mode 100644 (file)
index 0000000..8879936
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+       <name>org.simantics.district.region</name>
+       <comment></comment>
+       <projects>
+       </projects>
+       <buildSpec>
+               <buildCommand>
+                       <name>org.eclipse.jdt.core.javabuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.pde.ManifestBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.pde.SchemaBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+       </buildSpec>
+       <natures>
+               <nature>org.eclipse.pde.PluginNature</nature>
+               <nature>org.eclipse.jdt.core.javanature</nature>
+       </natures>
+</projectDescription>
diff --git a/org.simantics.district.region/META-INF/MANIFEST.MF b/org.simantics.district.region/META-INF/MANIFEST.MF
new file mode 100644 (file)
index 0000000..0e4fe90
--- /dev/null
@@ -0,0 +1,19 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Simantics District Region
+Bundle-SymbolicName: org.simantics.district.region
+Bundle-Version: 1.0.0.qualifier
+Bundle-Activator: org.simantics.district.region.Activator
+Require-Bundle: org.eclipse.core.runtime,
+ org.slf4j.api,
+ org.simantics.db,
+ org.simantics.district.region.ontology,
+ org.simantics.layer0;bundle-version="1.1.0",
+ org.simantics.db.layer0,
+ org.simantics.diagram.ontology,
+ org.simantics,
+ org.simantics.modeling.ontology,
+ org.simantics.modeling
+Bundle-RequiredExecutionEnvironment: JavaSE-1.8
+Bundle-ActivationPolicy: lazy
+Export-Package: org.simantics.district.region
diff --git a/org.simantics.district.region/build.properties b/org.simantics.district.region/build.properties
new file mode 100644 (file)
index 0000000..a4fd10d
--- /dev/null
@@ -0,0 +1,5 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+               .,\
+               scl/
diff --git a/org.simantics.district.region/scl/Simantics/Diagram/Regions.scl b/org.simantics.district.region/scl/Simantics/Diagram/Regions.scl
new file mode 100644 (file)
index 0000000..d1ecf10
--- /dev/null
@@ -0,0 +1,4 @@
+import "Simantics/Diagram"
+
+importJava "org.simantics.district.region.DiagramRegions" where
+    createRegionForDiagram :: Resource -> String -> Vector Double -> <WriteGraph, Proc> Resource
\ No newline at end of file
diff --git a/org.simantics.district.region/src/org/simantics/district/region/Activator.java b/org.simantics.district.region/src/org/simantics/district/region/Activator.java
new file mode 100644 (file)
index 0000000..2ed5d3a
--- /dev/null
@@ -0,0 +1,30 @@
+package org.simantics.district.region;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+public class Activator implements BundleActivator {
+
+       private static BundleContext context;
+
+       static BundleContext getContext() {
+               return context;
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
+        */
+       public void start(BundleContext bundleContext) throws Exception {
+               Activator.context = bundleContext;
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
+        */
+       public void stop(BundleContext bundleContext) throws Exception {
+               Activator.context = null;
+       }
+
+}
diff --git a/org.simantics.district.region/src/org/simantics/district/region/DiagramRegions.java b/org.simantics.district.region/src/org/simantics/district/region/DiagramRegions.java
new file mode 100644 (file)
index 0000000..57f31ae
--- /dev/null
@@ -0,0 +1,175 @@
+package org.simantics.district.region;
+
+import java.awt.Shape;
+import java.awt.geom.FlatteningPathIterator;
+import java.awt.geom.Path2D;
+import java.awt.geom.PathIterator;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+
+import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.QueryIndexUtils;
+import org.simantics.db.layer0.request.PossibleActiveModel;
+import org.simantics.db.layer0.util.RemoverUtil;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.district.region.ontology.DiagramRegionsResource;
+import org.simantics.layer0.Layer0;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class DiagramRegions {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(DiagramRegions.class);
+
+    public static Resource createRegionForDiagram(WriteGraph graph, Resource diagram, String label, double[] coordinates) throws DatabaseException {
+        if (coordinates == null || coordinates.length < 6 || coordinates.length % 2 != 0)
+            throw new IllegalArgumentException("Provided coordinates are not valid : " + Arrays.toString(coordinates));
+        Path2D path = new Path2D.Double();
+        double startX = coordinates[0];
+        double startY = coordinates[1];
+        path.moveTo(startX, startY);
+        for (int i = 2; i < coordinates.length; i++)
+            path.lineTo(coordinates[i], coordinates[++i]);
+
+        path.closePath(); // might not be needed
+        DiagramRegionsResource DRR = DiagramRegionsResource.getInstance(graph);
+        Layer0 L0 = Layer0.getInstance(graph);
+        Resource region = graph.newResource();
+        graph.claim(region, L0.InstanceOf, DRR.Region);
+        graph.claim(diagram, DRR.hasRegion, region);
+        graph.claim(region, DRR.regionOf, diagram);
+        graph.claimLiteral(region, L0.HasName, UUID.randomUUID().toString(), Bindings.STRING);
+        graph.claimLiteral(region, L0.HasLabel, label, Bindings.STRING);
+        graph.claimLiteral(region, DRR.Region_area, coordinates, Bindings.DOUBLE_ARRAY);
+        if (LOGGER.isDebugEnabled())
+            LOGGER.debug("Created region for diagram {} with label {} and area {}", diagram, label, Arrays.toString(coordinates));
+        return region;
+    }
+
+    public static void removeRegion(WriteGraph graph, Resource region) throws DatabaseException {
+        RemoverUtil.remove(graph, region);
+    }
+
+    public static class DiagramRegion {
+        
+        private Resource resource;
+        private String label;
+        private Shape shape;
+        
+        public DiagramRegion(Resource resource, String label, Shape coordinatesToShape) {
+            this.resource = resource;
+            this.label = label;
+            this.shape = coordinatesToShape;
+        }
+        
+        public Resource getResource() {
+            return resource;
+        }
+        
+        public String getLabel() {
+            return label;
+        }
+        
+        public Shape getShape() {
+            return shape;
+        }
+    }
+    
+    public static Collection<DiagramRegion> findRegions(ReadGraph graph, Resource model) throws DatabaseException {
+        List<Resource> diagrams = QueryIndexUtils.searchByType(graph, model, DiagramResource.getInstance(graph).Diagram);
+        Set<DiagramRegion> regions = new HashSet<>();
+        
+        Layer0 L0 = Layer0.getInstance(graph);
+        
+        for (Resource diagram : diagrams) {
+            Collection<Resource> region = graph.getObjects(diagram, DiagramRegionsResource.getInstance(graph).hasRegion);
+            for (Resource r : region) {
+                String label = graph.getRelatedValue(r, L0.HasLabel, Bindings.STRING);
+                Shape coordinatesToShape = DiagramRegions.coordinatesToShape(graph, r);
+                regions.add(new DiagramRegion(r, label, coordinatesToShape));
+            }
+        }
+        return regions;
+    }
+    
+    public static class DiagramRegionsRequest extends UniqueRead<Collection<DiagramRegion>> {
+
+        @Override
+        public Collection<DiagramRegion> perform(ReadGraph graph) throws DatabaseException {
+            return findRegions(graph, graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource())));
+        }
+    }
+
+    public static Shape coordinatesToShape(ReadGraph graph, Resource region) throws DatabaseException {
+        DiagramRegionsResource DRR = DiagramRegionsResource.getInstance(graph);
+        double[] coordinates = graph.getRelatedValue(region, DRR.Region_area, Bindings.DOUBLE_ARRAY);
+        Path2D path = new Path2D.Double();
+        double startX = coordinates[0];
+        double startY = coordinates[1];
+        path.moveTo(startX, startY);
+        for (int i = 2; i < coordinates.length; i++)
+            path.lineTo(coordinates[i], coordinates[++i]);
+
+        path.closePath(); // might not be needed
+        return path;
+    }
+
+    public static double[] shapeToCoordinates(Shape shape) {
+        PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), 1);
+        double points[] = new double[6];
+        double moveX = 0, moveY = 0;
+        double thisX = 0, thisY = 0;
+        int type = 0;
+
+        List<Double> coords = new ArrayList<>();
+        // float factor = 1;
+
+        while (!it.isDone()) {
+            type = it.currentSegment(points);
+            switch (type) {
+            case PathIterator.SEG_MOVETO:
+                moveX = points[0];
+                moveY = points[1];
+                coords.add(moveX);
+                coords.add(moveY);
+                break;
+
+            case PathIterator.SEG_CLOSE:
+                points[0] = moveX;
+                points[1] = moveY;
+                // Fall into....
+
+            case PathIterator.SEG_LINETO:
+                thisX = points[0];
+                thisY = points[1];
+                coords.add(thisX);
+                coords.add(thisY);
+                break;
+            }
+            it.next();
+        }
+        double[] finalCoords = new double[coords.size()];
+        for (int i = 0; i < coords.size(); i++) {
+            double d = coords.get(i);
+            finalCoords[i] = d;
+        }
+        return finalCoords;
+    }
+
+    public static Resource getDiagramForRegion(ReadGraph graph, Resource region) throws DatabaseException {
+        DiagramRegionsResource DRR = DiagramRegionsResource.getInstance(graph);
+        Resource diagram = graph.getSingleObject(region, DRR.regionOf);
+        return diagram;
+    }
+}
diff --git a/org.simantics.district.regions.feature/.project b/org.simantics.district.regions.feature/.project
new file mode 100644 (file)
index 0000000..4b7cafe
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+       <name>org.simantics.district.regions.feature</name>
+       <comment></comment>
+       <projects>
+       </projects>
+       <buildSpec>
+               <buildCommand>
+                       <name>org.eclipse.pde.FeatureBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+       </buildSpec>
+       <natures>
+               <nature>org.eclipse.pde.FeatureNature</nature>
+       </natures>
+</projectDescription>
diff --git a/org.simantics.district.regions.feature/build.properties b/org.simantics.district.regions.feature/build.properties
new file mode 100644 (file)
index 0000000..64f93a9
--- /dev/null
@@ -0,0 +1 @@
+bin.includes = feature.xml
diff --git a/org.simantics.district.regions.feature/feature.xml b/org.simantics.district.regions.feature/feature.xml
new file mode 100644 (file)
index 0000000..e7eb621
--- /dev/null
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<feature
+      id="org.simantics.district.regions.feature"
+      label="Simantics District Regions Feature"
+      version="1.0.0.qualifier">
+
+   <description url="http://www.example.com/description">
+      [Enter Feature Description here.]
+   </description>
+
+   <copyright url="http://www.example.com/copyright">
+      [Enter Copyright Description here.]
+   </copyright>
+
+   <license url="http://www.example.com/license">
+      [Enter License Description here.]
+   </license>
+
+   <plugin
+         id="org.simantics.district.region"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
+   <plugin
+         id="org.simantics.district.region.ontology"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
+   <plugin
+         id="org.simantics.district.region.ui"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
+</feature>