]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/NewConnectionPoint.java
(refs #7561) Allow extra parameters to the application of type alias
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / NewConnectionPoint.java
1 package org.simantics.modeling.ui.actions;
2
3 import java.util.Collection;
4 import java.util.HashMap;
5 import java.util.Map;
6
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;
32
33 public class NewConnectionPoint implements ActionFactory {
34
35     @Override
36     public Runnable create(Object target) {
37         if (!(target instanceof Resource))
38             return null;
39         final Resource componentType = (Resource)target;
40         return new Runnable() {
41             @Override
42             public void run() {
43                 try {
44                     
45                     Map<Resource, Pair<String, ImageDescriptor>> map = Simantics.sync(new UniqueRead<Map<Resource,Pair<String,ImageDescriptor>>>() {
46
47                         @Override
48                         public Map<Resource, Pair<String, ImageDescriptor>> perform(ReadGraph graph) throws DatabaseException {
49                             
50                             HashMap<Resource, Pair<String,ImageDescriptor>> map = new HashMap<Resource, Pair<String,ImageDescriptor>>(); 
51                             
52                             Layer0 L0 = Layer0.getInstance(graph);
53                             
54                             StructuralResource2 STR = StructuralResource2.getInstance(graph);
55                             ModelingResources MOD = ModelingResources.getInstance(graph);
56                             
57                             Instances query = graph.adapt(STR.ConnectionRelation, Instances.class);
58                             
59                             Resource model = graph.sync(new PossibleIndexRoot(componentType));
60                             
61 //                            List<String> domainNames = new ArrayList<String>();
62                             
63                             for(Resource _res : query.find(graph, model)) {
64
65                                 boolean isDiagramRelation = graph.hasStatement(_res, MOD.DiagramConnectionRelationToConnectionRelation);
66                                 if (isDiagramRelation)
67                                     continue;
68
69                                 Collection<Resource> domains = graph.getObjects(_res, L0.HasDomain);
70                                 
71                                 String name = null;
72                                 if(domains.size() == 0) {
73                                     continue;
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) {
85 //                                        if (!first)
86 //                                            sb.append(",");
87 //                                        first = false;
88 //                                        sb.append(s.substring(prefix.length()));
89 //                                    }
90 //                                    sb.append("}");
91                                     name = sb.toString();
92                                 } else {
93                                     // domains.size() == 1
94                                     name = NameUtils.getSafeName(graph, _res) + " (" + graph.getURI(domains.iterator().next()) + ")";
95                                 }
96                                 map.put(_res, new Pair<String, ImageDescriptor>(name, null));
97                                 
98                             }
99                             
100                             return map;
101                             
102                         }
103
104                     });
105                     
106                     final Resource[] cps = queryCps(map);
107                     if(cps.length == 0) return;
108                     
109                     Simantics.getSession().async(new WriteRequest() {
110                         @Override
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));
116                         }
117                         String createConnectionPointComment(ReadGraph graph, Resource target, Resource[] cps) throws DatabaseException {
118                             StringBuilder result = new StringBuilder();
119                             result.append("Created connection point");
120                             if (cps.length > 1)
121                                 result.append('s');
122                             result.append(" for ")
123                             .append(NameUtils.getSafeName(graph, componentType))
124                             .append(":\n");
125                             for (int i = 0; i < cps.length; ++i) {
126                                 result.append('\t');
127                                 result.append(NameUtils.getSafeName(graph, cps[i]));
128                                 result.append('\n');
129                             }
130                             return result.toString();
131                         }
132                     });
133                     
134                 } catch (DatabaseException e1) {
135                     ErrorLogger.defaultLogError(e1);
136                 }
137             }
138         };
139     }
140
141 //    private static String findLongestCommonPrefix(List<String> strs) {
142 //        int count = strs.size();
143 //        if (count == 0)
144 //            return "";
145 //        if (count == 1)
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());
150 //        int i = 0;
151 //        chars:
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)
156 //                        break chars;
157 //                }
158 //            }
159 //        return strs.get(0).substring(0, Math.max(0, i));
160 //    }
161
162     private Resource[] queryCps(Map<Resource, Pair<String, ImageDescriptor>> map) {
163
164         Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
165         ResourceSelectionDialog3<Resource> dialog = new ResourceSelectionDialog3<Resource>(shell, map, "Select connection point type") {
166             @Override
167             protected IDialogSettings getBaseDialogSettings() {
168                 return Activator.getDefault().getDialogSettings();
169             }
170         };
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);
176                 return res;
177             }
178         }
179         return Resource.NONE;
180         
181     }
182 }