|
34 | 34 | import sc.fiji.snt.SNTUI; |
35 | 35 | import sc.fiji.snt.viewer.Viewer3D; |
36 | 36 |
|
| 37 | +import javax.swing.*; |
| 38 | +import java.awt.*; |
| 39 | +import java.awt.event.WindowAdapter; |
| 40 | +import java.awt.event.WindowEvent; |
| 41 | + |
37 | 42 | /** |
38 | 43 | * Command class for GUI commands extending DynamicCommand |
39 | 44 | * |
@@ -149,6 +154,57 @@ protected void resetUI(final boolean validateDimensions, final int state) { |
149 | 154 | statusService.clearStatus(); |
150 | 155 | } |
151 | 156 |
|
| 157 | + /** |
| 158 | + * Finds the SciJava-generated prompt dialog by its title and attaches a |
| 159 | + * {@link WindowAdapter} that resets the UI state to {@link SNTUI#READY} when |
| 160 | + * the dialog is closed. This is necessary because {@link #init(boolean)} sets |
| 161 | + * the state to {@link SNTUI#RUNNING_CMD}, but if the user dismisses the |
| 162 | + * dialog without running the command, nothing else resets it. |
| 163 | + * <p> |
| 164 | + * Subclasses implementing {@link org.scijava.command.Interactive} should call |
| 165 | + * this method early (e.g., from {@link #run()}) to ensure the listener is |
| 166 | + * attached before the user can close the dialog. The dialog is cached: repeated |
| 167 | + * calls are no-ops once it has been found. |
| 168 | + * |
| 169 | + * @param title the exact title set via {@code getInfo().setLabel()} — must be |
| 170 | + * unique across open dialogs |
| 171 | + * @return the dialog, or {@code null} if not yet created by SciJava |
| 172 | + */ |
| 173 | + protected JDialog getPromptWithCloseHandler(final String title) { |
| 174 | + return getPromptWithCloseHandler(title, null); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Variant of {@link #getPromptWithCloseHandler(String)} that runs an extra |
| 179 | + * action when the dialog is closing (e.g., cleaning up listeners). |
| 180 | + * |
| 181 | + * @param title the dialog title |
| 182 | + * @param extraOnClose additional action to run on {@code windowClosing}, or |
| 183 | + * {@code null} for none. Runs <em>before</em> the UI |
| 184 | + * state is reset. |
| 185 | + * @return the dialog, or {@code null} if not yet created |
| 186 | + */ |
| 187 | + protected JDialog getPromptWithCloseHandler(final String title, final Runnable extraOnClose) { |
| 188 | + if (cachedPrompt == null) { |
| 189 | + for (final Window w : JDialog.getWindows()) { |
| 190 | + if (w instanceof JDialog && title.equals(((JDialog) w).getTitle())) { |
| 191 | + cachedPrompt = (JDialog) w; |
| 192 | + cachedPrompt.addWindowListener(new WindowAdapter() { |
| 193 | + @Override |
| 194 | + public void windowClosing(final WindowEvent ignored) { |
| 195 | + if (extraOnClose != null) extraOnClose.run(); |
| 196 | + if (ui != null) ui.changeState(SNTUI.READY); |
| 197 | + } |
| 198 | + }); |
| 199 | + break; |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + return cachedPrompt; |
| 204 | + } |
| 205 | + |
| 206 | + private JDialog cachedPrompt; |
| 207 | + |
152 | 208 | protected void notifyExternalDataLoaded() { //TODO: Implement listener |
153 | 209 | // If a display canvas is being used notify plugin |
154 | 210 | snt.updateDisplayCanvases(); |
|
0 commit comments