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