|
| 1 | +package org.scijava.ui; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | + |
| 5 | +import org.scijava.module.Module; |
| 6 | +import org.scijava.module.ModuleItem; |
| 7 | +import org.scijava.module.process.AbstractPreprocessorPlugin; |
| 8 | +import org.scijava.module.process.PreprocessorPlugin; |
| 9 | +import org.scijava.plugin.Parameter; |
| 10 | +import org.scijava.plugin.Plugin; |
| 11 | +import org.scijava.widget.InputHarvester; |
| 12 | + |
| 13 | +@Plugin(type = PreprocessorPlugin.class, priority = InputHarvester.PRIORITY + 1.0) |
| 14 | +public class FileListPreprocessor extends AbstractPreprocessorPlugin { |
| 15 | + |
| 16 | + @Parameter(required = false) |
| 17 | + private UIService uiService; |
| 18 | + |
| 19 | + @Override |
| 20 | + public void process(final Module module) { |
| 21 | + if (uiService == null) return; |
| 22 | + final ModuleItem<File[]> fileInput = getFilesInput(module); |
| 23 | + if (fileInput == null) return; |
| 24 | + |
| 25 | + final File[] files = fileInput.getValue(module); |
| 26 | + |
| 27 | + // show file chooser dialog box |
| 28 | + final File[] result = uiService.chooseFiles(files, null); |
| 29 | + if (result == null) { |
| 30 | + cancel(""); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + fileInput.setValue(module, result); |
| 35 | + module.resolveInput(fileInput.getName()); |
| 36 | + } |
| 37 | + |
| 38 | + // -- Helper methods -- |
| 39 | + |
| 40 | + /** |
| 41 | + * Gets the single unresolved {@link File} input parameter. If there is not |
| 42 | + * exactly one unresolved {@link File} input parameter, or if there are other |
| 43 | + * types of unresolved parameters, this method returns null. |
| 44 | + */ |
| 45 | + private ModuleItem<File[]> getFilesInput(final Module module) { |
| 46 | + ModuleItem<File[]> result = null; |
| 47 | + for (final ModuleItem<?> input : module.getInfo().inputs()) { |
| 48 | + if (module.isInputResolved(input.getName())) continue; |
| 49 | + final Class<?> type = input.getType(); |
| 50 | + if (!File[].class.isAssignableFrom(type)) { |
| 51 | + // not a File[] parameter; abort |
| 52 | + return null; |
| 53 | + } |
| 54 | + if (result != null) { |
| 55 | + // second File parameter; abort |
| 56 | + return null; |
| 57 | + } |
| 58 | + @SuppressWarnings("unchecked") |
| 59 | + final ModuleItem<File[]> fileInput = (ModuleItem<File[]>) input; |
| 60 | + result = fileInput; |
| 61 | + } |
| 62 | + return result; |
| 63 | + } |
| 64 | +} |
0 commit comments