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 AlignVerticalHandler 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)) {
57 double transform[] = graph.getPossibleRelatedValue(r, dr.HasTransform);
58 if (transform != null) {
60 transforms.add(transform);
66 for (double[] t : transforms) {
70 mx /= transforms.size();
71 my /= transforms.size();
73 // prevent moving symbols into the same position
75 for (double t[] : transforms) {
76 if (Math.abs(t[5] - my) < 0.1) {
83 for (int i = 0; i < transforms.size(); i++) {
84 Resource element = elements.get(i);
85 double t[] = transforms.get(i);
86 graph.claimLiteral(element, dr.HasTransform, new double[]{t[0],t[1],t[2],t[3],mx,t[5]});