]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Re-enabled CTRL+SHIFT+R resource search dialog in org.simantics.debug.ui 90/190/3
authorAntti Villberg <antti.villberg@semantum.fi>
Thu, 8 Dec 2016 09:06:36 +0000 (11:06 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 8 Dec 2016 14:10:57 +0000 (16:10 +0200)
This fixes the old problem where the index searches were not limited to
e.g. a 1000 hits but to Integer.MAX_VALUE instead. If the user happened
to write 'a' into the search field, this would return pretty much every
indexed resource and kill the JVM eventually.

This also fixes a problem from
org.simantics.modeling.ui.diagramEditor.DiagramViewer which did not
check diagram resource immutability before attempting to write a diagram
page description to it because it was missing. This can happen if you
use the resource search dialog to open e.g. diagram symbols from the
ontologies.

refs #6855

Change-Id: Id341871847e0ea8bdbc036e8023821ab0556a3cb

bundles/org.simantics.debug.ui/META-INF/MANIFEST.MF
bundles/org.simantics.debug.ui/plugin.xml
bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/SearchResourceDialog.java
bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/ShowInBrowser.java [new file with mode: 0644]
bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/internal/SearchResourceHandler.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramViewer.java

index e2e8859755ea155f9efd20af27dab8bb7454c38c..f548261ca560aadfaaf91276fdf0ff84bb45ffc2 100644 (file)
@@ -18,7 +18,10 @@ Require-Bundle: org.eclipse.core.runtime,
  org.simantics.wiki.ui;bundle-version="1.1.0",
  org.simantics.browsing.ui.swt;bundle-version="1.1.0",
  org.simantics.views.swt;bundle-version="1.0.0",
- org.simantics.structural2;bundle-version="1.1.1"
+ org.simantics.structural2;bundle-version="1.1.1",
+ org.simantics.browsing.ui.model,
+ org.simantics.modeling.ontology,
+ org.slf4j.api
 Bundle-ActivationPolicy: lazy
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Export-Package: org.simantics.debug.ui
index 94b8fe4323cf8e07f128dce7ee1bbd4e91adec51..3bf794e3f9eb5a85efe213cf28ce2f1ceacf510e 100644 (file)
    <extension\r
          point="org.eclipse.ui.handlers">\r
       <!-- Tuukka removed this to keep people from getting their workbench stuck when they accidentally press C-S-r -->\r
-      <!--\r
       <handler\r
             class="org.simantics.debug.ui.internal.SearchResourceHandler"\r
             commandId="org.eclipse.ui.navigate.openResource">\r
          <activeWhen>\r
-            <test\r
-                  args="true"\r
-                  property="org.simantics.ui.hasSession">\r
-            </test>\r
+         <with \r
+            variable="selection"> \r
+         <iterate \r
+               operator="or" ifEmpty="true"> \r
+         </iterate> \r
+      </with> \r
          </activeWhen>\r
       </handler>\r
-      -->\r
    </extension>\r
 \r
 </plugin>\r
index 8c88468e4ef424e7b956eb680968543e27c2025e..1ae23d7197c0b61bd5f039611872388f9641a739 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************\r
- * Copyright (c) 2007, 2013 Association for Decentralized Information Management\r
+ * Copyright (c) 2007, 2016 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
@@ -17,6 +17,7 @@ import java.util.Collection;
 import java.util.Collections;\r
 import java.util.Comparator;\r
 import java.util.HashSet;\r
+import java.util.List;\r
 import java.util.Map;\r
 import java.util.Set;\r
 import java.util.TreeSet;\r
@@ -27,14 +28,14 @@ import org.eclipse.core.runtime.CoreException;
 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.IDialogConstants;\r
 import org.eclipse.jface.dialogs.IDialogSettings;\r
 import org.eclipse.jface.resource.JFaceResources;\r
 import org.eclipse.jface.resource.LocalResourceManager;\r
 import org.eclipse.jface.resource.ResourceManager;\r
 import org.eclipse.jface.viewers.IStructuredSelection;\r
 import org.eclipse.jface.viewers.LabelProvider;\r
-import org.eclipse.swt.events.DisposeEvent;\r
-import org.eclipse.swt.events.DisposeListener;\r
+import org.eclipse.jface.viewers.StructuredSelection;\r
 import org.eclipse.swt.graphics.Image;\r
 import org.eclipse.swt.widgets.Composite;\r
 import org.eclipse.swt.widgets.Control;\r
@@ -46,11 +47,12 @@ import org.simantics.Simantics;
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.Session;\r
-import org.simantics.db.common.primitiverequest.Adapter;\r
+import org.simantics.db.common.primitiverequest.PossibleAdapter;\r
 import org.simantics.db.common.procedure.adapter.TransientCacheListener;\r
 import org.simantics.db.common.request.BinaryRead;\r
 import org.simantics.db.common.request.ObjectsWithType;\r
 import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.request.UniqueRead;\r
 import org.simantics.db.common.uri.UnescapedChildMapOfResource;\r
 import org.simantics.db.common.utils.NameUtils;\r
 import org.simantics.db.exception.DatabaseException;\r
@@ -64,24 +66,38 @@ import org.simantics.debug.ui.internal.DebugUtils;
 import org.simantics.layer0.Layer0;\r
 import org.simantics.operation.Layer0X;\r
 import org.simantics.scl.runtime.function.Function;\r
-import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.selection.ResourceWorkbenchSelectionElement;\r
 import org.simantics.ui.workbench.dialogs.ResourceLabelProvider;\r
 import org.simantics.utils.Container;\r
 import org.simantics.utils.ui.BundleUtils;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
 \r
 /**\r
  * TODO Add Debugger Composite as preview!\r
  */\r
 public class SearchResourceDialog extends FilteredItemsSelectionDialog {\r
 \r
+    private static final Logger LOGGER = LoggerFactory.getLogger(SearchResourceDialog.class);\r
+\r
+    /**\r
+     * The default maximum amount of Dependencies index hits to produce as results.\r
+     */\r
+    private static final int DEFAULT_MAX_INDEX_HITS = 1000;\r
+\r
+    private static final Pattern ID_PATTERN = Pattern.compile("\\$([0-9]+)");\r
+\r
     private static final String SEARCH_RESOURCE_DIALOG = "SearchResourceDialog"; //$NON-NLS-1$\r
 \r
-    Session session;\r
-    IStructuredSelection selection;\r
-    ResourceManager resourceManager;\r
+    private static final int SHOW_IN_BROWSER_ID = IDialogConstants.CLIENT_ID + 1;\r
+\r
+    private static final String SHOW_IN_BROWSER_LABEL = "Show In Browser";\r
 \r
-    // Needed in a subclass\r
-    protected IResourceFilter resourceFilter = ResourceSearch.FILTER_ALL;\r
+    private Session session;\r
+    @SuppressWarnings("unused")\r
+    private IStructuredSelection selection;\r
+    private ResourceManager resourceManager;\r
+    private IResourceFilter resourceFilter = ResourceSearch.FILTER_ALL;\r
 \r
     LabelProvider detailsLabelProvider = new LabelProvider() {\r
         @Override\r
@@ -143,69 +159,75 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
     public SearchResourceDialog(Session s, boolean multi, Shell shell, String title, IStructuredSelection selection) {\r
         super(shell, multi);\r
         this.session = s;\r
-        resourceManager = new LocalResourceManager(JFaceResources.getResources());\r
+        this.selection = selection;\r
         this.labelProvider = new ElementLabelProvider(shell.getDisplay());\r
-        setMessage("Enter ID, URI or name");\r
+        setMessage("Enter name, resource URI or ID");\r
         setListLabelProvider(labelProvider);\r
         setDetailsLabelProvider(detailsLabelProvider);\r
-        setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/cog_blue.png")));\r
         setTitle(title);\r
         //setInitialPattern("*", FilteredItemsSelectionDialog.FULL_SELECTION);\r
         setSelectionHistory(new ResourceSelectionHistory());\r
-        this.selection = selection;\r
         setSeparatorLabel("Previously selected above, others below");\r
     }\r
 \r
     @Override\r
     protected void configureShell(Shell shell) {\r
+        this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), shell);\r
+        setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/cog_blue.png")));\r
         super.configureShell(shell);\r
-        shell.addDisposeListener(new DisposeListener() {\r
-            @Override\r
-            public void widgetDisposed(DisposeEvent e) {\r
-                resourceManager.dispose();\r
-            }\r
-        });\r
+    }\r
+\r
+    @Override\r
+    protected void createButtonsForButtonBar(Composite parent) {\r
+        createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,\r
+                true);\r
+        createButton(parent, SHOW_IN_BROWSER_ID, SHOW_IN_BROWSER_LABEL,\r
+                true);\r
+        createButton(parent, IDialogConstants.CANCEL_ID,\r
+                IDialogConstants.CANCEL_LABEL, false);\r
+    }\r
+\r
+    @Override\r
+    protected void buttonPressed(int buttonId) {\r
+        if (buttonId == SHOW_IN_BROWSER_ID) {\r
+            okPressed();\r
+            LabeledResource lr = (LabeledResource) getFirstResult();\r
+            ShowInBrowser.defaultExecute(new StructuredSelection(new ResourceWorkbenchSelectionElement(lr.resource)));\r
+            return;\r
+        }\r
+        super.buttonPressed(buttonId);\r
     }\r
 \r
     class ResourceSelectionHistory extends FilteredItemsSelectionDialog.SelectionHistory {\r
 \r
         @Override\r
         protected Object restoreItemFromMemento(IMemento memento) {\r
-            String dada = memento.getTextData();\r
+            String data = memento.getTextData();\r
             try {\r
-                SerialisationSupport support = SimanticsUI.getSession().getService(SerialisationSupport.class);\r
-                final Resource r = support.getResource(Long.parseLong(dada));\r
-                if (r == null) {\r
+                SerialisationSupport support = Simantics.getSession().getService(SerialisationSupport.class);\r
+                Resource r = support.getResource(Long.parseLong(data));\r
+                if (r == null)\r
                     return null;\r
-                }\r
 \r
-                String name = session.syncRequest(new Read<String>() {\r
+                String name = session.syncRequest(new UniqueRead<String>() {\r
                     @Override\r
                     public String perform(ReadGraph g) throws DatabaseException {\r
-                        if (!resourceFilter.acceptResource(g, r)) {\r
+                        if (!resourceFilter.acceptResource(g, r))\r
                             return null;\r
-                        }\r
-                        String name = null;\r
                         try {\r
                             try {\r
-                                name = g.adapt(r, String.class);\r
+                                return g.adapt(r, String.class);\r
                             } catch (Exception ex) {\r
                                 System.out.println("Exception thrown from restoreItemFromMemento");\r
                             }\r
-                            if (name != null) {\r
-                                return name;\r
-                            }\r
                         } catch (Throwable t) {}\r
                         return "" + r.getResourceId();\r
                     }\r
                 });\r
                 if (name==null) return null;\r
                 return new LabeledResource(name, r);\r
-            } catch (NumberFormatException e) {\r
-                e.printStackTrace();\r
-                return null;\r
-            } catch (DatabaseException e) {\r
-                e.printStackTrace();\r
+            } catch (NumberFormatException | DatabaseException e) {\r
+                LOGGER.info("Search memento restoration failed.", e);\r
                 return null;\r
             }\r
         }\r
@@ -215,7 +237,7 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
         protected void storeItemToMemento(Object item, IMemento memento) {\r
             if(item instanceof Container) {\r
                 try {\r
-                    SerialisationSupport support = SimanticsUI.getSession().getService(SerialisationSupport.class);\r
+                    SerialisationSupport support = Simantics.getSession().getService(SerialisationSupport.class);\r
                     memento.putTextData(String.valueOf(support.getRandomAccessId(((Container<Resource>)item).get())));\r
                 } catch (DatabaseException e) {\r
                     e.printStackTrace();\r
@@ -268,7 +290,7 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
         final long referencedResourceId = referencedResourceId(pattern);\r
         final boolean findIds = referencedResourceId != 0;\r
 \r
-        progressMonitor.beginTask("Searching", IProgressMonitor.UNKNOWN);\r
+        //progressMonitor.beginTask("Searching", IProgressMonitor.UNKNOWN);\r
 \r
         try {\r
             session.syncRequest(new ReadRequest() {\r
@@ -308,13 +330,12 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
                             filter += "Name:" + pattern + "*";\r
 \r
                             Layer0 L0 = Layer0.getInstance(graph);\r
-                            \r
-                            HashSet<Resource> indexRoots = new HashSet<Resource>();\r
+\r
+                            Set<Resource> indexRoots = new HashSet<>();\r
                             indexRoots.addAll(graph.syncRequest(new ObjectsWithType(project, L0.ConsistsOf, L0.IndexRoot)));\r
                             indexRoots.addAll(graph.syncRequest(new OntologiesFromLibrary(graph.getRootLibrary())));\r
                             for (Resource indexRoot : indexRoots) {\r
-                                Collection<Resource> hits = new ArrayList<Resource>(find(graph, indexRoot, filter));\r
-                                hits.add(indexRoot);\r
+                                Collection<Resource> hits = find(graph, indexRoot, filter);\r
                                 for (Resource r : hits) {\r
                                     if (rf != null && !rf.acceptResource(graph, r))\r
                                         continue;\r
@@ -326,26 +347,10 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
                 }\r
 \r
                 public Collection<Resource> find(ReadGraph graph, Resource index, String filter) throws DatabaseException {\r
+                    //TimeLogger.resetTimeAndLog("find(" + graph.getURI(index) + ", " + filter + ")");\r
                     Collection<Resource> indexResult = graph.syncRequest(new QueryIndex(index, filter), TransientCacheListener.<Collection<Resource>>instance());\r
-\r
-//                    Layer0 L0 = Layer0.getInstance(graph);\r
-//                    Collection<Resource> linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot));\r
-//                    if (linkedRoots.isEmpty())\r
-                        return indexResult;\r
-\r
-//                    Collection<Resource> result = indexResult;\r
-//                    for (Resource dep : linkedRoots) {\r
-//                        Collection<Resource> linkedIndexResults = find(graph, dep, filter);\r
-//                        if (linkedIndexResults.isEmpty())\r
-//                            continue;\r
-//                        if (result == indexResult) {\r
-//                            result = new ArrayList<Resource>(indexResult.size() + linkedIndexResults.size());\r
-//                            result.addAll(indexResult);\r
-//                        } else {\r
-//                        }\r
-//                        result.addAll(linkedIndexResults);\r
-//                    }\r
-//                    return result;\r
+                    //TimeLogger.log("found " + indexResult.size());\r
+                    return indexResult;\r
                 }\r
 \r
             });\r
@@ -372,28 +377,26 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
             Layer0X L0X = Layer0X.getInstance(graph);\r
 \r
             @SuppressWarnings({ "unchecked", "rawtypes" })\r
-            Function dependencies = graph.syncRequest(new Adapter(L0X.Dependencies, Function.class), TransientCacheListener.<Function>instance());\r
+            Function dependencies = graph.syncRequest(new PossibleAdapter(L0X.DependencyResources, Function.class), TransientCacheListener.<Function>instance());\r
+            if (dependencies == null)\r
+                return Collections.emptyList();\r
 \r
             @SuppressWarnings("unchecked")\r
-            Collection<Map<String, Object>> results = (Collection<Map<String, Object>>)dependencies.apply(graph, parameter, parameter2);\r
+            List<Resource> results = (List<Resource>) dependencies.apply(graph, parameter, parameter2, DEFAULT_MAX_INDEX_HITS);\r
             if (results == null || results.isEmpty())\r
                 return Collections.emptyList();\r
 \r
-            // TreeSet to keep the results in deterministic order.\r
-            Set<Resource> resultSet = new TreeSet<Resource>();\r
-            for (Map<String, Object> entry : results) {\r
-                Resource res = (Resource)entry.get("Resource");\r
+            // TreeSet to keep the results in deterministic order and to prevent duplicates.\r
+            Set<Resource> resultSet = new TreeSet<>();\r
+            for (Resource res : results) {\r
                 if (res != null && !resultSet.contains(res))\r
                     resultSet.add(res);\r
             }\r
-\r
             return new ArrayList<Resource>(resultSet);\r
         }\r
 \r
     }\r
 \r
-    Pattern ID_PATTERN = Pattern.compile("\\$([0-9]+)");\r
-\r
     private long referencedResourceId(String pattern) {\r
         Matcher m = ID_PATTERN.matcher(pattern);\r
         if (m.matches()) {\r
@@ -424,14 +427,10 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog {
         //return item.toString();\r
     }\r
 \r
-    @SuppressWarnings("rawtypes")\r
     @Override\r
-    protected Comparator getItemsComparator() {\r
-        return new Comparator() {\r
-            @Override\r
-            public int compare(Object arg0, Object arg1) {\r
-                return arg0.toString().compareTo(arg1.toString());\r
-            }\r
+    protected Comparator<?> getItemsComparator() {\r
+        return (arg0, arg1) -> {\r
+            return arg0.toString().compareTo(arg1.toString());\r
         };\r
     }\r
 \r
diff --git a/bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/ShowInBrowser.java b/bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/ShowInBrowser.java
new file mode 100644 (file)
index 0000000..54acdd6
--- /dev/null
@@ -0,0 +1,126 @@
+/*******************************************************************************\r
+ * Copyright (c) 2016 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
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.debug.ui;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.Deque;\r
+import java.util.LinkedList;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.ui.IViewPart;\r
+import org.eclipse.ui.IWorkbenchPage;\r
+import org.eclipse.ui.PartInitException;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.GraphExplorer;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.common.NodeContextBuilder;\r
+import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;\r
+import org.simantics.browsing.ui.model.nodetypes.EntityNodeType;\r
+import org.simantics.browsing.ui.model.nodetypes.NodeType;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.ui.selection.WorkbenchSelectionUtils;\r
+import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ShowInBrowser extends AbstractHandler {\r
+\r
+       private static final String DEFAULT_BROWSER_VIEW_ID = "org.simantics.modeling.ui.browser"; //$NON-NLS-1$\r
+\r
+       private static Read<Collection<NodeContext>> getParentsRequest(BrowseContext bc, NodeContext context) {\r
+               return new UniqueRead<Collection<NodeContext>>() {\r
+                       @Override\r
+                       public Collection<NodeContext> perform(ReadGraph graph) throws DatabaseException {\r
+                               return bc.getParents(graph, context);\r
+                       }\r
+               };\r
+       }\r
+\r
+       private static Read<NodeType> getNodeTypeRequest(Resource element) {\r
+               return new UniqueRead<NodeType>() {\r
+                       @Override\r
+                       public NodeType perform(ReadGraph graph) throws DatabaseException {\r
+                               return EntityNodeType.getNodeTypeFor(graph, element);\r
+                       }\r
+               };\r
+       }\r
+\r
+       private static Collection<NodeContext> tryGetParents(BrowseContext bc, NodeContext context) {\r
+               try {\r
+                       return Simantics.getSession().syncRequest(getParentsRequest(bc, context));\r
+               } catch (DatabaseException e) {\r
+                       return Collections.emptyList();\r
+               }\r
+       }\r
+\r
+       private static boolean show(GraphExplorer explorer, BrowseContext browseContext, NodeContext context, Deque<NodeContext> path) {\r
+               if (explorer.isVisible(context))\r
+                       return explorer.selectPath(path);\r
+               else {\r
+                       for (NodeContext parent : tryGetParents(browseContext, context)) {\r
+                               path.addFirst(parent);\r
+                               if (show(explorer, browseContext, parent, path))\r
+                                       return true;\r
+                               path.removeFirst();\r
+                       }\r
+               }\r
+               return false;\r
+       }\r
+\r
+       public static Object defaultExecute(ISelection selection, String browserViewId) {\r
+               IViewPart browser = (IViewPart) WorkbenchUtils.findView(browserViewId);\r
+               if (browser == null)\r
+                       return null;\r
+\r
+               GraphExplorer explorer = (GraphExplorer) browser.getAdapter(GraphExplorer.class);\r
+               BrowseContext browseContext = (BrowseContext) browser.getAdapter(BrowseContext.class);\r
+               if (explorer == null || browseContext == null)\r
+                       return null;\r
+\r
+               try {\r
+                       final Resource element = WorkbenchSelectionUtils.getPossibleResource(selection);\r
+                       WorkbenchUtils.showView(browserViewId, IWorkbenchPage.VIEW_VISIBLE);\r
+                       NodeType nodeType = Simantics.getSession().syncRequest(getNodeTypeRequest(element));;\r
+                       NodeContext context = NodeContextBuilder.buildWithData(NodeType.KEY_SEQUENCE, new Object[] { element, nodeType });\r
+                       Deque<NodeContext> path = new LinkedList<>();\r
+                       path.add(context);\r
+                       if (show(explorer, browseContext, context, path)) {\r
+                               WorkbenchUtils.activateView(browserViewId);\r
+                       }\r
+               } catch (DatabaseException e) {\r
+               } catch (PartInitException e) {\r
+               }\r
+\r
+               return null;\r
+       }\r
+\r
+       public static Object defaultExecute(ISelection selection) {\r
+               return defaultExecute(selection, DEFAULT_BROWSER_VIEW_ID);\r
+       }\r
+\r
+       @Override\r
+       public Object execute(ExecutionEvent event) throws ExecutionException {\r
+               return defaultExecute(HandlerUtil.getCurrentSelectionChecked(event));\r
+       }\r
+\r
+}
\ No newline at end of file
index 38e60653c4ce4cfa7470cbc3ceb4083a94d440dd..3fb4013d8f7690516481e43ab0e0c358f753b966 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * Copyright (c) 2007, 2016 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
@@ -8,14 +8,16 @@
  *\r
  * Contributors:\r
  *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *     Semantum Oy - refactoring (#6855)\r
  *******************************************************************************/\r
 package org.simantics.debug.ui.internal;\r
 \r
+import java.util.function.BiConsumer;\r
+\r
 import org.eclipse.core.commands.AbstractHandler;\r
 import org.eclipse.core.commands.ExecutionEvent;\r
 import org.eclipse.core.commands.ExecutionException;\r
 import org.eclipse.jface.action.IAction;\r
-import org.eclipse.swt.widgets.Display;\r
 import org.eclipse.swt.widgets.Shell;\r
 import org.eclipse.ui.handlers.HandlerUtil;\r
 import org.simantics.Simantics;\r
@@ -29,6 +31,7 @@ import org.simantics.debug.ui.ResourceSearch;
 import org.simantics.debug.ui.SearchResourceDialog;\r
 import org.simantics.ui.workbench.action.ChooseActionRequest;\r
 import org.simantics.utils.Container;\r
+import org.simantics.utils.ui.SWTUtils;\r
 import org.simantics.utils.ui.action.IPriorityAction;\r
 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
 \r
@@ -39,7 +42,7 @@ public class SearchResourceHandler extends AbstractHandler {
         Shell shell = HandlerUtil.getActiveShellChecked(event);\r
         Session session = Simantics.getSession();\r
         SearchResourceDialog rld = new SearchResourceDialog(session, false, shell, "Open Resource");\r
-        rld.setResourceFilter(ResourceSearch.FILTER_RELATIONS);\r
+        rld.setResourceFilter(ResourceSearch.FILTER_ALL);\r
         rld.setBlockOnOpen(true);\r
         rld.open();\r
         if (rld.getResult() == null)\r
@@ -50,38 +53,32 @@ public class SearchResourceHandler extends AbstractHandler {
         for (Object o : rld.getResult()) {\r
             @SuppressWarnings("unchecked")\r
             Container<Resource> rc = (Container<Resource>) o;\r
-            openPreferredEditor(session, rc.get(), currentPerspectiveId);\r
+            openPreferredEditor(shell, session, rc.get(), currentPerspectiveId);\r
         }\r
 \r
         return null;\r
     }\r
 \r
-    void openPreferredEditor(Session s, final Resource r, final String defaultPerspective) {\r
+    private static void openPreferredEditor(Shell parent, Session s, Resource r, String defaultPerspective) {\r
+        findActions(s, r, defaultPerspective, (resourceName, actions) -> {\r
+            SWTUtils.asyncExec(parent, () -> {\r
+                IAction action = ChooseActionRequest.chooseAction(parent, actions, resourceName);\r
+                if (action != null)\r
+                    action.run();\r
+            });\r
+        });\r
+    }\r
 \r
+    private static void findActions(Session s, Resource r, String defaultPerspective, BiConsumer<String, IPriorityAction[]> consumer) {\r
         s.asyncRequest(new ReadRequest() {\r
-\r
-            String            resourceName;\r
-            IPriorityAction[] actions;\r
-\r
             @Override\r
             public void run(ReadGraph g) throws DatabaseException {\r
-                resourceName = NameUtils.getSafeName(g, r);\r
-                actions = ChooseActionRequest.findActions(g, r, defaultPerspective);\r
-                if (actions == null)\r
-                    return;\r
-                Display.getDefault().asyncExec(new Runnable() {\r
-                    @Override\r
-                    public void run() {\r
-                        IAction action = ChooseActionRequest.chooseAction(null, actions, resourceName);\r
-                        if (action == null)\r
-                            return;\r
-                        action.run();\r
-                    }\r
-                });\r
+                String resourceName = NameUtils.getSafeName(g, r);\r
+                IPriorityAction[] actions = ChooseActionRequest.findActions(g, r, defaultPerspective);\r
+                if (actions != null)\r
+                    consumer.accept(resourceName, actions);\r
             }\r
-\r
         });\r
-\r
     }\r
 \r
 }\r
index 8ac45cf021d31bb181dfe772207f418d05c62a60..07743860cfcd64720223dbc7aaa387c3feaee7ab 100644 (file)
@@ -156,7 +156,6 @@ import org.simantics.ui.workbench.TitleUpdater;
 import org.simantics.ui.workbench.ToolTipRequest;\r
 import org.simantics.ui.workbench.editor.input.InputValidationCombinators;\r
 import org.simantics.utils.DataContainer;\r
-import org.simantics.utils.datastructures.Callback;\r
 import org.simantics.utils.datastructures.hints.HintContext;\r
 import org.simantics.utils.datastructures.hints.HintListenerAdapter;\r
 import org.simantics.utils.datastructures.hints.IHintContext;\r
@@ -797,15 +796,14 @@ public class DiagramViewer
             sessionContext.getSession().asyncRequest(new WriteRequest() {\r
                 @Override\r
                 public void perform(WriteGraph graph) throws DatabaseException {\r
-                       CommonDBUtils.selectClusterSet(graph, diagramResource);\r
+                    if (graph.isImmutable(diagramResource))\r
+                        return;\r
+                    CommonDBUtils.selectClusterSet(graph, diagramResource);\r
                     DiagramGraphUtil.setDiagramDesc(graph, diagramResource, desc);\r
                 }\r
-            }, new Callback<DatabaseException>() {\r
-                @Override\r
-                public void run(DatabaseException parameter) {\r
-                    if (parameter != null)\r
-                        ErrorLogger.defaultLogError("Failed to write default diagram page description to database, see exception for details", parameter);\r
-                }\r
+            }, parameter -> {\r
+                if (parameter != null)\r
+                    ErrorLogger.defaultLogError("Failed to write default diagram page description to database, see exception for details", parameter);\r
             });\r
         }\r
 \r