Skip to content

Commit d831488

Browse files
authored
Merge pull request #315 from imagej/fix-open-recent
Restore DefaultLegacyOpener behavior to return null when File > Open Recent is used.
2 parents c24d491 + ace60db commit d831488

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/main/java/net/imagej/legacy/plugin/DefaultLegacyOpener.java

+20-6
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ private IOPlugin<?> getEagerHandler(String path) {
104104
public Object open(String path, final int planeIndex,
105105
final boolean displayResult)
106106
{
107+
// There are many ways this code can get hit and the return value is
108+
// critical in ensuring the proper subsequent behavior is taken.
109+
// In the cases of Open Recent, path will be non-null. HOWEVER, if we do
110+
// not handle the opening here we MUST return null to ensure the original
111+
// ImageJ picks it up appropriately.
112+
// In cases of File > Open..., path will be null and we will display a file
113+
// chooser. If we do not handle the opening here, we MUST return the path
114+
// that was selected - otherwise a second file chooser will be displayed
115+
// when falling back to the original ImageJ.
116+
// See https://github.com/imagej/imagej-legacy/issues/314
117+
String returnPath = null;
107118
final Context c = IJ1Helper.getLegacyContext();
108119

109120
legacyService = getCached(legacyService, LegacyService.class, c);
@@ -139,6 +150,9 @@ public Object open(String path, final int planeIndex,
139150
return Boolean.TRUE; // cancel the operation
140151
}
141152
path = selectedPath[0];
153+
// Since we displayed an opener dialog to the user, here we do want to
154+
// update the returnPath, to avoid triggering a second opener dialog
155+
returnPath = path;
142156
}
143157
if (path == null) return Boolean.TRUE; // cancel the operation
144158

@@ -151,12 +165,12 @@ public Object open(String path, final int planeIndex,
151165
final IOPlugin<?> opener = ioService.getOpener(path);
152166
if (opener == null) {
153167
logService.warn("No appropriate format found: " + path);
154-
return path;
168+
return returnPath;
155169
}
156170
data = opener.open(path);
157171
if (data == null) {
158172
logService.warn("Opening was canceled.");
159-
return path;
173+
return returnPath;
160174
}
161175
}
162176
else {
@@ -173,23 +187,23 @@ public Object open(String path, final int planeIndex,
173187
final IOPlugin<?> io = getEagerHandler(path);
174188
if (io == null) {
175189
logService.debug("No appropriate eager I/O plugin found: " + path);
176-
return path; // fall back to original ImageJ
190+
return returnPath; // fall back to original ImageJ
177191
}
178192
data = io.open(path);
179193
if (data == null) {
180194
logService.debug("Eager I/O plugin '" + io.getClass().getName() + "' opened nothing.");
181-
return path; // fall back to original ImageJ
195+
return returnPath; // fall back to original ImageJ
182196
}
183197
}
184198
catch (final IOException exc) {
185199
legacyService.handleException(exc);
186-
return path; // fall back to original ImageJ
200+
return returnPath; // fall back to original ImageJ
187201
}
188202
}
189203
}
190204
catch (final IOException exc) {
191205
legacyService.handleException(exc);
192-
return path; // fall back to original ImageJ
206+
return returnPath; // fall back to original ImageJ
193207
}
194208

195209
return handleData(c, data, path, displayResult);

0 commit comments

Comments
 (0)