Skip to content

Improved file inputs for scripts #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>13.1.0</version>
<version>17.0.0</version>
<relativePath />
</parent>

Expand Down
162 changes: 162 additions & 0 deletions src/main/java/org/scijava/convert/FileListConverters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2017 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
* Institute of Molecular Cell Biology and Genetics, University of
* Konstanz, and KNIME GmbH.
* %%
* 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 org.scijava.convert;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.scijava.Priority;
import org.scijava.plugin.Plugin;
import org.scijava.util.StringUtils;

/**
* A collection of {@link Converter} plugins for going between {@link String},
* {@link File} and {@code File[]}.
*
* @author Jan Eglinger
* @author Curtis Rueden
*/
public class FileListConverters {
// -- String to File (list) converters --

@Plugin(type = Converter.class, priority = Priority.NORMAL)
public static class StringToFileConverter extends
AbstractConverter<String, File>
{

@SuppressWarnings("unchecked")
@Override
public <T> T convert(final Object src, final Class<T> dest) {
return (T) new File((String) src);
}

@Override
public Class<File> getOutputType() {
return File.class;
}

@Override
public Class<String> getInputType() {
return String.class;
}

}

@Plugin(type = Converter.class, priority = Priority.NORMAL)
public static class StringToFileArrayConverter extends
AbstractConverter<String, File[]>
{

@SuppressWarnings("unchecked")
@Override
public <T> T convert(final Object src, final Class<T> dest) {
final String[] tokens = StringUtils.splitUnquoted((String) src, ",");
final List<File> fileList = new ArrayList<>();
for (final String filePath : tokens) {
fileList.add(new File(filePath.replaceAll("^\"|\"$", "")));
}
return (T) fileList.toArray(new File[fileList.size()]);
}

@Override
public Class<File[]> getOutputType() {
return File[].class;
}

@Override
public Class<String> getInputType() {
return String.class;
}

}

// TODO add StringToFileListConverter

// -- File (list) to String converters --

@Plugin(type = Converter.class, priority = Priority.NORMAL)
public static class FileToStringConverter extends
AbstractConverter<File, String>
{

@SuppressWarnings("unchecked")
@Override
public <T> T convert(final Object src, final Class<T> dest) {
return (T) ((File) src).getAbsolutePath();
}

@Override
public Class<String> getOutputType() {
return String.class;
}

@Override
public Class<File> getInputType() {
return File.class;
}

}

@Plugin(type = Converter.class, priority = Priority.NORMAL)
public static class FileArrayToStringConverter extends
AbstractConverter<File[], String>
{

@SuppressWarnings("unchecked")
@Override
public <T> T convert(final Object src, final Class<T> dest) {
final List<String> result = Arrays.asList((File[]) src).stream().map(
f -> {
final String path = f.getAbsolutePath();
return path.contains(",") ? "\"" + path + "\"" : path;
}).collect(Collectors.toList());
return (T) String.join(",", result);
}

@Override
public Class<String> getOutputType() {
return String.class;
}

@Override
public Class<File[]> getInputType() {
return File[].class;
}

}

// TODO add FileListToStringConverter
}
4 changes: 1 addition & 3 deletions src/main/java/org/scijava/module/DefaultModuleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,7 @@ public <T> void save(final ModuleItem<T> item, final T value) {
return;
}

// FIXME: Convert to string, instead of just calling toString.
// Otherwise many things (e.g. File[]) are persisted improperly.
final String sValue = value == null ? "" : value.toString();
final String sValue = value == null ? "" : convertService.convert(value, String.class);

// do not persist if object cannot be converted back from a string
if (!convertService.supports(sValue, item.getType())) return;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/scijava/ui/DefaultUIService.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,15 @@ public File chooseFile(final File file, final String style) {
}

