]> gerrit.simantics Code Review - simantics/platform.git/blob
87da411da9362942fe9dc76481078a0873741bd0
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor.handlers;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
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;
29
30 /**
31  * 
32  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
33  *
34  */
35 public class AlignVerticalHandler extends AbstractHandler {
36
37         @Override
38         public Object execute(ExecutionEvent event) throws ExecutionException {
39                 
40                 ISelection s = HandlerUtil.getCurrentSelectionChecked(event);
41                 final Resource resources[] = ResourceAdaptionUtils.toResources(s);
42                 
43                 if (resources.length < 2)
44                         return null;
45                 
46                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
47                         
48                         @Override
49                         public void perform(WriteGraph graph) throws DatabaseException {
50                                 DiagramResource dr = DiagramResource.getInstance(graph);
51                                 
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) {
59                                                         elements.add(r);
60                                                         transforms.add(transform);
61                                                 }
62                                         }
63                                 }
64                                 double mx = 0;
65                                 double my = 0;
66                                 for (double[] t : transforms) {
67                                         mx += t[4];
68                                         my += t[5];
69                                 }
70                                 mx /= transforms.size();
71                                 my /= transforms.size();
72                                 
73                                 // prevent moving symbols into the same position
74                                 int count = 0;
75                                 for (double t[] : transforms) {
76                                         if (Math.abs(t[5] - my) < 0.1) {
77                                                 count++;
78                                         }
79                                 }
80                                 if (count > 1)
81                                         return;
82                                 
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]});
87                                 }
88                         }
89                 });
90                 return null;
91         }
92
93 }