From: lempinen Date: Thu, 1 Dec 2011 13:52:45 +0000 (+0000) Subject: DnD support to AxisAndVariablesExplorer X-Git-Tag: simantics-1.6~81 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=25464880f209b52359f86d2a6f5e9a5a0ec1b8e2;p=simantics%2Fsysdyn.git DnD support to AxisAndVariablesExplorer git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@23386 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index 4dbbefac..acca8d8f 100644 Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ diff --git a/org.simantics.sysdyn.ontology/graph/ChartAxisAndVariablesViewpoint.pgraph b/org.simantics.sysdyn.ontology/graph/ChartAxisAndVariablesViewpoint.pgraph index e5c72a0c..9d3c5e9a 100644 --- a/org.simantics.sysdyn.ontology/graph/ChartAxisAndVariablesViewpoint.pgraph +++ b/org.simantics.sysdyn.ontology/graph/ChartAxisAndVariablesViewpoint.pgraph @@ -27,8 +27,10 @@ CBC @VP.customLabelRule JFREE.Series CBC.SeriesLabelRule CBC - @VP.dropActionContribution JFREE.Axis ACTIONS.DropAction 1.0 - @VP.dropActionContribution JFREE.Series ACTIONS.DropAction 1.0 + @VP.dropActionContribution JFREE.Axis ACTIONS.SeriesDropAction 1.0 + @VP.dropActionContribution JFREE.Series ACTIONS.SeriesDropAction 1.0 + @VP.dropActionContribution JFREE.Axis ACTIONS.AxisDropAction 2.0 + @VP.dropActionContribution JFREE.Series ACTIONS.AxisDropAction 2.0 CBC VP.BrowseContext.HasVisualsContribution _ : VP.VisualsContribution @@ -40,4 +42,5 @@ CBC ACTIONS = CAC.Actions : L0.Library -ACTIONS.DropAction : ACT.DropAction \ No newline at end of file +ACTIONS.SeriesDropAction : ACT.DropAction +ACTIONS.AxisDropAction : ACT.DropAction \ No newline at end of file diff --git a/org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java b/org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java index bcebfbec..4ce26dbd 100644 --- a/org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java +++ b/org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java @@ -48,7 +48,8 @@ public class SysdynResource { public final Resource Center; public final Resource ChartAxisAndVariablesActionContext; public final Resource ChartAxisAndVariablesActionContext_Actions; - public final Resource ChartAxisAndVariablesActionContext_Actions_DropAction; + public final Resource ChartAxisAndVariablesActionContext_Actions_AxisDropAction; + public final Resource ChartAxisAndVariablesActionContext_Actions_SeriesDropAction; public final Resource ChartAxisAndVariablesBrowseContext; public final Resource ChartAxisAndVariablesBrowseContext_AxisChildRule; public final Resource ChartAxisAndVariablesBrowseContext_AxisLabelRule; @@ -319,7 +320,8 @@ public class SysdynResource { public static final String Center = "http://www.simantics.org/Sysdyn-1.1/Center"; public static final String ChartAxisAndVariablesActionContext = "http://www.simantics.org/Sysdyn-1.1/ChartAxisAndVariablesActionContext"; public static final String ChartAxisAndVariablesActionContext_Actions = "http://www.simantics.org/Sysdyn-1.1/ChartAxisAndVariablesActionContext/Actions"; - public static final String ChartAxisAndVariablesActionContext_Actions_DropAction = "http://www.simantics.org/Sysdyn-1.1/ChartAxisAndVariablesActionContext/Actions/DropAction"; + public static final String ChartAxisAndVariablesActionContext_Actions_AxisDropAction = "http://www.simantics.org/Sysdyn-1.1/ChartAxisAndVariablesActionContext/Actions/AxisDropAction"; + public static final String ChartAxisAndVariablesActionContext_Actions_SeriesDropAction = "http://www.simantics.org/Sysdyn-1.1/ChartAxisAndVariablesActionContext/Actions/SeriesDropAction"; public static final String ChartAxisAndVariablesBrowseContext = "http://www.simantics.org/Sysdyn-1.1/ChartAxisAndVariablesBrowseContext"; public static final String ChartAxisAndVariablesBrowseContext_AxisChildRule = "http://www.simantics.org/Sysdyn-1.1/ChartAxisAndVariablesBrowseContext/AxisChildRule"; public static final String ChartAxisAndVariablesBrowseContext_AxisLabelRule = "http://www.simantics.org/Sysdyn-1.1/ChartAxisAndVariablesBrowseContext/AxisLabelRule"; @@ -600,7 +602,8 @@ public class SysdynResource { Center = getResourceOrNull(graph, URIs.Center); ChartAxisAndVariablesActionContext = getResourceOrNull(graph, URIs.ChartAxisAndVariablesActionContext); ChartAxisAndVariablesActionContext_Actions = getResourceOrNull(graph, URIs.ChartAxisAndVariablesActionContext_Actions); - ChartAxisAndVariablesActionContext_Actions_DropAction = getResourceOrNull(graph, URIs.ChartAxisAndVariablesActionContext_Actions_DropAction); + ChartAxisAndVariablesActionContext_Actions_AxisDropAction = getResourceOrNull(graph, URIs.ChartAxisAndVariablesActionContext_Actions_AxisDropAction); + ChartAxisAndVariablesActionContext_Actions_SeriesDropAction = getResourceOrNull(graph, URIs.ChartAxisAndVariablesActionContext_Actions_SeriesDropAction); ChartAxisAndVariablesBrowseContext = getResourceOrNull(graph, URIs.ChartAxisAndVariablesBrowseContext); ChartAxisAndVariablesBrowseContext_AxisChildRule = getResourceOrNull(graph, URIs.ChartAxisAndVariablesBrowseContext_AxisChildRule); ChartAxisAndVariablesBrowseContext_AxisLabelRule = getResourceOrNull(graph, URIs.ChartAxisAndVariablesBrowseContext_AxisLabelRule); diff --git a/org.simantics.sysdyn.ui/adapters.xml b/org.simantics.sysdyn.ui/adapters.xml index bde90b08..658f0808 100644 --- a/org.simantics.sysdyn.ui/adapters.xml +++ b/org.simantics.sysdyn.ui/adapters.xml @@ -10,5 +10,5 @@ VTT Technical Research Centre of Finland - initial API and implementation --> - none 0 fill 1 + none 0 fill 1 \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/newComponents/NewChartHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/newComponents/NewChartHandler.java index 3bf1b9a7..85d44c57 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/newComponents/NewChartHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/newComponents/NewChartHandler.java @@ -78,10 +78,10 @@ public class NewChartHandler extends AbstractHandler { Resource domainAxis = GraphUtils.create2(g, jfree.NumberAxis, l0.HasName, "NumberAxis" + UUID.randomUUID().toString(), - l0.HasLabel, "domain"); + l0.HasLabel, "Domain"); Resource rangeAxis = GraphUtils.create2(g, jfree.NumberAxis, l0.HasName, "NumberAxis" + UUID.randomUUID().toString(), - l0.HasLabel, "range"); + l0.HasLabel, "Range"); Resource dataset = GraphUtils.create2(g, jfree.XYDataset, l0.HasName, "XYDataset" + UUID.randomUUID().toString(), diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/graphexplorer/AxisDropAction.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/graphexplorer/AxisDropAction.java new file mode 100644 index 00000000..7d3872cc --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/graphexplorer/AxisDropAction.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * 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.trend.chart.graphexplorer; + +import java.util.List; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.utils.ListUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.DropActionFactory; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.JFreeChartResource; +import org.simantics.sysdyn.ui.trend.chart.properties.AxisAndVariablesExplorerComposite; +import org.simantics.ui.SimanticsUI; +import org.simantics.ui.utils.AdaptionUtils; + +/** + * Action for dropping axis on top of other axis or series in {@link AxisAndVariablesExplorerComposite} + * @author Teemu Lempinen + * + */ +public class AxisDropAction implements DropActionFactory { + + @Override + public Runnable create(ReadGraph g, Object target, Object source) throws DatabaseException { + // Make sure that both target and source are resources + Resource t = AdaptionUtils.adaptToSingle(target, Resource.class); + Resource s = AdaptionUtils.adaptToSingle(source, Resource.class); + + if(t == null || s == null) + return null; + + // Make sure that source and target are of correct type + JFreeChartResource jfree = JFreeChartResource.getInstance(g); + if(!g.isInstanceOf(s, jfree.Axis)) + return null; + if(!g.isInstanceOf(t, jfree.Series) && !g.isInstanceOf(t, jfree.Axis)) + return null; + + return getRunnable(t, s); + } + + /** + * Get the runnable for doing the drop action + * + * @param t target resource + * @param s source resource + * @return Runnable + */ + private Runnable getRunnable(final Resource t, final Resource s) { + Runnable runnable = new Runnable() { + + @Override + public void run() { + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + if(t == null || s == null) return; + JFreeChartResource jfree = JFreeChartResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + Resource target = t; + Resource source = s; + + // Dragged axis always exists in the same list with target axis, so it is safe to get the target index + Resource plot = graph.getPossibleObject(source, l0.PartOf); + Resource axisListResource = graph.getPossibleObject(plot, jfree.Plot_rangeAxisList); + List axisList = ListUtils.toList(graph, axisListResource); + if(graph.isInstanceOf(target, jfree.Series)) { + // Dropped a axis over a series -> get the axis of the series + Resource dataset = graph.getPossibleObject(target, l0.PartOf); + target = graph.getPossibleObject(dataset, jfree.Dataset_mapToRangeAxis); + } + + // move axis to target position + int targetIndex = axisList.indexOf(target); + axisList.remove(source); + axisList.add(targetIndex, source); + + // Update the range axis list + graph.deny(plot, jfree.Plot_rangeAxisList); + axisListResource = ListUtils.create(graph, axisList); + graph.claim(plot, jfree.Plot_rangeAxisList, axisListResource); + } + + }); + } + }; + return runnable; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/graphexplorer/DropAction.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/graphexplorer/SeriesDropAction.java similarity index 70% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/graphexplorer/DropAction.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/graphexplorer/SeriesDropAction.java index 7cb33bf4..e0bd0d63 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/graphexplorer/DropAction.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/graphexplorer/SeriesDropAction.java @@ -12,6 +12,7 @@ package org.simantics.sysdyn.ui.trend.chart.graphexplorer; import java.util.Collections; +import java.util.List; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -29,21 +30,30 @@ import org.simantics.ui.utils.AdaptionUtils; /** * Drop action for explorer in {@link ChartAxisAndVariablesTab}. This action is used for dropping - * both series and axis on top of each others. + * both series on axis or another sries * * @author Teemu Lempinen * */ -public class DropAction implements DropActionFactory { +public class SeriesDropAction implements DropActionFactory { @Override public Runnable create(ReadGraph g, Object target, Object source) throws DatabaseException { // Make sure that both target and source are resources Resource t = AdaptionUtils.adaptToSingle(target, Resource.class); Resource s = AdaptionUtils.adaptToSingle(source, Resource.class); - if(t != null && s != null) - return getRunnable(t, s); - return null; + + if(t == null || s == null) + return null; + + // Make sure that source and target are of correct type + JFreeChartResource jfree = JFreeChartResource.getInstance(g); + if(!g.isInstanceOf(s, jfree.Series)) + return null; + if(!g.isInstanceOf(t, jfree.Series) && !g.isInstanceOf(t, jfree.Axis)) + return null; + + return getRunnable(t, s); } /** @@ -67,26 +77,28 @@ public class DropAction implements DropActionFactory { Layer0 l0 = Layer0.getInstance(graph); Resource target = t; Resource source = s; + Resource droppedOnSeries = null; // Dropped a series over a series -> get target dataset - if(graph.isInstanceOf(target, jfree.Series) && graph.isInstanceOf(source, jfree.Series)) { + if(graph.isInstanceOf(target, jfree.Series)) { + droppedOnSeries = target; Resource dataset = graph.getPossibleObject(target, l0.PartOf); if(dataset != null) target = dataset; } // Dropped a series over an axis -> get target dataset - if(graph.isInstanceOf(target, jfree.Axis) && graph.isInstanceOf(source, jfree.Series)) { + if(graph.isInstanceOf(target, jfree.Axis)) { Resource dataset = graph.syncRequest(new PossibleObjectWithType(target, jfree.Dataset_mapToRangeAxis_Inverse, jfree.Dataset)); if(dataset != null) target = dataset; } // Move series to a dataset - if(graph.isInstanceOf(target, jfree.Dataset) && graph.isInstanceOf(source, jfree.Series)) { - // Remove from old dataset + if(graph.isInstanceOf(target, jfree.Dataset)) { + // Remove from old dataset if it was different than the new one Resource sourceDataset = graph.getPossibleObject(source, l0.PartOf); - if(sourceDataset != null) { + if(sourceDataset != null && !sourceDataset.equals(target)) { Resource sourceSeriesList = graph.getPossibleObject(sourceDataset, jfree.Dataset_seriesList); if(sourceSeriesList != null) ListUtils.removeElement(graph, sourceSeriesList, source); @@ -99,13 +111,24 @@ public class DropAction implements DropActionFactory { targetSeriesList = ListUtils.create(graph, Collections.emptyList()); graph.claim(target, jfree.Dataset_seriesList, targetSeriesList); } - - ListUtils.insertBack(graph, targetSeriesList, Collections.singleton(source)); - + + + // Series was dropped on another series. Move the dropped series to that place and recreate the list + if(droppedOnSeries != null) { + List list = ListUtils.toList(graph, targetSeriesList); + int targetIndex = list.indexOf(droppedOnSeries); + if(list.contains(source)) + list.remove(source); + list.add(targetIndex, source); + graph.deny(target, jfree.Dataset_seriesList); + targetSeriesList = ListUtils.create(graph, list); + graph.claim(target, jfree.Dataset_seriesList, targetSeriesList); + } else { + ListUtils.insertFront(graph, targetSeriesList, Collections.singleton(source)); + } + graph.claim(target, l0.ConsistsOf, source); } - - // TODO: Change the order of axis and series } }); }