Permalink
Browse files

add browser image preview to search

  • Loading branch information...
nittka committed Aug 29, 2018
1 parent 269c292 commit 869e9ea2c77909eadcdb13c5cd7a9dea564a4700
@@ -15,7 +15,8 @@ Require-Bundle: de.nittka.tooling.jtag;visibility:=reexport,
org.antlr.runtime,
org.eclipse.xtext.ui.codetemplates.ui,
org.eclipse.compare,
org.eclipse.search;bundle-version="3.10.0"
org.eclipse.search;bundle-version="3.10.0",
org.eclipse.ui.browser;bundle-version="3.4.300"
Import-Package: org.apache.log4j,
org.eclipse.xtext.xbase.lib
Bundle-RequiredExecutionEnvironment: J2SE-1.5
@@ -1,14 +1,18 @@
package de.nittka.tooling.jtag.ui.search;
import java.io.File;
import java.nio.file.Files;
import java.util.Iterator;
import java.util.List;
import javax.inject.Inject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.PlatformUI;
import org.eclipse.xtext.resource.IContainer;
import org.eclipse.xtext.resource.IContainer.Manager;
import org.eclipse.xtext.resource.IEObjectDescription;
@@ -35,6 +39,8 @@
private ResourceDescriptionsProvider indexProvider;
@Inject
private IResourceServiceProvider serviceProvider;
@Inject
private JtagSearchResultPreview preview;
private JtagSearch search;
@@ -52,9 +58,22 @@ public IStatus run(IProgressMonitor monitor) throws OperationCanceledException {
result.reset();
internalRun(monitor, result);
result.finish();
maybeOpenBrowser(result);
return (monitor.isCanceled()) ? Status.CANCEL_STATUS : Status.OK_STATUS;
}
private void maybeOpenBrowser(ReferenceSearchResult result){
try {
if(!result.getMatchingReferences().isEmpty()){
File tempFile= ResourcesPlugin.getWorkspace().getRoot().getLocation().append(".metadata").append("JtagSearchPreview.html").toFile();
Files.write(tempFile.toPath(), preview.createHtml(result).getBytes());
PlatformUI.getWorkbench().getBrowserSupport().createBrowser("JtagSearchPreview").openURL(tempFile.toURL());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void internalRun(IProgressMonitor monitor, IAcceptor<IReferenceDescription> acceptor){
//our implementation searches the index, matching each visible IResourceDescription against the actual search logic
IResourceDescriptions index = indexProvider.getResourceDescriptions(search.getResource());
@@ -0,0 +1,59 @@
package de.nittka.tooling.jtag.ui.search
import de.nittka.tooling.jtag.jtag.File
import de.nittka.tooling.jtag.ui.JtagFileURIs
import java.util.List
import javax.inject.Inject
import javax.inject.Provider
import org.eclipse.emf.ecore.EObject
import org.eclipse.xtext.resource.IReferenceDescription
import org.eclipse.xtext.resource.XtextResourceSet
import org.eclipse.xtext.ui.editor.findrefs.ReferenceSearchResult
class JtagSearchResultPreview {
@Inject
Provider<XtextResourceSet> rsProvider;
def String createHtml(ReferenceSearchResult searchResult){
val List<IReferenceDescription> matches=searchResult.matchingReferences
val rs=rsProvider.get
'''
<html>
<head>
<style>
.pic{
display:inline;
}
img{
padding:5px;
max-width:200px;
max-height:200px;
}
</style>
</head>
<body>
«FOR desc:matches»
«desc.render(rs)»
«ENDFOR»
</bod<>
</html>
'''
}
def String render(IReferenceDescription desc, XtextResourceSet rs){
val EObject e=rs.getEObject(desc.targetEObjectUri,true)
if(e instanceof File){
val file=e as File
val location=JtagFileURIs.getImageLocation(file)
if(location!==null){
val javaFile=new java.io.File(location)
'''
<div class="pic">
<img src="file://«location»" title="«javaFile.name» - «javaFile.parentFile.absolutePath»">
</div>
'''
}
}
}
}

0 comments on commit 869e9ea

Please sign in to comment.