]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.selectionview/src/org/simantics/selectionview/ConfigurationComposite.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / ConfigurationComposite.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.selectionview;\r
13 \r
14 import java.util.function.Consumer;\r
15 \r
16 import org.eclipse.jface.layout.GridDataFactory;\r
17 import org.eclipse.jface.layout.GridLayoutFactory;\r
18 import org.eclipse.jface.resource.FontDescriptor;\r
19 import org.eclipse.jface.resource.JFaceResources;\r
20 import org.eclipse.jface.resource.LocalResourceManager;\r
21 import org.eclipse.jface.viewers.ISelection;\r
22 import org.eclipse.swt.SWT;\r
23 import org.eclipse.swt.custom.ScrolledComposite;\r
24 import org.eclipse.swt.events.MouseAdapter;\r
25 import org.eclipse.swt.events.MouseEvent;\r
26 import org.eclipse.swt.graphics.Font;\r
27 import org.eclipse.swt.graphics.Point;\r
28 import org.eclipse.swt.widgets.Composite;\r
29 import org.eclipse.swt.widgets.Control;\r
30 import org.eclipse.swt.widgets.Display;\r
31 import org.eclipse.swt.widgets.Event;\r
32 import org.eclipse.swt.widgets.Listener;\r
33 import org.eclipse.swt.widgets.Tree;\r
34 import org.eclipse.ui.IWorkbenchSite;\r
35 import org.eclipse.ui.forms.widgets.FormToolkit;\r
36 import org.simantics.browsing.ui.swt.InputValidators;\r
37 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;\r
38 import org.simantics.browsing.ui.swt.widgets.Label;\r
39 import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;\r
40 import org.simantics.browsing.ui.swt.widgets.TrackedText;\r
41 import org.simantics.browsing.ui.swt.widgets.VariableStringPropertyTextModifier;\r
42 import org.simantics.browsing.ui.swt.widgets.impl.TextModifyListener;\r
43 import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent;\r
44 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
45 import org.simantics.databoard.Bindings;\r
46 import org.simantics.db.ReadGraph;\r
47 import org.simantics.db.Resource;\r
48 import org.simantics.db.common.request.PossibleProperty;\r
49 import org.simantics.db.common.request.ReadRequest;\r
50 import org.simantics.db.exception.DatabaseException;\r
51 import org.simantics.db.layer0.variable.InputValidator;\r
52 import org.simantics.db.layer0.variable.InputValidatorFactory;\r
53 import org.simantics.db.layer0.variable.Variable;\r
54 import org.simantics.db.layer0.variable.Variables;\r
55 import org.simantics.db.management.ISessionContext;\r
56 import org.simantics.layer0.Layer0;\r
57 import org.simantics.utils.ui.ISelectionUtils;\r
58 \r
59 /*\r
60  * \r
61  * @Deprecated in favor of ModelledTabContributor\r
62  * \r
63  */\r
64 abstract public class ConfigurationComposite extends PropertyTabContributorImpl {\r
65 \r
66         protected FormToolkit toolkit;\r
67         protected LocalResourceManager resourceManager;\r
68         protected Font font;\r
69         protected Font smallFont;\r
70         protected Font smallFont2;\r
71 \r
72         ScrolledComposite explorersScroll;\r
73         Composite explorers;\r
74 \r
75         public class ScrollListener implements Listener {\r
76 \r
77                 public Control[] composites;\r
78 \r
79                 final public int minSize;\r
80                 \r
81                 private boolean pending = false;\r
82 \r
83                 public ScrollListener(int minSize) {\r
84                         this.minSize = minSize;\r
85                 }\r
86 \r
87                 @Override\r
88                 public void handleEvent(Event event) {\r
89                         handleEvent();\r
90                 }\r
91 \r
92                 public void handleEvent() {\r
93                         \r
94                         if(pending) return;\r
95                         \r
96                         pending = true;\r
97                         \r
98                         Display.getCurrent().asyncExec(new Runnable() {\r
99 \r
100                                 @Override\r
101                                 public void run() {\r
102                                         doHandleEvent();\r
103                                 }\r
104                                 \r
105                         });\r
106                         \r
107                 }\r
108 \r
109                 public void doHandleEvent() {\r
110                         \r
111                         if(explorers == null || explorers.isDisposed()) return;\r
112                         \r
113                         pending = false;\r
114                         \r
115                         Point o = explorers.getSize();\r
116                         Point p = explorers.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);\r
117                         if(!o.equals(p)) {\r
118                                 explorers.setSize(p);\r
119                         }\r
120 \r
121                 }\r
122 \r
123         }\r
124 \r
125         protected ScrollListener listener;\r
126 \r
127         protected Composite createScroll(Composite body) {\r
128                 return createScroll(body, 1);\r
129         }\r
130 \r
131         protected Composite createScroll(Composite body, int minSize) {\r
132                 return createScroll(body, minSize, 1);\r
133         }\r
134 \r
135         protected Composite createScroll(Composite body, int minSize, int columnCount) {\r
136 \r
137                 listener = new ScrollListener(minSize);\r
138 \r
139                 explorersScroll = new ScrolledComposite(body, SWT.V_SCROLL | SWT.BORDER);\r
140                 explorersScroll.setBackground(Display.getCurrent().getSystemColor(\r
141                                 SWT.COLOR_WHITE));\r
142                 explorersScroll.setLayout(GridLayoutFactory.fillDefaults()\r
143                                 .margins(0, 0).spacing(0, 0).create());\r
144                 explorersScroll.setExpandVertical(false);\r
145                 explorersScroll.setExpandHorizontal(true);\r
146                 GridDataFactory.fillDefaults().grab(true, true).span(2, 1)\r
147                                 .applyTo(explorersScroll);\r
148 \r
149                 explorers = new Composite(explorersScroll, SWT.NONE);\r
150                 explorers.setBackground(Display.getCurrent().getSystemColor(\r
151                                 SWT.COLOR_WHITE));\r
152                 // Make sure that exploders are visible\r
153                 explorers.setSize(10, 300);\r
154                 explorers.setLayout(GridLayoutFactory.fillDefaults().margins(0, 0).numColumns(columnCount)\r
155                                 .spacing(0, 0).create());\r
156                 GridDataFactory.fillDefaults().grab(false, false).applyTo(explorers);\r
157 \r
158                 explorersScroll.setMinSize(100, 100);\r
159                 explorersScroll.setContent(explorers);\r
160 \r
161                 return explorers;\r
162 \r
163         }\r
164 \r
165         protected void registerScrollExplorers(Control... composites) {\r
166 \r
167                 listener.composites = composites;\r
168                 // listener.cache = new int[composites.length];\r
169 \r
170                 for (Control composite : composites) {\r
171                         if (composite instanceof GraphExplorerComposite) {\r
172                                 ScrollListener listener = this.listener;\r
173                                 if (listener == null)\r
174                                         listener = this.listener;\r
175                                 Tree tree = ((GraphExplorerComposite) composite)\r
176                                                 .getExplorerControl();\r
177                                 tree.addListener(SWT.SetData, listener);\r
178 //                              tree.addListener(SWT.Resize, listener);\r
179 //                              tree.addListener(SWT.Expand, listener);\r
180 //                              tree.addListener(SWT.Collapse, listener);\r
181                         }\r
182                 }\r
183 \r
184         }\r
185 \r
186         protected Label createHeaderLabel(final Composite body,\r
187                         final Composite headerComposite, final ISessionContext context,\r
188                         final WidgetSupport support) {\r
189 \r
190                 final Label header = new Label(headerComposite, support, 0);\r
191                 header.getWidget().addMouseListener(new MouseAdapter() {\r
192 \r
193                         @Override\r
194                         public void mouseUp(MouseEvent e) {\r
195 \r
196                                 final Display display = Display.getCurrent();\r
197                                 assert (display != null);\r
198 \r
199                                 context.getSession().asyncRequest(new ReadRequest() {\r
200 \r
201                                         @Override\r
202                                         public void run(ReadGraph graph) throws DatabaseException {\r
203 \r
204                                                 Object input = support.getInput();\r
205                                                 if (input != WidgetSupport.NO_INPUT && input != null\r
206                                                                 && input instanceof ISelection) {\r
207 \r
208                                                         final Variable inputVariable = ISelectionUtils\r
209                                                                         .filterSingleSelection((ISelection) input,\r
210                                                                                         Variable.class);\r
211                                                         final Variable nameVariable = inputVariable\r
212                                                                         .getPossibleProperty(graph, Variables.NAME);\r
213                                                         final String initial = nameVariable.getValue(graph,\r
214                                                                         Bindings.STRING);\r
215                                                         \r
216                                                         //final InputValidator validator = nameVariable.getInterface(graph, InputValidator.class);\r
217                                                         \r
218                                                         Layer0 L0 = Layer0.getInstance(graph);\r
219                                                         final Resource inputResource = inputVariable.getRepresents(graph);\r
220                                                         if(inputResource == null) return;\r
221                                                         final Resource nameResource = graph.getPossibleObject(inputResource, L0.HasName);\r
222                                                         if(nameResource == null) return;\r
223                                                         final Resource validatorResource = graph.syncRequest(new PossibleProperty(nameResource, "HasInputValidator"));\r
224                                                         if(validatorResource == null) return;\r
225 \r
226                                                         \r
227                                                         final InputValidatorFactory validatorFactory = graph.adapt(validatorResource, InputValidatorFactory.class); \r
228                                                         final InputValidator validator = validatorFactory.create(graph, nameVariable); \r
229                                                         \r
230                                                         if (initial != null && validator != null) {\r
231 \r
232                                                                 if (display.isDisposed())\r
233                                                                         return;\r
234 \r
235                                                                 display.asyncExec(new Runnable() {\r
236 \r
237                                                                         @Override\r
238                                                                         public void run() {\r
239 \r
240                                                                                 GridDataFactory.fillDefaults()\r
241                                                                                 .grab(true, false).span(1, 1)\r
242                                                                                 .align(SWT.CENTER, SWT.CENTER)\r
243                                                                                 .applyTo(header.getWidget());\r
244                                                                                 \r
245                                                                                 header.getWidget().setVisible(false);\r
246 \r
247                                                                                 final Composite editor = new Composite(headerComposite, SWT.NONE);\r
248                                                                                 editor.setLayout(GridLayoutFactory.fillDefaults()\r
249                                                                                                 .margins(0, 0).spacing(0, 0).create());\r
250                                                                                 editor.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));\r
251 \r
252 \r
253                                                                                 final TrackedText name = new TrackedText(\r
254                                                                                                 editor, support,\r
255                                                                                                 SWT.NONE);\r
256                                                                                 name.setText(initial);\r
257                                                                                 name.setInputValidator(InputValidators\r
258                                                                                                 .string(validator));\r
259                                                                                 name.setFont(font);\r
260                                                                                 name.addModifyListener(new VariableStringPropertyTextModifier(\r
261                                                                                                 "HasName"));\r
262                                                                                 name.addModifyListener(new TextModifyListener() {\r
263 \r
264                                                                                         @Override\r
265                                                                                         public void modifyText(\r
266                                                                                                         TrackedModifyEvent e) {\r
267 \r
268                                                                                                 editor.dispose();\r
269                                                                                                 header.getWidget().setVisible(\r
270                                                                                                                 true);\r
271                                                                                                 GridDataFactory\r
272                                                                                                                 .fillDefaults()\r
273                                                                                                                 .grab(true, false)\r
274                                                                                                                 .span(2, 1)\r
275                                                                                                                 .align(SWT.CENTER,\r
276                                                                                                                                 SWT.CENTER)\r
277                                                                                                                 .applyTo(\r
278                                                                                                                                 header.getWidget());\r
279 \r
280                                                                                         }\r
281                                                                                 });\r
282                                                                                 GridDataFactory.fillDefaults()\r
283                                                                                 .grab(true, false).span(1, 1)\r
284                                                                                 .align(SWT.FILL, SWT.FILL)\r
285                                                                                 .applyTo(name.getWidget());                                                                             \r
286 \r
287                                                                                 name.getWidget().forceFocus();\r
288                                                                                 name.startEdit(true);\r
289                                                                                 editor.moveAbove(null);\r
290                                                                                 \r
291                                                                                 final Label label = new Label(editor, support, SWT.NONE);\r
292                                                                                 label.setFont(smallFont2);\r
293                                                                                 label.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));\r
294                                                                                 label.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_RED));\r
295                                                                                 GridDataFactory.fillDefaults()\r
296                                                                                 .grab(true, false).span(1, 1)\r
297                                                                                 .align(SWT.FILL, SWT.FILL)\r
298                                                                                 .applyTo(label.getWidget());                                                                            \r
299 \r
300                                                                                 name.addValidationListener(new Consumer<String>() {\r
301 \r
302                                                                                         @Override\r
303                                                                                         public void accept(String error) {\r
304                                                                                                 if(error != null) label.setText(error);\r
305                                                                                                 else label.setText("");\r
306                                                                                         }\r
307                                                                                         \r
308                                                                                 });\r
309                                                                                 \r
310                                                                                 GridDataFactory.fillDefaults()\r
311                                                                                                 .grab(true, false).span(1, 1)\r
312                                                                                                 .align(SWT.FILL, SWT.FILL)\r
313                                                                                                 .applyTo(editor);\r
314                                                                                 body.layout(true);\r
315                                                                                 headerComposite.layout(true);\r
316 \r
317                                                                                 support.update(name);\r
318 \r
319                                                                         }\r
320 \r
321                                                                 });\r
322 \r
323                                                         }\r
324 \r
325                                                 }\r
326 \r
327                                         }\r
328 \r
329                                 });\r
330 \r
331                         }\r
332 \r
333                 });\r
334 \r
335                 header.setFont(font);\r
336                 header.setBackground(Display.getCurrent().getSystemColor(\r
337                                 SWT.COLOR_WHITE));\r
338                 GridDataFactory.fillDefaults().grab(true, false).span(2, 1)\r
339                                 .align(SWT.CENTER, SWT.CENTER).applyTo(header.getWidget());\r
340 \r
341                 return header;\r
342 \r
343         }\r
344 \r
345         protected Label createResourceHeaderLabel(final Composite body,\r
346                         final Composite headerComposite, final ISessionContext context,\r
347                         final WidgetSupport support) {\r
348 \r
349                 final Label header = new Label(headerComposite, support, 0);\r
350                 header.getWidget().addMouseListener(new MouseAdapter() {\r
351 \r
352                         @Override\r
353                         public void mouseUp(MouseEvent e) {\r
354 \r
355                                 final Display display = Display.getCurrent();\r
356                                 assert (display != null);\r
357 \r
358                                 context.getSession().asyncRequest(new ReadRequest() {\r
359 \r
360                                         @Override\r
361                                         public void run(ReadGraph graph) throws DatabaseException {\r
362 \r
363                                                 Object input = support.getInput();\r
364                                                 if (input != WidgetSupport.NO_INPUT && input != null\r
365                                                                 && input instanceof ISelection) {\r
366 \r
367                                                         Layer0 L0 = Layer0.getInstance(graph);\r
368 \r
369                                                         final Resource streamClass = ISelectionUtils\r
370                                                                         .filterSingleSelection((ISelection) input,\r
371                                                                                         Resource.class);\r
372                                                         // final Variable inputVariable =\r
373                                                         // ISelectionUtils.filterSingleSelection((ISelection)input,\r
374                                                         // Variable.class);\r
375                                                         // final Variable nameVariable =\r
376                                                         // inputVariable.getPossibleProperty(graph,\r
377                                                         // Variables.NAME);\r
378                                                         final String initial = graph.getRelatedValue(\r
379                                                                         streamClass, L0.HasName, Bindings.STRING);\r
380                                                         // final InputValidator validator =\r
381                                                         // nameVariable.getInterface(graph,\r
382                                                         // InputValidator.class);\r
383                                                         if (initial != null) {\r
384 \r
385                                                                 if (display.isDisposed())\r
386                                                                         return;\r
387 \r
388                                                                 display.asyncExec(new Runnable() {\r
389 \r
390                                                                         @Override\r
391                                                                         public void run() {\r
392 \r
393                                                                                 GridDataFactory.fillDefaults()\r
394                                                                                                 .grab(true, false).span(1, 1)\r
395                                                                                                 .align(SWT.CENTER, SWT.CENTER)\r
396                                                                                                 .applyTo(header.getWidget());\r
397                                                                                 header.getWidget().setVisible(false);\r
398 \r
399                                                                                 // header.getWidget().setVisible(false);\r
400                                                                                 // header.getWidget().setText("");\r
401                                                                                 // GridDataFactory.fillDefaults().grab(false,\r
402                                                                                 // false).span(1, 1).align(SWT.CENTER,\r
403                                                                                 // SWT.CENTER).applyTo(header.getWidget());\r
404 \r
405                                                                                 final TrackedText name = new TrackedText(\r
406                                                                                                 headerComposite, support,\r
407                                                                                                 SWT.NONE);\r
408                                                                                 name.setText(initial);\r
409                                                                                 name.setFont(font);\r
410                                                                                 name.addModifyListener(new StringPropertyModifier(\r
411                                                                                                 context, Layer0.URIs.HasName));\r
412                                                                                 name.addModifyListener(new TextModifyListener() {\r
413 \r
414                                                                                         @Override\r
415                                                                                         public void modifyText(\r
416                                                                                                         TrackedModifyEvent e) {\r
417 \r
418                                                                                                 name.getWidget().dispose();\r
419                                                                                                 header.getWidget().setVisible(\r
420                                                                                                                 true);\r
421                                                                                                 GridDataFactory\r
422                                                                                                                 .fillDefaults()\r
423                                                                                                                 .grab(true, false)\r
424                                                                                                                 .span(2, 1)\r
425                                                                                                                 .align(SWT.CENTER,\r
426                                                                                                                                 SWT.CENTER)\r
427                                                                                                                 .applyTo(\r
428                                                                                                                                 header.getWidget());\r
429 \r
430                                                                                                 // createResourceHeaderLabel(body,\r
431                                                                                                 // headerComposite, context,\r
432                                                                                                 // support);\r
433 \r
434                                                                                                 body.layout(true);\r
435                                                                                                 headerComposite.layout(true);\r
436 \r
437                                                                                         }\r
438                                                                                 });\r
439 \r
440                                                                                 name.getWidget().forceFocus();\r
441                                                                                 name.startEdit(true);\r
442                                                                                 name.getWidget().moveAbove(null);\r
443 \r
444                                                                                 GridDataFactory.fillDefaults()\r
445                                                                                                 .grab(true, false).span(1, 1)\r
446                                                                                                 .align(SWT.FILL, SWT.FILL)\r
447                                                                                                 .applyTo(name.getWidget());\r
448                                                                                 body.layout(true);\r
449                                                                                 headerComposite.layout(true);\r
450 \r
451                                                                                 support.update(name);\r
452 \r
453                                                                         }\r
454 \r
455                                                                 });\r
456 \r
457                                                         }\r
458 \r
459                                                 }\r
460 \r
461                                         }\r
462 \r
463                                 });\r
464 \r
465                         }\r
466 \r
467                 });\r
468 \r
469                 header.setFont(font);\r
470                 header.setBackground(Display.getCurrent().getSystemColor(\r
471                                 SWT.COLOR_WHITE));\r
472                 GridDataFactory.fillDefaults().grab(true, false).span(2, 1)\r
473                                 .align(SWT.CENTER, SWT.CENTER).applyTo(header.getWidget());\r
474 \r
475                 return header;\r
476 \r
477         }\r
478 \r
479         protected Composite createHeaderComposite(Composite body) {\r
480                 return createHeaderComposite(body, 2);\r
481 \r
482         }\r
483 \r
484         protected Composite createHeaderComposite(Composite body, int width) {\r
485 \r
486                 Composite headerComposite = new Composite(body, 0);\r
487                 headerComposite.setBackground(body.getDisplay().getSystemColor(\r
488                                 SWT.COLOR_BLACK));\r
489                 GridDataFactory.fillDefaults().span(2, 1).grab(true, false)\r
490                                 .applyTo(headerComposite);\r
491                 GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(1)\r
492                                 .extendedMargins(2, 2, 2, 2).applyTo(headerComposite);\r
493 \r
494                 Composite headerComposite2 = new Composite(headerComposite, 0);\r
495                 headerComposite2.setBackground(body.getDisplay().getSystemColor(\r
496                                 SWT.COLOR_WHITE));\r
497                 GridDataFactory.fillDefaults().span(1, 1).grab(true, false)\r
498                                 .applyTo(headerComposite2);\r
499                 GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(width)\r
500                                 .extendedMargins(3, 3, 3, 3).applyTo(headerComposite2);\r
501 \r
502                 return headerComposite2;\r
503 \r
504         }\r
505 \r
506         protected void create(Composite body, IWorkbenchSite site,\r
507                         final ISessionContext context, WidgetSupport support) {\r
508 \r
509         }\r
510 \r
511         final public void createControls(Composite body, IWorkbenchSite site,\r
512                         final ISessionContext context, WidgetSupport support) {\r
513 \r
514                 resourceManager = new LocalResourceManager(JFaceResources.getResources(), body);\r
515                 toolkit = new FormToolkit(body.getDisplay());\r
516 \r
517                 body.addListener(SWT.Dispose, new Listener() {\r
518                         @Override\r
519                         public void handleEvent(Event event) {\r
520                                 toolkit.dispose();\r
521                         }\r
522                 });\r
523 \r
524                 font = (Font) resourceManager.get(FontDescriptor.createFrom("Arial", 14, SWT.NONE));\r
525                 smallFont = (Font) resourceManager.get(FontDescriptor.createFrom("Arial", 10, SWT.NONE));\r
526                 smallFont2 = (Font) resourceManager.get(FontDescriptor.createFrom("Arial", 8, SWT.NONE));\r
527 \r
528                 create(body, site, context, support);\r
529 \r
530         }\r
531 \r
532         @Override\r
533         public int hashCode() {\r
534                 return getClass().hashCode();\r
535         }\r
536 \r
537         @Override\r
538         public boolean equals(Object object) {\r
539                 if (this == object)\r
540                         return true;\r
541                 else if (object == null)\r
542                         return false;\r
543                 return getClass().equals(object.getClass());\r
544         }\r
545 \r
546         @Override\r
547         public String getPartName(ISelection forSelection) {\r
548                 return "Selection";\r
549         }\r
550 \r
551 }\r