]> gerrit.simantics Code Review - simantics/platform.git/blob
d9bf77e47555210c64a31b0d46f89501bbd05c07
[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.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;
45
46 public class SCLExpressionView extends ViewPart {
47
48     private TableViewer tableViewer;
49     private TableViewerColumn expressionColumn;
50     private TableViewerColumn locationColumn;
51
52     private ImageRegistry imageRegistry;
53     private IStructuredContentProvider expressionContentProvider;
54     private SCLExpressionItemFilter expressionFilter;
55
56     public SCLExpressionView() {
57         imageRegistry = Activator.getDefault().getImageRegistry();
58         expressionContentProvider = ArrayContentProvider.getInstance();
59     }
60
61     @Override
62     public void init(IViewSite site, IMemento memento) throws PartInitException {
63         super.init(site, memento);
64         IAction action = new Action("Refresh") {
65             @Override
66             public void run() {
67                 scheduleUpdateEntries();
68             }
69         };
70         action.setImageDescriptor(imageRegistry.getDescriptor("arrow_refresh"));
71         site.getActionBars().getToolBarManager().add(action);
72     }
73
74     private void createFilter(Composite parent) {
75         expressionFilter = new SCLExpressionItemFilter();
76
77         Text searchText = new Text(parent, SWT.BORDER | SWT.SEARCH);
78         GridDataFactory.fillDefaults().grab(true, false).applyTo(searchText);
79         searchText.addKeyListener(new KeyAdapter() {
80
81             @Override
82             public void keyReleased(KeyEvent e) {
83                 expressionFilter.setSearchText(searchText.getText());
84                 tableViewer.refresh();
85             }
86         });
87     }
88
89     @Override
90     public void createPartControl(Composite parent) {
91         parent.setLayout(new GridLayout(1, false));
92         GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);
93         createFilter(parent);
94
95         Composite tableParent = new Composite(parent, SWT.NONE);
96         GridDataFactory.fillDefaults().grab(true, true).applyTo(tableParent);
97
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);
103
104         Table table = tableViewer.getTable();
105         table.setHeaderVisible(true);
106         table.setLinesVisible(true);
107
108         TableColumnLayout layout = new TableColumnLayout();
109         tableParent.setLayout(layout);
110
111         expressionColumn = new TableViewerColumn(tableViewer, SWT.NONE);
112         expressionColumn.getColumn().setText("Expression");
113         expressionColumn.getColumn().setResizable(true);
114         expressionColumn.setLabelProvider(new ColumnLabelProvider() {
115             @Override
116             public String getText(Object element) {
117                 SCLExpressionTableEntry entry = (SCLExpressionTableEntry) element;
118                 return entry.getContent();
119             }
120         });
121
122         layout.setColumnData(expressionColumn.getColumn(), new ColumnWeightData(100));
123
124         locationColumn = new TableViewerColumn(tableViewer, SWT.NONE);
125         locationColumn.getColumn().setText("Location");
126         locationColumn.getColumn().setResizable(false);
127         locationColumn.setLabelProvider(new ColumnLabelProvider() {
128             @Override
129             public String getText(Object element) {
130                 SCLExpressionTableEntry entry = (SCLExpressionTableEntry) element;
131                 if (entry.getLocation() != null)
132                     return entry.getLocation();
133                 return "null";
134             }
135         });
136
137         layout.setColumnData(locationColumn.getColumn(), new ColumnWeightData(100));
138         tableViewer.addDoubleClickListener(new IDoubleClickListener() {
139             @Override
140             public void doubleClick(DoubleClickEvent event) {
141                 IStructuredSelection selection = (IStructuredSelection) event.getSelection();
142                 SCLExpressionTableEntry entry = (SCLExpressionTableEntry) selection.getFirstElement();
143
144                 openResource(tableViewer.getControl().getDisplay().getActiveShell(), entry.getResource());
145             }
146         });
147         scheduleUpdateEntries();
148     }
149
150     private void scheduleUpdateEntries() {
151         Job updateJob = new DatabaseJob("Update used SCL expressions") {
152
153             @Override
154             protected IStatus run(IProgressMonitor monitor) {
155                 try {
156                     Collection<SCLExpressionTableEntry> result = Simantics.getSession()
157                             .syncRequest(new UsedSCLExpressionsRequest());
158                     tableViewer.getTable().getDisplay().asyncExec(() -> {
159                         tableViewer.setInput(result);
160                     });
161                     return Status.OK_STATUS;
162                 } catch (DatabaseException e) {
163                     e.printStackTrace();
164                     return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not update SCL expressions", e);
165                 }
166             }
167         };
168         updateJob.schedule();
169     }
170
171     private static void openResource(Shell shell, Resource resource) {
172         try {
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) {
178             return;
179         }
180     }
181
182     @Override
183     public void setFocus() {
184         tableViewer.getControl().setFocus();
185     }
186
187     @Override
188     public void dispose() {
189         super.dispose();
190     }
191
192 }