]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ComparatorSelector.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / ComparatorSelector.java
index b3fcd798af1a2bfa33e14940f7ad59595dcdb13d..2248bc42a74efce569c0b8f43a96a13749b6a2a4 100644 (file)
-/*******************************************************************************\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.browsing.ui.swt;\r
-\r
-import java.util.Collection;\r
-\r
-import org.eclipse.jface.layout.GridDataFactory;\r
-import org.eclipse.jface.viewers.IPostSelectionProvider;\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.jface.viewers.ISelectionChangedListener;\r
-import org.eclipse.jface.viewers.SelectionChangedEvent;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.events.SelectionEvent;\r
-import org.eclipse.swt.events.SelectionListener;\r
-import org.eclipse.swt.graphics.Font;\r
-import org.eclipse.swt.graphics.FontData;\r
-import org.eclipse.swt.widgets.Combo;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.simantics.browsing.ui.BuiltinKeys;\r
-import org.simantics.browsing.ui.GraphExplorer;\r
-import org.simantics.browsing.ui.NodeContext;\r
-import org.simantics.browsing.ui.common.processors.UserSelectedComparableFactoryQueryProcessor;\r
-import org.simantics.browsing.ui.content.ComparableContextFactory;\r
-import org.simantics.db.layer0.SelectionHints;\r
-import org.simantics.utils.datastructures.Pair;\r
-import org.simantics.utils.ui.ISelectionUtils;\r
-import org.simantics.utils.ui.LayoutUtils;\r
-\r
-/**\r
- * TODO: support multiselection within the IGraphExplorer\r
- */\r
-public class ComparatorSelector extends Composite {\r
-\r
-    Font smallFont;\r
-\r
-    public ComparatorSelector(final GraphExplorer explorer, final UserSelectedComparableFactoryQueryProcessor queryProcessor, Composite parent, int style) {\r
-\r
-        super(parent, style);\r
-\r
-        setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());\r
-\r
-        setLayout(LayoutUtils.createNoBorderGridLayout(1, false));\r
-\r
-        final Combo combo = new Combo(this, SWT.READ_ONLY);\r
-\r
-        Font initialFont = combo.getFont();\r
-        FontData[] fontData = initialFont.getFontData();\r
-        for (int i = 0; i < fontData.length; i++) {\r
-            fontData[i].setHeight(fontData[i].getHeight() - 1);\r
-        }\r
-        smallFont = new Font(getDisplay(), fontData);\r
-        combo.setFont(smallFont);\r
-\r
-        combo.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());\r
-        combo.add("-- No comparators --");\r
-        combo.select(0);\r
-\r
-        combo.addSelectionListener(new SelectionListener() {\r
-\r
-            @Override\r
-            public void widgetDefaultSelected(SelectionEvent e) {\r
-                widgetSelected(e);\r
-            }\r
-\r
-            @SuppressWarnings("unchecked")\r
-            @Override\r
-            public void widgetSelected(SelectionEvent e) {\r
-\r
-                int index = combo.getSelectionIndex();\r
-\r
-                Pair<ComparableContextFactory, NodeContext> pair =\r
-                    (Pair<ComparableContextFactory, NodeContext>)combo.getData(String.valueOf(index));\r
-                if (pair == null)\r
-                    return;\r
-\r
-                queryProcessor.select(pair.second, pair.first);\r
-\r
-            }\r
-\r
-        });\r
-\r
-        IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);\r
-\r
-        selectionProvider.addSelectionChangedListener(new ISelectionChangedListener() {\r
-\r
-            @Override\r
-            public void selectionChanged(SelectionChangedEvent event) {\r
-\r
-                ISelection selection = event.getSelection();\r
-                \r
-                NodeContext context = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MAIN, NodeContext.class);\r
-\r
-                if(context == null) {\r
-                    context = explorer.getRoot();\r
-                }\r
-\r
-                assert(context != null);\r
-\r
-                Collection<ComparableContextFactory> factories = explorer.query(context, BuiltinKeys.COMPARABLE_FACTORIES);\r
-                if(factories == null) {\r
-                    combo.removeAll();\r
-                    combo.add("-- No comparators --");\r
-                    combo.select(0);\r
-                    return;\r
-                }\r
-\r
-                ComparableContextFactory oldSelection = queryProcessor.getComparator(context);\r
-\r
-                combo.removeAll();\r
-                if(factories != null) {\r
-                    for(ComparableContextFactory f : factories) {\r
-                        int index = combo.getItemCount();\r
-                        combo.add(f.toString());\r
-                        combo.setData(String.valueOf(index), new Pair<ComparableContextFactory, NodeContext>(f, context));\r
-                        if(f == oldSelection) {\r
-                            combo.select(index);\r
-                        }\r
-                    }\r
-                }\r
-                if(combo.getItemCount() == 0) {\r
-                    combo.add("-- No comparators --");\r
-                }\r
-                if(combo.getSelectionIndex() == -1) {\r
-                    combo.select(0);\r
-                }\r
-\r
-            }\r
-\r
-        });\r
-\r
-    }\r
-\r
-    @Override\r
-    public void dispose() {\r
-        if (isDisposed())\r
-            return;\r
-        smallFont.dispose();\r
-        super.dispose();\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 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;
+
+import java.util.Collection;
+
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.viewers.IPostSelectionProvider;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.graphics.Font;
+import org.eclipse.swt.graphics.FontData;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.simantics.browsing.ui.BuiltinKeys;
+import org.simantics.browsing.ui.GraphExplorer;
+import org.simantics.browsing.ui.NodeContext;
+import org.simantics.browsing.ui.common.processors.UserSelectedComparableFactoryQueryProcessor;
+import org.simantics.browsing.ui.content.ComparableContextFactory;
+import org.simantics.db.layer0.SelectionHints;
+import org.simantics.utils.datastructures.Pair;
+import org.simantics.utils.ui.ISelectionUtils;
+import org.simantics.utils.ui.LayoutUtils;
+
+/**
+ * TODO: support multiselection within the IGraphExplorer
+ */
+public class ComparatorSelector extends Composite {
+
+    Font smallFont;
+
+    public ComparatorSelector(final GraphExplorer explorer, final UserSelectedComparableFactoryQueryProcessor queryProcessor, Composite parent, int style) {
+
+        super(parent, style);
+
+        setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
+
+        setLayout(LayoutUtils.createNoBorderGridLayout(1, false));
+
+        final Combo combo = new Combo(this, SWT.READ_ONLY);
+
+        Font initialFont = combo.getFont();
+        FontData[] fontData = initialFont.getFontData();
+        for (int i = 0; i < fontData.length; i++) {
+            fontData[i].setHeight(fontData[i].getHeight() - 1);
+        }
+        smallFont = new Font(getDisplay(), fontData);
+        combo.setFont(smallFont);
+
+        combo.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
+        combo.add("-- No comparators --");
+        combo.select(0);
+
+        combo.addSelectionListener(new SelectionListener() {
+
+            @Override
+            public void widgetDefaultSelected(SelectionEvent e) {
+                widgetSelected(e);
+            }
+
+            @SuppressWarnings("unchecked")
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+
+                int index = combo.getSelectionIndex();
+
+                Pair<ComparableContextFactory, NodeContext> pair =
+                    (Pair<ComparableContextFactory, NodeContext>)combo.getData(String.valueOf(index));
+                if (pair == null)
+                    return;
+
+                queryProcessor.select(pair.second, pair.first);
+
+            }
+
+        });
+
+        IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
+
+        selectionProvider.addSelectionChangedListener(new ISelectionChangedListener() {
+
+            @Override
+            public void selectionChanged(SelectionChangedEvent event) {
+
+                ISelection selection = event.getSelection();
+                
+                NodeContext context = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MAIN, NodeContext.class);
+
+                if(context == null) {
+                    context = explorer.getRoot();
+                }
+
+                assert(context != null);
+
+                Collection<ComparableContextFactory> factories = explorer.query(context, BuiltinKeys.COMPARABLE_FACTORIES);
+                if(factories == null) {
+                    combo.removeAll();
+                    combo.add("-- No comparators --");
+                    combo.select(0);
+                    return;
+                }
+
+                ComparableContextFactory oldSelection = queryProcessor.getComparator(context);
+
+                combo.removeAll();
+                if(factories != null) {
+                    for(ComparableContextFactory f : factories) {
+                        int index = combo.getItemCount();
+                        combo.add(f.toString());
+                        combo.setData(String.valueOf(index), new Pair<ComparableContextFactory, NodeContext>(f, context));
+                        if(f == oldSelection) {
+                            combo.select(index);
+                        }
+                    }
+                }
+                if(combo.getItemCount() == 0) {
+                    combo.add("-- No comparators --");
+                }
+                if(combo.getSelectionIndex() == -1) {
+                    combo.select(0);
+                }
+
+            }
+
+        });
+
+    }
+
+    @Override
+    public void dispose() {
+        if (isDisposed())
+            return;
+        smallFont.dispose();
+        super.dispose();
+    }
+
+}