Skip to content

Commit

Permalink
Change location of demos (unfinished)
Browse files Browse the repository at this point in the history
Adds tests for command and macros
Fix #274 by adding a StringToSourceAndConverter.java converter
  • Loading branch information
NicoKiaru committed May 10, 2024
1 parent b125e84 commit 7613e0b
Show file tree
Hide file tree
Showing 19 changed files with 430 additions and 171 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@
<scope>test</scope>
</dependency>

<!-- to test ij1 macro scripting -->
<dependency>
<groupId>net.imagej</groupId>
<artifactId>imagej-legacy</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*-
* #%L
* BigDataViewer-Playground
* %%
* Copyright (C) 2019 - 2024 Nicolas Chiaruttini, EPFL - Robert Haase, MPI CBG - Christian Tischer, EMBL
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package sc.fiji.bdvpg.scijava.command.bdv;

import bdv.util.BdvHandle;
import ij.IJ;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import sc.fiji.bdvpg.scijava.ScijavaBdvDefaults;
import sc.fiji.bdvpg.scijava.command.BdvPlaygroundActionCommand;
import sc.fiji.bdvpg.scijava.services.SourceAndConverterBdvDisplayService;

@SuppressWarnings({ "CanBeFinal", "unused" }) // Because SciJava command fields
// are set by SciJava
// pre-processors

