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.ISelection;
21 import org.eclipse.jface.viewers.IStructuredContentProvider;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.StructuredSelection;
24 import org.eclipse.jface.viewers.TableViewer;
25 import org.eclipse.jface.viewers.TableViewerColumn;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.KeyAdapter;
28 import org.eclipse.swt.events.KeyEvent;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.swt.widgets.Table;
33 import org.eclipse.swt.widgets.Text;
34 import org.eclipse.ui.IMemento;
35 import org.eclipse.ui.IViewSite;
36 import org.eclipse.ui.PartInitException;
37 import org.eclipse.ui.part.ViewPart;
38 import org.simantics.DatabaseJob;
39 import org.simantics.Simantics;
40 import org.simantics.db.Resource;
41 import org.simantics.db.exception.DatabaseException;
42 import org.simantics.modeling.ui.Activator;
43 import org.simantics.ui.workbench.action.ChooseActionRequest;
44 import org.simantics.utils.ui.workbench.WorkbenchUtils;
46 public class SCLExpressionView extends ViewPart {
48 private TableViewer tableViewer;
49 private TableViewerColumn expressionColumn;
50 private TableViewerColumn locationColumn;
52 private ImageRegistry imageRegistry;
53 private IStructuredContentProvider expressionContentProvider;
54 private SCLExpressionItemFilter expressionFilter;
56 public SCLExpressionView() {
57 imageRegistry = Activator.getDefault().getImageRegistry();
58 expressionContentProvider = ArrayContentProvider.getInstance();
62 public void init(IViewSite site, IMemento memento) throws PartInitException {
63 super.init(site, memento);
64 IAction action = new Action("Refresh") {
67 scheduleUpdateEntries();
70 action.setImageDescriptor(imageRegistry.getDescriptor("arrow_refresh"));
71 site.getActionBars().getToolBarManager().add(action);
74 private void createFilter(Composite parent) {
75 expressionFilter = new SCLExpressionItemFilter();
77 Text searchText = new Text(parent, SWT.BORDER | SWT.SEARCH);
78 GridDataFactory.fillDefaults().grab(true, false).applyTo(searchText);
79 searchText.addKeyListener(new KeyAdapter() {
82 public void keyReleased(KeyEvent e) {
83 expressionFilter.setSearchText(searchText.getText());
84 tableViewer.refresh();
90 public void createPartControl(Composite parent) {
91 parent.setLayout(new GridLayout(1, false));
92 GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);
95 Composite tableParent = new Composite(parent, SWT.NONE);
96 GridDataFactory.fillDefaults().grab(true, true).applyTo(tableParent);
98 tableViewer = new TableViewer(tableParent,
99 SWT.FULL_SELECTION | SWT.SINGLE | SWT.V_SCROLL | SWT.H_SCROLL | SWT.VIRTUAL);
100 ColumnViewerToolTipSupport.enableFor(tableViewer);
101 tableViewer.setContentProvider(expressionContentProvider);
102 tableViewer.addFilter(expressionFilter);
104 Table table = tableViewer.getTable();
105 table.setHeaderVisible(true);
106 table.setLinesVisible(true);
108 TableColumnLayout layout = new TableColumnLayout();
109 tableParent.setLayout(layout);
111 expressionColumn = new TableViewerColumn(tableViewer, SWT.NONE);
112 expressionColumn.getColumn().setText("Expression");
113 expressionColumn.getColumn().setResizable(true);
114 expressionColumn.setLabelProvider(new ColumnLabelProvider() {
116 public String getText(Object element) {
117 SCLExpressionTableEntry entry = (SCLExpressionTableEntry) element;
118 return entry.getContent();
122 layout.setColumnData(expressionColumn.getColumn(), new ColumnWeightData(100));
124 locationColumn = new TableViewerColumn(tableViewer, SWT.NONE);
125 locationColumn.getColumn().setText("Location");
126 locationColumn.getColumn().setResizable(false);
127 locationColumn.setLabelProvider(new ColumnLabelProvider() {
129 public String getText(Object element) {
130 SCLExpressionTableEntry entry = (SCLExpressionTableEntry) element;
131 if (entry.getLocation() != null)
132 return entry.getLocation();
137 layout.setColumnData(locationColumn.getColumn(), new ColumnWeightData(100));
138 tableViewer.addDoubleClickListener(new IDoubleClickListener() {
140 public void doubleClick(DoubleClickEvent event) {
141 IStructuredSelection selection = (IStructuredSelection) event.getSelection();
142 SCLExpressionTableEntry entry = (SCLExpressionTableEntry) selection.getFirstElement();
144 openResource(tableViewer.getControl().getDisplay().getActiveShell(), entry.getResource());
147 scheduleUpdateEntries();
150 private void scheduleUpdateEntries() {
151 Job updateJob = new DatabaseJob("Update used SCL expressions") {
154 protected IStatus run(IProgressMonitor monitor) {
156 Collection<SCLExpressionTableEntry> result = Simantics.getSession()
157 .syncRequest(new UsedSCLExpressionsRequest());
158 tableViewer.getTable().getDisplay().asyncExec(() -> {
159 tableViewer.setInput(result);
161 return Status.OK_STATUS;
162 } catch (DatabaseException e) {
164 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not update SCL expressions", e);
168 updateJob.schedule();
171 private static void openResource(Shell shell, Resource resource) {
173 ISelection input = new StructuredSelection(resource);
174 String perspectiveId = WorkbenchUtils.getCurrentPerspectiveId();
175 // Try the doubleClick-extensions
176 Simantics.getSession().asyncRequest(new ChooseActionRequest(shell, input, perspectiveId, false, true));
177 } catch (NumberFormatException e) {
183 public void setFocus() {
184 tableViewer.getControl().setFocus();
188 public void dispose() {