1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
14 import java.util.Collection;
16 import org.eclipse.jface.layout.GridDataFactory;
17 import org.eclipse.jface.viewers.IPostSelectionProvider;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.ISelectionChangedListener;
20 import org.eclipse.jface.viewers.SelectionChangedEvent;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.events.SelectionListener;
24 import org.eclipse.swt.graphics.Font;
25 import org.eclipse.swt.graphics.FontData;
26 import org.eclipse.swt.widgets.Combo;
27 import org.eclipse.swt.widgets.Composite;
28 import org.simantics.browsing.ui.BuiltinKeys;
29 import org.simantics.browsing.ui.GraphExplorer;
30 import org.simantics.browsing.ui.NodeContext;
31 import org.simantics.browsing.ui.common.processors.UserSelectedComparableFactoryQueryProcessor;
32 import org.simantics.browsing.ui.content.ComparableContextFactory;
33 import org.simantics.db.layer0.SelectionHints;
34 import org.simantics.utils.datastructures.Pair;
35 import org.simantics.utils.ui.ISelectionUtils;
36 import org.simantics.utils.ui.LayoutUtils;
39 * TODO: support multiselection within the IGraphExplorer
41 public class ComparatorSelector extends Composite {
45 public ComparatorSelector(final GraphExplorer explorer, final UserSelectedComparableFactoryQueryProcessor queryProcessor, Composite parent, int style) {
49 setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
51 setLayout(LayoutUtils.createNoBorderGridLayout(1, false));
53 final Combo combo = new Combo(this, SWT.READ_ONLY);
55 Font initialFont = combo.getFont();
56 FontData[] fontData = initialFont.getFontData();
57 for (int i = 0; i < fontData.length; i++) {
58 fontData[i].setHeight(fontData[i].getHeight() - 1);
60 smallFont = new Font(getDisplay(), fontData);
61 combo.setFont(smallFont);
63 combo.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
64 combo.add("-- No comparators --");
67 combo.addSelectionListener(new SelectionListener() {
70 public void widgetDefaultSelected(SelectionEvent e) {
74 @SuppressWarnings("unchecked")
76 public void widgetSelected(SelectionEvent e) {
78 int index = combo.getSelectionIndex();
80 Pair<ComparableContextFactory, NodeContext> pair =
81 (Pair<ComparableContextFactory, NodeContext>)combo.getData(String.valueOf(index));
85 queryProcessor.select(pair.second, pair.first);
91 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
93 selectionProvider.addSelectionChangedListener(new ISelectionChangedListener() {
96 public void selectionChanged(SelectionChangedEvent event) {
98 ISelection selection = event.getSelection();
100 NodeContext context = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MAIN, NodeContext.class);
102 if(context == null) {
103 context = explorer.getRoot();
106 assert(context != null);
108 Collection<ComparableContextFactory> factories = explorer.query(context, BuiltinKeys.COMPARABLE_FACTORIES);
109 if(factories == null) {
111 combo.add("-- No comparators --");
116 ComparableContextFactory oldSelection = queryProcessor.getComparator(context);
119 if(factories != null) {
120 for(ComparableContextFactory f : factories) {
121 int index = combo.getItemCount();
122 combo.add(f.toString());
123 combo.setData(String.valueOf(index), new Pair<ComparableContextFactory, NodeContext>(f, context));
124 if(f == oldSelection) {
129 if(combo.getItemCount() == 0) {
130 combo.add("-- No comparators --");
132 if(combo.getSelectionIndex() == -1) {
143 public void dispose() {