Skip to content

Commit f4f8529

Browse files
committed
Add OpenLocation command to resolve Strings
1 parent af8646b commit f4f8529

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* #%L
3+
* Core commands for SciJava applications.
4+
* %%
5+
* Copyright (C) 2010 - 2023 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.plugins.commands.io;
33+
34+
import java.io.IOException;
35+
import java.net.URISyntaxException;
36+
37+
import org.scijava.ItemIO;
38+
import org.scijava.command.Command;
39+
import org.scijava.command.ContextCommand;
40+
import org.scijava.io.IOPlugin;
41+
import org.scijava.io.IOService;
42+
import org.scijava.io.location.Location;
43+
import org.scijava.io.location.LocationService;
44+
import org.scijava.log.LogService;
45+
import org.scijava.menu.MenuConstants;
46+
import org.scijava.plugin.Attr;
47+
import org.scijava.plugin.Menu;
48+
import org.scijava.plugin.Parameter;
49+
import org.scijava.plugin.Plugin;
50+
import org.scijava.ui.DialogPrompt;
51+
import org.scijava.ui.UIService;
52+
53+
/**
54+
* Opens the selected location.
55+
*
56+
* @author Curtis Rueden
57+
* @author Mark Hiner
58+
*/
59+
@Plugin(type = Command.class, iconPath = "/icons/commands/folder_picture.png",
60+
menu = {
61+
@Menu(label = MenuConstants.FILE_LABEL, weight = MenuConstants.FILE_WEIGHT,
62+
mnemonic = MenuConstants.FILE_MNEMONIC),
63+
@Menu(label = "Open...", weight = 1, mnemonic = 'o', accelerator = "^O") },
64+
attrs = { @Attr(name = "no-legacy") })
65+
public class OpenLocation extends ContextCommand {
66+
67+
@Parameter
68+
private LogService log;
69+
70+
@Parameter
71+
private IOService ioService;
72+
73+
@Parameter
74+
private UIService uiService;
75+
76+
@Parameter
77+
private LocationService locationService;
78+
79+
@Parameter(label = "Location to open")
80+
private String location;
81+
82+
@Parameter(type = ItemIO.OUTPUT, label = "Data")
83+
private Object data;
84+
85+
@Override
86+
public void run() {
87+
try {
88+
Location source = locationService.resolve(location);
89+
final IOPlugin<?> opener = ioService.getOpener(source);
90+
if (opener == null) {
91+
error("No appropriate format found: " + location);
92+
return;
93+
}
94+
data = opener.open(location);
95+
if (data == null) {
96+
cancel(null);
97+
return;
98+
}
99+
}
100+
catch (final IOException exc) {
101+
log.error(exc);
102+
error(exc.getMessage());
103+
} catch (URISyntaxException e) {
104+
log.error(e);
105+
error(e.getMessage());
106+
}
107+
}
108+
109+
public String getInputLocation() {
110+
return location;
111+
}
112+
113+
public void setInputLocation(final String location) {
114+
this.location = location;
115+
}
116+
117+
public Object getData() {
118+
return data;
119+
}
120+
121+
public void setData(final Object data) {
122+
this.data = data;
123+
}
124+
125+
// -- Helper methods --
126+
127+
private void error(final String message) {
128+
uiService.showDialog(message, DialogPrompt.MessageType.ERROR_MESSAGE);
129+
}
130+
131+
}

0 commit comments

Comments
 (0)