]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/handler/TerminalConnectionDirectionsMenuContribution.java
(refs #7595) Started SCL/Reflection module
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / handler / TerminalConnectionDirectionsMenuContribution.java
1 /*******************************************************************************
2  * Copyright (c) 2013 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.handler;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
18
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;
39
40 /**
41  * @author Tuukka Lehtonen
42  */
43 public class TerminalConnectionDirectionsMenuContribution extends DynamicMenuContribution {
44
45         private static final IContributionItem[] NONE = {};
46
47         private static final int[] DIRECTION_MASK = { 8, 4, 1, 2 };
48         private static final String[] DIRECTION_NAME = { "Up", "Left", "Right", "Down" };
49
50         @Override
51         protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {
52                 return getTerminalDirectionItems(graph, selection).toArray(NONE);
53         }
54
55         public static Collection<IContributionItem> getTerminalDirectionItems(ReadGraph graph, Object[] selection) throws DatabaseException {
56                 DiagramResource DIA = DiagramResource.getInstance(graph);
57
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);
64                 }
65                 if (terminals.isEmpty())
66                         return Collections.emptyList();
67
68                 int jointMask = 0;
69                 for (Resource terminal : terminals) {
70                         Integer dirs = graph.getPossibleRelatedValue(terminal, DIA.Terminal_AllowedDirections, Bindings.INTEGER);
71                         int mask = dirs == null ? 0 : dirs;
72                         jointMask |= mask;
73                 }
74
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);
84                         actions.add(action);
85                 }
86
87                 return Collections.<IContributionItem>singleton(
88                                 new ContributionItem() {
89                                         @Override
90                                         public void fill(Menu menu, int index) {
91                                                 MenuItem directions = new MenuItem(menu, SWT.CASCADE, index);
92                                                 directions.setText("Allowed Routing Directions");
93
94                                                 Menu subMenu = new Menu(menu);
95                                                 directions.setMenu(subMenu);
96
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());
102                                                 }
103                                         }
104                                 }
105                                 );
106         }
107
108         static class ToggleDirectionAction extends Action implements SelectionListener {
109
110                 Session session;
111                 Collection<Resource> terminals;
112                 int orMask;
113                 int andMask;
114
115                 public ToggleDirectionAction(Session session,
116                                 List<Resource> terminals, int orMask, int andMask,
117                                 String name) {
118                         super(name, Action.AS_CHECK_BOX);
119                         this.session = session;
120                         this.terminals = terminals;
121                         this.orMask = orMask;
122                         this.andMask = andMask;
123                 }
124
125                 @Override
126                 public void run() {
127                         session.asyncRequest(new WriteRequest() {
128                                 @Override
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;
134                                                 mask &= andMask;
135                                                 mask |= orMask;
136                                                 graph.claimLiteral(terminal, DIA.Terminal_AllowedDirections, mask, Bindings.INTEGER);
137                                         }
138                                 }
139                         }, new Callback<DatabaseException>() {
140                                 @Override
141                                 public void run(DatabaseException parameter) {
142                                         if (parameter != null)
143                                                 ExceptionUtils.logError(parameter);
144                                 }
145                         });
146                 }
147
148                 @Override
149                 public void widgetDefaultSelected(SelectionEvent e) {
150                         widgetSelected(e);
151                 }
152
153                 @Override
154                 public void widgetSelected(SelectionEvent e) {
155                         run();
156                 }
157
158         }
159
160 }