X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2FdiagramEditor%2Fhandlers%2FAlignVerticalHandler.java;fp=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2FdiagramEditor%2Fhandlers%2FAlignVerticalHandler.java;h=87da411da9362942fe9dc76481078a0873741bd0;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=66f5290e08ddf071cba570c83ffee12229a82374;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/AlignVerticalHandler.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/AlignVerticalHandler.java index 66f5290e0..87da411da 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/AlignVerticalHandler.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/AlignVerticalHandler.java @@ -1,93 +1,93 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 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.modeling.ui.diagramEditor.handlers; - -import java.util.ArrayList; -import java.util.List; - -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.ui.handlers.HandlerUtil; -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.diagram.stubs.DiagramResource; -import org.simantics.ui.SimanticsUI; -import org.simantics.ui.utils.ResourceAdaptionUtils; - -/** - * - * @author Marko Luukkainen - * - */ -public class AlignVerticalHandler extends AbstractHandler { - - @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - - ISelection s = HandlerUtil.getCurrentSelectionChecked(event); - final Resource resources[] = ResourceAdaptionUtils.toResources(s); - - if (resources.length < 2) - return null; - - SimanticsUI.getSession().asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - DiagramResource dr = DiagramResource.getInstance(graph); - - List elements = new ArrayList(); - List transforms = new ArrayList(); - for (Resource r : resources) { - //System.out.println(r + " " + GraphUtils.getReadableName(graph, r)); - if (graph.isInstanceOf(r, dr.Element) && !graph.isInstanceOf(r, dr.Connection)) { - double transform[] = graph.getPossibleRelatedValue(r, dr.HasTransform); - if (transform != null) { - elements.add(r); - transforms.add(transform); - } - } - } - double mx = 0; - double my = 0; - for (double[] t : transforms) { - mx += t[4]; - my += t[5]; - } - mx /= transforms.size(); - my /= transforms.size(); - - // prevent moving symbols into the same position - int count = 0; - for (double t[] : transforms) { - if (Math.abs(t[5] - my) < 0.1) { - count++; - } - } - if (count > 1) - return; - - for (int i = 0; i < transforms.size(); i++) { - Resource element = elements.get(i); - double t[] = transforms.get(i); - graph.claimLiteral(element, dr.HasTransform, new double[]{t[0],t[1],t[2],t[3],mx,t[5]}); - } - } - }); - return null; - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 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.modeling.ui.diagramEditor.handlers; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.ui.handlers.HandlerUtil; +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.diagram.stubs.DiagramResource; +import org.simantics.ui.SimanticsUI; +import org.simantics.ui.utils.ResourceAdaptionUtils; + +/** + * + * @author Marko Luukkainen + * + */ +public class AlignVerticalHandler extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + + ISelection s = HandlerUtil.getCurrentSelectionChecked(event); + final Resource resources[] = ResourceAdaptionUtils.toResources(s); + + if (resources.length < 2) + return null; + + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + DiagramResource dr = DiagramResource.getInstance(graph); + + List elements = new ArrayList(); + List transforms = new ArrayList(); + for (Resource r : resources) { + //System.out.println(r + " " + GraphUtils.getReadableName(graph, r)); + if (graph.isInstanceOf(r, dr.Element) && !graph.isInstanceOf(r, dr.Connection)) { + double transform[] = graph.getPossibleRelatedValue(r, dr.HasTransform); + if (transform != null) { + elements.add(r); + transforms.add(transform); + } + } + } + double mx = 0; + double my = 0; + for (double[] t : transforms) { + mx += t[4]; + my += t[5]; + } + mx /= transforms.size(); + my /= transforms.size(); + + // prevent moving symbols into the same position + int count = 0; + for (double t[] : transforms) { + if (Math.abs(t[5] - my) < 0.1) { + count++; + } + } + if (count > 1) + return; + + for (int i = 0; i < transforms.size(); i++) { + Resource element = elements.get(i); + double t[] = transforms.get(i); + graph.claimLiteral(element, dr.HasTransform, new double[]{t[0],t[1],t[2],t[3],mx,t[5]}); + } + } + }); + return null; + } + +}