1 package org.simantics.district.selection.ui;
3 import java.util.Collection;
5 import org.eclipse.jface.resource.ImageDescriptor;
6 import org.eclipse.jface.resource.JFaceResources;
7 import org.eclipse.jface.resource.LocalResourceManager;
8 import org.eclipse.jface.resource.ResourceManager;
9 import org.eclipse.jface.viewers.ColumnLabelProvider;
10 import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
11 import org.eclipse.jface.viewers.DoubleClickEvent;
12 import org.eclipse.jface.viewers.IDoubleClickListener;
13 import org.eclipse.jface.viewers.IStructuredContentProvider;
14 import org.eclipse.jface.viewers.IStructuredSelection;
15 import org.eclipse.jface.viewers.TableViewer;
16 import org.eclipse.jface.viewers.TableViewerColumn;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.graphics.ImageData;
20 import org.eclipse.swt.graphics.PaletteData;
21 import org.eclipse.swt.graphics.RGB;
22 import org.eclipse.swt.layout.FillLayout;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.Table;
26 import org.simantics.Simantics;
27 import org.simantics.db.ReadGraph;
28 import org.simantics.db.common.procedure.adapter.SyncListenerAdapter;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.district.selection.ElementSelector;
31 import org.simantics.district.selection.ui.parts.ElementSelectionView;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class ElementSelectorTableUI extends Composite {
37 private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectorTableUI.class);
39 private TableViewer viewer;
41 private ElementSelectionView view;
43 private ResourceManager resourceManager;
45 public ElementSelectorTableUI(Composite parent, int style, ElementSelectionView view) {
48 resourceManager = new LocalResourceManager(JFaceResources.getResources(), this);
52 parent.setLayout(new FillLayout());
53 // GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
54 // GridLayoutFactory.fillDefaults().numColumns(1).applyTo(this);
55 this.setLayout(new FillLayout());
57 viewer = createViewer();
58 addSelectionListener();
60 createSelectorListener(parent);
65 public Table getTable() {
66 return viewer.getTable();
69 public ElementSelector getSelectedItem() {
70 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
71 return selection != null ? (ElementSelector) selection.getFirstElement() : null;
74 private TableViewer createViewer() {
75 return new TableViewer(this, SWT.FULL_SELECTION | SWT.SINGLE);
78 private void configureTable() {
79 Table table = viewer.getTable();
80 table.setHeaderVisible(true);
81 table.setLinesVisible(true);
87 private void enableToolTips() {
88 ColumnViewerToolTipSupport.enableFor(viewer);
91 private void setContentProvider() {
92 viewer.setContentProvider(new IStructuredContentProvider() {
94 public Object[] getElements(Object inputElement) {
95 if (inputElement == null || !(inputElement instanceof Collection))
98 return ((Collection<?>)inputElement).toArray();
103 private TableViewerColumn createNameColumn() {
104 TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
105 column.getColumn().setText("Name");
106 column.getColumn().setWidth(200);
107 column.getColumn().setResizable(true);
108 column.setLabelProvider(new ColumnLabelProvider() {
110 public String getText(Object element) {
111 ElementSelector selector = (ElementSelector) element;
112 return " " + selector.getName();
116 public Image getImage(Object element) {
117 ElementSelector selector = (ElementSelector) element;
118 float[] color = selector.getColor();
122 RGB rgb = ElementSelectionTools.literalToColor(color).getRgb();
124 PaletteData paletteData = new PaletteData(new RGB[] { new RGB(255, 255, 255), rgb });
126 // Create image with a colored horizontal bar
127 // TODO: Indicate line width
128 Image image = resourceManager.createImage(ImageDescriptor.createFromImageDataProvider(zoom -> {
129 int height = 8 * zoom / 100;
130 int width = 40 * zoom / 100;
131 ImageData imageData = new ImageData(width, height, 1, paletteData);
132 for (int x = 0; x < width; x++) {
133 for (int y = 0; y < height; y++) {
134 imageData.setPixel(x, y, 1);
147 private TableViewerColumn createQueryColumn() {
148 TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
149 column.getColumn().setText("Query");
150 column.getColumn().setWidth(600);
151 column.getColumn().setResizable(true);
152 column.setLabelProvider(new ColumnLabelProvider() {
154 public String getText(Object element) {
155 ElementSelector selector = (ElementSelector) element;
156 return selector.getExpression();
162 private void createSelectorListener(Composite parent) {
163 Simantics.getSession().asyncRequest(new ElementSelectionTools.SelectionsRequest(), new SyncListenerAdapter<Collection<ElementSelector>>() {
164 public void execute(ReadGraph graph, Collection<ElementSelector> result) {
165 parent.getDisplay().asyncExec(() -> {
166 viewer.setInput(result);
171 public void exception(ReadGraph graph, Throwable t) throws DatabaseException {
172 LOGGER.error("Error getting element selector list", t);
176 public boolean isDisposed() {
177 return ElementSelectorTableUI.this.isDisposed();
182 private void addSelectionListener() {
183 viewer.addDoubleClickListener(new DoubleClickListener());
186 private final class DoubleClickListener implements IDoubleClickListener {
188 public void doubleClick(DoubleClickEvent event) {
189 IStructuredSelection selection = (IStructuredSelection) event.getViewer().getSelection();
190 Display display = event.getViewer().getControl().getDisplay();
191 ElementSelector query = (ElementSelector) selection.getFirstElement();
193 view.performSelection(display, query);