]> gerrit.simantics Code Review - simantics/platform.git/blob
32a42a47355e2087883e980a8d44a74e68430d32
[simantics/platform.git] /
1 package org.simantics.scl.ui.imports.internal;
2
3 import gnu.trove.procedure.TObjectProcedure;
4
5 import java.util.Comparator;
6
7 import org.eclipse.core.runtime.CoreException;
8 import org.eclipse.core.runtime.IProgressMonitor;
9 import org.eclipse.core.runtime.IStatus;
10 import org.eclipse.core.runtime.Status;
11 import org.eclipse.jface.dialogs.IDialogSettings;
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.simantics.scl.osgi.SCLOsgi;
18 import org.simantics.scl.ui.Activator;
19
20 public class SCLModuleSelectionDialog extends FilteredItemsSelectionDialog {
21
22     private static final String SCL_MODULE_SELECTION_DIALOG = "SCL_MODULE_SELECTION_DIALOG";
23     
24     public SCLModuleSelectionDialog(Shell shell) {
25         super(shell, true);
26     }
27
28     @Override
29     protected Control createExtendedContentArea(Composite parent) {
30         return null;
31     }
32
33     @Override
34     protected IDialogSettings getDialogSettings() {
35         IDialogSettings settings = org.simantics.scl.ui.Activator.getInstance()
36                 .getDialogSettings().getSection(SCL_MODULE_SELECTION_DIALOG);
37         if (settings == null)
38             settings = Activator.getInstance()
39             .getDialogSettings().addNewSection(SCL_MODULE_SELECTION_DIALOG);
40         return settings;
41     }
42
43     @Override
44     protected IStatus validateItem(Object item) {
45         return Status.OK_STATUS;
46     }
47
48     @Override
49     protected ItemsFilter createFilter() {
50         return new ItemsFilter() {
51             {
52                 String patternText = getPattern();
53                 patternMatcher = new SearchPattern();
54                 if(patternText != null && patternText.length() > 0)
55                     patternMatcher.setPattern(patternText);
56                 else 
57                     patternMatcher.setPattern("*");
58             }
59             
60             @Override
61             public boolean matchItem(Object item) {
62                 if(getPattern().indexOf('/') > 0)
63                     return matches((String)item);
64                 else {
65                     for(String part : ((String)item).split("/"))
66                         if(matches(part))
67                             return true;
68                     return false;
69                 }
70             }
71
72             @Override
73             public boolean isConsistentItem(Object item) {
74                 return true;
75             }
76             
77         };
78     }
79
80     Comparator<String> comparator = new Comparator<String>() {
81         @Override
82         public int compare(String o1, String o2) {
83             return o1.compareTo(o2);
84         }
85     };
86     
87     @SuppressWarnings("rawtypes")
88     @Override
89     protected Comparator getItemsComparator() {
90         return comparator;
91     }
92
93     @Override
94     protected void fillContentProvider(final AbstractContentProvider contentProvider,
95             final ItemsFilter itemsFilter, IProgressMonitor progressMonitor)
96             throws CoreException {
97         SCLOsgi.SOURCE_REPOSITORY.forAllModules(new TObjectProcedure<String>() {
98             @Override
99             public boolean execute(String module) {
100                 contentProvider.add(module, itemsFilter);
101                 return true;
102             }
103         });
104         if(progressMonitor != null)
105             progressMonitor.done();
106     }
107
108     @Override
109     public String getElementName(Object item) {
110         return (String)item;
111     }
112
113 }