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