]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/Button.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / widgets / Button.java
diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/Button.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/Button.java
new file mode 100644 (file)
index 0000000..fe8a399
--- /dev/null
@@ -0,0 +1,282 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 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.browsing.ui.swt.widgets;\r
+\r
+import java.util.concurrent.CopyOnWriteArrayList;\r
+\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.eclipse.jface.resource.ResourceManager;\r
+import org.eclipse.swt.events.SelectionListener;\r
+import org.eclipse.swt.graphics.Image;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.common.ErrorLogger;\r
+import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory;\r
+import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.common.request.ParametrizedRead;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.db.procedure.Listener;\r
+import org.simantics.utils.ui.SWTUtils;\r
+\r
+public class Button extends WidgetImpl {\r
+\r
+    private ReadFactory<?, String> textFactory;\r
+    private ReadFactory<?, String> tooltipFactory;\r
+    private ReadFactory<?, ImageDescriptor> imageFactory;\r
+    private ReadFactory<?, Boolean> selectionFactory;\r
+    private CopyOnWriteArrayList<SelectionListener> selectionListeners = new CopyOnWriteArrayList<SelectionListener>();\r
+\r
+    private final org.eclipse.swt.widgets.Button button;\r
+\r
+    public Button(Composite parent, WidgetSupport support, int style) {\r
+       super(support);\r
+        button = new org.eclipse.swt.widgets.Button(parent, style);\r
+        support.register(this);\r
+    }\r
+\r
+    public void setTextFactory(ReadFactory<?, String> textFactory) {\r
+        this.textFactory = textFactory;\r
+    }\r
+\r
+    public void setTooltipFactory(ReadFactory<?, String> tooltipFactory) {\r
+        this.tooltipFactory = tooltipFactory;\r
+    }\r
+\r
+    public void setImageFactory(ReadFactory<?, ImageDescriptor> imageFactory) {\r
+        this.imageFactory = imageFactory;\r
+    }\r
+\r
+    public void setSelectionFactory(ReadFactory<?, Boolean> selectionFactory) {\r
+        this.selectionFactory = selectionFactory;\r
+    }\r
+\r
+    public org.eclipse.swt.widgets.Button getWidget() {\r
+        return button;\r
+    }\r
+\r
+    @Override\r
+    public Control getControl() {\r
+        return button;\r
+    }\r
+\r
+    @Override\r
+    public void setInput(ISessionContext context, Object input) {\r
+\r
+        if (selectionListeners != null) {\r
+            for (SelectionListener listener : selectionListeners) {\r
+                if(listener instanceof Widget) {\r
+                    ((Widget) listener).setInput(context, input);\r
+                }\r
+            }\r
+        }\r
+\r
+\r
+        if(textFactory != null) {\r
+            textFactory.listen(context, input, new Listener<String>() {\r
+\r
+                @Override\r
+                public void exception(final Throwable t) {\r
+                    SWTUtils.asyncExec(button, new Runnable() {\r
+\r
+                        @Override\r
+                        public void run() {\r
+                            if(isDisposed()) return;\r
+//                            System.out.println("Button received new text: " + text);\r
+                            button.setText(t.toString());\r
+                        }\r
+\r
+                    });\r
+                }\r
+\r
+                @Override\r
+                public void execute(final String text) {\r
+                    SWTUtils.asyncExec(button, new Runnable() {\r
+\r
+                        @Override\r
+                        public void run() {\r
+                            if(isDisposed()) return;\r
+//                            System.out.println("Button received new text: " + text);\r
+                            button.setText(text);\r
+                        }\r
+\r
+                    });\r
+                }\r
+\r
+                @Override\r
+                public boolean isDisposed() {\r
+                    return button.isDisposed();\r
+                }\r
+\r
+            });\r
+        }\r
+\r
+        if(tooltipFactory != null) {\r
+            tooltipFactory.listen(context, input, new Listener<String>() {\r
+\r
+                @Override\r
+                public void exception(Throwable t) {\r
+                    ErrorLogger.defaultLogError(t);\r
+                }\r
+\r
+                @Override\r
+                public void execute(final String text) {\r
+                    SWTUtils.asyncExec(button, new Runnable() {\r
+\r
+                        @Override\r
+                        public void run() {\r
+                            if(isDisposed()) return;\r
+//                            System.out.println("Button received new tooltip: " + text);\r
+                            button.setToolTipText(text);\r
+                        }\r
+\r
+                    });\r
+                }\r
+\r
+                @Override\r
+                public boolean isDisposed() {\r
+                    return button.isDisposed();\r
+                }\r
+\r
+            });\r
+        }\r
+\r
+        if(imageFactory != null) {\r
+            imageFactory.listen(context, input, new Listener<ImageDescriptor>() {\r
+\r
+                @Override\r
+                public void exception(Throwable t) {\r
+                    ErrorLogger.defaultLogError(t);\r
+                }\r
+\r
+                @Override\r
+                public void execute(final ImageDescriptor imageDescriptor) {\r
+                    SWTUtils.asyncExec(button, new Runnable() {\r
+\r
+                        @Override\r
+                        public void run() {\r
+\r
+                            if(isDisposed()) return;\r
+//                            System.out.println("Button received new image");\r
+                            ResourceManager rm = support.getParameter(WidgetSupport.RESOURCE_MANAGER);\r
+                            if (rm != null) {\r
+                                Image image = (Image) rm.get(imageDescriptor);\r
+                                button.setImage(image);\r
+                            }\r
+                            // TODO: how can we resize without this knife??\r
+                            button.getParent().layout();\r
+                            button.getParent().getParent().layout();\r
+\r
+                        }\r
+\r
+                    });\r
+                }\r
+\r
+                @Override\r
+                public boolean isDisposed() {\r
+                    return button.isDisposed();\r
+                }\r
+\r
+            });\r
+        }\r
+\r
+        if (selectionFactory != null) {\r
+            selectionFactory.listen(context, input, new Listener<Boolean>() {\r
+                @Override\r
+                public void exception(Throwable t) {\r
+                    ErrorLogger.defaultLogError(t);\r
+                }\r
+                @Override\r
+                public void execute(final Boolean selected) {\r
+                    SWTUtils.asyncExec(button, new Runnable() {\r
+                        @Override\r
+                        public void run() {\r
+                            if(isDisposed()) return;\r
+                            button.setSelection(selected);\r
+                        }\r
+                    });\r
+                }\r
+                @Override\r
+                public boolean isDisposed() {\r
+                    return button.isDisposed();\r
+                }\r
+            });\r
+        }\r
+    }\r
+\r
+    public void setText(String text) {\r
+        button.setText(text);\r
+    }\r
+\r
+    public <T> void setText(final ParametrizedRead<T, String> read) {\r
+               \r
+       Simantics.getSession().async(new UniqueRead<String>() {\r
+\r
+               @Override\r
+               public String perform(ReadGraph graph) throws DatabaseException {\r
+                       T input = support.getInput(graph);\r
+                       return graph.syncRequest(read.get(input));\r
+               }\r
+\r
+       }, new Listener<String>() {\r
+\r
+               @Override\r
+               public void exception(Throwable t) {\r
+                       t.printStackTrace();\r
+               }\r
+\r
+               @Override\r
+               public void execute(final String text) {\r
+\r
+                       if(isDisposed()) return;\r
+\r
+                       button.getDisplay().asyncExec(new Runnable() {\r
+\r
+                               @Override\r
+                               public void run() {\r
+                                       button.setText(text);\r
+                               }\r
+\r
+                       });\r
+               }\r
+\r
+               @Override\r
+               public boolean isDisposed() {\r
+                       return button.isDisposed();\r
+               }\r
+\r
+       });\r
+\r
+    }\r
+    \r
+    public void setTooltipText(String text) {\r
+        button.setToolTipText(text);\r
+    }\r
+\r
+    public void setImage(Image image) {\r
+        button.setImage(image);\r
+    }\r
+\r
+    public void setSelection(boolean selected) {\r
+        button.setSelection(selected);\r
+    }\r
+\r
+    public synchronized void addSelectionListener(SelectionListener listener) {\r
+        selectionListeners.add(listener);\r
+        button.addSelectionListener(listener);\r
+    }\r
+\r
+}\r