--- /dev/null
+/*******************************************************************************\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;\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.UserSelectedViewpointFactoryQueryProcessor;\r
+import org.simantics.browsing.ui.content.ViewpointFactory;\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 ViewpointSelector extends Composite {\r
+\r
+ Font smallFont;\r
+\r
+ public ViewpointSelector(final GraphExplorer explorer, final UserSelectedViewpointFactoryQueryProcessor 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 viewpoints --");\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<ViewpointFactory, NodeContext> pair =\r
+ (Pair<ViewpointFactory, 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
+ 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<ViewpointFactory> factories = explorer.query(context, BuiltinKeys.VIEWPOINT_FACTORIES);\r
+\r
+ ViewpointFactory oldSelection = queryProcessor.getSelection(context);\r
+\r
+ combo.removeAll();\r
+ if(factories != null) {\r
+ for(ViewpointFactory f : factories) {\r
+ int index = combo.getItemCount();\r
+ combo.add(f.toString());\r
+ combo.setData(String.valueOf(index), new Pair<ViewpointFactory, NodeContext>(f, context));\r
+ if(f == oldSelection) {\r
+ combo.select(index);\r
+ }\r
+ }\r
+ }\r
+ if(combo.getItemCount() == 0) {\r
+ combo.add("-- No viewpoints --");\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