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