1 package org.simantics.modeling.ui.actions;
3 import java.util.Collection;
4 import java.util.HashMap;
7 import org.eclipse.jface.dialogs.IDialogSettings;
8 import org.eclipse.jface.resource.ImageDescriptor;
9 import org.eclipse.jface.window.Window;
10 import org.eclipse.swt.widgets.Shell;
11 import org.eclipse.ui.PlatformUI;
12 import org.simantics.Simantics;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.Resource;
15 import org.simantics.db.WriteGraph;
16 import org.simantics.db.common.request.PossibleIndexRoot;
17 import org.simantics.db.common.request.UniqueRead;
18 import org.simantics.db.common.request.WriteRequest;
19 import org.simantics.db.common.utils.NameUtils;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.adapter.ActionFactory;
22 import org.simantics.db.layer0.adapter.Instances;
23 import org.simantics.db.layer0.util.Layer0Utils;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.modeling.ModelingResources;
26 import org.simantics.modeling.ui.Activator;
27 import org.simantics.modeling.userComponent.ComponentTypeCommands;
28 import org.simantics.structural.stubs.StructuralResource2;
29 import org.simantics.ui.workbench.dialogs.ResourceSelectionDialog3;
30 import org.simantics.utils.datastructures.Pair;
31 import org.simantics.utils.ui.ErrorLogger;
33 public class NewConnectionPoint implements ActionFactory {
36 public Runnable create(Object target) {
37 if (!(target instanceof Resource))
39 final Resource componentType = (Resource)target;
40 return new Runnable() {
45 Map<Resource, Pair<String, ImageDescriptor>> map = Simantics.sync(new UniqueRead<Map<Resource,Pair<String,ImageDescriptor>>>() {
48 public Map<Resource, Pair<String, ImageDescriptor>> perform(ReadGraph graph) throws DatabaseException {
50 HashMap<Resource, Pair<String,ImageDescriptor>> map = new HashMap<Resource, Pair<String,ImageDescriptor>>();
52 Layer0 L0 = Layer0.getInstance(graph);
54 StructuralResource2 STR = StructuralResource2.getInstance(graph);
55 ModelingResources MOD = ModelingResources.getInstance(graph);
57 Instances query = graph.adapt(STR.ConnectionRelation, Instances.class);
59 Resource model = graph.sync(new PossibleIndexRoot(componentType));
61 // List<String> domainNames = new ArrayList<String>();
63 for(Resource _res : query.find(graph, model)) {
65 boolean isDiagramRelation = graph.hasStatement(_res, MOD.DiagramConnectionRelationToConnectionRelation);
66 if (isDiagramRelation)
69 Collection<Resource> domains = graph.getObjects(_res, L0.HasDomain);
72 if(domains.size() == 0) {
74 } else if (domains.size() > 1) {
75 StringBuilder sb = new StringBuilder();
76 sb.append(NameUtils.getSafeName(graph, _res));
77 // domainNames.clear();
78 // for (Resource domain : domains)
79 // domainNames.add(NameUtils.getSafeName(graph, domain));
80 // Collections.sort(domainNames, AlphanumComparator.CASE_INSENSITIVE_COMPARATOR);
81 // String prefix = findLongestCommonPrefix(domainNames);
82 // sb.append(" - ").append(prefix).append("{");
83 // boolean first = true;
84 // for (String s : domainNames) {
88 // sb.append(s.substring(prefix.length()));
93 // domains.size() == 1
94 name = NameUtils.getSafeName(graph, _res) + " (" + graph.getURI(domains.iterator().next()) + ")";
96 map.put(_res, new Pair<String, ImageDescriptor>(name, null));
106 final Resource[] cps = queryCps(map);
107 if(cps.length == 0) return;
109 Simantics.getSession().async(new WriteRequest() {
111 public void perform(WriteGraph graph) throws DatabaseException {
112 graph.markUndoPoint();
113 for (Resource cp : cps)
114 ComponentTypeCommands.createConnectionPoint(graph, componentType, cp);
115 Layer0Utils.addCommentMetadata(graph, createConnectionPointComment(graph, componentType, cps));
117 String createConnectionPointComment(ReadGraph graph, Resource target, Resource[] cps) throws DatabaseException {
118 StringBuilder result = new StringBuilder();
119 result.append("Created connection point");
122 result.append(" for ")
123 .append(NameUtils.getSafeName(graph, componentType))
125 for (int i = 0; i < cps.length; ++i) {
127 result.append(NameUtils.getSafeName(graph, cps[i]));
130 return result.toString();
134 } catch (DatabaseException e1) {
135 ErrorLogger.defaultLogError(e1);
141 // private static String findLongestCommonPrefix(List<String> strs) {
142 // int count = strs.size();
146 // return strs.get(0);
147 // int maxLen = Integer.MAX_VALUE;
148 // for (int n = 0; n < count; ++n)
149 // maxLen = Math.min(maxLen, strs.get(n).length());
152 // for (; i < maxLen; ++i) {
153 // char ch = strs.get(0).charAt(i);
154 // for (int n = 1; n < count; ++n) {
155 // if (strs.get(n).charAt(i) != ch)
159 // return strs.get(0).substring(0, Math.max(0, i));
162 private Resource[] queryCps(Map<Resource, Pair<String, ImageDescriptor>> map) {
164 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
165 ResourceSelectionDialog3<Resource> dialog = new ResourceSelectionDialog3<Resource>(shell, map, "Select connection point type") {
167 protected IDialogSettings getBaseDialogSettings() {
168 return Activator.getDefault().getDialogSettings();
171 if (dialog.open() == Window.OK) {
172 Object[] result = dialog.getResult();
173 if (result != null && result.length > 0) {
174 final Resource[] res = new Resource[result.length];
175 System.arraycopy(result, 0, res, 0, result.length);
179 return Resource.NONE;