Skip to content

Add method signatures and preprocessor to select multiple files #276

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 3 commits into from
Jul 31, 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: 2 additions & 0 deletions src/main/java/org/scijava/module/DefaultModuleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ 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();

// do not persist if object cannot be converted back from a string
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/scijava/ui/DefaultUIService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
package org.scijava.ui;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -319,6 +320,18 @@ public File chooseFile(final File file, final String style) {
return ui == null ? null : ui.chooseFile(title, file, style);
}

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

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

@Override
public void showContextMenu(final String menuRoot, final Display<?> display,
final int x, final int y)
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/org/scijava/ui/FileListPreprocessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.scijava.ui;

import java.io.File;

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;

@Plugin(type = PreprocessorPlugin.class, priority = InputHarvester.PRIORITY + 1.0)
public class FileListPreprocessor extends AbstractPreprocessorPlugin {

@Parameter(required = false)
private UIService uiService;

@Override
public void process(final Module module) {
if (uiService == null) return;
final ModuleItem<File[]> fileInput = getFilesInput(module);
if (fileInput == null) return;

final File[] files = fileInput.getValue(module);

// show file chooser dialog box
final File[] result = uiService.chooseFiles(files, null);
if (result == null) {
cancel("");
return;
}

fileInput.setValue(module, result);
module.resolveInput(fileInput.getName());
}

// -- Helper methods --

/**
* Gets the single unresolved {@link File} input parameter. If there is not
* exactly one unresolved {@link File} input parameter, or if there are other
* types of unresolved parameters, this method returns null.
*/
private ModuleItem<File[]> getFilesInput(final Module module) {
ModuleItem<File[]> result = null;
for (final ModuleItem<?> input : module.getInfo().inputs()) {
if (module.isInputResolved(input.getName())) continue;
final Class<?> type = input.getType();
if (!File[].class.isAssignableFrom(type)) {
// not a File[] parameter; abort
return null;
}
if (result != null) {
// second File parameter; abort
return null;
}
@SuppressWarnings("unchecked")
final ModuleItem<File[]> fileInput = (ModuleItem<File[]>) input;
result = fileInput;
}
return result;
}
}
24 changes: 23 additions & 1 deletion src/main/java/org/scijava/ui/UIService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
package org.scijava.ui;

import java.io.File;
import java.io.FileFilter;
import java.util.List;

import org.scijava.app.StatusService;
Expand Down Expand Up @@ -294,6 +295,28 @@ DialogPrompt.Result showDialog(String message, String title,
*/
File chooseFile(String title, File file, String style);

/**
* Prompts the user to select one or multiple files.
* <p>
* The prompt is displayed in the default user interface.
* </p>
*
* @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);

/**
* Prompts the user to select one or multiple files.
* <p>
* The prompt is displayed in the default user interface.
* </p>
*
* @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);

/**
* Displays a popup context menu for the given display at the specified
* position.
Expand All @@ -309,5 +332,4 @@ DialogPrompt.Result showDialog(String message, String title,
* @see StatusService#getStatusMessage(String, StatusEvent)
*/
String getStatusMessage(StatusEvent statusEvent);

}
30 changes: 29 additions & 1 deletion src/main/java/org/scijava/ui/UserInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
package org.scijava.ui;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.List;

import org.scijava.Disposable;
import org.scijava.display.Display;
Expand Down Expand Up @@ -185,6 +188,32 @@ default File chooseFile(String title, File file, String style) {
throw new UnsupportedOperationException();
}

/**
* Prompts the user to choose a list of files.
*
* @param files The initial value displayed in the file chooser prompt.
* @param filter A filter allowing to restrict file choice.
* @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) {
throw new UnsupportedOperationException();
}

/**
* Prompts the user to choose a list of files.
*
* @param fileList The initial value displayed in the file chooser prompt.
* @param filter A filter allowing to restrict file choice.
* @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) {
final File[] initialFiles = fileList.toArray(new File[fileList.size()]);
final File[] chosenFiles = chooseFiles(initialFiles, filter);
return chosenFiles == null ? null : Arrays.asList(chosenFiles);
}

/**
* Displays a popup context menu for the given display at the specified
* position.
Expand All @@ -199,5 +228,4 @@ default File chooseFile(String title, File file, String style) {

/** Returns true if this UI requires the EDT. */
boolean requiresEDT();

}
7 changes: 7 additions & 0 deletions src/main/java/org/scijava/widget/FileListWidget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.scijava.widget;

import java.io.File;

public interface FileListWidget<U> extends InputWidget<File[], U> {
// NB: No changes to interface.
}