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.diagram.handler;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.Comparator;
18 import java.util.List;
20 import org.eclipse.jface.action.Action;
21 import org.eclipse.jface.action.ContributionItem;
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.action.IContributionItem;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.SelectionListener;
27 import org.eclipse.swt.widgets.Menu;
28 import org.eclipse.swt.widgets.MenuItem;
29 import org.simantics.databoard.Bindings;
30 import org.simantics.db.ReadGraph;
31 import org.simantics.db.Resource;
32 import org.simantics.db.Session;
33 import org.simantics.db.Statement;
34 import org.simantics.db.WriteGraph;
35 import org.simantics.db.common.CommentMetadata;
36 import org.simantics.db.common.request.IndexRoot;
37 import org.simantics.db.common.request.WriteRequest;
38 import org.simantics.db.exception.DatabaseException;
39 import org.simantics.diagram.content.ConnectionUtil;
40 import org.simantics.diagram.stubs.DiagramResource;
41 import org.simantics.layer0.Layer0;
42 import org.simantics.modeling.ModelingResources;
43 import org.simantics.scl.commands.Command;
44 import org.simantics.scl.commands.Commands;
45 import org.simantics.structural.stubs.StructuralResource2;
46 import org.simantics.ui.contribution.DynamicMenuContribution;
47 import org.simantics.utils.datastructures.Callback;
48 import org.simantics.utils.ui.AdaptionUtils;
49 import org.simantics.utils.ui.ExceptionUtils;
52 * @author Tuukka Lehtonen
54 public class ConnectionRoutingMenuContribution extends DynamicMenuContribution {
57 private static final IContributionItem[] NONE = {};
60 protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {
61 ArrayList<IContributionItem> result = new ArrayList<IContributionItem>(4);
63 result.addAll(getConnectionRoutingItems(graph, selection));
64 result.addAll(getAttachedConnectionRoutingItems(graph, selection));
66 return result.toArray(NONE);
69 public static Collection<IContributionItem> getConnectionRoutingItems(ReadGraph graph, Object[] selection) throws DatabaseException {
70 Layer0 l0 = Layer0.getInstance(graph);
71 DiagramResource dr = DiagramResource.getInstance(graph);
73 final Collection<Resource> routings = graph.getObjects(dr.Routing, l0.SuperrelationOf);
74 if (routings.size() == 0)
75 return Collections.emptyList();
77 final List<Resource> connections = new ArrayList<Resource>();
78 for (Object s : selection) {
79 Resource connection = AdaptionUtils.adaptToSingle(s, Resource.class);
80 if (connection == null)
81 return Collections.emptyList();
82 if (!graph.isInstanceOf(connection, dr.Connection))
83 return Collections.emptyList();
84 // Route graph connection routing is not selected per-connection, but per-terminal.
85 if (graph.isInstanceOf(connection, dr.RouteGraphConnection))
86 return Collections.emptyList();
87 connections.add(connection);
89 if (connections.isEmpty())
90 return Collections.emptyList();
92 // See if all connections have a common routing.
93 Resource currentRoute = null;
94 for (Resource connection : connections) {
95 Statement currentRouting = graph.getPossibleStatement(connection, dr.Routing);
96 if (currentRouting != null) {
97 Resource route = currentRouting.getPredicate();
98 if (currentRoute == null)
100 else if (!currentRoute.equals(route)) {
107 final List<SetRoutingAction> actions = new ArrayList<SetRoutingAction>();
108 for (Resource routing : routings) {
109 String text = graph.adapt(routing, String.class);
110 SetRoutingAction action = new SetRoutingAction(graph.getSession(), connections, routing, text);
111 if (routing.equals(currentRoute))
112 action.setChecked(true);
119 return Collections.<IContributionItem>singleton(
120 new ContributionItem() {
122 public void fill(Menu menu, int index) {
123 MenuItem openWith = new MenuItem(menu, SWT.CASCADE, index);
124 openWith.setText("Connection Routing");
125 Menu subMenu = new Menu(menu);
126 openWith.setMenu(subMenu);
128 for (SetRoutingAction a : actions) {
129 MenuItem item = new MenuItem(subMenu, SWT.CHECK);
130 item.setText(a.getText());
131 item.addSelectionListener(a);
132 item.setSelection(a.isChecked());
139 public static Collection<IContributionItem> getAttachedConnectionRoutingItems(ReadGraph graph, Object[] selection) throws DatabaseException {
140 Layer0 l0 = Layer0.getInstance(graph);
141 DiagramResource dr = DiagramResource.getInstance(graph);
142 StructuralResource2 sr = StructuralResource2.getInstance(graph);
143 ModelingResources MOD = ModelingResources.getInstance(graph);
145 Resource ReferenceProvider = graph.getPossibleResource("http://www.apros.fi/Apros-6.1/ReferenceProvider");
146 // Just continuing to do stupid things as above ;)
147 Resource connectionType = graph.getPossibleResource("http://www.simantics.org/Kcleco-5.0/FlowConnection");
150 final Collection<Resource> routings = graph.getObjects(dr.Routing, l0.SuperrelationOf);
151 if (routings.size() == 0)
152 return Collections.emptyList();
154 final List<Resource> connections = new ArrayList<Resource>();
156 // For route graph connections
157 final List<Resource> connectors = new ArrayList<Resource>();
158 boolean directRoutings = false;
159 boolean straightRoutings = false;
161 ArrayList<Resource> elements = new ArrayList<Resource>();
162 for (Object s : selection) {
163 Resource element = AdaptionUtils.adaptToSingle(s, Resource.class);
165 return Collections.emptyList();
166 if (!graph.isInstanceOf(element, dr.Element) || graph.isInstanceOf(element, dr.Connection))
167 return Collections.emptyList();
169 elements.add(element);
171 for(Statement stat : graph.getStatements(element, sr.IsConnectedTo)) {
172 Resource diagramRelation = stat.getPredicate();
173 Resource connectionRelation = graph.getPossibleObject(diagramRelation, MOD.DiagramConnectionRelationToConnectionRelation);
174 if(connectionRelation == null)
177 if(ReferenceProvider != null && !graph.isInstanceOf(connectionRelation, ReferenceProvider))
179 Resource connector = stat.getObject();
181 Resource connection = ConnectionUtil.tryGetConnection(graph, connector);
182 if (connection != null) {
183 if (graph.isInstanceOf(connection, dr.RouteGraphConnection)) {
184 if (connectionType != null && graph.isInstanceOf(connection, connectionType))
186 connectors.add(connector);
187 boolean direct = Boolean.TRUE.equals( graph.getPossibleRelatedValue2(connector, dr.Connector_straight, Bindings.BOOLEAN) );
188 directRoutings |= direct;
189 straightRoutings |= !direct;
191 connections.add(connection);
196 if (connectors.isEmpty() && connections.isEmpty())
197 return Collections.emptyList();
199 // See if all connections have a common routing.
200 Resource currentRoute = null;
201 for (Resource connection : connections) {
202 Statement currentRouting = graph.getPossibleStatement(connection, dr.Routing);
203 if (currentRouting != null) {
204 Resource route = currentRouting.getPredicate();
205 if (currentRoute == null)
206 currentRoute = route;
207 else if (!currentRoute.equals(route)) {
214 final List<SelectionListenerAction> actions = new ArrayList<SelectionListenerAction>();
216 if (!connections.isEmpty()) {
217 // Handle old style connections and their routing
218 for (Resource routing : routings) {
219 String text = graph.adapt(routing, String.class);
220 SetRoutingAction action = new SetRoutingAction(graph.getSession(), connections, routing, text);
221 if (routing.equals(currentRoute)) {
222 action.setChecked(true);
227 if (!connectors.isEmpty()) {
228 // Handle route graph connections
229 SelectionListenerAction direct = new SetAttachedRouteGraphRoutingAction(graph.getSession(), elements, true, "Direct");
230 SelectionListenerAction straight = new SetAttachedRouteGraphRoutingAction(graph.getSession(), elements, false, "Straight-angled");
231 direct.setChecked(directRoutings);
232 straight.setChecked(straightRoutings);
234 actions.add(straight);
239 return Collections.<IContributionItem>singleton(
240 new ContributionItem() {
242 public void fill(Menu menu, int index) {
243 MenuItem openWith = new MenuItem(menu, SWT.CASCADE, index);
244 openWith.setText("Attached Connection Routing");
245 Menu subMenu = new Menu(menu);
246 openWith.setMenu(subMenu);
248 for (SelectionListenerAction a : actions) {
249 MenuItem item = new MenuItem(subMenu, SWT.CHECK);
250 item.setText(a.getText());
251 item.addSelectionListener(a);
252 item.setSelection(a.isChecked());
259 private static void sort(List<? extends IAction> actions) {
260 // Sort the open with actions in string order.
261 Collections.sort(actions, new Comparator<IAction>() {
263 public int compare(IAction o1, IAction o2) {
264 return o1.getText().compareToIgnoreCase(o2.getText());
269 static abstract class SelectionListenerAction extends Action implements SelectionListener {
270 public SelectionListenerAction(String text) {
275 static class SetRoutingAction extends SelectionListenerAction {
278 Collection<Resource> connections;
281 public SetRoutingAction(Session session, Collection<Resource> connections, Resource routingTag, String text) {
283 this.session = session;
284 this.connections = connections;
285 this.routingTag = routingTag;
290 session.asyncRequest(new WriteRequest() {
292 public void perform(WriteGraph graph) throws DatabaseException {
293 DiagramResource dr = DiagramResource.getInstance(graph);
294 for (Resource connection : connections) {
295 graph.deny(connection, dr.Routing);
296 graph.claim(connection, routingTag, connection);
299 }, new Callback<DatabaseException>() {
301 public void run(DatabaseException parameter) {
302 if (parameter != null)
303 ExceptionUtils.logError(parameter);
309 public void widgetDefaultSelected(SelectionEvent e) {
314 public void widgetSelected(SelectionEvent e) {
320 static class SetAttachedRouteGraphRoutingAction extends SelectionListenerAction {
323 Collection<Resource> elements;
326 public SetAttachedRouteGraphRoutingAction(Session session, Collection<Resource> elements, boolean straight, String text) {
328 this.session = session;
329 this.elements = elements;
330 this.straight = straight;
335 session.asyncRequest(new WriteRequest() {
337 public void perform(WriteGraph graph) throws DatabaseException {
338 Command command = Commands.get(graph, "Simantics/Diagram/setStraightConnectionLines");
339 for(Resource element : elements)
340 command.execute(graph,
341 graph.syncRequest(new IndexRoot(element)),
344 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
345 graph.addMetadata(cm.add("Set routing for an element."));
347 }, new Callback<DatabaseException>() {
349 public void run(DatabaseException parameter) {
350 if (parameter != null)
351 ExceptionUtils.logError(parameter);
357 public void widgetDefaultSelected(SelectionEvent e) {
362 public void widgetSelected(SelectionEvent e) {