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