1 package org.simantics.district.selection.ui;
3 import java.util.Collection;
5 import org.eclipse.jface.viewers.ColumnLabelProvider;
6 import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
7 import org.eclipse.jface.viewers.DoubleClickEvent;
8 import org.eclipse.jface.viewers.IDoubleClickListener;
9 import org.eclipse.jface.viewers.IStructuredSelection;
10 import org.eclipse.jface.viewers.ITreeContentProvider;
11 import org.eclipse.jface.viewers.TreeSelection;
12 import org.eclipse.jface.viewers.TreeViewer;
13 import org.eclipse.jface.viewers.TreeViewerColumn;
14 import org.eclipse.jface.viewers.Viewer;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.layout.FillLayout;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Tree;
21 import org.simantics.Simantics;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.common.procedure.adapter.SyncListenerAdapter;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.district.selection.ElementSelector;
26 import org.simantics.district.selection.ui.parts.ElementSelectionView;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class ElementSelectorTableUI extends Composite {
32 private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectorTableUI.class);
34 private TreeViewer viewer;
36 private ElementSelectionView view;
38 public ElementSelectorTableUI(Composite parent, int style, ElementSelectionView view) {
42 parent.setLayout(new FillLayout());
43 // GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
44 // GridLayoutFactory.fillDefaults().numColumns(1).applyTo(this);
45 this.setLayout(new FillLayout());
47 viewer = createViewer();
48 addSelectionListener();
50 createSelectorListener(parent);
55 public Tree getTree() {
56 return viewer.getTree();
59 public ElementSelector getSelectedItem() {
60 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
61 return selection != null ? (ElementSelector) selection.getFirstElement() : null;
64 private TreeViewer createViewer() {
65 return new TreeViewer(this, SWT.FULL_SELECTION);
68 private void configureTable() {
69 Tree table = viewer.getTree();
70 table.setHeaderVisible(true);
71 table.setLinesVisible(true);
77 private void enableToolTips() {
78 ColumnViewerToolTipSupport.enableFor(viewer);
81 private void setContentProvider() {
82 viewer.setContentProvider(new ITreeContentProvider() {
84 public boolean hasChildren(Object element) {
89 public Object getParent(Object element) {
94 public Object[] getElements(Object inputElement) {
95 if (inputElement == null || !(inputElement instanceof Collection))
98 return ((Collection<?>)inputElement).toArray();
102 public Object[] getChildren(Object parentElement) {
107 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
108 ITreeContentProvider.super.inputChanged(viewer, oldInput, newInput);
113 private TreeViewerColumn createNameColumn() {
114 TreeViewerColumn column = new TreeViewerColumn(viewer, SWT.NONE);
115 column.getColumn().setText("Name");
116 column.getColumn().setWidth(200);
117 column.getColumn().setResizable(true);
118 column.setLabelProvider(new ColumnLabelProvider() {
120 public String getText(Object element) {
121 ElementSelector selector = (ElementSelector) element;
122 return selector.getName();
126 public Image getImage(Object element) {
133 private TreeViewerColumn createQueryColumn() {
134 TreeViewerColumn column = new TreeViewerColumn(viewer, SWT.NONE);
135 column.getColumn().setText("Query");
136 column.getColumn().setWidth(600);
137 column.getColumn().setResizable(true);
138 column.setLabelProvider(new ColumnLabelProvider() {
140 public String getText(Object element) {
141 ElementSelector selector = (ElementSelector) element;
142 return selector.getExpression();
146 public Image getImage(Object element) {
153 private void createSelectorListener(Composite parent) {
154 Simantics.getSession().asyncRequest(new ElementSelectionTools.SelectionsRequest(), new SyncListenerAdapter<Collection<ElementSelector>>() {
155 public void execute(ReadGraph graph, Collection<ElementSelector> result) {
156 parent.getDisplay().asyncExec(() -> {
157 viewer.setInput(result);
162 public void exception(ReadGraph graph, Throwable t) throws DatabaseException {
163 LOGGER.error("Error getting element selector list", t);
167 public boolean isDisposed() {
168 return ElementSelectorTableUI.this.isDisposed();
173 private void addSelectionListener() {
174 viewer.addDoubleClickListener(new DoubleClickListener());
177 private final class DoubleClickListener implements IDoubleClickListener {
179 public void doubleClick(DoubleClickEvent event) {
180 TreeSelection selection = (TreeSelection) event.getViewer().getSelection();
181 Display display = event.getViewer().getControl().getDisplay();
182 ElementSelector query = (ElementSelector) selection.getFirstElement();
184 view.performSelection(display, query);