@Override
public File[] chooseFiles(File[] files, FileFilter filter) {
public File[] chooseFiles(File parent, File[] files, FileFilter filter, String style) {
final UserInterface ui = getDefaultUI();
return ui == null ? null : ui.chooseFiles(files, filter);
return ui == null ? null : ui.chooseFiles(parent, files, filter, style);
}

@Override
public List<File> chooseFiles(List<File> fileList, FileFilter filter) {
public List<File> chooseFiles(File parent, List<File> fileList, FileFilter filter, String style) {
final UserInterface ui = getDefaultUI();
return ui == null ? null : ui.chooseFiles(fileList, filter);
return ui == null ? null : ui.chooseFiles(parent, fileList, filter, style);
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/scijava/ui/FileListPreprocessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public void process(final Module module) {
final File[] files = fileInput.getValue(module);

// show file chooser dialog box
final File[] result = uiService.chooseFiles(files, null);
// TODO decide how to create filter from style attributes
// TODO retrieve parent folder??
final File[] result = uiService.chooseFiles(null, files, null, fileInput.getWidgetStyle());
if (result == null) {
cancel("");
return;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/scijava/ui/UIService.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ DialogPrompt.Result showDialog(String message, String title,
* @param files The initial value displayed in the file chooser prompt.
* @param filter A filter allowing to restrict the choice of files
*/
File[] chooseFiles(File[] files, FileFilter filter);
File[] chooseFiles(File parent, File[] files, FileFilter filter, String style);

/**
* Prompts the user to select one or multiple files.
Expand All @@ -315,7 +315,7 @@ DialogPrompt.Result showDialog(String message, String title,
* @param fileList The initial value displayed in the file chooser prompt.
* @param filter A filter allowing to restrict the choice of files
*/
List<File> chooseFiles(List<File> fileList, FileFilter filter);
List<File> chooseFiles(File parent, List<File> fileList, FileFilter filter, String style);

/**
* Displays a popup context menu for the given display at the specified
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/scijava/ui/UserInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,26 +191,30 @@ default File chooseFile(String title, File file, String style) {
/**
* Prompts the user to choose a list of files.
*
* @param parent Parent folder for file selection
* @param files The initial value displayed in the file chooser prompt.
* @param filter A filter allowing to restrict file choice.
* @param style File selection style (files, directories, or both) and optional filters
* @return The selected {@link File}s chosen by the user, or null if the
* user cancels the prompt.
*/
default File[] chooseFiles(File[] files, FileFilter filter) {
default File[] chooseFiles(File parent, File[] files, FileFilter filter, String style) {
throw new UnsupportedOperationException();
}

/**
* Prompts the user to choose a list of files.
*
* @param parent Parent folder for file selection
* @param fileList The initial value displayed in the file chooser prompt.
* @param filter A filter allowing to restrict file choice.
* @param style File selection style (files, directories, or both) and optional filters
* @return The selected {@link File}s chosen by the user, or null if the
* user cancels the prompt.
*/
default List<File> chooseFiles(List<File> fileList, FileFilter filter) {
default List<File> chooseFiles(File parent, List<File> fileList, FileFilter filter, String style) {
final File[] initialFiles = fileList.toArray(new File[fileList.size()]);
final File[] chosenFiles = chooseFiles(initialFiles, filter);
final File[] chosenFiles = chooseFiles(parent, initialFiles, filter, style);
return chosenFiles == null ? null : Arrays.asList(chosenFiles);
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/scijava/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

import java.io.File;
import java.text.DecimalFormatSymbols;
import java.util.regex.Pattern;

/**
* Useful methods for working with {@link String}s.
Expand All @@ -80,6 +81,16 @@ private StringUtils() {
// NB: prevent instantiation of utility class.
}

/**
* Splits a string only at separators outside of quotation marks ({@code "}).
* Does not handle escaped quotes.
*/
public static String[] splitUnquoted(final String s, final String separator) {
// See https://stackoverflow.com/a/1757107/1919049
return s.split(Pattern.quote(separator) +
"(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);
}

/** Normalizes the decimal separator for the user's locale. */
public static String sanitizeDouble(String value) {
value = value.replaceAll("[^0-9,\\.]", "");
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/org/scijava/widget/FileListWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,24 @@
import java.io.File;

public interface FileListWidget<U> extends InputWidget<File[], U> {
// NB: No changes to interface.
/**
* Widget style to allow file selection only
*
* @see org.scijava.plugin.Parameter#style()
*/
String FILES_ONLY = "files";

/**
* Widget style to allow directory selection only
*
* @see org.scijava.plugin.Parameter#style()
*/
String DIRECTORIES_ONLY = "directories";

/**
* Widget style to allow selection of both files and directories
*
* @see org.scijava.plugin.Parameter#style()
*/
String FILES_AND_DIRECTORIES = "both";
}
Loading