1 package org.simantics.modeling.ui.scl.expressions;
3 import java.util.Collection;
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;
44 public class SCLExpressionView extends ViewPart {
46 private TableViewer tableViewer;
47 private TableViewerColumn expressionColumn;
48 private TableViewerColumn locationColumn;
50 private ImageRegistry imageRegistry;
51 private IStructuredContentProvider expressionContentProvider;
52 private SCLExpressionItemFilter expressionFilter;
54 public SCLExpressionView() {
55 imageRegistry = Activator.getDefault().getImageRegistry();
56 expressionContentProvider = ArrayContentProvider.getInstance();
60 public void init(IViewSite site, IMemento memento) throws PartInitException {
61 super.init(site, memento);
62 IAction action = new Action("Refresh") {
65 scheduleUpdateEntries();
68 action.setImageDescriptor(imageRegistry.getDescriptor("arrow_refresh"));
69 site.getActionBars().getToolBarManager().add(action);
72 private void createFilter(Composite parent) {
73 expressionFilter = new SCLExpressionItemFilter();
75 Text searchText = new Text(parent, SWT.BORDER | SWT.SEARCH);
76 GridDataFactory.fillDefaults().grab(true, false).applyTo(searchText);
77 searchText.addKeyListener(new KeyAdapter() {
80 public void keyReleased(KeyEvent e) {
81 expressionFilter.setSearchText(searchText.getText());
82 tableViewer.refresh();
88 public void createPartControl(Composite parent) {
89 parent.setLayout(new GridLayout(1, false));
90 GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);
93 Composite tableParent = new Composite(parent, SWT.NONE);
94 GridDataFactory.fillDefaults().grab(true, true).applyTo(tableParent);
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);
102 Table table = tableViewer.getTable();
103 table.setHeaderVisible(true);
104 table.setLinesVisible(true);
106 TableColumnLayout layout = new TableColumnLayout();
107 tableParent.setLayout(layout);
109 expressionColumn = new TableViewerColumn(tableViewer, SWT.NONE);
110 expressionColumn.getColumn().setText("Expression");
111 expressionColumn.getColumn().setResizable(true);
112 expressionColumn.setLabelProvider(new ColumnLabelProvider() {
114 public String getText(Object element) {
115 SCLExpressionTableEntry entry = (SCLExpressionTableEntry) element;
116 return entry.getContent();
120 layout.setColumnData(expressionColumn.getColumn(), new ColumnWeightData(100));
122 locationColumn = new TableViewerColumn(tableViewer, SWT.NONE);
123 locationColumn.getColumn().setText("Location");
124 locationColumn.getColumn().setResizable(false);
125 locationColumn.setLabelProvider(new ColumnLabelProvider() {
127 public String getText(Object element) {
128 SCLExpressionTableEntry entry = (SCLExpressionTableEntry) element;
129 if (entry.getLocation() != null)
130 return entry.getLocation();
135 layout.setColumnData(locationColumn.getColumn(), new ColumnWeightData(100));
136 tableViewer.addDoubleClickListener(new IDoubleClickListener() {
138 public void doubleClick(DoubleClickEvent event) {
139 IStructuredSelection selection = (IStructuredSelection) event.getSelection();
140 SCLExpressionTableEntry entry = (SCLExpressionTableEntry) selection.getFirstElement();
142 openResource(tableViewer.getControl().getDisplay().getActiveShell(), entry.getResource());
145 scheduleUpdateEntries();
148 private void scheduleUpdateEntries() {
149 Job updateJob = new DatabaseJob("Update used SCL expressions") {
152 protected IStatus run(IProgressMonitor monitor) {
154 Collection<SCLExpressionTableEntry> result = Simantics.getSession()
155 .syncRequest(new UsedSCLExpressionsRequest());
156 tableViewer.getTable().getDisplay().asyncExec(() -> {
157 tableViewer.setInput(result);
159 return Status.OK_STATUS;
160 } catch (DatabaseException e) {
162 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not update SCL expressions", e);
166 updateJob.schedule();
169 private static void openResource(Shell shell, Resource resource) {
170 DefaultActions.performDefaultAction(shell, new StructuredSelection(resource));
174 public void setFocus() {
175 tableViewer.getControl().setFocus();
179 public void dispose() {