X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2FComparatorSelector.java;h=2248bc42a74efce569c0b8f43a96a13749b6a2a4;hp=b3fcd798af1a2bfa33e14940f7ad59595dcdb13d;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ComparatorSelector.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ComparatorSelector.java index b3fcd798a..2248bc42a 100644 --- a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ComparatorSelector.java +++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ComparatorSelector.java @@ -1,150 +1,150 @@ -/******************************************************************************* - * 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 pair = - (Pair)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 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(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(); - } - -} +/******************************************************************************* + * 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 pair = + (Pair)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 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(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(); + } + +}