]> gerrit.simantics Code Review - simantics/district.git/blob
4603eb8eb58ee6dbc2ee45e4627bfe20e95c4687
[simantics/district.git] /
1 package org.simantics.district.selection.ui;
2
3 import java.util.Collection;
4
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;
34
35 public class ElementSelectorTableUI extends Composite {
36
37         private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectorTableUI.class);
38
39         private TableViewer viewer;
40
41         private ElementSelectionView view;
42
43         private ResourceManager resourceManager;
44
45         public ElementSelectorTableUI(Composite parent, int style, ElementSelectionView view) {
46                 super(parent, style);
47                 
48                 resourceManager = new LocalResourceManager(JFaceResources.getResources(), this);
49                 
50                 this.view = view;
51                 
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());
56
57                 viewer = createViewer();
58                 addSelectionListener();
59                 setContentProvider();
60                 createSelectorListener(parent);
61                 enableToolTips();
62                 configureTable();
63         }
64
65         public Table getTable() {
66                 return viewer.getTable();
67         }
68
69         public ElementSelector getSelectedItem() {
70                 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
71                 return selection != null ? (ElementSelector) selection.getFirstElement() : null;
72         }
73
74         private TableViewer createViewer() {
75                 return new TableViewer(this, SWT.FULL_SELECTION | SWT.SINGLE);
76         }
77
78         private void configureTable() {
79                 Table table = viewer.getTable();
80                 table.setHeaderVisible(true);
81                 table.setLinesVisible(true);
82
83                 createNameColumn();
84                 createQueryColumn();
85         }
86
87         private void enableToolTips() {
88                 ColumnViewerToolTipSupport.enableFor(viewer);
89         }
90
91         private void setContentProvider() {
92                 viewer.setContentProvider(new IStructuredContentProvider() {
93                         @Override
94                         public Object[] getElements(Object inputElement) {
95                                 if (inputElement == null || !(inputElement instanceof Collection))
96                                         return new Object[0];
97                                 
98                                 return ((Collection<?>)inputElement).toArray();
99                         }
100                 });
101         }
102
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() {
109                         @Override
110                         public String getText(Object element) {
111                                 ElementSelector selector = (ElementSelector) element;
112                                 return " " + selector.getName();
113                         }
114                         
115                         @Override
116                         public Image getImage(Object element) {
117                                 ElementSelector selector = (ElementSelector) element;
118                                 float[] color = selector.getColor();
119                                 if (color == null)
120                                         return null;
121                                 
122                                 RGB rgb = ElementSelectionTools.literalToColor(color).getRgb();
123                                 
124                                 PaletteData paletteData = new PaletteData(new RGB[] { new RGB(255, 255, 255), rgb });
125                                 
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);
135                                                 }
136                                         }
137                                         
138                                         return imageData;
139                                 }));
140                                 
141                                 return image;
142                         }
143                 });
144                 return column;
145         }
146
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() {
153                         @Override
154                         public String getText(Object element) {
155                                 ElementSelector selector = (ElementSelector) element;
156                                 return selector.getExpression();
157                         }
158                 });
159                 return column;
160         }
161
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);
167                                 });
168                         }
169
170                         @Override
171                         public void exception(ReadGraph graph, Throwable t) throws DatabaseException {
172                                 LOGGER.error("Error getting element selector list", t);
173                         }
174
175                         @Override
176                         public boolean isDisposed() {
177                                 return ElementSelectorTableUI.this.isDisposed();
178                         }
179                 });
180         }
181
182         private void addSelectionListener() {
183                 viewer.addDoubleClickListener(new DoubleClickListener());
184         }
185
186         private final class DoubleClickListener implements IDoubleClickListener {
187                 @Override
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();
192                         
193                         view.performSelection(display, query);
194                 }
195         }
196 }