1 /*******************************************************************************
2 * Copyright (c) 2013 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 * Semantum Oy - 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.List;
19 import org.eclipse.jface.action.Action;
20 import org.eclipse.jface.action.ContributionItem;
21 import org.eclipse.jface.action.IContributionItem;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.events.SelectionListener;
25 import org.eclipse.swt.widgets.Menu;
26 import org.eclipse.swt.widgets.MenuItem;
27 import org.simantics.databoard.Bindings;
28 import org.simantics.db.ReadGraph;
29 import org.simantics.db.Resource;
30 import org.simantics.db.Session;
31 import org.simantics.db.WriteGraph;
32 import org.simantics.db.common.request.WriteRequest;
33 import org.simantics.db.exception.DatabaseException;
34 import org.simantics.diagram.stubs.DiagramResource;
35 import org.simantics.ui.contribution.DynamicMenuContribution;
36 import org.simantics.utils.datastructures.Callback;
37 import org.simantics.utils.ui.AdaptionUtils;
38 import org.simantics.utils.ui.ExceptionUtils;
41 * @author Tuukka Lehtonen
43 public class TerminalConnectionDirectionsMenuContribution extends DynamicMenuContribution {
45 private static final IContributionItem[] NONE = {};
47 private static final int[] DIRECTION_MASK = { 8, 4, 1, 2 };
48 private static final String[] DIRECTION_NAME = { "Up", "Left", "Right", "Down" };
51 protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {
52 return getTerminalDirectionItems(graph, selection).toArray(NONE);
55 public static Collection<IContributionItem> getTerminalDirectionItems(ReadGraph graph, Object[] selection) throws DatabaseException {
56 DiagramResource DIA = DiagramResource.getInstance(graph);
58 final List<Resource> terminals = new ArrayList<Resource>();
59 for (Object s : selection) {
60 Resource connection = AdaptionUtils.adaptToSingle(s, Resource.class);
61 if (connection == null || !graph.isInstanceOf(connection, DIA.Terminal))
62 return Collections.emptyList();
63 terminals.add(connection);
65 if (terminals.isEmpty())
66 return Collections.emptyList();
69 for (Resource terminal : terminals) {
70 Integer dirs = graph.getPossibleRelatedValue(terminal, DIA.Terminal_AllowedDirections, Bindings.INTEGER);
71 int mask = dirs == null ? 0 : dirs;
75 final List<ToggleDirectionAction> actions = new ArrayList<ToggleDirectionAction>(4);
76 for (int i = 0; i < DIRECTION_NAME.length; ++i) {
77 String name = DIRECTION_NAME[i];
78 int mask = DIRECTION_MASK[i];
79 boolean wasChecked = (jointMask & mask) != 0;
80 int orMask = wasChecked ? 0 : mask;
81 int andMask = wasChecked ? ~mask : 0xffffffff;
82 ToggleDirectionAction action = new ToggleDirectionAction(graph.getSession(), terminals, orMask, andMask, name);
83 action.setChecked(wasChecked);
87 return Collections.<IContributionItem>singleton(
88 new ContributionItem() {
90 public void fill(Menu menu, int index) {
91 MenuItem directions = new MenuItem(menu, SWT.CASCADE, index);
92 directions.setText("Allowed Routing Directions");
94 Menu subMenu = new Menu(menu);
95 directions.setMenu(subMenu);
97 for (ToggleDirectionAction a : actions) {
98 MenuItem item = new MenuItem(subMenu, SWT.CHECK);
99 item.setText(a.getText());
100 item.addSelectionListener(a);
101 item.setSelection(a.isChecked());
108 static class ToggleDirectionAction extends Action implements SelectionListener {
111 Collection<Resource> terminals;
115 public ToggleDirectionAction(Session session,
116 List<Resource> terminals, int orMask, int andMask,
118 super(name, Action.AS_CHECK_BOX);
119 this.session = session;
120 this.terminals = terminals;
121 this.orMask = orMask;
122 this.andMask = andMask;
127 session.asyncRequest(new WriteRequest() {
129 public void perform(WriteGraph graph) throws DatabaseException {
130 DiagramResource DIA = DiagramResource.getInstance(graph);
131 for (Resource terminal : terminals) {
132 Integer dirs = graph.getPossibleRelatedValue(terminal, DIA.Terminal_AllowedDirections, Bindings.INTEGER);
133 int mask = dirs == null ? 0 : dirs;
136 graph.claimLiteral(terminal, DIA.Terminal_AllowedDirections, mask, Bindings.INTEGER);
139 }, new Callback<DatabaseException>() {
141 public void run(DatabaseException parameter) {
142 if (parameter != null)
143 ExceptionUtils.logError(parameter);
149 public void widgetDefaultSelected(SelectionEvent e) {
154 public void widgetSelected(SelectionEvent e) {