scijava-common icon indicating copy to clipboard operation
scijava-common copied to clipboard

Default to required=false and persist=false for inputs with ItemVisibility.MESSAGE

Open imagejan opened this issue 8 years ago • 3 comments

When using a message parameter:

@Parameter(visibility=ItemVisibility.MESSAGE)
String message = "Some informative message";

it would help to have required=false and persist=false by default, as this is almost always what you want. Otherwise, you'll have to provide this parameter when e.g. running from command line.

Suggested by @xulman.

imagejan avatar Sep 22 '17 15:09 imagejan

This issue has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/bonej-fractal-dimension-and-ij-robot-how-to-automatically-press-ok-on-dialog-box/39459/8

imagesc-bot avatar Jun 24 '20 19:06 imagesc-bot

This issue has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/display-text-label-with-script-parameters/10772/12

imagesc-bot avatar Sep 03 '20 19:09 imagesc-bot

Just reviving this thread after having a potential solution to my use case.

The problem I faced was not with inputs required or not, but rather with messages and buttons that are here to give some info to the user. The problem is that if all inputs are programmatically given, the messages still show up:

image

What I've done is a PreProcessor Plugin that looks at all inputs. If the only unresolved inputs have a 'message' visibility type, then I resolve them, and they don't show up.

I put this in a repo of mine, but I think it makes sense in scijava common.

import org.scijava.ItemVisibility;
import org.scijava.log.LogService;
import org.scijava.module.Module;
import org.scijava.module.ModuleItem;
import org.scijava.module.process.AbstractPreprocessorPlugin;
import org.scijava.module.process.PreprocessorPlugin;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.widget.InputHarvester;

/**
 * Scijava processor that resolves all inputs with MESSAGE {@link ItemVisibility},
 * if they are the only ones remaining, before the InputHarvester kicks in
 */
@Plugin(type = PreprocessorPlugin.class, priority = InputHarvester.PRIORITY+1) // We want it to kick in before the swing input harvester
public class MessageResolverProcessor extends AbstractPreprocessorPlugin {

    int unresolvedInputsExceptMessageCount = 0;
    int messagesCount = 0;

    @Parameter
    LogService logger;

    @Override
    public void process(Module module) {

        if (module.getInfo()==null) {
            logger.warn("null getInfo for module "+module);
            return;
        }

        module.getInputs().forEach((name, input) -> {

            ModuleItem<?> inputKind = module.getInfo().getInput(name);
            if (inputKind == null) {
                logger.warn("null input "+name+" for module "+module);
                return; // avoid doing anything
            }

            ItemVisibility visibility = inputKind.getVisibility();

            if (visibility==null) {
                logger.warn("null visibility for input "+name+" for module "+module);
                return; // avoid doing anything
            }

            if (visibility.equals(ItemVisibility.MESSAGE)) {
                messagesCount++;
            } else {
                if (!module.isInputResolved(name)) unresolvedInputsExceptMessageCount++;
            }
        });

        if (messagesCount > 0) {
            if (unresolvedInputsExceptMessageCount == 0) {
                // No need for null check, it's been done before
                module.getInputs().forEach((name, input) -> {
                    if (module.getInfo().getInput(name).getVisibility().equals(ItemVisibility.MESSAGE)) {
                        module.resolveInput(name);
                    }
                });
            }
        }

    }
}

NicoKiaru avatar Sep 26 '22 15:09 NicoKiaru