]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.selectionview/src/org/simantics/selectionview/ConfigurationComposite.java b/bundles/org.simantics.selectionview/src/org/simantics/selectionview/ConfigurationComposite.java
new file mode 100644 (file)
index 0000000..d4bcf15
--- /dev/null
@@ -0,0 +1,551 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.selectionview;\r
+\r
+import java.util.function.Consumer;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.jface.resource.FontDescriptor;\r
+import org.eclipse.jface.resource.JFaceResources;\r
+import org.eclipse.jface.resource.LocalResourceManager;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.custom.ScrolledComposite;\r
+import org.eclipse.swt.events.MouseAdapter;\r
+import org.eclipse.swt.events.MouseEvent;\r
+import org.eclipse.swt.graphics.Font;\r
+import org.eclipse.swt.graphics.Point;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.swt.widgets.Event;\r
+import org.eclipse.swt.widgets.Listener;\r
+import org.eclipse.swt.widgets.Tree;\r
+import org.eclipse.ui.IWorkbenchSite;\r
+import org.eclipse.ui.forms.widgets.FormToolkit;\r
+import org.simantics.browsing.ui.swt.InputValidators;\r
+import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;\r
+import org.simantics.browsing.ui.swt.widgets.Label;\r
+import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;\r
+import org.simantics.browsing.ui.swt.widgets.TrackedText;\r
+import org.simantics.browsing.ui.swt.widgets.VariableStringPropertyTextModifier;\r
+import org.simantics.browsing.ui.swt.widgets.impl.TextModifyListener;\r
+import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.PossibleProperty;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.InputValidator;\r
+import org.simantics.db.layer0.variable.InputValidatorFactory;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+/*\r
+ * \r
+ * @Deprecated in favor of ModelledTabContributor\r
+ * \r
+ */\r
+abstract public class ConfigurationComposite extends PropertyTabContributorImpl {\r
+\r
+       protected FormToolkit toolkit;\r
+       protected LocalResourceManager resourceManager;\r
+       protected Font font;\r
+       protected Font smallFont;\r
+       protected Font smallFont2;\r
+\r
+       ScrolledComposite explorersScroll;\r
+       Composite explorers;\r
+\r
+       public class ScrollListener implements Listener {\r
+\r
+               public Control[] composites;\r
+\r
+               final public int minSize;\r
+               \r
+               private boolean pending = false;\r
+\r
+               public ScrollListener(int minSize) {\r
+                       this.minSize = minSize;\r
+               }\r
+\r
+               @Override\r
+               public void handleEvent(Event event) {\r
+                       handleEvent();\r
+               }\r
+\r
+               public void handleEvent() {\r
+                       \r
+                       if(pending) return;\r
+                       \r
+                       pending = true;\r
+                       \r
+                       Display.getCurrent().asyncExec(new Runnable() {\r
+\r
+                               @Override\r
+                               public void run() {\r
+                                       doHandleEvent();\r
+                               }\r
+                               \r
+                       });\r
+                       \r
+               }\r
+\r
+               public void doHandleEvent() {\r
+                       \r
+                       if(explorers == null || explorers.isDisposed()) return;\r
+                       \r
+                       pending = false;\r
+                       \r
+                       Point o = explorers.getSize();\r
+                       Point p = explorers.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);\r
+                       if(!o.equals(p)) {\r
+                               explorers.setSize(p);\r
+                       }\r
+\r
+               }\r
+\r
+       }\r
+\r
+       protected ScrollListener listener;\r
+\r
+       protected Composite createScroll(Composite body) {\r
+               return createScroll(body, 1);\r
+       }\r
+\r
+       protected Composite createScroll(Composite body, int minSize) {\r
+               return createScroll(body, minSize, 1);\r
+       }\r
+\r
+       protected Composite createScroll(Composite body, int minSize, int columnCount) {\r
+\r
+               listener = new ScrollListener(minSize);\r
+\r
+               explorersScroll = new ScrolledComposite(body, SWT.V_SCROLL | SWT.BORDER);\r
+               explorersScroll.setBackground(Display.getCurrent().getSystemColor(\r
+                               SWT.COLOR_WHITE));\r
+               explorersScroll.setLayout(GridLayoutFactory.fillDefaults()\r
+                               .margins(0, 0).spacing(0, 0).create());\r
+               explorersScroll.setExpandVertical(false);\r
+               explorersScroll.setExpandHorizontal(true);\r
+               GridDataFactory.fillDefaults().grab(true, true).span(2, 1)\r
+                               .applyTo(explorersScroll);\r
+\r
+               explorers = new Composite(explorersScroll, SWT.NONE);\r
+               explorers.setBackground(Display.getCurrent().getSystemColor(\r
+                               SWT.COLOR_WHITE));\r
+               // Make sure that exploders are visible\r
+               explorers.setSize(10, 300);\r
+               explorers.setLayout(GridLayoutFactory.fillDefaults().margins(0, 0).numColumns(columnCount)\r
+                               .spacing(0, 0).create());\r
+               GridDataFactory.fillDefaults().grab(false, false).applyTo(explorers);\r
+\r
+               explorersScroll.setMinSize(100, 100);\r
+               explorersScroll.setContent(explorers);\r
+\r
+               return explorers;\r
+\r
+       }\r
+\r
+       protected void registerScrollExplorers(Control... composites) {\r
+\r
+               listener.composites = composites;\r
+               // listener.cache = new int[composites.length];\r
+\r
+               for (Control composite : composites) {\r
+                       if (composite instanceof GraphExplorerComposite) {\r
+                               ScrollListener listener = this.listener;\r
+                               if (listener == null)\r
+                                       listener = this.listener;\r
+                               Tree tree = ((GraphExplorerComposite) composite)\r
+                                               .getExplorerControl();\r
+                               tree.addListener(SWT.SetData, listener);\r
+//                             tree.addListener(SWT.Resize, listener);\r
+//                             tree.addListener(SWT.Expand, listener);\r
+//                             tree.addListener(SWT.Collapse, listener);\r
+                       }\r
+               }\r
+\r
+       }\r
+\r
+       protected Label createHeaderLabel(final Composite body,\r
+                       final Composite headerComposite, final ISessionContext context,\r
+                       final WidgetSupport support) {\r
+\r
+               final Label header = new Label(headerComposite, support, 0);\r
+               header.getWidget().addMouseListener(new MouseAdapter() {\r
+\r
+                       @Override\r
+                       public void mouseUp(MouseEvent e) {\r
+\r
+                               final Display display = Display.getCurrent();\r
+                               assert (display != null);\r
+\r
+                               context.getSession().asyncRequest(new ReadRequest() {\r
+\r
+                                       @Override\r
+                                       public void run(ReadGraph graph) throws DatabaseException {\r
+\r
+                                               Object input = support.getInput();\r
+                                               if (input != WidgetSupport.NO_INPUT && input != null\r
+                                                               && input instanceof ISelection) {\r
+\r
+                                                       final Variable inputVariable = ISelectionUtils\r
+                                                                       .filterSingleSelection((ISelection) input,\r
+                                                                                       Variable.class);\r
+                                                       final Variable nameVariable = inputVariable\r
+                                                                       .getPossibleProperty(graph, Variables.NAME);\r
+                                                       final String initial = nameVariable.getValue(graph,\r
+                                                                       Bindings.STRING);\r
+                                                       \r
+                                                       //final InputValidator validator = nameVariable.getInterface(graph, InputValidator.class);\r
+                                                       \r
+                                                       Layer0 L0 = Layer0.getInstance(graph);\r
+                                                       final Resource inputResource = inputVariable.getRepresents(graph);\r
+                                                       if(inputResource == null) return;\r
+                                                       final Resource nameResource = graph.getPossibleObject(inputResource, L0.HasName);\r
+                                                       if(nameResource == null) return;\r
+                                                       final Resource validatorResource = graph.syncRequest(new PossibleProperty(nameResource, "HasInputValidator"));\r
+                                                       if(validatorResource == null) return;\r
+\r
+                                                       \r
+                                                       final InputValidatorFactory validatorFactory = graph.adapt(validatorResource, InputValidatorFactory.class); \r
+                                                       final InputValidator validator = validatorFactory.create(graph, nameVariable); \r
+                                                       \r
+                                                       if (initial != null && validator != null) {\r
+\r
+                                                               if (display.isDisposed())\r
+                                                                       return;\r
+\r
+                                                               display.asyncExec(new Runnable() {\r
+\r
+                                                                       @Override\r
+                                                                       public void run() {\r
+\r
+                                                                               GridDataFactory.fillDefaults()\r
+                                                                               .grab(true, false).span(1, 1)\r
+                                                                               .align(SWT.CENTER, SWT.CENTER)\r
+                                                                               .applyTo(header.getWidget());\r
+                                                                               \r
+                                                                               header.getWidget().setVisible(false);\r
+\r
+                                                                               final Composite editor = new Composite(headerComposite, SWT.NONE);\r
+                                                                               editor.setLayout(GridLayoutFactory.fillDefaults()\r
+                                                                                               .margins(0, 0).spacing(0, 0).create());\r
+                                                                               editor.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));\r
+\r
+\r
+                                                                               final TrackedText name = new TrackedText(\r
+                                                                                               editor, support,\r
+                                                                                               SWT.NONE);\r
+                                                                               name.setText(initial);\r
+                                                                               name.setInputValidator(InputValidators\r
+                                                                                               .string(validator));\r
+                                                                               name.setFont(font);\r
+                                                                               name.addModifyListener(new VariableStringPropertyTextModifier(\r
+                                                                                               "HasName"));\r
+                                                                               name.addModifyListener(new TextModifyListener() {\r
+\r
+                                                                                       @Override\r
+                                                                                       public void modifyText(\r
+                                                                                                       TrackedModifyEvent e) {\r
+\r
+                                                                                               editor.dispose();\r
+                                                                                               header.getWidget().setVisible(\r
+                                                                                                               true);\r
+                                                                                               GridDataFactory\r
+                                                                                                               .fillDefaults()\r
+                                                                                                               .grab(true, false)\r
+                                                                                                               .span(2, 1)\r
+                                                                                                               .align(SWT.CENTER,\r
+                                                                                                                               SWT.CENTER)\r
+                                                                                                               .applyTo(\r
+                                                                                                                               header.getWidget());\r
+\r
+                                                                                       }\r
+                                                                               });\r
+                                                                               GridDataFactory.fillDefaults()\r
+                                                                               .grab(true, false).span(1, 1)\r
+                                                                               .align(SWT.FILL, SWT.FILL)\r
+                                                                               .applyTo(name.getWidget());                                                                             \r
+\r
+                                                                               name.getWidget().forceFocus();\r
+                                                                               name.startEdit(true);\r
+                                                                               editor.moveAbove(null);\r
+                                                                               \r
+                                                                               final Label label = new Label(editor, support, SWT.NONE);\r
+                                                                               label.setFont(smallFont2);\r
+                                                                               label.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));\r
+                                                                               label.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_RED));\r
+                                                                               GridDataFactory.fillDefaults()\r
+                                                                               .grab(true, false).span(1, 1)\r
+                                                                               .align(SWT.FILL, SWT.FILL)\r
+                                                                               .applyTo(label.getWidget());                                                                            \r
+\r
+                                                                               name.addValidationListener(new Consumer<String>() {\r
+\r
+                                                                                       @Override\r
+                                                                                       public void accept(String error) {\r
+                                                                                               if(error != null) label.setText(error);\r
+                                                                                               else label.setText("");\r
+                                                                                       }\r
+                                                                                       \r
+                                                                               });\r
+                                                                               \r
+                                                                               GridDataFactory.fillDefaults()\r
+                                                                                               .grab(true, false).span(1, 1)\r
+                                                                                               .align(SWT.FILL, SWT.FILL)\r
+                                                                                               .applyTo(editor);\r
+                                                                               body.layout(true);\r
+                                                                               headerComposite.layout(true);\r
+\r
+                                                                               support.update(name);\r
+\r
+                                                                       }\r
+\r
+                                                               });\r
+\r
+                                                       }\r
+\r
+                                               }\r
+\r
+                                       }\r
+\r
+                               });\r
+\r
+                       }\r
+\r
+               });\r
+\r
+               header.setFont(font);\r
+               header.setBackground(Display.getCurrent().getSystemColor(\r
+                               SWT.COLOR_WHITE));\r
+               GridDataFactory.fillDefaults().grab(true, false).span(2, 1)\r
+                               .align(SWT.CENTER, SWT.CENTER).applyTo(header.getWidget());\r
+\r
+               return header;\r
+\r
+       }\r
+\r
+       protected Label createResourceHeaderLabel(final Composite body,\r
+                       final Composite headerComposite, final ISessionContext context,\r
+                       final WidgetSupport support) {\r
+\r
+               final Label header = new Label(headerComposite, support, 0);\r
+               header.getWidget().addMouseListener(new MouseAdapter() {\r
+\r
+                       @Override\r
+                       public void mouseUp(MouseEvent e) {\r
+\r
+                               final Display display = Display.getCurrent();\r
+                               assert (display != null);\r
+\r
+                               context.getSession().asyncRequest(new ReadRequest() {\r
+\r
+                                       @Override\r
+                                       public void run(ReadGraph graph) throws DatabaseException {\r
+\r
+                                               Object input = support.getInput();\r
+                                               if (input != WidgetSupport.NO_INPUT && input != null\r
+                                                               && input instanceof ISelection) {\r
+\r
+                                                       Layer0 L0 = Layer0.getInstance(graph);\r
+\r
+                                                       final Resource streamClass = ISelectionUtils\r
+                                                                       .filterSingleSelection((ISelection) input,\r
+                                                                                       Resource.class);\r
+                                                       // final Variable inputVariable =\r
+                                                       // ISelectionUtils.filterSingleSelection((ISelection)input,\r
+                                                       // Variable.class);\r
+                                                       // final Variable nameVariable =\r
+                                                       // inputVariable.getPossibleProperty(graph,\r
+                                                       // Variables.NAME);\r
+                                                       final String initial = graph.getRelatedValue(\r
+                                                                       streamClass, L0.HasName, Bindings.STRING);\r
+                                                       // final InputValidator validator =\r
+                                                       // nameVariable.getInterface(graph,\r
+                                                       // InputValidator.class);\r
+                                                       if (initial != null) {\r
+\r
+                                                               if (display.isDisposed())\r
+                                                                       return;\r
+\r
+                                                               display.asyncExec(new Runnable() {\r
+\r
+                                                                       @Override\r
+                                                                       public void run() {\r
+\r
+                                                                               GridDataFactory.fillDefaults()\r
+                                                                                               .grab(true, false).span(1, 1)\r
+                                                                                               .align(SWT.CENTER, SWT.CENTER)\r
+                                                                                               .applyTo(header.getWidget());\r
+                                                                               header.getWidget().setVisible(false);\r
+\r
+                                                                               // header.getWidget().setVisible(false);\r
+                                                                               // header.getWidget().setText("");\r
+                                                                               // GridDataFactory.fillDefaults().grab(false,\r
+                                                                               // false).span(1, 1).align(SWT.CENTER,\r
+                                                                               // SWT.CENTER).applyTo(header.getWidget());\r
+\r
+                                                                               final TrackedText name = new TrackedText(\r
+                                                                                               headerComposite, support,\r
+                                                                                               SWT.NONE);\r
+                                                                               name.setText(initial);\r
+                                                                               name.setFont(font);\r
+                                                                               name.addModifyListener(new StringPropertyModifier(\r
+                                                                                               context, Layer0.URIs.HasName));\r
+                                                                               name.addModifyListener(new TextModifyListener() {\r
+\r
+                                                                                       @Override\r
+                                                                                       public void modifyText(\r
+                                                                                                       TrackedModifyEvent e) {\r
+\r
+                                                                                               name.getWidget().dispose();\r
+                                                                                               header.getWidget().setVisible(\r
+                                                                                                               true);\r
+                                                                                               GridDataFactory\r
+                                                                                                               .fillDefaults()\r
+                                                                                                               .grab(true, false)\r
+                                                                                                               .span(2, 1)\r
+                                                                                                               .align(SWT.CENTER,\r
+                                                                                                                               SWT.CENTER)\r
+                                                                                                               .applyTo(\r
+                                                                                                                               header.getWidget());\r
+\r
+                                                                                               // createResourceHeaderLabel(body,\r
+                                                                                               // headerComposite, context,\r
+                                                                                               // support);\r
+\r
+                                                                                               body.layout(true);\r
+                                                                                               headerComposite.layout(true);\r
+\r
+                                                                                       }\r
+                                                                               });\r
+\r
+                                                                               name.getWidget().forceFocus();\r
+                                                                               name.startEdit(true);\r
+                                                                               name.getWidget().moveAbove(null);\r
+\r
+                                                                               GridDataFactory.fillDefaults()\r
+                                                                                               .grab(true, false).span(1, 1)\r
+                                                                                               .align(SWT.FILL, SWT.FILL)\r
+                                                                                               .applyTo(name.getWidget());\r
+                                                                               body.layout(true);\r
+                                                                               headerComposite.layout(true);\r
+\r
+                                                                               support.update(name);\r
+\r
+                                                                       }\r
+\r
+                                                               });\r
+\r
+                                                       }\r
+\r
+                                               }\r
+\r
+                                       }\r
+\r
+                               });\r
+\r
+                       }\r
+\r
+               });\r
+\r
+               header.setFont(font);\r
+               header.setBackground(Display.getCurrent().getSystemColor(\r
+                               SWT.COLOR_WHITE));\r
+               GridDataFactory.fillDefaults().grab(true, false).span(2, 1)\r
+                               .align(SWT.CENTER, SWT.CENTER).applyTo(header.getWidget());\r
+\r
+               return header;\r
+\r
+       }\r
+\r
+       protected Composite createHeaderComposite(Composite body) {\r
+               return createHeaderComposite(body, 2);\r
+\r
+       }\r
+\r
+       protected Composite createHeaderComposite(Composite body, int width) {\r
+\r
+               Composite headerComposite = new Composite(body, 0);\r
+               headerComposite.setBackground(body.getDisplay().getSystemColor(\r
+                               SWT.COLOR_BLACK));\r
+               GridDataFactory.fillDefaults().span(2, 1).grab(true, false)\r
+                               .applyTo(headerComposite);\r
+               GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(1)\r
+                               .extendedMargins(2, 2, 2, 2).applyTo(headerComposite);\r
+\r
+               Composite headerComposite2 = new Composite(headerComposite, 0);\r
+               headerComposite2.setBackground(body.getDisplay().getSystemColor(\r
+                               SWT.COLOR_WHITE));\r
+               GridDataFactory.fillDefaults().span(1, 1).grab(true, false)\r
+                               .applyTo(headerComposite2);\r
+               GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(width)\r
+                               .extendedMargins(3, 3, 3, 3).applyTo(headerComposite2);\r
+\r
+               return headerComposite2;\r
+\r
+       }\r
+\r
+       protected void create(Composite body, IWorkbenchSite site,\r
+                       final ISessionContext context, WidgetSupport support) {\r
+\r
+       }\r
+\r
+       final public void createControls(Composite body, IWorkbenchSite site,\r
+                       final ISessionContext context, WidgetSupport support) {\r
+\r
+               resourceManager = new LocalResourceManager(JFaceResources.getResources(), body);\r
+               toolkit = new FormToolkit(body.getDisplay());\r
+\r
+               body.addListener(SWT.Dispose, new Listener() {\r
+                       @Override\r
+                       public void handleEvent(Event event) {\r
+                               toolkit.dispose();\r
+                       }\r
+               });\r
+\r
+               font = (Font) resourceManager.get(FontDescriptor.createFrom("Arial", 14, SWT.NONE));\r
+               smallFont = (Font) resourceManager.get(FontDescriptor.createFrom("Arial", 10, SWT.NONE));\r
+               smallFont2 = (Font) resourceManager.get(FontDescriptor.createFrom("Arial", 8, SWT.NONE));\r
+\r
+               create(body, site, context, support);\r
+\r
+       }\r
+\r
+       @Override\r
+       public int hashCode() {\r
+               return getClass().hashCode();\r
+       }\r
+\r
+       @Override\r
+       public boolean equals(Object object) {\r
+               if (this == object)\r
+                       return true;\r
+               else if (object == null)\r
+                       return false;\r
+               return getClass().equals(object.getClass());\r
+       }\r
+\r
+       @Override\r
+       public String getPartName(ISelection forSelection) {\r
+               return "Selection";\r
+       }\r
+\r
+}\r