1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor.handlers;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.ui.handlers.HandlerUtil;
22 import org.simantics.db.Resource;
23 import org.simantics.db.WriteGraph;
24 import org.simantics.db.common.request.WriteRequest;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.diagram.stubs.DiagramResource;
27 import org.simantics.ui.SimanticsUI;
28 import org.simantics.ui.utils.ResourceAdaptionUtils;
32 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
35 public class AlignHorizontalHandler extends AbstractHandler {
38 public Object execute(ExecutionEvent event) throws ExecutionException {
40 ISelection s = HandlerUtil.getCurrentSelectionChecked(event);
41 final Resource resources[] = ResourceAdaptionUtils.toResources(s);
43 if (resources.length < 2)
46 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
49 public void perform(WriteGraph graph) throws DatabaseException {
50 DiagramResource dr = DiagramResource.getInstance(graph);
52 List<Resource> elements = new ArrayList<Resource>();
53 List<double[]> transforms = new ArrayList<double[]>();
54 for (Resource r : resources) {
55 //System.out.println(r + " " + GraphUtils.getReadableName(graph, r));
56 if (graph.isInstanceOf(r, dr.Element) && !graph.isInstanceOf(r, dr.Connection)) {
58 double transform[] = graph.getPossibleRelatedValue(r, dr.HasTransform);
59 if (transform != null) {
61 transforms.add(transform);
67 for (double[] t : transforms) {
71 mx /= transforms.size();
72 my /= transforms.size();
74 // prevent moving symbols into the same position
76 for (double t[] : transforms) {
77 if (Math.abs(t[4] - mx) < 0.1) {
84 for (int i = 0; i < transforms.size(); i++) {
85 Resource element = elements.get(i);
86 double t[] = transforms.get(i);
87 graph.claimLiteral(element, dr.HasTransform, new double[]{t[0],t[1],t[2],t[3],t[4],my});