]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/PluginSelectionDialog.java
(refs #7362) Creation of new SCL modules in SCL module browser
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / modulebrowser / PluginSelectionDialog.java
1 package org.simantics.scl.ui.modulebrowser;
2
3 import java.util.Collection;
4 import java.util.Comparator;
5
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;
19
20 public class PluginSelectionDialog extends FilteredItemsSelectionDialog {
21
22     private static final String PLUGIN_SELECTION_DIALOG = "PLUGIN_SELECTION_DIALOG";
23     
24     public static class Entry {
25         Bundle bundle;
26         int packageMatchLength;
27         public Entry(Bundle bundle, int packageMatchLength) {
28             this.bundle = bundle;
29             this.packageMatchLength = packageMatchLength;
30         }
31     }
32     Collection<Entry> entries;
33     
34     public PluginSelectionDialog(Shell shell, Collection<Entry> entries) {
35         super(shell, true);
36         this.entries = entries;
37         setListLabelProvider(new LabelProvider() {
38             @Override
39             public String getText(Object element) {
40                 Entry entry = (Entry)element;
41                 if(entry == null)
42                     return "NULL";
43                 return entry.bundle.getSymbolicName();
44             }
45         });
46     }
47
48     @Override
49     protected Control createExtendedContentArea(Composite parent) {
50         return null;
51     }
52
53     @Override
54     protected IDialogSettings getDialogSettings() {
55         IDialogSettings settings = org.simantics.scl.ui.Activator.getInstance()
56                 .getDialogSettings().getSection(PLUGIN_SELECTION_DIALOG);
57         if (settings == null)
58             settings = Activator.getInstance()
59             .getDialogSettings().addNewSection(PLUGIN_SELECTION_DIALOG);
60         return settings;
61     }
62
63     @Override
64     protected IStatus validateItem(Object item) {
65         return Status.OK_STATUS;
66     }
67
68     @Override
69     protected ItemsFilter createFilter() {
70         return new ItemsFilter() {
71             {
72                 String patternText = getPattern();
73                 patternMatcher = new SearchPattern();
74                 if(patternText != null && patternText.length() > 0)
75                     patternMatcher.setPattern(patternText);
76                 else 
77                     patternMatcher.setPattern("*");
78             }
79             
80             @Override
81             public boolean matchItem(Object item) {
82                 Entry entry = (Entry)item;
83                 String name = entry.bundle.getSymbolicName();
84                 if(getPattern().indexOf('/') > 0)
85                     return matches(name);
86                 else {
87                     for(String part : name.split("/"))
88                         if(matches(part))
89                             return true;
90                     return false;
91                 }
92             }
93
94             @Override
95             public boolean isConsistentItem(Object item) {
96                 return true;
97             }
98             
99         };
100     }
101
102     Comparator<Entry> comparator = new Comparator<Entry>() {
103         @Override
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());
108         }
109     };
110     
111     @SuppressWarnings("rawtypes")
112     @Override
113     protected Comparator getItemsComparator() {
114         return comparator;
115     }
116
117     @Override
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();
125     }
126
127     @Override
128     public String getElementName(Object item) {
129         Entry entry = (Entry)item;
130         return entry.bundle.getSymbolicName();
131     }
132
133 }