From: jsimomaa Date: Tue, 6 Nov 2018 08:03:04 +0000 (+0200) Subject: Enabling/Disabling map painting X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F21%2F2421%2F2;p=simantics%2Fdistrict.git Enabling/Disabling map painting gitlab #15 Change-Id: I034a334c4a47753342f9871ae68551ceda0b1d19 --- diff --git a/org.simantics.district.maps/src/org/simantics/maps/eclipse/MapPainter.java b/org.simantics.district.maps/src/org/simantics/maps/eclipse/MapPainter.java index ee0c71a6..cdf0be15 100644 --- a/org.simantics.district.maps/src/org/simantics/maps/eclipse/MapPainter.java +++ b/org.simantics.district.maps/src/org/simantics/maps/eclipse/MapPainter.java @@ -97,6 +97,11 @@ public class MapPainter extends AbstractCanvasParticipant { updateNode(); setDirty(); return true; + } else if (e.command.equals( Commands.ENABLE_PAINTING )) { + enablePainting(); + updateNode(); + setDirty(); + return true; } return false; } @@ -138,4 +143,7 @@ public class MapPainter extends AbstractCanvasParticipant { setHint(KEY_MAP_ENABLED, enabled); } + private void enablePainting() { + setHint(Hints.KEY_DISABLE_PAINTING, false); + } } diff --git a/org.simantics.district.network.ontology/graph/DistrictNetworkDiagramSettings.pgraph b/org.simantics.district.network.ontology/graph/DistrictNetworkDiagramSettings.pgraph index 0ca711a1..eb2416e9 100644 --- a/org.simantics.district.network.ontology/graph/DistrictNetworkDiagramSettings.pgraph +++ b/org.simantics.district.network.ontology/graph/DistrictNetworkDiagramSettings.pgraph @@ -78,6 +78,10 @@ DN.Diagram L0.HasLabel "Node Scale Bias" >-- DN.Diagram.nodeScaleProperty --> DN.Vertex.ScaleProperty -- DN.Diagram.backgroundColor ==> "String" -- DN.Diagram.drawMapEnabled ==> "Boolean" + - + + + diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java index f3e55671..c6c510e0 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java @@ -2,6 +2,11 @@ package org.simantics.district.network.ui; import java.util.concurrent.TimeUnit; +import org.simantics.db.ReadGraph; +import org.simantics.db.common.request.UniqueRead; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.procedure.Listener; +import org.simantics.district.network.DistrictNetworkUtil; import org.simantics.district.network.ui.participants.DNPointerInteractor; import org.simantics.district.network.ui.participants.MapRulerPainter; import org.simantics.g2d.canvas.ICanvasContext; @@ -19,10 +24,16 @@ import org.simantics.g2d.participant.ZoomToAreaHandler; import org.simantics.maps.MapScalingTransform; import org.simantics.maps.eclipse.MapPainter; import org.simantics.modeling.ui.diagramEditor.DiagramViewer; +import org.simantics.scenegraph.g2d.events.command.CommandEvent; +import org.simantics.scenegraph.g2d.events.command.Commands; import org.simantics.utils.datastructures.hints.IHintContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DistrictDiagramViewer extends DiagramViewer { + private static final Logger LOGGER = LoggerFactory.getLogger(DistrictDiagramViewer.class); + @Override protected void addDiagramParticipants(ICanvasContext ctx) { ctx.add(new ZOrderHandler()); @@ -68,4 +79,33 @@ public class DistrictDiagramViewer extends DiagramViewer { ctx.add(new ZoomToAreaHandler()); } + @Override + protected void loadPageSettings(ICanvasContext ctx) { + super.loadPageSettings(ctx); + // this might be the wrong place to start such listening but at least + // super.loadPageSettings() does async-db-operations + sessionContext.getSession().asyncRequest(new UniqueRead() { + + @Override + public Boolean perform(ReadGraph graph) throws DatabaseException { + return DistrictNetworkUtil.drawMapEnabled(graph, getInputResource()); + } + }, new Listener() { + + @Override + public void execute(Boolean result) { + canvasContext.getEventQueue().queueEvent(new CommandEvent(canvasContext, System.currentTimeMillis(), result ? Commands.MAP_ENABLE : Commands.MAP_DISABLE)); + } + + @Override + public void exception(Throwable t) { + LOGGER.error("Could not listen draw map for {}", getInputResource(), t); + } + + @Override + public boolean isDisposed() { + return DistrictDiagramViewer.this.isDisposed(); + } + }); + } } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/DrawMapToggleHandler.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/DrawMapToggleHandler.java new file mode 100644 index 00000000..ddec27ba --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/DrawMapToggleHandler.java @@ -0,0 +1,58 @@ +package org.simantics.district.network.ui.contributions; + +import java.util.List; + +import javax.inject.Named; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.e4.core.di.annotations.CanExecute; +import org.eclipse.e4.core.di.annotations.Execute; +import org.eclipse.e4.ui.services.IServiceConstants; +import org.eclipse.jface.viewers.ISelection; +import org.simantics.DatabaseJob; +import org.simantics.Simantics; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.SelectionHints; +import org.simantics.district.network.DistrictNetworkUtil; +import org.simantics.district.network.ui.internal.Activator; +import org.simantics.utils.ui.ISelectionUtils; + +public class DrawMapToggleHandler { + + @CanExecute + public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) { + final List diagrams = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class); + return diagrams.size() > 0; + } + + @Execute + public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) { + final List diagrams = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class); + Job job = new DatabaseJob("Toggle draw map") { + + @Override + protected IStatus run(IProgressMonitor monitor) { + try { + Simantics.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + DistrictNetworkUtil.toggleDrawMap(graph, diagrams.get(0)); + } + }); + } catch (DatabaseException e) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e); + } + return Status.OK_STATUS; + } + }; + job.setUser(true); + job.schedule(); + } +} diff --git a/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java index 64aab473..82108517 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java +++ b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java @@ -8,7 +8,10 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.utils.OrderedSetUtils; +import org.simantics.db.exception.BindingException; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; +import org.simantics.db.exception.ServiceException; import org.simantics.db.layer0.request.PossibleVariable; import org.simantics.db.layer0.variable.Variable; import org.simantics.diagram.stubs.DiagramResource; @@ -204,4 +207,17 @@ public class DistrictNetworkUtil { return null; } + public static void toggleDrawMap(WriteGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException { + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN); + if (current == null) + current = true; + graph.claimLiteral(diagram, DN.Diagram_drawMapEnabled, !current, Bindings.BOOLEAN); + } + + public static Boolean drawMapEnabled(ReadGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException { + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN); + return current != null ? current : true; + } } 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 index f4b63978..562dd24b 100644 --- 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 @@ -80,7 +80,6 @@ public class DiagramRegionsTableUI extends Composite { @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()); 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 index 1fff18d1..4ad66198 100644 --- 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 @@ -17,12 +17,16 @@ 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; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @author Jani Simomaa */ public class DiagramRegionsTableView { + private static final Logger LOGGER = LoggerFactory.getLogger(DiagramRegionsTableView.class); + DiagramRegionsTableUI ui; /** @@ -65,7 +69,8 @@ public class DiagramRegionsTableView { 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); + if (!(menuService.registerContextMenu(this.ui.getTree(), POPUP_ID))) + LOGGER.warn("Could not register context menu {}", POPUP_ID); } @PreDestroy 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 index 47290bd7..7a1e105b 100644 --- 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 @@ -57,10 +57,10 @@ public class ZoomToRegionHandler { @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()); + zoomTo((Control) part.getWidget(), activeShell.getDisplay(), region.getResource()); } - public static void zoomTo(MPart part, Display display, Resource region) { + public static void zoomTo(Control control, Display display, Resource region) { Resource dia; try { dia = Simantics.getSession().syncRequest(new UniqueRead() { @@ -76,7 +76,7 @@ public class ZoomToRegionHandler { IEditorPart activeEditor = WorkbenchUtils.getActiveEditor(); LOGGER.info("activeWorkbenchPart {}", beforePerformingDefaultAction); LOGGER.info("activeEditor {}", activeEditor); - DefaultActions.performDefaultAction((Control)part.getWidget(), dia); + DefaultActions.performDefaultAction(control, dia); Simantics.getSession().asyncRequest(new ReadRequest() { @Override @@ -90,7 +90,7 @@ public class ZoomToRegionHandler { try { Thread.sleep(150); } catch (InterruptedException e) { - e.printStackTrace(); + LOGGER.error("Could not sleep", e); } afterPerformingDefaultAction = WorkbenchUtils.getActiveEditor(); LOGGER.info("afterActiveWorkbenchPart {}", afterPerformingDefaultAction); @@ -118,7 +118,7 @@ public class ZoomToRegionHandler { } }); } catch (DatabaseException e) { - e.printStackTrace(); + LOGGER.error("Could not zoom to region", e); } } } 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 index 57f31ae4..98a3ea4f 100644 --- a/org.simantics.district.region/src/org/simantics/district/region/DiagramRegions.java +++ b/org.simantics.district.region/src/org/simantics/district/region/DiagramRegions.java @@ -149,7 +149,7 @@ public final class DiagramRegions { points[0] = moveX; points[1] = moveY; // Fall into.... - + break; case PathIterator.SEG_LINETO: thisX = points[0]; thisY = points[1];