1 package org.simantics.modeling.ui.scl.imports;
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.swt.widgets.Composite;
12 import org.eclipse.swt.widgets.Control;
13 import org.eclipse.swt.widgets.Shell;
14 import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog;
15 import org.eclipse.ui.dialogs.SearchPattern;
16 import org.simantics.Simantics;
17 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.request.Read;
20 import org.simantics.modeling.ui.Activator;
21 import org.simantics.utils.ui.dialogs.ShowError;
23 public class ModuleSelectionDialog extends FilteredItemsSelectionDialog {
25 private static final String SCL_MODULE_SELECTION_DIALOG = "SCL_MODULE_SELECTION_DIALOG";
27 Read<Collection<String>> query;
29 public ModuleSelectionDialog(Shell shell, Read<Collection<String>> query) {
35 protected Control createExtendedContentArea(Composite parent) {
40 protected IDialogSettings getDialogSettings() {
41 IDialogSettings settings = Activator.getDefault()
42 .getDialogSettings().getSection(SCL_MODULE_SELECTION_DIALOG);
44 settings = Activator.getDefault()
45 .getDialogSettings().addNewSection(SCL_MODULE_SELECTION_DIALOG);
50 protected IStatus validateItem(Object item) {
51 return Status.OK_STATUS;
55 protected ItemsFilter createFilter() {
56 return new ItemsFilter() {
58 String patternText = getPattern();
59 patternMatcher = new SearchPattern();
60 if(patternText != null && patternText.length() > 0)
61 patternMatcher.setPattern(patternText);
63 patternMatcher.setPattern("*");
67 public boolean matchItem(Object item) {
68 if(getPattern().indexOf('/') > 0)
69 return matches((String)item);
71 for(String part : ((String)item).split("/"))
79 public boolean isConsistentItem(Object item) {
86 Comparator<String> comparator = new Comparator<String>() {
88 public int compare(String o1, String o2) {
89 return o1.compareTo(o2);
93 @SuppressWarnings("rawtypes")
95 protected Comparator getItemsComparator() {
100 protected void fillContentProvider(final AbstractContentProvider contentProvider,
101 final ItemsFilter itemsFilter, IProgressMonitor progressMonitor)
102 throws CoreException {
104 for(String module : Simantics.getSession().syncRequest(query,
105 TransientCacheListener.<Collection<String>>instance()))
106 contentProvider.add(module, itemsFilter);
107 } catch (DatabaseException e) {
108 ShowError.showError("Failed to find modules",
109 "Exception was thrown during search for modules", e);
113 if(progressMonitor != null)
114 progressMonitor.done();
118 public String getElementName(Object item) {