|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2017 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics, University of |
| 8 | + * Konstanz, and KNIME GmbH. |
| 9 | + * %% |
| 10 | + * Redistribution and use in source and binary forms, with or without |
| 11 | + * modification, are permitted provided that the following conditions are met: |
| 12 | + * |
| 13 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 14 | + * this list of conditions and the following disclaimer. |
| 15 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 16 | + * this list of conditions and the following disclaimer in the documentation |
| 17 | + * and/or other materials provided with the distribution. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. |
| 30 | + * #L% |
| 31 | + */ |
| 32 | + |
| 33 | +package org.scijava.command; |
| 34 | + |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.concurrent.ExecutionException; |
| 38 | + |
| 39 | +import org.scijava.Context; |
| 40 | +import org.scijava.module.process.PreprocessorPlugin; |
| 41 | + |
| 42 | +/** |
| 43 | + * A way to build a dynamic set of inputs, whose values are then harvested by |
| 44 | + * the preprocessing framework. |
| 45 | + * <p> |
| 46 | + * The {@link #run()} method of this command does nothing. If you want something |
| 47 | + * custom to happen during execution, use a normal {@link Command} instead: |
| 48 | + * either implement {@link Command directly}, or extend {@link ContextCommand} |
| 49 | + * or {@link DynamicCommand}. |
| 50 | + * </p> |
| 51 | + * <p> |
| 52 | + * Here is are some examples of usage: |
| 53 | + * </p> |
| 54 | + * |
| 55 | + * <pre> |
| 56 | + * {@code |
| 57 | + * // Single input, no configuration. |
| 58 | + * Inputs inputs = new Inputs(context); |
| 59 | + * inputs.addInput("sigma", Double.class); |
| 60 | + * Double sigma = (Double) inputs.harvest().get("sigma"); |
| 61 | + * |
| 62 | + * // Two inputs, no configuration. |
| 63 | + * Inputs inputs = new Inputs(context); |
| 64 | + * inputs.addInput("name", String.class); |
| 65 | + * inputs.addInput("age", Integer.class); |
| 66 | + * Map<String, Object> values = inputs.harvest(); |
| 67 | + * String name = (String) values.get("name"); |
| 68 | + * Integer age = (Integer) values.get("age"); |
| 69 | + * |
| 70 | + * // Inputs with configuration. |
| 71 | + * Inputs inputs = new Inputs(context); |
| 72 | + * MutableModuleItem<String> wordInput = inputs.addInput("word", String.class); |
| 73 | + * wordInput.setLabel("Favorite word"); |
| 74 | + * wordInput.setChoices(Arrays.asList("quick", "brown", "fox")); |
| 75 | + * wordInput.setDefaultValue("fox"); |
| 76 | + * MutableModuleItem<Double> opacityInput = inputs.addInput("opacity", Double.class); |
| 77 | + * opacityInput.setMinimumValue(0.0); |
| 78 | + * opacityInput.setMaximumValue(1.0); |
| 79 | + * opacityInput.setDefaultValue(0.5); |
| 80 | + * opacityInput.setWidgetStyle(NumberWidget.SCROLL_BAR_STYLE); |
| 81 | + * inputs.harvest(); |
| 82 | + * String word = wordInput.getValue(inputs); |
| 83 | + * Double opacity = opacityInput.getValue(inputs); |
| 84 | + * } |
| 85 | + * </pre> |
| 86 | + * |
| 87 | + * @author Curtis Rueden |
| 88 | + */ |
| 89 | +public final class Inputs extends DynamicCommand { |
| 90 | + |
| 91 | + public Inputs(final Context context) { |
| 92 | + context.inject(this); |
| 93 | + } |
| 94 | + |
| 95 | + public Map<String, Object> harvest() { |
| 96 | + try { |
| 97 | + final List<PreprocessorPlugin> pre = // |
| 98 | + pluginService.createInstancesOfType(PreprocessorPlugin.class); |
| 99 | + return moduleService.run(this, true, pre, null).get().getInputs(); |
| 100 | + } |
| 101 | + catch (final InterruptedException | ExecutionException exc) { |
| 102 | + throw new RuntimeException(exc); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments