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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.selectionview;
\r
14 import java.util.function.Consumer;
\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
61 * @Deprecated in favor of ModelledTabContributor
\r
64 abstract public class ConfigurationComposite extends PropertyTabContributorImpl {
\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
72 ScrolledComposite explorersScroll;
\r
73 Composite explorers;
\r
75 public class ScrollListener implements Listener {
\r
77 public Control[] composites;
\r
79 final public int minSize;
\r
81 private boolean pending = false;
\r
83 public ScrollListener(int minSize) {
\r
84 this.minSize = minSize;
\r
88 public void handleEvent(Event event) {
\r
92 public void handleEvent() {
\r
98 Display.getCurrent().asyncExec(new Runnable() {
\r
101 public void run() {
\r
109 public void doHandleEvent() {
\r
111 if(explorers == null || explorers.isDisposed()) return;
\r
115 Point o = explorers.getSize();
\r
116 Point p = explorers.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
\r
118 explorers.setSize(p);
\r
125 protected ScrollListener listener;
\r
127 protected Composite createScroll(Composite body) {
\r
128 return createScroll(body, 1);
\r
131 protected Composite createScroll(Composite body, int minSize) {
\r
132 return createScroll(body, minSize, 1);
\r
135 protected Composite createScroll(Composite body, int minSize, int columnCount) {
\r
137 listener = new ScrollListener(minSize);
\r
139 explorersScroll = new ScrolledComposite(body, SWT.V_SCROLL | SWT.BORDER);
\r
140 explorersScroll.setBackground(Display.getCurrent().getSystemColor(
\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
149 explorers = new Composite(explorersScroll, SWT.NONE);
\r
150 explorers.setBackground(Display.getCurrent().getSystemColor(
\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
158 explorersScroll.setMinSize(100, 100);
\r
159 explorersScroll.setContent(explorers);
\r
165 protected void registerScrollExplorers(Control... composites) {
\r
167 listener.composites = composites;
\r
168 // listener.cache = new int[composites.length];
\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
186 protected Label createHeaderLabel(final Composite body,
\r
187 final Composite headerComposite, final ISessionContext context,
\r
188 final WidgetSupport support) {
\r
190 final Label header = new Label(headerComposite, support, 0);
\r
191 header.getWidget().addMouseListener(new MouseAdapter() {
\r
194 public void mouseUp(MouseEvent e) {
\r
196 final Display display = Display.getCurrent();
\r
197 assert (display != null);
\r
199 context.getSession().asyncRequest(new ReadRequest() {
\r
202 public void run(ReadGraph graph) throws DatabaseException {
\r
204 Object input = support.getInput();
\r
205 if (input != WidgetSupport.NO_INPUT && input != null
\r
206 && input instanceof ISelection) {
\r
208 final Variable inputVariable = ISelectionUtils
\r
209 .filterSingleSelection((ISelection) input,
\r
211 final Variable nameVariable = inputVariable
\r
212 .getPossibleProperty(graph, Variables.NAME);
\r
213 final String initial = nameVariable.getValue(graph,
\r
216 //final InputValidator validator = nameVariable.getInterface(graph, InputValidator.class);
\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
227 final InputValidatorFactory validatorFactory = graph.adapt(validatorResource, InputValidatorFactory.class);
\r
228 final InputValidator validator = validatorFactory.create(graph, nameVariable);
\r
230 if (initial != null && validator != null) {
\r
232 if (display.isDisposed())
\r
235 display.asyncExec(new Runnable() {
\r
238 public void run() {
\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
245 header.getWidget().setVisible(false);
\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
253 final TrackedText name = new TrackedText(
\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
262 name.addModifyListener(new TextModifyListener() {
\r
265 public void modifyText(
\r
266 TrackedModifyEvent e) {
\r
269 header.getWidget().setVisible(
\r
278 header.getWidget());
\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
287 name.getWidget().forceFocus();
\r
288 name.startEdit(true);
\r
289 editor.moveAbove(null);
\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
300 name.addValidationListener(new Consumer<String>() {
\r
303 public void accept(String error) {
\r
304 if(error != null) label.setText(error);
\r
305 else label.setText("");
\r
310 GridDataFactory.fillDefaults()
\r
311 .grab(true, false).span(1, 1)
\r
312 .align(SWT.FILL, SWT.FILL)
\r
315 headerComposite.layout(true);
\r
317 support.update(name);
\r
335 header.setFont(font);
\r
336 header.setBackground(Display.getCurrent().getSystemColor(
\r
338 GridDataFactory.fillDefaults().grab(true, false).span(2, 1)
\r
339 .align(SWT.CENTER, SWT.CENTER).applyTo(header.getWidget());
\r
345 protected Label createResourceHeaderLabel(final Composite body,
\r
346 final Composite headerComposite, final ISessionContext context,
\r
347 final WidgetSupport support) {
\r
349 final Label header = new Label(headerComposite, support, 0);
\r
350 header.getWidget().addMouseListener(new MouseAdapter() {
\r
353 public void mouseUp(MouseEvent e) {
\r
355 final Display display = Display.getCurrent();
\r
356 assert (display != null);
\r
358 context.getSession().asyncRequest(new ReadRequest() {
\r
361 public void run(ReadGraph graph) throws DatabaseException {
\r
363 Object input = support.getInput();
\r
364 if (input != WidgetSupport.NO_INPUT && input != null
\r
365 && input instanceof ISelection) {
\r
367 Layer0 L0 = Layer0.getInstance(graph);
\r
369 final Resource streamClass = ISelectionUtils
\r
370 .filterSingleSelection((ISelection) input,
\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
385 if (display.isDisposed())
\r
388 display.asyncExec(new Runnable() {
\r
391 public void run() {
\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
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
405 final TrackedText name = new TrackedText(
\r
406 headerComposite, support,
\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
415 public void modifyText(
\r
416 TrackedModifyEvent e) {
\r
418 name.getWidget().dispose();
\r
419 header.getWidget().setVisible(
\r
428 header.getWidget());
\r
430 // createResourceHeaderLabel(body,
\r
431 // headerComposite, context,
\r
435 headerComposite.layout(true);
\r
440 name.getWidget().forceFocus();
\r
441 name.startEdit(true);
\r
442 name.getWidget().moveAbove(null);
\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
449 headerComposite.layout(true);
\r
451 support.update(name);
\r
469 header.setFont(font);
\r
470 header.setBackground(Display.getCurrent().getSystemColor(
\r
472 GridDataFactory.fillDefaults().grab(true, false).span(2, 1)
\r
473 .align(SWT.CENTER, SWT.CENTER).applyTo(header.getWidget());
\r
479 protected Composite createHeaderComposite(Composite body) {
\r
480 return createHeaderComposite(body, 2);
\r
484 protected Composite createHeaderComposite(Composite body, int width) {
\r
486 Composite headerComposite = new Composite(body, 0);
\r
487 headerComposite.setBackground(body.getDisplay().getSystemColor(
\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
494 Composite headerComposite2 = new Composite(headerComposite, 0);
\r
495 headerComposite2.setBackground(body.getDisplay().getSystemColor(
\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
502 return headerComposite2;
\r
506 protected void create(Composite body, IWorkbenchSite site,
\r
507 final ISessionContext context, WidgetSupport support) {
\r
511 final public void createControls(Composite body, IWorkbenchSite site,
\r
512 final ISessionContext context, WidgetSupport support) {
\r
514 resourceManager = new LocalResourceManager(JFaceResources.getResources(), body);
\r
515 toolkit = new FormToolkit(body.getDisplay());
\r
517 body.addListener(SWT.Dispose, new Listener() {
\r
519 public void handleEvent(Event event) {
\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
528 create(body, site, context, support);
\r
533 public int hashCode() {
\r
534 return getClass().hashCode();
\r
538 public boolean equals(Object object) {
\r
539 if (this == object)
\r
541 else if (object == null)
\r
543 return getClass().equals(object.getClass());
\r
547 public String getPartName(ISelection forSelection) {
\r
548 return "Selection";
\r