@Plugin(type = BdvPlaygroundActionCommand.class,
menuPath = ScijavaBdvDefaults.RootMenu +
"BDV>BDV - Close bdv windows",
description = "Closes one or several bdv windows.")
public class MultiBdvCloseCommand implements BdvPlaygroundActionCommand {

@Parameter(label = "Select BDV Windows", persist = false)
BdvHandle[] bdvhs;

@Parameter
SourceAndConverterBdvDisplayService bdvDisplayService;

@Override
public void run() {
if (bdvhs.length == 0) IJ.log("Please make sure to select a Bdv window.");
for (BdvHandle bdvh : bdvhs) {
bdvh.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*-
* #%L
* BigDataViewer-Playground
* %%
* Copyright (C) 2019 - 2024 Nicolas Chiaruttini, EPFL - Robert Haase, MPI CBG - Christian Tischer, EMBL
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package sc.fiji.bdvpg.scijava.converters;

import bdv.viewer.SourceAndConverter;
import org.scijava.convert.AbstractConverter;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import sc.fiji.bdvpg.scijava.services.SourceAndConverterService;

import javax.swing.tree.TreePath;
import java.util.List;

/**
* @param <I> String class
*/

@SuppressWarnings("unused")
@Plugin(type = org.scijava.convert.Converter.class)
public class StringToSourceAndConverter<I extends String> extends
AbstractConverter<I, SourceAndConverter<?>>
{

@Parameter
SourceAndConverterService sacsService;

@Override
public <T> T convert(Object src, Class<T> dest) {
String str = (String) src;
TreePath tp = sacsService.getUI().getTreePathFromString(str);
if (tp != null) {
List<SourceAndConverter<?>> potentialSources = sacsService.getUI().getSourceAndConvertersFromTreePath(tp);
if (potentialSources.size()!=1) {
log().warn("The specified parameters refers no either no source or too many sources");
return null;
} else {
return (T) potentialSources.get(0);
}
}
else {
return null;
}
}

@Override
public Class getOutputType() {
return SourceAndConverter.class;
}

@Override
public Class<I> getInputType() {
return (Class<I>) String.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
import org.scijava.command.CommandService;
import org.scijava.module.ModuleItem;
import org.scijava.object.ObjectService;
import org.scijava.options.OptionsService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.plugin.PluginService;
Expand All @@ -75,9 +74,6 @@
import org.scijava.ui.UIService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sc.fiji.bdvpg.bdv.supplier.DefaultBdvSupplier;
import sc.fiji.bdvpg.bdv.supplier.IBdvSupplier;
import sc.fiji.bdvpg.bdv.supplier.SerializableBdvOptions;
import sc.fiji.bdvpg.cache.AbstractGlobalCache;
import sc.fiji.bdvpg.cache.GlobalCacheBuilder;
import sc.fiji.bdvpg.cache.GlobalLoaderCache;
Expand All @@ -88,10 +84,8 @@
import sc.fiji.bdvpg.sourceandconverter.SourceAndConverterHelper;
import sc.fiji.bdvpg.spimdata.EntityHandler;
import sc.fiji.bdvpg.spimdata.IEntityHandlerService;
import sc.fiji.persist.ScijavaGsonHelper;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
Expand Down Expand Up @@ -308,7 +302,7 @@ public int getUniqueId(SourceAndConverter<?> sac) {
}

public void setUniqueId(SourceAndConverter<?> sac, Integer id) {
setMetadata(sac, UNIQUE_ID_KEY, new Integer(id));
setMetadata(sac, UNIQUE_ID_KEY, id);
}

public synchronized void register(Collection<SourceAndConverter<?>> sources) {
Expand Down Expand Up @@ -760,11 +754,13 @@ public void initialize() {
globalCache = GlobalCacheBuilder.builder().create();
}

if (!context().getService(UIService.class).isHeadless()) {
logger.debug(
"uiService detected : Constructing JPanel for BdvSourceAndConverterService");
ui = new SourceAndConverterServiceUI(this, context());
uiAvailable = true;
if (context().getService(UIService.class)!=null) {
if (!context().getService(UIService.class).isHeadless()) {
logger.debug(
"uiService detected : Constructing JPanel for BdvSourceAndConverterService");
ui = new SourceAndConverterServiceUI(this, context());
uiAvailable = true;
}
}

SourceAndConverterServices.setSourceAndConverterService(this);
Expand Down Expand Up @@ -810,7 +806,7 @@ public Set<AbstractSpimData<?>> getSpimDatasets() {
}

/**
* @return a list of action name / keys / identifiers
* @return a set of action name / keys / identifiers
*/
public Set<String> getActionsKeys() {
return actionMap.keySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import sc.fiji.bdvpg.services.SourceAndConverterServices;

import java.io.File;
import java.nio.file.Paths;
import java.util.function.Function;
import java.util.regex.Pattern;

import static sc.fiji.bdvpg.scijava.services.SourceAndConverterService.SPIM_DATA_LOCATION;

Expand Down Expand Up @@ -74,24 +74,8 @@ public AbstractSpimData<?> apply(String dataLocation) {
try {
sd = new XmlIoSpimData().load(dataLocation);
SourceAndConverterServices.getSourceAndConverterService().register(sd);
String pattern = Pattern.quote(System.getProperty("file.separator"));
String[] parts = dataLocation.split(pattern);
if (parts.length > 0) {
if (parts[parts.length - 1] != null) {
SourceAndConverterServices.getSourceAndConverterService()
.setSpimDataName(sd, parts[parts.length - 1]);
}
else {
logger.error("Wrong parsing of spimdata name (not enough parts) : " +
dataLocation);
}
}
else {
logger.error("Wrong parsing of spimdata name (can't be split): " +
dataLocation);
}
SourceAndConverterServices.getSourceAndConverterService().setMetadata(sd,
SPIM_DATA_LOCATION, dataLocation);
SourceAndConverterServices.getSourceAndConverterService().setSpimDataName(sd, Paths.get(dataLocation).getFileName().toString());
SourceAndConverterServices.getSourceAndConverterService().setMetadata(sd, SPIM_DATA_LOCATION, dataLocation);
}
catch (SpimDataException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,32 @@
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package sc.fiji.bdvpg.bdv;
package sc.fiji.bdvpg.demos.bdv;

import net.imagej.ImageJ;
import org.junit.After;
import org.junit.Test;
import sc.fiji.bdvpg.TestHelper;
import org.scijava.Context;
import org.scijava.ui.UIService;
import org.scijava.ui.swing.SwingUI;
import sc.fiji.bdvpg.scijava.services.SourceAndConverterBdvDisplayService;
import sc.fiji.bdvpg.services.SourceAndConverterServices;

public class BdvCreatorDemo
{
/**
* Demos a way to create a Bdv window which will be registered by the {@link SourceAndConverterBdvDisplayService}
* It is thus discoverable by all the other components of BigDataViewer-playground, in particular
* as scijava parameters typed {@link bdv.util.BdvHandle} or {@link bdv.util.BdvHandle[]} in
* {@link org.scijava.command.Command}.
*/
public class BdvCreatorDemo {
public static void main( String[] args ) {
// Create the application context with all available services a runtime
final Context ctx = new Context();

static ImageJ ij;
public static void main( String[] args )
{
// Create the ImageJ application context with all available services; necessary for SourceAndConverterServices creation
ij = new ImageJ();
TestHelper.startFiji(ij);//ij.ui().showUI();
// Show UI
ctx.service(UIService.class).showUI(SwingUI.NAME);

// Creates a BDV since none exists yet
ij.get(SourceAndConverterBdvDisplayService.class).getActiveBdv();
}
// Creates a new BDV
ctx.getService(SourceAndConverterBdvDisplayService.class).getNewBdv();

@Test
public void demoRunOk() {
main(new String[]{""});
// Returns the previous BDV or the one "on top", meaning the one clicked by the user
ctx.getService(SourceAndConverterBdvDisplayService.class).getActiveBdv();
}

@After
public void closeFiji() {
TestHelper.closeFijiAndBdvs(ij);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package sc.fiji.bdvpg.bdv.navigate;
package sc.fiji.bdvpg.demos.bdv.navigate;

import bdv.util.BdvHandle;
import bdv.util.RandomAccessibleIntervalSource;
Expand All @@ -40,35 +40,32 @@
import net.imglib2.type.numeric.RealType;
import net.imglib2.util.Util;
import net.imglib2.view.Views;
import org.junit.After;
import org.junit.Test;
import sc.fiji.bdvpg.TestHelper;
import sc.fiji.bdvpg.bdv.navigate.PositionLogger;
import sc.fiji.bdvpg.bdv.navigate.ViewerTransformAdjuster;
import sc.fiji.bdvpg.behaviour.ClickBehaviourInstaller;
import sc.fiji.bdvpg.scijava.services.SourceAndConverterBdvDisplayService;
import sc.fiji.bdvpg.services.SourceAndConverterServices;
import sc.fiji.bdvpg.sourceandconverter.SourceAndConverterHelper;

/**
* ViewTransformSetAndLogDemo
* <p>
* <p>
* <p>
* Demos how to log the current mouse position and how to install this behaviour on a Bdv window.
* Press Ctrl+D in Bdv and see the current mouse position being logged.
* Author: @haesleinhuepf
* 12 2019
*/
public class LogMousePositionDemo {

static ImageJ ij;

public static <T extends RealType<T>> void main(String... args) {

// Create the ImageJ application context with all available services; necessary for SourceAndConverterServices creation
ij = new ImageJ();
ImageJ ij = new ImageJ();
TestHelper.startFiji(ij);//ij.ui().showUI();

// load and convert an image
// Load and convert an image
ImagePlus imp = IJ.openImage("src/test/resources/blobs.tif");
RandomAccessibleInterval<T> rai = ImageJFunctions.wrapReal(imp);

// Adds a third dimension because BDV needs 3D
rai = Views.addDimension( rai, 0, 0 );

Expand All @@ -85,21 +82,11 @@ public static <T extends RealType<T>> void main(String... args) {
// Adjust BDV View on the SourceAndConverter
new ViewerTransformAdjuster(bdvHandle, sac).run();

// add a click behavior for logging mouse positions
new ClickBehaviourInstaller( bdvHandle, (x, y ) -> new PositionLogger( bdvHandle ).run() ).install( "Log mouse position", "ctrl D" );
// Add a click behavior for logging mouse positions
new ClickBehaviourInstaller( bdvHandle, (x, y) -> new PositionLogger( bdvHandle ).run() ).install( "Log mouse position", "ctrl D" );

// log the current position
// Log the current position
new PositionLogger( bdvHandle ).run();
}

@Test
public void demoRunOk() {
main("");
}

@After
public void closeFiji() {
TestHelper.closeFijiAndBdvs(ij);
}

}
Loading

0 comments on commit 7613e0b

Please sign in to comment.