]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/issues/SCLIssuesContentProvider.java
Prime SCL BindingRegistry to shave ~0.5s from startup
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / issues / SCLIssuesContentProvider.java
1 package org.simantics.scl.ui.issues;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.concurrent.atomic.AtomicBoolean;
7 import java.util.stream.Collectors;
8
9 import org.eclipse.jface.viewers.IStructuredContentProvider;
10 import org.eclipse.jface.viewers.Viewer;
11 import org.eclipse.swt.widgets.Control;
12 import org.simantics.scl.osgi.issues.SCLIssueFactoryProvider;
13 import org.simantics.scl.osgi.issues.SCLIssueProviderFactory.SCLIssueProvider;
14 import org.simantics.scl.osgi.issues.SCLIssuesTableEntry;
15
16 public class SCLIssuesContentProvider implements IStructuredContentProvider {
17
18     public static final int MAX_ISSUE_COUNT = 1000;
19
20     private Viewer viewer;
21     boolean disposed = false;
22     private AtomicBoolean refreshInProgress = new AtomicBoolean(false);
23     private List<SCLIssueProvider> issueProviders = new ArrayList<>();
24
25     @Override
26     public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
27         this.viewer = viewer;
28         if (!issueProviders.isEmpty()) {
29             issueProviders.forEach(p -> p.dispose());
30             issueProviders.clear();
31         }
32         if(newInput != null) {
33             issueProviders = SCLIssueFactoryProvider.getSCLIssueProviderFactories().stream().map(f -> f.getSCLIssueProvider()).collect(Collectors.toList());
34             issueProviders.forEach(p -> p.listenIssues(() -> refresh()));
35         }
36     }
37
38     private void refresh() {
39         if(!refreshInProgress.compareAndSet(false, true))
40             return;
41         if(viewer == null)
42             return;
43         Control control = viewer.getControl();
44         if(control.isDisposed() || disposed)
45             return;
46         control.getDisplay().asyncExec(new Runnable() {
47             @Override
48             public void run() {
49                 if(control.isDisposed() || disposed)
50                     return;
51                 refreshInProgress.set(false);
52                 viewer.refresh();
53             }
54         });
55     }
56
57     public void dispose() {
58         if(this.disposed)
59             return;
60         issueProviders.forEach(p -> p.dispose());
61         issueProviders.clear();
62         this.disposed = true;
63     }
64
65     @Override
66     public Object[] getElements(Object inputElement) {
67         ArrayList<SCLIssuesTableEntry> result = new ArrayList<>();
68         issueProviders.forEach(p -> result.addAll(p.getIssues()));
69         Collections.sort(result);
70         return result.toArray();
71     }
72
73 }