]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/workbench/dialogs/ResourceSelectionDialog3.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / dialogs / ResourceSelectionDialog3.java
diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/dialogs/ResourceSelectionDialog3.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/dialogs/ResourceSelectionDialog3.java
new file mode 100644 (file)
index 0000000..1b8d408
--- /dev/null
@@ -0,0 +1,209 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.ui.workbench.dialogs;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.Map;\r
+\r
+import org.eclipse.core.runtime.CoreException;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.jface.dialogs.IDialogSettings;\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.eclipse.jface.resource.JFaceResources;\r
+import org.eclipse.jface.resource.LocalResourceManager;\r
+import org.eclipse.jface.viewers.ILabelProvider;\r
+import org.eclipse.jface.viewers.LabelProvider;\r
+import org.eclipse.swt.graphics.Image;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.ui.IMemento;\r
+import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog;\r
+import org.eclipse.ui.dialogs.SearchPattern;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.InvalidResourceReferenceException;\r
+import org.simantics.db.service.SerialisationSupport;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.internal.Activator;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+public abstract class ResourceSelectionDialog3<T> extends FilteredItemsSelectionDialog {\r
+\r
+    private final Map<T, Pair<String, ImageDescriptor>> contentMap;\r
+    private final String title;\r
+    private LocalResourceManager resourceManager;\r
+\r
+    class ResourceSelectionHistory extends FilteredItemsSelectionDialog.SelectionHistory {\r
+\r
+        @Override\r
+        protected Object restoreItemFromMemento(IMemento memento) {\r
+            // FIXME: somehow create a collective transaction inside which the Graph.getRandomAccessReference should be invoked.\r
+//            Resource r = SimanticsUI.getSession().getRandomAccessReference(memento.getTextData());\r
+//            return r;\r
+            return null;\r
+        }\r
+\r
+        @Override\r
+        protected void storeItemToMemento(Object item, IMemento memento) {\r
+            if(item instanceof Resource) {\r
+                try {\r
+                    SerialisationSupport support = SimanticsUI.getSession().getService(SerialisationSupport.class);\r
+                    memento.putTextData(support.getResourceSerializer().createRandomAccessId((Resource)item));\r
+                } catch (InvalidResourceReferenceException e) {\r
+                    e.printStackTrace();\r
+                }\r
+            }\r
+        }\r
+        \r
+    };\r
+\r
+    public ResourceSelectionDialog3(Shell shell, Map<T, Pair<String, ImageDescriptor>> parameter, String title) {\r
+        this(shell, parameter, title, true);\r
+    }\r
+\r
+    @Override\r
+    protected Control createContents(Composite parent) {\r
+        this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent);\r
+        return super.createContents(parent);\r
+    }\r
+    \r
+    public ResourceSelectionDialog3(Shell shell, Map<T, Pair<String, ImageDescriptor>> parameter, String title, boolean multi) {\r
+        \r
+        super(shell, multi);\r
+        this.contentMap = parameter;\r
+        this.title = title;\r
+\r
+        ILabelProvider labelProvider = new LabelProvider() {\r
+            @Override\r
+            public String getText(Object element) {\r
+                Pair<String, ImageDescriptor> pair = contentMap.get(element);\r
+                if(pair != null) return pair.first;\r
+                else return null;\r
+            }\r
+            @Override\r
+            public Image getImage(Object element) {\r
+                Pair<String, ImageDescriptor> pair = contentMap.get(element);\r
+                if(pair != null && pair.second != null) return resourceManager.createImage(pair.second);\r
+                else return null;\r
+            }\r
+        }; \r
+\r
+        setListLabelProvider(labelProvider);\r
+        setDetailsLabelProvider(labelProvider);\r
+\r
+        setSelectionHistory(new ResourceSelectionHistory());\r
+        setTitle(title);\r
+        \r
+    }\r
+\r
+    @Override\r
+    protected Control createExtendedContentArea(Composite parent) {\r
+        // Don't create anything extra at the moment\r
+        return null;\r
+    }\r
+\r
+    class ResourceSelectionDialogItemsFilter extends FilteredItemsSelectionDialog.ItemsFilter {\r
+        \r
+        public ResourceSelectionDialogItemsFilter() {\r
+            String patternText = getPattern();\r
+            patternMatcher = new SearchPattern();\r
+            if(patternText != null && patternText.length() > 0)\r
+                patternMatcher.setPattern(patternText);\r
+            else \r
+                patternMatcher.setPattern("*");\r
+        }\r
+\r
+        @Override\r
+        public boolean isConsistentItem(Object item) {\r
+            return true;\r
+        }\r
+\r
+        @Override\r
+        public boolean matchItem(Object item) {\r
+            return matches(contentMap.get(item).first);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    protected ItemsFilter createFilter() {\r
+        return new ResourceSelectionDialogItemsFilter();\r
+    }\r
+\r
+    @Override\r
+    protected void fillContentProvider(AbstractContentProvider contentProvider,\r
+            ItemsFilter itemsFilter, IProgressMonitor progressMonitor) throws CoreException {\r
+        for(T o : contentMap.keySet())\r
+            contentProvider.add(o, itemsFilter);\r
+        if (progressMonitor != null)\r
+            progressMonitor.done();\r
+    }\r
+\r
+    protected abstract IDialogSettings getBaseDialogSettings();\r
+\r
+    @Override\r
+    protected IDialogSettings getDialogSettings() {\r
+        IDialogSettings base = getBaseDialogSettings();\r
+        if (base == null) base = Activator.getDefault().getDialogSettings();\r
+        IDialogSettings settings = base.getSection(title);\r
+        if (settings == null)\r
+            settings = base.addNewSection(title);\r
+        return settings;\r
+    }\r
+\r
+    @Override\r
+    public String getElementName(Object item) {\r
+        return contentMap.get(item).first;\r
+    }\r
+\r
+    @Override\r
+    protected Comparator<?> getItemsComparator() {\r
+        return new Comparator<Object>() {\r
+\r
+            @Override\r
+            public int compare(Object o1, Object o2) {\r
+                return contentMap.get(o1).first.compareToIgnoreCase(contentMap.get(o2).first);\r
+            }\r
+            \r
+        };\r
+    }\r
+\r
+    @Override\r
+    protected IStatus validateItem(Object item) {\r
+        return Status.OK_STATUS;\r
+    }\r
+\r
+    /**\r
+     * Made publicly visible.\r
+     * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#updateStatus(org.eclipse.core.runtime.IStatus)\r
+     */\r
+    @Override\r
+    public void updateStatus(IStatus status) {\r
+        super.updateStatus(status);\r
+    }\r
+\r
+    @SuppressWarnings("unchecked")\r
+       public Collection<T> getResultT() {\r
+       Object[] res = getResult();\r
+       if(res == null) return Collections.emptyList();\r
+       ArrayList<T> result = new ArrayList<T>();\r
+       for(Object o : res) {\r
+               result.add((T)o);\r
+       }\r
+       return result;\r
+    }\r
+    \r
+}\r