]> gerrit.simantics Code Review - simantics/district.git/blob
cdddd928bedd2f081321c2147c8592c1246170aa
[simantics/district.git] /
1 package org.simantics.district.selection.ui;
2
3 import java.util.Collection;
4
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;
29
30 public class ElementSelectorTableUI extends Composite {
31
32         private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectorTableUI.class);
33
34         private TreeViewer viewer;
35
36         private ElementSelectionView view;
37
38         public ElementSelectorTableUI(Composite parent, int style, ElementSelectionView view) {
39                 super(parent, style);
40                 this.view = view;
41                 
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());
46
47                 viewer = createViewer();
48                 addSelectionListener();
49                 setContentProvider();
50                 createSelectorListener(parent);
51                 enableToolTips();
52                 configureTable();
53         }
54
55         public Tree getTree() {
56                 return viewer.getTree();
57         }
58
59         public ElementSelector getSelectedItem() {
60                 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
61                 return selection != null ? (ElementSelector) selection.getFirstElement() : null;
62         }
63
64         private TreeViewer createViewer() {
65                 return new TreeViewer(this, SWT.FULL_SELECTION);
66         }
67
68         private void configureTable() {
69                 Tree table = viewer.getTree();
70                 table.setHeaderVisible(true);
71                 table.setLinesVisible(true);
72
73                 createNameColumn();
74                 createQueryColumn();
75         }
76
77         private void enableToolTips() {
78                 ColumnViewerToolTipSupport.enableFor(viewer);
79         }
80
81         private void setContentProvider() {
82                 viewer.setContentProvider(new ITreeContentProvider() {
83                         @Override
84                         public boolean hasChildren(Object element) {
85                                 return false;
86                         }
87         
88                         @Override
89                         public Object getParent(Object element) {
90                                 return null;
91                         }
92         
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                         @Override
102                         public Object[] getChildren(Object parentElement) {
103                                 return null;
104                         }
105         
106                         @Override
107                         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
108                                 ITreeContentProvider.super.inputChanged(viewer, oldInput, newInput);
109                         }
110                 });
111         }
112
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() {
119                         @Override
120                         public String getText(Object element) {
121                                 ElementSelector selector = (ElementSelector) element;
122                                 return selector.getName();
123                         }
124                         
125                         @Override
126                         public Image getImage(Object element) {
127                                 return null;
128                         }
129                 });
130                 return column;
131         }
132
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() {
139                         @Override
140                         public String getText(Object element) {
141                                 ElementSelector selector = (ElementSelector) element;
142                                 return selector.getExpression();
143                         }
144                         
145                         @Override
146                         public Image getImage(Object element) {
147                                 return null;
148                         }
149                 });
150                 return column;
151         }
152
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);
158                                 });
159                         }
160
161                         @Override
162                         public void exception(ReadGraph graph, Throwable t) throws DatabaseException {
163                                 LOGGER.error("Error getting element selector list", t);
164                         }
165
166                         @Override
167                         public boolean isDisposed() {
168                                 return ElementSelectorTableUI.this.isDisposed();
169                         }
170                 });
171         }
172
173         private void addSelectionListener() {
174                 viewer.addDoubleClickListener(new DoubleClickListener());
175         }
176
177         private final class DoubleClickListener implements IDoubleClickListener {
178                 @Override
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();
183                         
184                         view.performSelection(display, query);
185                 }
186         }
187 }