]> gerrit.simantics Code Review - simantics/3d.git/blob
54a21056ec1ca6a82b876883179c679d00d8ef77
[simantics/3d.git] /
1 package org.simantics.proconf.g3d.shapeeditor.tools;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.List;\r
6 \r
7 import org.eclipse.jface.action.IMenuManager;\r
8 import org.eclipse.jface.action.IToolBarManager;\r
9 import org.eclipse.jface.dialogs.InputDialog;\r
10 import org.eclipse.jface.dialogs.MessageDialog;\r
11 import org.eclipse.swt.SWT;\r
12 import org.eclipse.swt.events.SelectionAdapter;\r
13 import org.eclipse.swt.events.SelectionEvent;\r
14 import org.eclipse.swt.graphics.Rectangle;\r
15 import org.eclipse.swt.layout.FillLayout;\r
16 import org.eclipse.swt.layout.FormAttachment;\r
17 import org.eclipse.swt.layout.FormData;\r
18 import org.eclipse.swt.layout.FormLayout;\r
19 import org.eclipse.swt.layout.GridData;\r
20 import org.eclipse.swt.layout.GridLayout;\r
21 import org.eclipse.swt.widgets.Button;\r
22 import org.eclipse.swt.widgets.Composite;\r
23 import org.eclipse.swt.widgets.Event;\r
24 import org.eclipse.swt.widgets.Label;\r
25 import org.eclipse.swt.widgets.Listener;\r
26 import org.eclipse.swt.widgets.Sash;\r
27 import org.eclipse.swt.widgets.Tree;\r
28 import org.eclipse.ui.forms.IFormColors;\r
29 import org.eclipse.ui.forms.events.ExpansionAdapter;\r
30 import org.eclipse.ui.forms.events.ExpansionEvent;\r
31 import org.eclipse.ui.forms.widgets.FormToolkit;\r
32 import org.eclipse.ui.forms.widgets.ScrolledForm;\r
33 import org.eclipse.ui.forms.widgets.Section;\r
34 import org.simantics.db.Graph;\r
35 import org.simantics.db.GraphRequestAdapter;\r
36 import org.simantics.db.GraphRequestStatus;\r
37 import org.simantics.db.Resource;\r
38 import org.simantics.equation.stubs.SecondOrderScalarPolynomial;\r
39 import org.simantics.animation.stubs.Animation;\r
40 import org.simantics.animation.stubs.Interpolator;\r
41 import org.simantics.layer0.utils.EntityFactory;\r
42 import org.simantics.layer0.utils.IEntity;\r
43 import org.simantics.layer0.utils.ResourceDebugUtils;\r
44 import org.simantics.proconf.browsing.GraphExplorer;\r
45 import org.simantics.proconf.browsing.GraphExplorerInputFactory;\r
46 import org.simantics.proconf.browsing.views.PropertyTable;\r
47 import org.simantics.proconf.g3d.actions.ContextAction;\r
48 import org.simantics.proconf.g3d.base.EditorContribution;\r
49 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
50 import org.simantics.proconf.g3d.common.StructuredResourceSelection;\r
51 import org.simantics.proconf.g3d.shapeeditor.ShapeEditorResources;\r
52 import org.simantics.proconf.g3d.shapeeditor.common.ViewpointGenerator;\r
53 import org.simantics.proconf.g3d.shapeeditor.views.ShapeEditorBase;\r
54 import org.simantics.proconf.g3d.stubs.G3DModel;\r
55 import org.simantics.proconf.g3d.tools.OESelectionListener;\r
56 import org.simantics.proconf.g3d.tools.PropertyTree;\r
57 import org.simantics.utils.ui.jface.BaseSelectionProvider;\r
58 \r
59 \r
60 public class ParameterizationContribution implements EditorContribution {\r
61 \r
62         private ShapeEditorBase parent;\r
63         private List<ContextAction> actions = new ArrayList<ContextAction>();\r
64         private Composite sideComposite;\r
65         private Sash sash;\r
66         \r
67         \r
68         public ParameterizationContribution(ThreeDimensionalEditorBase parent) {\r
69                 this.parent = (ShapeEditorBase) parent;\r
70         }\r
71         \r
72         @Override\r
73         public void createControl(final Composite parentComposite) {\r
74                 FormLayout flayout = new FormLayout();\r
75                 parentComposite.setLayout(flayout);\r
76                 sash = new Sash(parentComposite,SWT.VERTICAL);\r
77                 \r
78                 sideComposite = new Composite(parentComposite,SWT.BORDER);\r
79                 FormData data = new FormData();\r
80                 data.top = new FormAttachment(0, 0);\r
81                 data.left = new FormAttachment(0, 0);\r
82                 data.right = new FormAttachment(sash, 0, SWT.LEFT);\r
83                 data.bottom = new FormAttachment(100,0);\r
84                 this.parent.getRenderingComposite().setLayoutData(data);\r
85                 sideComposite.setLayout(new FillLayout(SWT.VERTICAL));\r
86                 data = new FormData();\r
87                 data.top = new FormAttachment(0, 0);\r
88                 data.bottom = new FormAttachment(100,0);\r
89                 data.right = new FormAttachment(100,0);\r
90                 data.left = new FormAttachment(sash,0,SWT.RIGHT);\r
91                 sideComposite.setLayoutData(data);\r
92                 \r
93                 final int limit = 20, percent = 50;\r
94                 final FormData sashData = new FormData();\r
95                 sashData.left = new FormAttachment (percent, 0);\r
96                 sashData.top = new FormAttachment(0, 0);\r
97                 sashData.bottom = new FormAttachment(100,0);\r
98                 \r
99                 sash.setLayoutData (sashData);\r
100                 sash.addListener (SWT.Selection, new Listener () {\r
101                         public void handleEvent (Event e) {\r
102                                 Rectangle sashRect = sash.getBounds ();\r
103                                 Rectangle shellRect = parentComposite.getClientArea ();\r
104                                 int right = shellRect.width - sashRect.width - limit;\r
105                                 e.x = Math.max (Math.min (e.x, right), limit);\r
106                                 if (e.x != sashRect.x)  {\r
107                                         sashData.left = new FormAttachment (0, e.x);\r
108                                         parentComposite.layout ();\r
109                                 }\r
110                         }\r
111                 });\r
112                 createForm(sideComposite);\r
113                 \r
114                 hierarchyExplorer.setInput(parent.getSessionContext(),GraphExplorerInputFactory.clone(hierarchyExplorer.getInput())\r
115                     .input(parent.getModelResource())\r
116                     .viewpoint(ViewpointGenerator.createObjectStructureViewpoint())\r
117                     .toInput());\r
118 \r
119 //        parameterExplorer.setInput(parent.getSessionContext(),GraphExplorerInputFactory.clone(parameterExplorer.getInput())\r
120 //                .input(parent.getModelResource())\r
121 //                .viewpoint(ViewpointGenerator.createObjectSizingParameterViewpoint(ShapeEditorResources.g3dResource.HasSizingParameter))\r
122 //                .toInput());\r
123                 parameterTable.setSession(parent.getSession());\r
124                 parameterTable.setInput(new StructuredResourceSelection(parent.getModelResource()));\r
125         \r
126         }\r
127         \r
128         @Override\r
129         public void disposeControl() {\r
130                 sideComposite.dispose();\r
131                 sash.dispose();\r
132                 \r
133         }\r
134         \r
135         @Override\r
136         public void dispose() {\r
137 \r
138         }\r
139         \r
140         @Override\r
141         public void fillContextMenu(Graph graph, IMenuManager manager,\r
142                         StructuredResourceSelection selection) {\r
143 \r
144         }\r
145         \r
146         @Override\r
147         public void fillLocalPullDown(IMenuManager manager) {\r
148 \r
149         }\r
150         \r
151         @Override\r
152         public void fillLocalToolBar(IToolBarManager manager) {\r
153 \r
154         }\r
155         \r
156         @Override\r
157         public Collection<ContextAction> getActions() {\r
158                 return actions;\r
159         }\r
160         \r
161         @Override\r
162         public String getName() {\r
163                 return "Parameterization";\r
164         }\r
165         \r
166         @Override\r
167         public void initialize(Graph graph) {\r
168 \r
169         }\r
170         \r
171         @Override\r
172         public void run() {\r
173                 \r
174         }\r
175         \r
176         private ScrolledForm form;\r
177         private BaseSelectionProvider defaultInputSelectionProvider = new BaseSelectionProvider();\r
178         protected FormToolkit toolkit;\r
179         \r
180 \r
181     \r
182     private void createForm(Composite parent) {\r
183         toolkit = new FormToolkit(parent.getDisplay());\r
184                 form = getToolkit().createScrolledForm(parent);\r
185 \r
186                 GridLayout layout = new GridLayout(2, false);\r
187                 form.getBody().setLayout(layout);\r
188                 form.getBody().setLayoutData(\r
189                                 new GridData(GridData.FILL, GridData.FILL, true, true));\r
190 \r
191                 // By default make this ViewPart use a default ISelectionProvider\r
192                 // that will offer the viewparts input resource as its selection.\r
193                 // The Resource is wrapped into a ResourceSelection object.\r
194                 // Any widgets created in createWidgets may override the default\r
195                 // selection provider.\r
196                 //getEditorSite().setSelectionProvider(defaultInputSelectionProvider);\r
197 \r
198                 beforeCreateWidgets();\r
199                 createWidgets();\r
200 \r
201                 //reload();\r
202 \r
203                 form.setText(getFormText());\r
204 \r
205                 // Finally Set the default selection which will have an effect only\r
206                 // if nothing in createWidgets has overridden the default selection\r
207                 // provider.\r
208 //              ISelection s = ISelectionUtils\r
209 //                              .createSelection(new StructuredResourceSelection(\r
210 //                                              getInputResource()));\r
211 //              defaultInputSelectionProvider.setSelection(s);\r
212     }\r
213     \r
214         \r
215         public ScrolledForm getActiveForm() {\r
216                 return form;\r
217         }\r
218 \r
219         protected Composite getBody() {\r
220                 return form.getBody();\r
221         }\r
222         \r
223         public Composite newGridSection(int formColumns, int childColumns,\r
224                         boolean equalWidth, boolean grabVertical, String text,\r
225                         String description) {\r
226                 return newGridSection(getBody(), formColumns, childColumns, equalWidth,\r
227                                 grabVertical, text, description);\r
228         }\r
229 \r
230         public Composite newGridSection(Composite parent, int formColumns,\r
231                         int childColumns, boolean equalWidth, boolean grabVertical,\r
232                         String text, String description) {\r
233                 FormToolkit toolkit = getToolkit();\r
234 \r
235                 Section section = toolkit.createSection(parent, Section.DESCRIPTION\r
236                                 | Section.TWISTIE | Section.TITLE_BAR | Section.EXPANDED);\r
237                 section.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true,\r
238                                 grabVertical, formColumns, 1));\r
239                 section.addExpansionListener(new ExpansionAdapter() {\r
240                         public void expansionStateChanged(ExpansionEvent e) {\r
241                                 //System.out.println("SinglePageTypeEditor: expansionStateChanged " + e);\r
242                                 //reflow(true);\r
243                         }\r
244                 });\r
245                 section.setText(text);\r
246                 section.setDescription(description);\r
247                 Composite sectionClient = toolkit.createComposite(section);\r
248                 sectionClient.setLayout(new GridLayout(childColumns, equalWidth));\r
249                 sectionClient.setLayoutData(new GridData());\r
250                 section.setClient(sectionClient);\r
251                 return sectionClient;\r
252         }\r
253         \r
254         public FormToolkit getToolkit() {\r
255                 return toolkit;\r
256         }\r
257         \r
258         public void reflow(boolean flushCache) {\r
259                 //System.out.println("FormTypeEditorBase.reflow(" + flushCache + ")");\r
260                 getActiveForm().reflow(flushCache);\r
261         }\r
262         \r
263         protected void beforeCreateWidgets() {\r
264         }\r
265         \r
266 //      private TrackedText modelName = null;\r
267     StructuredResourceSelection hierarchySelection = new StructuredResourceSelection();\r
268     StructuredResourceSelection parameterSelection = new StructuredResourceSelection();\r
269     //GraphExplorer parameterExplorer;\r
270     PropertyTable parameterTable;\r
271     GraphExplorer hierarchyExplorer;\r
272     Button deleteParameterButton;\r
273     Button clearButton;\r
274     PropertyTree propertiesTree;\r
275     Button generateButton;\r
276     Button askButton;\r
277     Button overrideButton;\r
278     \r
279     protected String getFormText() {\r
280         return "Parameterization Editor";\r
281     }\r
282 \r
283     protected void createWidgets() {\r
284         createModelPropertiesGroup(newGridSection(2, 1, false, false, "Model Properties",\r
285         "Basic properties for this viewpoint"));\r
286         createParametrizationGroup(newGridSection(2, 1, false, false, "Parameterization",\r
287         "Create parameterization for selected shapes"));\r
288         //getSite().setSelectionProvider(this);\r
289     }\r
290     \r
291     private void createParametrizationGroup(Composite parent) {\r
292         toolkit.paintBordersFor(parent);\r
293         toolkit.setBorderStyle(SWT.BORDER);\r
294         \r
295         overrideButton = toolkit.createButton(parent, "Overwrite previous parameterizations", SWT.CHECK);\r
296         overrideButton.addSelectionListener(new SelectionAdapter() {\r
297             public void widgetSelected(SelectionEvent e) {\r
298                 askButton.setEnabled(overrideButton.getSelection());\r
299                 \r
300             }\r
301         });\r
302 \r
303         \r
304         askButton = toolkit.createButton(parent, "Ask before overwriting", SWT.CHECK);\r
305         askButton.addSelectionListener(new SelectionAdapter() {\r
306             public void widgetSelected(SelectionEvent e) {\r
307                 \r
308                 \r
309             }\r
310         });\r
311         askButton.setEnabled(false);\r
312         \r
313         generateButton = toolkit.createButton(parent, "Generate linear parameterization", SWT.PUSH);\r
314         generateButton.addSelectionListener(new SelectionAdapter() {\r
315             public void widgetSelected(SelectionEvent e) {\r
316                 generateMappings();\r
317                 \r
318             }\r
319         });\r
320         generateButton.setEnabled(false);\r
321         \r
322         \r
323     }\r
324     \r
325     private void createModelPropertiesGroup(final Composite parent) {\r
326   \r
327         toolkit.paintBordersFor(parent);\r
328         toolkit.setBorderStyle(SWT.BORDER);\r
329         \r
330         GridData gridData1 = new GridData(SWT.FILL, SWT.TOP, false, false, 1, 1);\r
331         \r
332         \r
333         // Parameters\r
334         Label parameterLabel = toolkit.createLabel(parent, "Parameters:");\r
335         parameterLabel.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));\r
336         parameterLabel.setLayoutData(gridData1);\r
337         \r
338 //        parameterExplorer = new GraphExplorer(parent, SWT.SINGLE); //new OntologyExplorer("ParameterExplorer", this, getInput("ParameterExplorer", model.getId()));\r
339 //        parameterExplorer.getViewer().addPostSelectionChangedListener(new OESelectionListener() {\r
340 //\r
341 //           protected void resourceSelectionUpdated(StructuredResourceSelection sel) {\r
342 //                 parameterSelection = sel;\r
343 //               if (parameterSelection.size() == 0) {\r
344 //                   deleteParameterButton.setEnabled(false);\r
345 //                   clearButton.setEnabled(false);\r
346 //               } else {\r
347 //                   deleteParameterButton.setEnabled(true);\r
348 //                   clearButton.setEnabled(true);\r
349 //               }\r
350 //               updateGenerateButtonStatus();\r
351 //           }\r
352 //              \r
353 //        });\r
354 //        Tree oe = parameterExplorer.getTree();\r
355 //        toolkit.adapt(oe, true, true);\r
356         \r
357         parameterTable = new PropertyTable(parent,SWT.NONE);\r
358         parameterTable.getViewer().addPostSelectionChangedListener(new OESelectionListener() {\r
359 \r
360            protected void resourceSelectionUpdated(StructuredResourceSelection sel) {\r
361                    parameterSelection = sel;\r
362                if (parameterSelection.size() == 0) {\r
363                    deleteParameterButton.setEnabled(false);\r
364                    clearButton.setEnabled(false);\r
365                } else {\r
366                    deleteParameterButton.setEnabled(true);\r
367                    clearButton.setEnabled(true);\r
368                }\r
369                updateGenerateButtonStatus();\r
370            }\r
371                 \r
372         });\r
373         \r
374         GridData gd3 = new GridData(GridData.FILL, GridData.FILL, true, true,1,1);\r
375         gd3.heightHint = 50;\r
376         //oe.setLayoutData(gd3);\r
377 \r
378         parameterTable.setLayoutData(gd3);\r
379         \r
380         // Buttons to add and remove parameters\r
381         toolkit.createLabel(parent, "");\r
382         Composite buttons = toolkit.createComposite(parent);\r
383 \r
384         buttons.setLayout(new FillLayout(SWT.HORIZONTAL));\r
385         Button newParameterButton = toolkit.createButton(buttons, "New Parameter", SWT.PUSH);\r
386         newParameterButton.addSelectionListener(new SelectionAdapter() {\r
387             public void widgetSelected(SelectionEvent e) {\r
388                 InputDialog dialog = new InputDialog(ParameterizationContribution.this.parent.getRenderingComposite().getShell(),"New Parameter","Enter parameter's name","new parameter",null);\r
389                 if (dialog.open() == InputDialog.CANCEL)\r
390                     return;\r
391                 final String name = dialog.getValue();\r
392                 if (name.length() < 1) \r
393                     return; // TODO : show error\r
394                 \r
395                 ParameterizationContribution.this.parent.getSession().asyncWrite(new GraphRequestAdapter() {\r
396                         public GraphRequestStatus perform(Graph g) throws Exception {\r
397                                 //fi.vtt.simantics.layer0.stubs.Double parameter = DoubleFactory.createDefault(g);\r
398                         //getModel(g).addStatement(ShapeEditorResources.g3dResource.HasSizingParameter, parameter);\r
399                         //parameter.setValue(new double[]{1.0});\r
400                         //parameter.setName(name);\r
401                                 Resource parameter = ParameterizationContribution.this.parent.getModel(g).addRelatedScalarDouble(ShapeEditorResources.g3dResource.HasSizingParameter, 1.0).getResource();\r
402                                 IEntity thing = EntityFactory.create(g,parameter);\r
403                                 thing.setName(name);\r
404                                 return GraphRequestStatus.transactionComplete();\r
405                         };\r
406                 });\r
407                     \r
408                 \r
409             }\r
410         });\r
411         deleteParameterButton = toolkit.createButton(buttons, "Delete Parameter", SWT.PUSH);\r
412         deleteParameterButton.addSelectionListener(new SelectionAdapter() {\r
413             public void widgetSelected(SelectionEvent e) {\r
414                 //StructuredResourceSelection s = (StructuredResourceSelection)parameterScheme.getSelection();\r
415                 final StructuredResourceSelection s = parameterSelection;\r
416                 if (s.size() != 1)\r
417                     return;\r
418                 // we'll have to start write transaction, since we may have to change the graph\r
419                 ParameterizationContribution.this.parent.getSession().asyncWrite(new GraphRequestAdapter() {\r
420                         boolean proceed;\r
421                         IEntity selectedParameter;\r
422                         public GraphRequestStatus perform(Graph g) throws Exception {\r
423                                  selectedParameter = EntityFactory.create(g,s.getSelectionList().get(0));\r
424                                 \r
425                         Collection<IEntity> parameters = selectedParameter.getRelatedObjects(ShapeEditorResources.g3dResource.HasSizingParameter);\r
426                         if (parameters.size() > 0) {\r
427                             final MessageDialog dialog = new MessageDialog(ParameterizationContribution.this.parent.getRenderingComposite().getShell(),"Deleting a parameter",null,"Parameter is in use, doe you wan't to remove it?",MessageDialog.QUESTION,new String[]{"OK","Cancel"},1);\r
428                             parent.getDisplay().syncExec(new Runnable() {\r
429                                 @Override\r
430                                 public void run() {\r
431                                          proceed = (dialog.open() != 1);\r
432                                 }\r
433                             });\r
434                             if (proceed) {\r
435                                 ParameterizationContribution.this.parent.getModel(g).removeStatement(ShapeEditorResources.g3dResource.HasSizingParameter,selectedParameter);\r
436                                         }\r
437                         }\r
438                        \r
439                         return GraphRequestStatus.transactionComplete();\r
440                         }\r
441                 });\r
442             }\r
443         });\r
444         deleteParameterButton.setEnabled(false);\r
445         \r
446         clearButton = toolkit.createButton(buttons, "Clear Parameter", SWT.PUSH);\r
447         clearButton.addSelectionListener(new SelectionAdapter() {\r
448             public void widgetSelected(SelectionEvent e) {\r
449                 final StructuredResourceSelection s = parameterSelection;\r
450                 if (s.size() != 1)\r
451                     return;\r
452                 ParameterizationContribution.this.parent.getSession().asyncWrite(new GraphRequestAdapter() {\r
453                         boolean proceed;\r
454                         @Override\r
455                         public GraphRequestStatus perform(Graph g) throws Exception {\r
456                                  IEntity selectedParameter = EntityFactory.create(g,s.getSelectionList().get(0));\r
457                          Collection<IEntity> equations = selectedParameter.getRelatedObjects(ShapeEditorResources.equationResource.HasTarget);\r
458                                  final String name = selectedParameter.getName();\r
459                                  if (equations.size() > 0) {\r
460                                          final MessageDialog dialog = new MessageDialog(ParameterizationContribution.this.parent.getRenderingComposite().getShell(),"Clearing a parameter",null,"Do you wan't to clear parameterization for "+ name + " ?\nIt has " + equations.size() + " mappings.",MessageDialog.QUESTION,new String[]{"OK","Cancel"},1);     \r
461                                          parent.getDisplay().syncExec(new Runnable() {\r
462                                                  public void run() {\r
463                                                          proceed = (dialog.open() != 1);\r
464                                                  };\r
465                                          });\r
466                                          if (proceed) {\r
467                                                  for (IEntity eq : equations) {\r
468                                                          eq.removeRelatedStatements(ShapeEditorResources.equationResource.HasSource);\r
469                                                          eq.removeRelatedStatements(ShapeEditorResources.equationResource.HasTarget);\r
470                                                  }\r
471                                                  return GraphRequestStatus.transactionComplete();\r
472                                          }\r
473                                  }\r
474                                  return GraphRequestStatus.transactionCancel();\r
475                         }\r
476                 });\r
477             \r
478                 \r
479             }\r
480         });\r
481         clearButton.setEnabled(false);\r
482         \r
483         Label hierarchyLabel = toolkit.createLabel(parent, "Model Hierarchy:");\r
484         hierarchyLabel.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));\r
485         hierarchyLabel.setLayoutData(gridData1);\r
486         \r
487         hierarchyExplorer = new GraphExplorer(parent,SWT.MULTI); //new OntologyExplorer("HierarchyExplorer", this, getInput("HierarchyExplorer", model.getId()));\r
488         Tree oeh = hierarchyExplorer.getTree();//hierarchyExplorer.getControl(parent, 1, OntologyExplorer.OntologyTree, SWT.MULTI);\r
489         toolkit.adapt(oeh, true, true);\r
490         \r
491         GridData gd4 = new GridData(GridData.FILL, GridData.FILL, true, true,1,1);\r
492         gd4.heightHint = 200;\r
493         oeh.setLayoutData(gd4);\r
494         hierarchyExplorer.getViewer().addPostSelectionChangedListener(new OESelectionListener() {\r
495 \r
496           protected void resourceSelectionUpdated(StructuredResourceSelection sel) {\r
497                   hierarchySelection = sel;\r
498               updatePropertiesTable();\r
499               updateGenerateButtonStatus();\r
500           }\r
501   \r
502           private void updatePropertiesTable() {\r
503               propertiesTree.setProperties(hierarchySelection);\r
504               \r
505           }\r
506                 \r
507         });\r
508 \r
509         Label propertiesLabel = toolkit.createLabel(parent, "Available properties:");\r
510         propertiesLabel.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));\r
511         propertiesLabel.setLayoutData(gridData1); \r
512         \r
513         //propertiesTable = toolkit.createTable(parent, SWT.MULTI);\r
514         Tree tree = toolkit.createTree(parent, SWT.MULTI);\r
515         propertiesTree = new PropertyTree(tree,ParameterizationContribution.this.parent.getSession());\r
516         //\r
517         GridData gd5 = new GridData(GridData.FILL, GridData.FILL, true, true,1,1);\r
518         gd5.heightHint = 200;\r
519         //propertiesTable.setLayoutData(gd5);\r
520         tree.setLayoutData(gd5);\r
521         tree.addSelectionListener(new SelectionAdapter() {\r
522 \r
523             @Override\r
524             public void widgetSelected(SelectionEvent e) {\r
525                 updateGenerateButtonStatus();\r
526             }\r
527             \r
528         });\r
529 \r
530         \r
531     }\r
532         \r
533     private void updateGenerateButtonStatus() {\r
534         if (parameterSelection.size() == 0) {\r
535             generateButton.setEnabled(false);\r
536             return;\r
537         }\r
538         if (hierarchySelection.size() == 0) {\r
539             generateButton.setEnabled(false);\r
540             return;\r
541         }\r
542         if (propertiesTree.getTree().getSelection().length == 0) {\r
543             generateButton.setEnabled(false);\r
544             return;\r
545         }\r
546         generateButton.setEnabled(true);\r
547     }\r
548     \r
549     List<Resource> res;\r
550     \r
551     private void generateMappings() {\r
552                 final StructuredResourceSelection selectedShapes = hierarchySelection;\r
553                 final StructuredResourceSelection selectedParameter = parameterSelection;\r
554                 \r
555                 final boolean override = overrideButton.getSelection();\r
556                 final boolean ask = askButton.getSelection();\r
557                 //TreeItem[] selectedProperties = propertiesTree.getTree().getSelection();\r
558                 \r
559                 assert (selectedParameter.size() == 1);\r
560                 assert (selectedShapes.size() > 0);\r
561                 //assert(selectedProperties.length > 0);\r
562                 parent.getSession().asyncWrite(new GraphRequestAdapter() {\r
563                         @Override\r
564                         public GraphRequestStatus perform(Graph g) throws Exception {\r
565                                 //fi.vtt.simantics.layer0.stubs.Double parameter = DoubleFactory.create(g,selectedParameter.getSelectionList().get(0));\r
566                                 IEntity parameter = EntityFactory.create(g,selectedParameter.getSelectionList().get(0));\r
567                                 Collection<IEntity> prop = parameter.getRelatedObjects(ShapeEditorResources.g3dResource.HasDefaultDoubleValue);\r
568                                 //fi.vtt.simantics.layer0.stubs.Double defaultValue;\r
569                                 double value = g.getScalarDouble(parameter.getResource());\r
570                                 if (prop.size() == 0) {\r
571                                         //defaultValue = DoubleFactory.createDefault(g);\r
572                                         //parameter.addStatement(ShapeEditorResources.g3dResource.HasDefaultDoubleValue, defaultValue);\r
573                                         parameter.addRelatedScalarDouble(ShapeEditorResources.g3dResource.HasDefaultDoubleValue, value);\r
574                                 } else {\r
575                                         //defaultValue = DoubleFactory.create(prop.iterator().next());\r
576                                         g.setScalarDouble(prop.iterator().next().getResource(),value);\r
577                                 }\r
578                                 //defaultValue.setValue(parameter.getValue());\r
579                                 final Graph graph = g;\r
580                                 parent.getRenderingComposite().getDisplay().syncExec(new Runnable() {\r
581                                         public void run() {\r
582                                                 res = propertiesTree.findLeafPropertyInstances(graph, selectedShapes.getSelectionList());\r
583                                         };\r
584                                 });\r
585                                  \r
586                                 \r
587                                 double refValue = parameter.toProperty().getScalarDouble();\r
588                                 ArrayList<Resource> mappedProperties = new ArrayList<Resource>();\r
589                                 \r
590                                 for (Resource r : res) {\r
591                                         IEntity propertyThing = EntityFactory.create(g,r);\r
592                                         assert (propertyThing.isInstanceOf(g.getBuiltins().Double));\r
593                                         final String name = ResourceDebugUtils.getReadableNameForEntity(propertyThing);\r
594                                         double rb = propertyThing.toProperty().getScalarDouble();\r
595                                         System.out.println("Mapping to " + name + " " + rb);\r
596                                         Collection<IEntity> equations = propertyThing.getRelatedObjects(ShapeEditorResources.equationResource.HasSource);\r
597                                         if (equations.size() != 0) {\r
598                                                 mappedProperties.add(propertyThing.getResource());\r
599                                                 if (override) {\r
600                                                         System.out.println("Override");\r
601                                                         boolean over = true;\r
602                                                         if (ask) {\r
603                                                                 IEntity t = propertyThing;\r
604                                                                 while (t.isInstanceOf(g.getBuiltins().Property)) {\r
605                                                                         Collection<IEntity> ts = t.getRelatedObjects(g.getBuiltins().PropertyOf);\r
606                                                                         // FIXME : traverse all possible routes\r
607                                                                         t = ts.iterator().next();\r
608                                                                 }\r
609                                                                 //StructuredResourceSelection selection = new StructuredResourceSelection(t.getResource());\r
610                                                                 // TODO : do the selection!\r
611                                                                 //hierarchyScheme.setSelection(selection);\r
612                                                                 //fireSelectionChanged();\r
613                                                                 MessageDialog dialog = new MessageDialog(ParameterizationContribution.this.parent.getRenderingComposite().getShell(),\r
614                                                                                 "Override mapping",\r
615                                                                                 null,\r
616                                                                                 "Override mapping to property of highlighted shape?",\r
617                                                                                 MessageDialog.QUESTION, new String[] {\r
618                                                                                                 "Yes","No", "Cancel" }, 0);\r
619                                                                 int i = dialog.open();\r
620                                                                 if (i == 2)\r
621                                                                         return GraphRequestStatus.transactionCancel();\r
622                                                                 over = (i == 0);\r
623                                                         }\r
624                                                         if (over) {\r
625                                                                 \r
626                                                         }\r
627                                                 } else {\r
628                                                         if (equations.size() != 1) {\r
629                                                                 throw new RuntimeException("One property can have only one euquation as source function.");\r
630                                                         }\r
631                                                         IEntity equation = equations.iterator().next();\r
632                                                         if (!equation.isInstanceOf(ShapeEditorResources.equationResource.SecondOrderScalarPolynomial)) {\r
633                                                                 throw new RuntimeException("Only Second order scalar polynomials are supported");\r
634                                                         }\r
635                                                         SecondOrderScalarPolynomial s = new SecondOrderScalarPolynomial(equation);\r
636                                                         s.setA(new double[]{0.0});\r
637                                                         s.setB(new double[]{rb / refValue});\r
638                                                         s.setC(new double[]{0.0});\r
639                                                                 \r
640                                                 }\r
641                                         } else { //override\r
642                                                 // create relation\r
643                                                 SecondOrderScalarPolynomial s = SecondOrderScalarPolynomial.createDefault(g);\r
644                                                 s.setA(new double[]{0.0});\r
645                                                 s.setB(new double[]{rb / refValue});\r
646                                                 s.setC(new double[]{0.0});\r
647                                                 \r
648                                                 // these relations have been instantiated, but addStatements won't delete them so we have to delete them manually.\r
649                                                 s.removeRelatedStatements(ShapeEditorResources.equationResource.HasTarget);\r
650                                                 s.removeRelatedStatements(ShapeEditorResources.equationResource.HasSource);\r
651                                                 \r
652                                                 parameter.addStatement(ShapeEditorResources.equationResource.HasTarget, s);\r
653                                                 \r
654                                                 s.addStatement(ShapeEditorResources.equationResource.HasTarget, propertyThing);\r
655                                                 mappedProperties.add(propertyThing.getResource());\r
656                                         }\r
657                                         \r
658                                 }\r
659                                 G3DModel model = parent.getModel(g);\r
660                                 Collection<Animation> animations = model.getAnimation();\r
661                                 for (Animation animation : animations) {\r
662                                         Collection<Interpolator> interpolators = animation.getInterpolator();\r
663                                         for (Interpolator interpolator : interpolators) {\r
664                                                 IEntity target = interpolator.getTarget();\r
665                                                 for (Resource property : mappedProperties) {\r
666                                                         if (target.getResource().equals(property)) {\r
667                                                                 ShapeEditorResources.curveBuilder.createDefault(interpolator);\r
668                                                         }\r
669                                                 }\r
670                                         }\r
671                                 }\r
672                                 return GraphRequestStatus.transactionComplete();\r
673                         }\r
674                 });\r
675 \r
676 \r
677 \r
678         }\r
679         \r
680 }\r