X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.debug.ui%2Fsrc%2Forg%2Fsimantics%2Fdebug%2Fui%2FSearchResourceDialog.java;fp=bundles%2Forg.simantics.debug.ui%2Fsrc%2Forg%2Fsimantics%2Fdebug%2Fui%2FSearchResourceDialog.java;h=1ae23d7197c0b61bd5f039611872388f9641a739;hp=8c88468e4ef424e7b956eb680968543e27c2025e;hb=0a314ce9afeeac53b4d4653125cf56cb99be7976;hpb=6470931159a573043ba403cc94cd5e5e3916fd62 diff --git a/bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/SearchResourceDialog.java b/bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/SearchResourceDialog.java index 8c88468e4..1ae23d719 100644 --- a/bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/SearchResourceDialog.java +++ b/bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/SearchResourceDialog.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2013 Association for Decentralized Information Management + * Copyright (c) 2007, 2016 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -17,6 +17,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; @@ -27,14 +28,14 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; +import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.IDialogSettings; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.resource.LocalResourceManager; import org.eclipse.jface.resource.ResourceManager; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.LabelProvider; -import org.eclipse.swt.events.DisposeEvent; -import org.eclipse.swt.events.DisposeListener; +import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; @@ -46,11 +47,12 @@ import org.simantics.Simantics; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; -import org.simantics.db.common.primitiverequest.Adapter; +import org.simantics.db.common.primitiverequest.PossibleAdapter; import org.simantics.db.common.procedure.adapter.TransientCacheListener; import org.simantics.db.common.request.BinaryRead; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.request.UniqueRead; import org.simantics.db.common.uri.UnescapedChildMapOfResource; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; @@ -64,24 +66,38 @@ import org.simantics.debug.ui.internal.DebugUtils; import org.simantics.layer0.Layer0; import org.simantics.operation.Layer0X; import org.simantics.scl.runtime.function.Function; -import org.simantics.ui.SimanticsUI; +import org.simantics.ui.selection.ResourceWorkbenchSelectionElement; import org.simantics.ui.workbench.dialogs.ResourceLabelProvider; import org.simantics.utils.Container; import org.simantics.utils.ui.BundleUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * TODO Add Debugger Composite as preview! */ public class SearchResourceDialog extends FilteredItemsSelectionDialog { + private static final Logger LOGGER = LoggerFactory.getLogger(SearchResourceDialog.class); + + /** + * The default maximum amount of Dependencies index hits to produce as results. + */ + private static final int DEFAULT_MAX_INDEX_HITS = 1000; + + private static final Pattern ID_PATTERN = Pattern.compile("\\$([0-9]+)"); + private static final String SEARCH_RESOURCE_DIALOG = "SearchResourceDialog"; //$NON-NLS-1$ - Session session; - IStructuredSelection selection; - ResourceManager resourceManager; + private static final int SHOW_IN_BROWSER_ID = IDialogConstants.CLIENT_ID + 1; + + private static final String SHOW_IN_BROWSER_LABEL = "Show In Browser"; - // Needed in a subclass - protected IResourceFilter resourceFilter = ResourceSearch.FILTER_ALL; + private Session session; + @SuppressWarnings("unused") + private IStructuredSelection selection; + private ResourceManager resourceManager; + private IResourceFilter resourceFilter = ResourceSearch.FILTER_ALL; LabelProvider detailsLabelProvider = new LabelProvider() { @Override @@ -143,69 +159,75 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog { public SearchResourceDialog(Session s, boolean multi, Shell shell, String title, IStructuredSelection selection) { super(shell, multi); this.session = s; - resourceManager = new LocalResourceManager(JFaceResources.getResources()); + this.selection = selection; this.labelProvider = new ElementLabelProvider(shell.getDisplay()); - setMessage("Enter ID, URI or name"); + setMessage("Enter name, resource URI or ID"); setListLabelProvider(labelProvider); setDetailsLabelProvider(detailsLabelProvider); - setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/cog_blue.png"))); setTitle(title); //setInitialPattern("*", FilteredItemsSelectionDialog.FULL_SELECTION); setSelectionHistory(new ResourceSelectionHistory()); - this.selection = selection; setSeparatorLabel("Previously selected above, others below"); } @Override protected void configureShell(Shell shell) { + this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), shell); + setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/cog_blue.png"))); super.configureShell(shell); - shell.addDisposeListener(new DisposeListener() { - @Override - public void widgetDisposed(DisposeEvent e) { - resourceManager.dispose(); - } - }); + } + + @Override + protected void createButtonsForButtonBar(Composite parent) { + createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, + true); + createButton(parent, SHOW_IN_BROWSER_ID, SHOW_IN_BROWSER_LABEL, + true); + createButton(parent, IDialogConstants.CANCEL_ID, + IDialogConstants.CANCEL_LABEL, false); + } + + @Override + protected void buttonPressed(int buttonId) { + if (buttonId == SHOW_IN_BROWSER_ID) { + okPressed(); + LabeledResource lr = (LabeledResource) getFirstResult(); + ShowInBrowser.defaultExecute(new StructuredSelection(new ResourceWorkbenchSelectionElement(lr.resource))); + return; + } + super.buttonPressed(buttonId); } class ResourceSelectionHistory extends FilteredItemsSelectionDialog.SelectionHistory { @Override protected Object restoreItemFromMemento(IMemento memento) { - String dada = memento.getTextData(); + String data = memento.getTextData(); try { - SerialisationSupport support = SimanticsUI.getSession().getService(SerialisationSupport.class); - final Resource r = support.getResource(Long.parseLong(dada)); - if (r == null) { + SerialisationSupport support = Simantics.getSession().getService(SerialisationSupport.class); + Resource r = support.getResource(Long.parseLong(data)); + if (r == null) return null; - } - String name = session.syncRequest(new Read() { + String name = session.syncRequest(new UniqueRead() { @Override public String perform(ReadGraph g) throws DatabaseException { - if (!resourceFilter.acceptResource(g, r)) { + if (!resourceFilter.acceptResource(g, r)) return null; - } - String name = null; try { try { - name = g.adapt(r, String.class); + return g.adapt(r, String.class); } catch (Exception ex) { System.out.println("Exception thrown from restoreItemFromMemento"); } - if (name != null) { - return name; - } } catch (Throwable t) {} return "" + r.getResourceId(); } }); if (name==null) return null; return new LabeledResource(name, r); - } catch (NumberFormatException e) { - e.printStackTrace(); - return null; - } catch (DatabaseException e) { - e.printStackTrace(); + } catch (NumberFormatException | DatabaseException e) { + LOGGER.info("Search memento restoration failed.", e); return null; } } @@ -215,7 +237,7 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog { protected void storeItemToMemento(Object item, IMemento memento) { if(item instanceof Container) { try { - SerialisationSupport support = SimanticsUI.getSession().getService(SerialisationSupport.class); + SerialisationSupport support = Simantics.getSession().getService(SerialisationSupport.class); memento.putTextData(String.valueOf(support.getRandomAccessId(((Container)item).get()))); } catch (DatabaseException e) { e.printStackTrace(); @@ -268,7 +290,7 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog { final long referencedResourceId = referencedResourceId(pattern); final boolean findIds = referencedResourceId != 0; - progressMonitor.beginTask("Searching", IProgressMonitor.UNKNOWN); + //progressMonitor.beginTask("Searching", IProgressMonitor.UNKNOWN); try { session.syncRequest(new ReadRequest() { @@ -308,13 +330,12 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog { filter += "Name:" + pattern + "*"; Layer0 L0 = Layer0.getInstance(graph); - - HashSet indexRoots = new HashSet(); + + Set indexRoots = new HashSet<>(); indexRoots.addAll(graph.syncRequest(new ObjectsWithType(project, L0.ConsistsOf, L0.IndexRoot))); indexRoots.addAll(graph.syncRequest(new OntologiesFromLibrary(graph.getRootLibrary()))); for (Resource indexRoot : indexRoots) { - Collection hits = new ArrayList(find(graph, indexRoot, filter)); - hits.add(indexRoot); + Collection hits = find(graph, indexRoot, filter); for (Resource r : hits) { if (rf != null && !rf.acceptResource(graph, r)) continue; @@ -326,26 +347,10 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog { } public Collection find(ReadGraph graph, Resource index, String filter) throws DatabaseException { + //TimeLogger.resetTimeAndLog("find(" + graph.getURI(index) + ", " + filter + ")"); Collection indexResult = graph.syncRequest(new QueryIndex(index, filter), TransientCacheListener.>instance()); - -// Layer0 L0 = Layer0.getInstance(graph); -// Collection linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot)); -// if (linkedRoots.isEmpty()) - return indexResult; - -// Collection result = indexResult; -// for (Resource dep : linkedRoots) { -// Collection linkedIndexResults = find(graph, dep, filter); -// if (linkedIndexResults.isEmpty()) -// continue; -// if (result == indexResult) { -// result = new ArrayList(indexResult.size() + linkedIndexResults.size()); -// result.addAll(indexResult); -// } else { -// } -// result.addAll(linkedIndexResults); -// } -// return result; + //TimeLogger.log("found " + indexResult.size()); + return indexResult; } }); @@ -372,28 +377,26 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog { Layer0X L0X = Layer0X.getInstance(graph); @SuppressWarnings({ "unchecked", "rawtypes" }) - Function dependencies = graph.syncRequest(new Adapter(L0X.Dependencies, Function.class), TransientCacheListener.instance()); + Function dependencies = graph.syncRequest(new PossibleAdapter(L0X.DependencyResources, Function.class), TransientCacheListener.instance()); + if (dependencies == null) + return Collections.emptyList(); @SuppressWarnings("unchecked") - Collection> results = (Collection>)dependencies.apply(graph, parameter, parameter2); + List results = (List) dependencies.apply(graph, parameter, parameter2, DEFAULT_MAX_INDEX_HITS); if (results == null || results.isEmpty()) return Collections.emptyList(); - // TreeSet to keep the results in deterministic order. - Set resultSet = new TreeSet(); - for (Map entry : results) { - Resource res = (Resource)entry.get("Resource"); + // TreeSet to keep the results in deterministic order and to prevent duplicates. + Set resultSet = new TreeSet<>(); + for (Resource res : results) { if (res != null && !resultSet.contains(res)) resultSet.add(res); } - return new ArrayList(resultSet); } } - Pattern ID_PATTERN = Pattern.compile("\\$([0-9]+)"); - private long referencedResourceId(String pattern) { Matcher m = ID_PATTERN.matcher(pattern); if (m.matches()) { @@ -424,14 +427,10 @@ public class SearchResourceDialog extends FilteredItemsSelectionDialog { //return item.toString(); } - @SuppressWarnings("rawtypes") @Override - protected Comparator getItemsComparator() { - return new Comparator() { - @Override - public int compare(Object arg0, Object arg1) { - return arg0.toString().compareTo(arg1.toString()); - } + protected Comparator getItemsComparator() { + return (arg0, arg1) -> { + return arg0.toString().compareTo(arg1.toString()); }; }