X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=org.simantics.district.selection.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fselection%2Fui%2FElementSelectorTableUI.java;h=cdddd928bedd2f081321c2147c8592c1246170aa;hb=fc5581727a984b57dade7a627ac408ea5ad3f811;hp=0786ce5381b2ea994e8d4a42d1e2bdb1f4d3ca55;hpb=09b78f5c26bb8b521beb6eeeb5c3f44ac1aa4d07;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectorTableUI.java b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectorTableUI.java index 0786ce53..cdddd928 100644 --- a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectorTableUI.java +++ b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectorTableUI.java @@ -1,20 +1,13 @@ package org.simantics.district.selection.ui; -import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; -import java.util.List; -import org.eclipse.e4.ui.workbench.modeling.ESelectionService; import org.eclipse.jface.viewers.ColumnLabelProvider; import org.eclipse.jface.viewers.ColumnViewerToolTipSupport; import org.eclipse.jface.viewers.DoubleClickEvent; import org.eclipse.jface.viewers.IDoubleClickListener; -import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.ITreeContentProvider; -import org.eclipse.jface.viewers.SelectionChangedEvent; -import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.viewers.TreeSelection; import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.jface.viewers.TreeViewerColumn; @@ -23,27 +16,14 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Tree; import org.simantics.Simantics; -import org.simantics.browsing.ui.common.AdaptableHintContext; import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; import org.simantics.db.common.procedure.adapter.SyncListenerAdapter; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.exception.RuntimeDatabaseException; -import org.simantics.db.layer0.QueryIndexUtils; -import org.simantics.db.layer0.SelectionHints; -import org.simantics.db.layer0.request.ActiveModels; -import org.simantics.db.request.Read; -import org.simantics.district.selection.ElementSelectionResource; -import org.simantics.district.selection.ElementSelectionUtils; import org.simantics.district.selection.ElementSelector; -import org.simantics.layer0.Layer0; -import org.simantics.scl.runtime.Lists; -import org.simantics.scl.runtime.function.FunctionImpl1; -import org.simantics.ui.selection.AnyResource; -import org.simantics.ui.selection.AnyVariable; -import org.simantics.ui.selection.WorkbenchSelectionContentType; +import org.simantics.district.selection.ui.parts.ElementSelectionView; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,63 +32,64 @@ public class ElementSelectorTableUI extends Composite { private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectorTableUI.class); private TreeViewer viewer; - private TreeViewerColumn column1; - private TreeViewerColumn column2; - public ElementSelectorTableUI(ESelectionService selectionService, Composite parent, int style) { + private ElementSelectionView view; + + public ElementSelectorTableUI(Composite parent, int style, ElementSelectionView view) { super(parent, style); + this.view = view; + parent.setLayout(new FillLayout()); // GridDataFactory.fillDefaults().grab(true, true).applyTo(this); // GridLayoutFactory.fillDefaults().numColumns(1).applyTo(this); this.setLayout(new FillLayout()); - viewer = new TreeViewer(this, SWT.FULL_SELECTION); - viewer.addDoubleClickListener(new IDoubleClickListener() { - @Override - public void doubleClick(DoubleClickEvent event) { - TreeSelection selection = (TreeSelection) viewer.getSelection(); - ElementSelector query = (ElementSelector) selection.getFirstElement(); - try { - List elements = Simantics.getSession().syncRequest(new Read>() { - @SuppressWarnings("unchecked") - @Override - public List perform(ReadGraph graph) throws DatabaseException { - Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource()); - if (model == null) { - LOGGER.warn("No active model"); - return Collections.emptyList(); - } - - List result = query.selectElementsFrom(graph, model); - return Lists.map(new FunctionImpl1() { - public AdaptableHintContext apply(Resource p0) { - AdaptableHintContext selectionElement = new SelectionElement(SelectionHints.STD_KEYS, graph); - selectionElement.setHint(SelectionHints.KEY_MAIN, p0); - selectionElement.setHint(SelectionHints.KEY_MODEL, model); - return selectionElement; - } - }, result); - } - }); - - selectionService.setPostSelection(new StructuredSelection(elements)); - } catch (DatabaseException e) { - LOGGER.error("Element selection query failed", e); - } - } - }); - + viewer = createViewer(); + addSelectionListener(); + setContentProvider(); + createSelectorListener(parent); + enableToolTips(); + configureTable(); + } + + public Tree getTree() { + return viewer.getTree(); + } + + public ElementSelector getSelectedItem() { + IStructuredSelection selection = (IStructuredSelection) viewer.getSelection(); + return selection != null ? (ElementSelector) selection.getFirstElement() : null; + } + + private TreeViewer createViewer() { + return new TreeViewer(this, SWT.FULL_SELECTION); + } + + private void configureTable() { + Tree table = viewer.getTree(); + table.setHeaderVisible(true); + table.setLinesVisible(true); + + createNameColumn(); + createQueryColumn(); + } + + private void enableToolTips() { + ColumnViewerToolTipSupport.enableFor(viewer); + } + + private void setContentProvider() { viewer.setContentProvider(new ITreeContentProvider() { @Override public boolean hasChildren(Object element) { return false; } - + @Override public Object getParent(Object element) { return null; } - + @Override public Object[] getElements(Object inputElement) { if (inputElement == null || !(inputElement instanceof Collection)) @@ -116,47 +97,25 @@ public class ElementSelectorTableUI extends Composite { return ((Collection)inputElement).toArray(); } - + @Override public Object[] getChildren(Object parentElement) { return null; } - + @Override public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { ITreeContentProvider.super.inputChanged(viewer, oldInput, newInput); } }); - - Simantics.getSession().asyncRequest(new SelectionsRequest(), new SyncListenerAdapter>() { - public void execute(ReadGraph graph, Collection result) { - parent.getDisplay().asyncExec(() -> { - viewer.setInput(result); - }); - } - - @Override - public void exception(ReadGraph graph, Throwable t) throws DatabaseException { - LOGGER.error("Error getting element selector list", t); - } - - @Override - public boolean isDisposed() { - return ElementSelectorTableUI.this.isDisposed(); - } - }); - - ColumnViewerToolTipSupport.enableFor(viewer); - - Tree table = viewer.getTree(); - table.setHeaderVisible(true); - table.setLinesVisible(true); + } - column1 = new TreeViewerColumn(viewer, SWT.NONE); - column1.getColumn().setText("Name"); - column1.getColumn().setWidth(200); - column1.getColumn().setResizable(true); - column1.setLabelProvider(new ColumnLabelProvider() { + private TreeViewerColumn createNameColumn() { + TreeViewerColumn column = new TreeViewerColumn(viewer, SWT.NONE); + column.getColumn().setText("Name"); + column.getColumn().setWidth(200); + column.getColumn().setResizable(true); + column.setLabelProvider(new ColumnLabelProvider() { @Override public String getText(Object element) { ElementSelector selector = (ElementSelector) element; @@ -168,12 +127,15 @@ public class ElementSelectorTableUI extends Composite { return null; } }); + return column; + } - column2 = new TreeViewerColumn(viewer, SWT.NONE); - column2.getColumn().setText("Query"); - column2.getColumn().setWidth(600); - column2.getColumn().setResizable(true); - column2.setLabelProvider(new ColumnLabelProvider() { + private TreeViewerColumn createQueryColumn() { + TreeViewerColumn column = new TreeViewerColumn(viewer, SWT.NONE); + column.getColumn().setText("Query"); + column.getColumn().setWidth(600); + column.getColumn().setResizable(true); + column.setLabelProvider(new ColumnLabelProvider() { @Override public String getText(Object element) { ElementSelector selector = (ElementSelector) element; @@ -185,69 +147,41 @@ public class ElementSelectorTableUI extends Composite { return null; } }); + return column; } - public Tree getTree() { - return viewer.getTree(); - } - - public ElementSelector getSelectedItem() { - IStructuredSelection selection = (IStructuredSelection) viewer.getSelection(); - return selection != null ? (ElementSelector) selection.getFirstElement() : null; - } - - private final class SelectionElement extends AdaptableHintContext { - private final ReadGraph graph; - - private SelectionElement(Key[] keys, ReadGraph graph) { - super(keys); - this.graph = graph; - } + private void createSelectorListener(Composite parent) { + Simantics.getSession().asyncRequest(new ElementSelectionTools.SelectionsRequest(), new SyncListenerAdapter>() { + public void execute(ReadGraph graph, Collection result) { + parent.getDisplay().asyncExec(() -> { + viewer.setInput(result); + }); + } - @SuppressWarnings("unchecked") - public T getContent(WorkbenchSelectionContentType contentType) { - Resource element = getHint(SelectionHints.KEY_MAIN); - if (contentType instanceof AnyResource) { - return (T)element; + @Override + public void exception(ReadGraph graph, Throwable t) throws DatabaseException { + LOGGER.error("Error getting element selector list", t); } - else if (contentType instanceof AnyVariable) { - try { - return (T)ElementSelector.getVariableForElement(graph, element); - } catch (DatabaseException e) { - return null; - } + + @Override + public boolean isDisposed() { + return ElementSelectorTableUI.this.isDisposed(); } - - return null; - } + }); + } + + private void addSelectionListener() { + viewer.addDoubleClickListener(new DoubleClickListener()); } - public static class SelectionsRequest implements Read> { + private final class DoubleClickListener implements IDoubleClickListener { @Override - public Collection perform(ReadGraph graph) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - ElementSelectionResource ES = ElementSelectionResource.getInstance(graph); - - Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource()); - if (model == null) { - return Collections.emptyList(); - } - - List libs = QueryIndexUtils.searchByType(graph, model, ES.SelectionLibrary); - if (libs.isEmpty()) - return Collections.emptyList(); - - Resource lib = libs.get(0); - - List result = new ArrayList<>(); - for (Resource selection : graph.getObjects(lib, L0.ConsistsOf)) { - if (!graph.isInstanceOf(selection, ES.Selection)) - continue; - - result.add(ElementSelector.getSelector(graph, selection)); - } + public void doubleClick(DoubleClickEvent event) { + TreeSelection selection = (TreeSelection) event.getViewer().getSelection(); + Display display = event.getViewer().getControl().getDisplay(); + ElementSelector query = (ElementSelector) selection.getFirstElement(); - return result; + view.performSelection(display, query); } } }