]> gerrit.simantics Code Review - simantics/platform.git/blob
8d07d60b1bb111ab1fbd96619e4b3eedc84a84d3
[simantics/platform.git] /
1 package org.simantics.modeling.ui.scl.imports;
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.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;
22
23 public class ModuleSelectionDialog extends FilteredItemsSelectionDialog {
24
25     private static final String SCL_MODULE_SELECTION_DIALOG = "SCL_MODULE_SELECTION_DIALOG";
26     
27     Read<Collection<String>> query;
28     
29     public ModuleSelectionDialog(Shell shell, Read<Collection<String>> query) {
30         super(shell, true);
31         this.query = query;
32     }
33
34     @Override
35     protected Control createExtendedContentArea(Composite parent) {
36         return null;
37     }
38
39     @Override
40     protected IDialogSettings getDialogSettings() {
41         IDialogSettings settings = Activator.getDefault()
42                 .getDialogSettings().getSection(SCL_MODULE_SELECTION_DIALOG);
43         if (settings == null)
44             settings = Activator.getDefault()
45             .getDialogSettings().addNewSection(SCL_MODULE_SELECTION_DIALOG);
46         return settings;
47     }
48
49     @Override
50     protected IStatus validateItem(Object item) {
51         return Status.OK_STATUS;
52     }
53
54     @Override
55     protected ItemsFilter createFilter() {
56         return new ItemsFilter() {
57             {
58                 String patternText = getPattern();
59                 patternMatcher = new SearchPattern();
60                 if(patternText != null && patternText.length() > 0)
61                     patternMatcher.setPattern(patternText);
62                 else 
63                     patternMatcher.setPattern("*");
64             }
65             
66             @Override
67             public boolean matchItem(Object item) {
68                 if(getPattern().indexOf('/') > 0)
69                     return matches((String)item);
70                 else {
71                     for(String part : ((String)item).split("/"))
72                         if(matches(part))
73                             return true;
74                     return false;
75                 }
76             }
77
78             @Override
79             public boolean isConsistentItem(Object item) {
80                 return true;
81             }
82             
83         };
84     }
85
86     Comparator<String> comparator = new Comparator<String>() {
87         @Override
88         public int compare(String o1, String o2) {
89             return o1.compareTo(o2);
90         }
91     };
92     
93     @SuppressWarnings("rawtypes")
94     @Override
95     protected Comparator getItemsComparator() {
96         return comparator;
97     }
98
99     @Override
100     protected void fillContentProvider(final AbstractContentProvider contentProvider,
101             final ItemsFilter itemsFilter, IProgressMonitor progressMonitor)
102             throws CoreException {
103         try {
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);
110             close();
111         }
112
113         if(progressMonitor != null)
114             progressMonitor.done();
115     }
116
117     @Override
118     public String getElementName(Object item) {
119         return (String)item;
120     }
121
122 }