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%2Fwidgets%2FCCombo.java;h=99a4490b024bc10956d3eb74ca18b4f0e13c8de1;hp=f74c9ce3e7b1c8d9d6c54ab6e1b75609449b9617;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/CCombo.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/CCombo.java index f74c9ce3e..99a4490b0 100644 --- a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/CCombo.java +++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/CCombo.java @@ -1,203 +1,203 @@ -/******************************************************************************* - * 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 selectionListeners = new CopyOnWriteArrayList(); - private List> 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 void setAvailable(final ParametrizedRead>> read) { - - Simantics.getSession().async(new UniqueRead>>() { - - @Override - public List> perform(ReadGraph graph) throws DatabaseException { - T input = support.getInput(graph); - System.err.println("read: " + read); - return graph.syncRequest(read.get(input)); - } - - }, new Listener>>() { - - @Override - public void exception(Throwable t) { - t.printStackTrace(); - } - - @Override - public void execute(final List> 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 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 void setSelection(final ParametrizedRead read) { - - Simantics.getSession().async(new UniqueRead() { - - @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() { - - @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 Pair getData(int index) { - if (index == -1) - return null; - return (Pair) itemList.get(index); - } - - /** - * @return selected combo list item or null if no item is - * selected - */ - public Pair getSelectedData() { - return getData(combo.getSelectionIndex()); - } - -} +/******************************************************************************* + * 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 selectionListeners = new CopyOnWriteArrayList(); + private List> 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 void setAvailable(final ParametrizedRead>> read) { + + Simantics.getSession().async(new UniqueRead>>() { + + @Override + public List> perform(ReadGraph graph) throws DatabaseException { + T input = support.getInput(graph); + System.err.println("read: " + read); + return graph.syncRequest(read.get(input)); + } + + }, new Listener>>() { + + @Override + public void exception(Throwable t) { + t.printStackTrace(); + } + + @Override + public void execute(final List> 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 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 void setSelection(final ParametrizedRead read) { + + Simantics.getSession().async(new UniqueRead() { + + @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() { + + @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 Pair getData(int index) { + if (index == -1) + return null; + return (Pair) itemList.get(index); + } + + /** + * @return selected combo list item or null if no item is + * selected + */ + public Pair getSelectedData() { + return getData(combo.getSelectionIndex()); + } + +}