]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/CCombo.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / widgets / CCombo.java
index f74c9ce3e7b1c8d9d6c54ab6e1b75609449b9617..99a4490b024bc10956d3eb74ca18b4f0e13c8de1 100644 (file)
-/*******************************************************************************\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.List;\r
-import java.util.concurrent.CopyOnWriteArrayList;\r
-\r
-import org.eclipse.swt.events.SelectionListener;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.Display;\r
-import org.simantics.Simantics;\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.datastructures.Pair;\r
-\r
-public class CCombo extends WidgetImpl {\r
-\r
-    private final CopyOnWriteArrayList<SelectionListener> selectionListeners = new CopyOnWriteArrayList<SelectionListener>();\r
-    private List<Pair<String, Object>> itemList;\r
-\r
-    final private Display display;\r
-    final private org.eclipse.swt.custom.CCombo combo;\r
-\r
-    public CCombo(Composite parent, WidgetSupport support, int style) {\r
-       super(support);\r
-        this.display = parent.getDisplay();\r
-        combo = new org.eclipse.swt.custom.CCombo(parent, style);\r
-        combo.setData("org.simantics.browsing.ui.widgets.Combo", this);\r
-        support.register(this);\r
-    }\r
-\r
-    public <T> void setAvailable(final ParametrizedRead<T, List<Pair<String, Object>>> read) {\r
-       \r
-       Simantics.getSession().async(new UniqueRead<List<Pair<String, Object>>>() {\r
-\r
-                       @Override\r
-                       public List<Pair<String, Object>> perform(ReadGraph graph) throws DatabaseException {\r
-                               T input = support.getInput(graph);\r
-                               System.err.println("read: " + read);\r
-                               return graph.syncRequest(read.get(input));\r
-                       }\r
-               \r
-       }, new Listener<List<Pair<String, Object>>>() {\r
-\r
-            @Override\r
-            public void exception(Throwable t) {\r
-                t.printStackTrace();\r
-            }\r
-\r
-            @Override\r
-            public void execute(final List<Pair<String, Object>> items) {\r
-                if(isDisposed()) return;\r
-                display.asyncExec(new Runnable() {\r
-\r
-                    @Override\r
-                    public void run() {\r
-                        if(isDisposed()) return;\r
-                        itemList = items;\r
-                        for(SelectionListener listener : selectionListeners) combo.removeSelectionListener(listener);\r
-                        combo.setData(items);\r
-                        combo.clearSelection();\r
-                        try {\r
-                            combo.removeAll();\r
-                        } catch (Throwable t) {\r
-                            t.printStackTrace();\r
-                        }\r
-                        if (items != null) {\r
-                            int index = 0;\r
-                            for(Pair<String, Object> key : items) {\r
-                                combo.add(key.first);\r
-                                combo.setData(key.first, index++);\r
-                            }\r
-                            String selectionKey = (String)combo.getData("_SelectionKey");\r
-                            if(selectionKey != null) {\r
-                                Integer selectionIndex = (Integer)combo.getData(selectionKey);\r
-                                if(selectionIndex != null) combo.select(selectionIndex);\r
-                            }\r
-                        }\r
-                        for(SelectionListener listener : selectionListeners) combo.addSelectionListener(listener);\r
-                    }\r
-\r
-                });\r
-            }\r
-\r
-            @Override\r
-            public boolean isDisposed() {\r
-                return combo.isDisposed();\r
-            }\r
-\r
-        });\r
-       \r
-    }\r
-\r
-    public <T> void setSelection(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
-                               System.err.println("read: " + read);\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 selectionKey) {\r
-                if(isDisposed()) return;\r
-                display.asyncExec(new Runnable() {\r
-\r
-                    @Override\r
-                    public void run() {\r
-                        if(isDisposed()) return;\r
-                        if(selectionKey == null) return;\r
-                        for(SelectionListener listener : selectionListeners) combo.removeSelectionListener(listener);\r
-                        combo.setData("_SelectionKey", selectionKey);\r
-                        Integer selectionIndex = (Integer)combo.getData(selectionKey);\r
-                        if(selectionIndex != null) combo.select(selectionIndex);\r
-                        for(SelectionListener listener : selectionListeners) combo.addSelectionListener(listener);\r
-                    }\r
-\r
-                });\r
-            }\r
-\r
-            @Override\r
-            public boolean isDisposed() {\r
-               return combo.isDisposed();\r
-            }\r
-\r
-        });\r
-       \r
-    }\r
-\r
-    public org.eclipse.swt.custom.CCombo getWidget() {\r
-        return combo;\r
-    }\r
-\r
-    @Override\r
-    public Control getControl() {\r
-        return combo;\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
-\r
-    public synchronized void addSelectionListener(SelectionListener listener) {\r
-        selectionListeners.add(listener);\r
-        combo.addSelectionListener(listener);\r
-    }\r
-\r
-    /**\r
-     * @param index\r
-     * @return\r
-     * @throws IndexOutOfBoundsException if index is outside widget bounds\r
-     */\r
-    @SuppressWarnings("unchecked")\r
-    public <T> Pair<String, T> getData(int index) {\r
-        if (index == -1)\r
-            return null;\r
-        return (Pair<String, T>) itemList.get(index);\r
-    }\r
-\r
-    /**\r
-     * @return selected combo list item or <code>null</code> if no item is\r
-     *         selected\r
-     */\r
-    public <T> Pair<String, T> getSelectedData() {\r
-        return getData(combo.getSelectionIndex());\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.browsing.ui.swt.widgets;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.simantics.Simantics;
+import org.simantics.browsing.ui.swt.widgets.impl.Widget;
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.common.request.ParametrizedRead;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.management.ISessionContext;
+import org.simantics.db.procedure.Listener;
+import org.simantics.utils.datastructures.Pair;
+
+public class CCombo extends WidgetImpl {
+
+    private final CopyOnWriteArrayList<SelectionListener> selectionListeners = new CopyOnWriteArrayList<SelectionListener>();
+    private List<Pair<String, Object>> itemList;
+
+    final private Display display;
+    final private org.eclipse.swt.custom.CCombo combo;
+
+    public CCombo(Composite parent, WidgetSupport support, int style) {
+       super(support);
+        this.display = parent.getDisplay();
+        combo = new org.eclipse.swt.custom.CCombo(parent, style);
+        combo.setData("org.simantics.browsing.ui.widgets.Combo", this);
+        support.register(this);
+    }
+
+    public <T> void setAvailable(final ParametrizedRead<T, List<Pair<String, Object>>> read) {
+       
+       Simantics.getSession().async(new UniqueRead<List<Pair<String, Object>>>() {
+
+                       @Override
+                       public List<Pair<String, Object>> perform(ReadGraph graph) throws DatabaseException {
+                               T input = support.getInput(graph);
+                               System.err.println("read: " + read);
+                               return graph.syncRequest(read.get(input));
+                       }
+               
+       }, new Listener<List<Pair<String, Object>>>() {
+
+            @Override
+            public void exception(Throwable t) {
+                t.printStackTrace();
+            }
+
+            @Override
+            public void execute(final List<Pair<String, Object>> items) {
+                if(isDisposed()) return;
+                display.asyncExec(new Runnable() {
+
+                    @Override
+                    public void run() {
+                        if(isDisposed()) return;
+                        itemList = items;
+                        for(SelectionListener listener : selectionListeners) combo.removeSelectionListener(listener);
+                        combo.setData(items);
+                        combo.clearSelection();
+                        try {
+                            combo.removeAll();
+                        } catch (Throwable t) {
+                            t.printStackTrace();
+                        }
+                        if (items != null) {
+                            int index = 0;
+                            for(Pair<String, Object> key : items) {
+                                combo.add(key.first);
+                                combo.setData(key.first, index++);
+                            }
+                            String selectionKey = (String)combo.getData("_SelectionKey");
+                            if(selectionKey != null) {
+                                Integer selectionIndex = (Integer)combo.getData(selectionKey);
+                                if(selectionIndex != null) combo.select(selectionIndex);
+                            }
+                        }
+                        for(SelectionListener listener : selectionListeners) combo.addSelectionListener(listener);
+                    }
+
+                });
+            }
+
+            @Override
+            public boolean isDisposed() {
+                return combo.isDisposed();
+            }
+
+        });
+       
+    }
+
+    public <T> void setSelection(final ParametrizedRead<T, String> read) {
+       
+       Simantics.getSession().async(new UniqueRead<String>() {
+
+                       @Override
+                       public String perform(ReadGraph graph) throws DatabaseException {
+                               T input = support.getInput(graph);
+                               System.err.println("read: " + read);
+                               return graph.syncRequest(read.get(input));
+                       }
+               
+       }, new Listener<String>() {
+
+            @Override
+            public void exception(Throwable t) {
+                t.printStackTrace();
+            }
+
+            @Override
+            public void execute(final String selectionKey) {
+                if(isDisposed()) return;
+                display.asyncExec(new Runnable() {
+
+                    @Override
+                    public void run() {
+                        if(isDisposed()) return;
+                        if(selectionKey == null) return;
+                        for(SelectionListener listener : selectionListeners) combo.removeSelectionListener(listener);
+                        combo.setData("_SelectionKey", selectionKey);
+                        Integer selectionIndex = (Integer)combo.getData(selectionKey);
+                        if(selectionIndex != null) combo.select(selectionIndex);
+                        for(SelectionListener listener : selectionListeners) combo.addSelectionListener(listener);
+                    }
+
+                });
+            }
+
+            @Override
+            public boolean isDisposed() {
+               return combo.isDisposed();
+            }
+
+        });
+       
+    }
+
+    public org.eclipse.swt.custom.CCombo getWidget() {
+        return combo;
+    }
+
+    @Override
+    public Control getControl() {
+        return combo;
+    }
+
+    @Override
+    public void setInput(ISessionContext context, Object input) {
+
+        if (selectionListeners != null) {
+            for (SelectionListener listener : selectionListeners) {
+                if(listener instanceof Widget) {
+                    ((Widget) listener).setInput(context, input);
+                }
+            }
+        }
+
+    }
+
+    public synchronized void addSelectionListener(SelectionListener listener) {
+        selectionListeners.add(listener);
+        combo.addSelectionListener(listener);
+    }
+
+    /**
+     * @param index
+     * @return
+     * @throws IndexOutOfBoundsException if index is outside widget bounds
+     */
+    @SuppressWarnings("unchecked")
+    public <T> Pair<String, T> getData(int index) {
+        if (index == -1)
+            return null;
+        return (Pair<String, T>) itemList.get(index);
+    }
+
+    /**
+     * @return selected combo list item or <code>null</code> if no item is
+     *         selected
+     */
+    public <T> Pair<String, T> getSelectedData() {
+        return getData(combo.getSelectionIndex());
+    }
+
+}