1 package org.simantics.scl.ui.modulebrowser;
3 import java.util.Collection;
4 import java.util.Comparator;
6 import org.eclipse.core.runtime.CoreException;
7 import org.eclipse.core.runtime.IProgressMonitor;
8 import org.eclipse.core.runtime.IStatus;
9 import org.eclipse.core.runtime.Status;
10 import org.eclipse.jface.dialogs.IDialogSettings;
11 import org.eclipse.jface.viewers.LabelProvider;
12 import org.eclipse.swt.widgets.Composite;
13 import org.eclipse.swt.widgets.Control;
14 import org.eclipse.swt.widgets.Shell;
15 import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog;
16 import org.eclipse.ui.dialogs.SearchPattern;
17 import org.osgi.framework.Bundle;
18 import org.simantics.scl.ui.Activator;
20 public class PluginSelectionDialog extends FilteredItemsSelectionDialog {
22 private static final String PLUGIN_SELECTION_DIALOG = "PLUGIN_SELECTION_DIALOG";
24 public static class Entry {
26 int packageMatchLength;
27 public Entry(Bundle bundle, int packageMatchLength) {
29 this.packageMatchLength = packageMatchLength;
32 Collection<Entry> entries;
34 public PluginSelectionDialog(Shell shell, Collection<Entry> entries) {
36 this.entries = entries;
37 setListLabelProvider(new LabelProvider() {
39 public String getText(Object element) {
40 Entry entry = (Entry)element;
43 return entry.bundle.getSymbolicName();
49 protected Control createExtendedContentArea(Composite parent) {
54 protected IDialogSettings getDialogSettings() {
55 IDialogSettings settings = org.simantics.scl.ui.Activator.getInstance()
56 .getDialogSettings().getSection(PLUGIN_SELECTION_DIALOG);
58 settings = Activator.getInstance()
59 .getDialogSettings().addNewSection(PLUGIN_SELECTION_DIALOG);
64 protected IStatus validateItem(Object item) {
65 return Status.OK_STATUS;
69 protected ItemsFilter createFilter() {
70 return new ItemsFilter() {
72 String patternText = getPattern();
73 patternMatcher = new SearchPattern();
74 if(patternText != null && patternText.length() > 0)
75 patternMatcher.setPattern(patternText);
77 patternMatcher.setPattern("*");
81 public boolean matchItem(Object item) {
82 Entry entry = (Entry)item;
83 String name = entry.bundle.getSymbolicName();
84 if(getPattern().indexOf('/') > 0)
87 for(String part : name.split("/"))
95 public boolean isConsistentItem(Object item) {
102 Comparator<Entry> comparator = new Comparator<Entry>() {
104 public int compare(Entry o1, Entry o2) {
105 if(o1.packageMatchLength != o2.packageMatchLength)
106 return Integer.compare(o2.packageMatchLength, o1.packageMatchLength);
107 return o1.bundle.getSymbolicName().compareTo(o2.bundle.getSymbolicName());
111 @SuppressWarnings("rawtypes")
113 protected Comparator getItemsComparator() {
118 protected void fillContentProvider(final AbstractContentProvider contentProvider,
119 final ItemsFilter itemsFilter, IProgressMonitor progressMonitor)
120 throws CoreException {
121 for(Entry entry : entries)
122 contentProvider.add(entry, itemsFilter);
123 if(progressMonitor != null)
124 progressMonitor.done();
128 public String getElementName(Object item) {
129 Entry entry = (Entry)item;
130 return entry.bundle.getSymbolicName();