From f4f85294e4d022dba9a36f12abd404c3d5446f8a Mon Sep 17 00:00:00 2001 From: Elliott Magnuson Date: Sun, 19 Mar 2023 03:29:31 -0500 Subject: [PATCH] Add OpenLocation command to resolve Strings --- .../plugins/commands/io/OpenLocation.java | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 src/main/java/org/scijava/plugins/commands/io/OpenLocation.java diff --git a/src/main/java/org/scijava/plugins/commands/io/OpenLocation.java b/src/main/java/org/scijava/plugins/commands/io/OpenLocation.java new file mode 100644 index 0000000..f9076fc --- /dev/null +++ b/src/main/java/org/scijava/plugins/commands/io/OpenLocation.java @@ -0,0 +1,131 @@ +/* + * #%L + * Core commands for SciJava applications. + * %% + * Copyright (C) 2010 - 2023 Board of Regents of the University of + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck + * Institute of Molecular Cell Biology and Genetics. + * %% + * 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.plugins.commands.io; + +import java.io.IOException; +import java.net.URISyntaxException; + +import org.scijava.ItemIO; +import org.scijava.command.Command; +import org.scijava.command.ContextCommand; +import org.scijava.io.IOPlugin; +import org.scijava.io.IOService; +import org.scijava.io.location.Location; +import org.scijava.io.location.LocationService; +import org.scijava.log.LogService; +import org.scijava.menu.MenuConstants; +import org.scijava.plugin.Attr; +import org.scijava.plugin.Menu; +import org.scijava.plugin.Parameter; +import org.scijava.plugin.Plugin; +import org.scijava.ui.DialogPrompt; +import org.scijava.ui.UIService; + +/** + * Opens the selected location. + * + * @author Curtis Rueden + * @author Mark Hiner + */ +@Plugin(type = Command.class, iconPath = "/icons/commands/folder_picture.png", + menu = { + @Menu(label = MenuConstants.FILE_LABEL, weight = MenuConstants.FILE_WEIGHT, + mnemonic = MenuConstants.FILE_MNEMONIC), + @Menu(label = "Open...", weight = 1, mnemonic = 'o', accelerator = "^O") }, + attrs = { @Attr(name = "no-legacy") }) +public class OpenLocation extends ContextCommand { + + @Parameter + private LogService log; + + @Parameter + private IOService ioService; + + @Parameter + private UIService uiService; + + @Parameter + private LocationService locationService; + + @Parameter(label = "Location to open") + private String location; + + @Parameter(type = ItemIO.OUTPUT, label = "Data") + private Object data; + + @Override + public void run() { + try { + Location source = locationService.resolve(location); + final IOPlugin opener = ioService.getOpener(source); + if (opener == null) { + error("No appropriate format found: " + location); + return; + } + data = opener.open(location); + if (data == null) { + cancel(null); + return; + } + } + catch (final IOException exc) { + log.error(exc); + error(exc.getMessage()); + } catch (URISyntaxException e) { + log.error(e); + error(e.getMessage()); + } + } + + public String getInputLocation() { + return location; + } + + public void setInputLocation(final String location) { + this.location = location; + } + + public Object getData() { + return data; + } + + public void setData(final Object data) { + this.data = data; + } + + // -- Helper methods -- + + private void error(final String message) { + uiService.showDialog(message, DialogPrompt.MessageType.ERROR_MESSAGE); + } + +} \ No newline at end of file