]> gerrit.simantics Code Review - simantics/platform.git/blob
eeea9d586b895a5cd1f2af7a86b2001061605ded
[simantics/platform.git] /
1 package org.simantics.modeling.ui.scl.expressions;
2
3 import java.util.Collection;
4
5 import org.eclipse.core.runtime.IProgressMonitor;
6 import org.eclipse.core.runtime.IStatus;
7 import org.eclipse.core.runtime.Status;
8 import org.eclipse.core.runtime.jobs.Job;
9 import org.eclipse.jface.action.Action;
10 import org.eclipse.jface.action.IAction;
11 import org.eclipse.jface.layout.GridDataFactory;
12 import org.eclipse.jface.layout.TableColumnLayout;
13 import org.eclipse.jface.resource.ImageRegistry;
14 import org.eclipse.jface.viewers.ArrayContentProvider;
15 import org.eclipse.jface.viewers.ColumnLabelProvider;
16 import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
17 import org.eclipse.jface.viewers.ColumnWeightData;
18 import org.eclipse.jface.viewers.DoubleClickEvent;
19 import org.eclipse.jface.viewers.IDoubleClickListener;
20 import org.eclipse.jface.viewers.IStructuredContentProvider;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.jface.viewers.TableViewer;
24 import org.eclipse.jface.viewers.TableViewerColumn;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.KeyAdapter;
27 import org.eclipse.swt.events.KeyEvent;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.swt.widgets.Table;
32 import org.eclipse.swt.widgets.Text;
33 import org.eclipse.ui.IMemento;
34 import org.eclipse.ui.IViewSite;
35 import org.eclipse.ui.PartInitException;
36 import org.eclipse.ui.part.ViewPart;
37 import org.simantics.DatabaseJob;
38 import org.simantics.Simantics;
39 import org.simantics.db.Resource;
40 import org.simantics.db.exception.DatabaseException;
41 import org.simantics.modeling.ui.Activator;
42 import org.simantics.ui.workbench.action.DefaultActions;
43
44 public class SCLExpressionView extends ViewPart {
45
46     private TableViewer tableViewer;
47     private TableViewerColumn expressionColumn;
48     private TableViewerColumn locationColumn;
49
50     private ImageRegistry imageRegistry;
51     private IStructuredContentProvider expressionContentProvider;
52     private SCLExpressionItemFilter expressionFilter;
53
54     public SCLExpressionView() {
55         imageRegistry = Activator.getDefault().getImageRegistry();
56         expressionContentProvider = ArrayContentProvider.getInstance();
57     }
58
59     @Override
60     public void init(IViewSite site, IMemento memento) throws PartInitException {
61         super.init(site, memento);
62         IAction action = new Action("Refresh") {
63             @Override
64             public void run() {
65                 scheduleUpdateEntries();
66             }
67         };
68         action.setImageDescriptor(imageRegistry.getDescriptor("arrow_refresh"));
69         site.getActionBars().getToolBarManager().add(action);
70     }
71
72     private void createFilter(Composite parent) {
73         expressionFilter = new SCLExpressionItemFilter();
74
75         Text searchText = new Text(parent, SWT.BORDER | SWT.SEARCH);
76         GridDataFactory.fillDefaults().grab(true, false).applyTo(searchText);
77         searchText.addKeyListener(new KeyAdapter() {
78
79             @Override
80             public void keyReleased(KeyEvent e) {
81                 expressionFilter.setSearchText(searchText.getText());
82                 tableViewer.refresh();
83             }
84         });
85     }
86
87     @Override
88     public void createPartControl(Composite parent) {
89         parent.setLayout(new GridLayout(1, false));
90         GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);
91         createFilter(parent);
92
93         Composite tableParent = new Composite(parent, SWT.NONE);
94         GridDataFactory.fillDefaults().grab(true, true).applyTo(tableParent);
95
96         tableViewer = new TableViewer(tableParent,
97                 SWT.FULL_SELECTION | SWT.SINGLE | SWT.V_SCROLL | SWT.H_SCROLL | SWT.VIRTUAL);
98         ColumnViewerToolTipSupport.enableFor(tableViewer);
99         tableViewer.setContentProvider(expressionContentProvider);
100         tableViewer.addFilter(expressionFilter);
101
102         Table table = tableViewer.getTable();
103         table.setHeaderVisible(true);
104         table.setLinesVisible(true);
105
106         TableColumnLayout layout = new TableColumnLayout();
107         tableParent.setLayout(layout);
108
109         expressionColumn = new TableViewerColumn(tableViewer, SWT.NONE);
110         expressionColumn.getColumn().setText("Expression");
111         expressionColumn.getColumn().setResizable(true);
112         expressionColumn.setLabelProvider(new ColumnLabelProvider() {
113             @Override
114             public String getText(Object element) {
115                 SCLExpressionTableEntry entry = (SCLExpressionTableEntry) element;
116                 return entry.getContent();
117             }
118         });
119
120         layout.setColumnData(expressionColumn.getColumn(), new ColumnWeightData(100));
121
122         locationColumn = new TableViewerColumn(tableViewer, SWT.NONE);
123         locationColumn.getColumn().setText("Location");
124         locationColumn.getColumn().setResizable(false);
125         locationColumn.setLabelProvider(new ColumnLabelProvider() {
126             @Override
127             public String getText(Object element) {
128                 SCLExpressionTableEntry entry = (SCLExpressionTableEntry) element;
129                 if (entry.getLocation() != null)
130                     return entry.getLocation();
131                 return "null";
132             }
133         });
134
135         layout.setColumnData(locationColumn.getColumn(), new ColumnWeightData(100));
136         tableViewer.addDoubleClickListener(new IDoubleClickListener() {
137             @Override
138             public void doubleClick(DoubleClickEvent event) {
139                 IStructuredSelection selection = (IStructuredSelection) event.getSelection();
140                 SCLExpressionTableEntry entry = (SCLExpressionTableEntry) selection.getFirstElement();
141
142                 openResource(tableViewer.getControl().getDisplay().getActiveShell(), entry.getResource());
143             }
144         });
145         scheduleUpdateEntries();
146     }
147
148     private void scheduleUpdateEntries() {
149         Job updateJob = new DatabaseJob("Update used SCL expressions") {
150
151             @Override
152             protected IStatus run(IProgressMonitor monitor) {
153                 try {
154                     Collection<SCLExpressionTableEntry> result = Simantics.getSession()
155                             .syncRequest(new UsedSCLExpressionsRequest());
156                     tableViewer.getTable().getDisplay().asyncExec(() -> {
157                         tableViewer.setInput(result);
158                     });
159                     return Status.OK_STATUS;
160                 } catch (DatabaseException e) {
161                     e.printStackTrace();
162                     return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not update SCL expressions", e);
163                 }
164             }
165         };
166         updateJob.schedule();
167     }
168
169     private static void openResource(Shell shell, Resource resource) {
170         DefaultActions.performDefaultAction(shell, new StructuredSelection(resource));
171     }
172
173     @Override
174     public void setFocus() {
175         tableViewer.getControl().setFocus();
176     }
177
178     @Override
179     public void dispose() {
180         super.dispose();
181     }
182
183 }