From: gmungoc Date: Mon, 10 Dec 2018 10:20:15 +0000 (+0000) Subject: JAL-3048 DialogRunner changes (wip) X-Git-Tag: Release_2_11_4_0~45^2~18^2~343^2~7 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=a4aa14220a6a53391ca8e171bc774cef908057a3;p=jalview.git JAL-3048 DialogRunner changes (wip) --- diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index f2ff1d4..7dbed05 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -3997,7 +3997,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, JvOptionPane.WARNING_MESSAGE); } } - }).showOpenDialog(this); + }); + chooser.showOpenDialog(this); } public TreePanel showNewickTree(NewickFile nf, String treeTitle) @@ -5711,7 +5712,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, } }; - }).showOpenDialog(null); + }); + chooser.showOpenDialog(null); } diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index 320ebc2..6b7a278 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -1156,7 +1156,8 @@ public class Desktop extends jalview.jbgui.GDesktop new FileLoader().LoadFile(viewport, selectedFile, DataSourceType.FILE, format); } - }).showOpenDialog(this); + }); + chooser.showOpenDialog(this); } /** diff --git a/src/jalview/gui/JvOptionPane.java b/src/jalview/gui/JvOptionPane.java index a141e14..92a57ca 100644 --- a/src/jalview/gui/JvOptionPane.java +++ b/src/jalview/gui/JvOptionPane.java @@ -21,6 +21,7 @@ package jalview.gui; +import jalview.bin.Jalview; import jalview.util.dialogrunner.DialogRunner; import jalview.util.dialogrunner.DialogRunnerI; import jalview.util.dialogrunner.RunResponse; @@ -36,11 +37,9 @@ import javax.swing.Icon; import javax.swing.JOptionPane; import javax.swing.JPanel; -public class JvOptionPane extends JOptionPane - implements DialogRunnerI, PropertyChangeListener +public class JvOptionPane extends JOptionPane implements DialogRunnerI, + PropertyChangeListener { - // BH 2018 no changes needed here. - private static final long serialVersionUID = -3019167117756785229L; private static Object mockResponse = JvOptionPane.CANCEL_OPTION; @@ -49,10 +48,17 @@ public class JvOptionPane extends JOptionPane private Component parentComponent; - public JvOptionPane(final Component parentComponent) + private DialogRunnerI runner = new DialogRunner(); + + /* + * JalviewJS reports user choice in the dialog as the selected + * option (text); this list allows conversion to index (int) + */ + List ourOptions; + + public JvOptionPane(final Component parent) { - - this.parentComponent = parentComponent; + this.parentComponent = Jalview.isJS() ? this : parent; } public static int showConfirmDialog(Component parentComponent, @@ -704,18 +710,17 @@ public class JvOptionPane extends JOptionPane return interactiveMode; } - public static void setInteractiveMode(boolean interactiveMode) + public static void setInteractiveMode(boolean interactive) { - JvOptionPane.interactiveMode = interactiveMode; + JvOptionPane.interactiveMode = interactive; } - @SuppressWarnings("unused") private static String getPrefix(int messageType) { - String prefix = ""; // JavaScript only - - if (/** @j2sNative true || */ - false) + String prefix = ""; + + // JavaScript only + if (Jalview.isJS()) { switch (messageType) { @@ -732,9 +737,6 @@ public class JvOptionPane extends JOptionPane return prefix; } - DialogRunner runner = new DialogRunner(this); - - private List ourOptions; /** * create a new option dialog that can be used to register responses - along * lines of showOptionDialog @@ -761,7 +763,7 @@ public class JvOptionPane extends JOptionPane if (!isInteractiveMode()) { - runner.firstRun((int) getMockResponse()); + runner.handleResponse(getMockResponse()); } // two uses: // @@ -777,25 +779,20 @@ public class JvOptionPane extends JOptionPane // // 2) UserDefinedColors warning about saving over a name already defined // - Component parent; - /** - * @j2sNative - * parent = this; - */ - { - parent = parentComponent; - } - ; + ourOptions = Arrays.asList(options); - int response = JOptionPane.showOptionDialog(parent, message, title, + + int response = JOptionPane.showOptionDialog(parentComponent, message, title, optionType, messageType, icon, options, initialValue); - /** - * @j2sNative + + /* + * In Java, the response is returned to this thread and handled here; + * (for Javascript, see propertyChange) */ + if (!Jalview.isJS()) { - runner.firstRun(response); + runner.handleResponse(response); } - } public void showInternalDialog(JPanel mainPanel, String title, @@ -804,66 +801,56 @@ public class JvOptionPane extends JOptionPane { if (!isInteractiveMode()) { - runner.firstRun((int) getMockResponse()); - } - Component parent; - /** - * @j2sNative parent = this; - */ - { - parent = parentComponent; + runner.handleResponse(getMockResponse()); } - ourOptions = Arrays.asList(options); + ourOptions = Arrays.asList(options); int response; - if (parent!=this) { - - response = JOptionPane.showInternalOptionDialog(parent, mainPanel, + if (parentComponent != this) + { + response = JOptionPane.showInternalOptionDialog(parentComponent, mainPanel, title, yesNoCancelOption, questionMessage, icon, options, initresponse); } else { - response = JOptionPane.showOptionDialog(parent, mainPanel, title, + response = JOptionPane.showOptionDialog(parentComponent, mainPanel, title, yesNoCancelOption, questionMessage, icon, options, initresponse); } - /** - * @j2sNative - */ + if (!Jalview.isJS()) { - runner.firstRun(response); + runner.handleResponse(response); } } @Override public JvOptionPane addResponse(RunResponse action) { - runner.addResponse(action); return this; } - public JvOptionPane defaultResponse(Runnable runnable) - { - runner.setDefaultResponse(runnable); - return this; - } - + /** + * JalviewJS signals option selection by a property change event + * for the option e.g. "OK". This methods responds to that by + * running the response action that corresponds to that option. + * + * @param evt + */ @Override public void propertyChange(PropertyChangeEvent evt) { - int ourOption = ourOptions.indexOf(evt.getNewValue()); - if (ourOption == -1) + Object newValue = evt.getNewValue(); + int ourOption = ourOptions.indexOf(newValue); + if (ourOption >= 0) { - // try our luck.. - runner.run(evt.getNewValue()); + runner.handleResponse(ourOption); } else { - runner.run(ourOption); + // try our luck.. + runner.handleResponse(newValue); } } - - } diff --git a/src/jalview/io/JalviewFileChooser.java b/src/jalview/io/JalviewFileChooser.java index 6838824..73a99e3 100755 --- a/src/jalview/io/JalviewFileChooser.java +++ b/src/jalview/io/JalviewFileChooser.java @@ -22,12 +22,12 @@ package jalview.io; import jalview.bin.Cache; +import jalview.bin.Jalview; import jalview.gui.JvOptionPane; import jalview.util.MessageManager; import jalview.util.Platform; import jalview.util.dialogrunner.DialogRunner; import jalview.util.dialogrunner.DialogRunnerI; -import jalview.util.dialogrunner.Response; import jalview.util.dialogrunner.RunResponse; import java.awt.Component; @@ -61,11 +61,87 @@ import javax.swing.plaf.basic.BasicFileChooserUI; * @author AMW * */ -public class JalviewFileChooser extends JFileChooser - implements PropertyChangeListener, DialogRunnerI +public class JalviewFileChooser extends JFileChooser implements DialogRunnerI, + PropertyChangeListener { - DialogRunner runner = new DialogRunner<>(this); + private DialogRunnerI runner = new DialogRunner(); + File selectedFile = null; + + /** + * On user selecting a file to save to, this response is run to check if the + * file already exists, and if so show a dialog to prompt for confirmation of + * overwrite. + */ + RunResponse overwriteCheck = new RunResponse(JalviewFileChooser.APPROVE_OPTION) + { + @Override + public void run() + { + selectedFile = getSelectedFile(); + + if (selectedFile == null) + { + // Workaround for Java 9,10 on OSX - no selected file, but there is a + // filename typed in + // TODO is this needed in Java 8 or 11? + try + { + String filename = ((BasicFileChooserUI) getUI()).getFileName(); + if (filename != null && filename.length() > 0) + { + selectedFile = new File(getCurrentDirectory(), filename); + } + } catch (Throwable x) + { + System.err.println( + "Unexpected exception when trying to get filename."); + x.printStackTrace(); + } + } + if (selectedFile == null) + { + setReturnValue(JalviewFileChooser.CANCEL_OPTION); + return; + } + // JBP Note - this code was executed regardless of 'SAVE' being pressed + // need to see if there were side effects + if (getFileFilter() instanceof JalviewFileFilter) + { + JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter(); + + if (!jvf.accept(getSelectedFile())) + { + String withExtension = getSelectedFile() + "." + + jvf.getAcceptableExtension(); + setSelectedFile(new File(withExtension)); + } + } + // All good, so we continue to save + setReturnValue(JalviewFileChooser.APPROVE_OPTION); + + // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE + // USER PROMPTED FOR A NEW FILENAME + if (!Jalview.isJS()) + { + if (getSelectedFile().exists()) + { + // JAL-3048 - may not need to raise this for browser saves + // yes/no cancel + int confirm = JvOptionPane.showConfirmDialog(JalviewFileChooser.this, + MessageManager.getString("label.overwrite_existing_file"), + MessageManager.getString("label.file_already_exists"), + JvOptionPane.YES_NO_OPTION); + + if (confirm != JvOptionPane.YES_OPTION) + { + setReturnValue(JalviewFileChooser.CANCEL_OPTION); + } + } + } + }; + }; + /** * Factory method to return a file chooser that offers readable alignment file * formats @@ -146,7 +222,7 @@ public class JalviewFileChooser extends JFileChooser } JalviewFileChooser(String dir, String[] extensions, String[] descs, - String selected, boolean allFiles) + String selected, boolean acceptAny) { super(safePath(dir)); if (extensions.length == descs.length) @@ -156,7 +232,7 @@ public class JalviewFileChooser extends JFileChooser { formats.add(new String[] { extensions[i], descs[i] }); } - init(formats, selected, allFiles); + init(formats, selected, acceptAny); } else { @@ -165,18 +241,6 @@ public class JalviewFileChooser extends JFileChooser } } - @Override - public void propertyChange(PropertyChangeEvent evt) - { - // TODO other properties need runners... - switch (evt.getPropertyName()) - { - case "SelectedFile": - runner.run(APPROVE_OPTION); - break; - } - } - private static File safePath(String dir) { if (dir == null) @@ -200,13 +264,11 @@ public class JalviewFileChooser extends JFileChooser @Override public int showOpenDialog(Component parent) { - runner.resetResponses(); + // runner.resetResponses(); int value = super.showOpenDialog(this); - /** - * @j2sNative - */ + if (!Jalview.isJS()) { - runner.firstRun(value); + runner.handleResponse(value); } return value; } @@ -216,17 +278,17 @@ public class JalviewFileChooser extends JFileChooser * @param formats * a list of {extensions, description} for each file format * @param selected - * @param allFiles + * @param acceptAny * if true, 'any format' option is included */ - void init(List formats, String selected, boolean allFiles) + void init(List formats, String selected, boolean acceptAny) { JalviewFileFilter chosen = null; // SelectAllFilter needs to be set first before adding further // file filters to fix bug on Mac OSX - setAcceptAllFileFilterUsed(allFiles); + setAcceptAllFileFilterUsed(acceptAny); for (String[] format : formats) { @@ -317,93 +379,14 @@ public class JalviewFileChooser extends JFileChooser } return null; } - - File ourselectedFile = null; @Override public File getSelectedFile() { - File selfile = super.getSelectedFile(); - if (selfile == null && ourselectedFile != null) - { - return ourselectedFile; - } - return selfile; + File f = super.getSelectedFile(); + return f == null ? selectedFile : f; } - Component saveparent; - RunResponse overwriteCheck = new RunResponse( - JalviewFileChooser.APPROVE_OPTION) - { - @Override - public void run() - { - ourselectedFile = getSelectedFile(); - - if (getSelectedFile() == null) - { - // Workaround for Java 9,10 on OSX - no selected file, but there is a - // filename typed in - try - { - String filename = ((BasicFileChooserUI) getUI()).getFileName(); - if (filename != null && filename.length() > 0) - { - ourselectedFile = new File(getCurrentDirectory(), filename); - } - } catch (Throwable x) - { - System.err.println( - "Unexpected exception when trying to get filename."); - x.printStackTrace(); - } - } - if (ourselectedFile == null) - { - returned = new Response(JalviewFileChooser.CANCEL_OPTION); - return; - } - // JBP Note - this code was executed regardless of 'SAVE' being pressed - // need to see if there were side effects - if (getFileFilter() instanceof JalviewFileFilter) - { - JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter(); - - if (!jvf.accept(getSelectedFile())) - { - String withExtension = getSelectedFile() + "." - + jvf.getAcceptableExtension(); - setSelectedFile(new File(withExtension)); - } - } - // All good, so we continue to save - returned = new Response(JalviewFileChooser.APPROVE_OPTION); - - // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE - // USER PROMPTED FOR A NEW FILENAME - /** - * @j2sNative - */ - { - if (getSelectedFile().exists()) - { - // JAL-3048 - may not need to raise this for browser saves - - // yes/no cancel - int confirm = JvOptionPane.showConfirmDialog(saveparent, - MessageManager.getString("label.overwrite_existing_file"), - MessageManager.getString("label.file_already_exists"), - JvOptionPane.YES_NO_OPTION); - - if (confirm != JvOptionPane.YES_OPTION) - { - returned = new Response(JalviewFileChooser.CANCEL_OPTION); - } - } - } - }; - }; - /** * Overridden for JalviewJS compatibility: only one thread in Javascript, * so we can't wait for user choice in another thread and then perform the @@ -417,30 +400,28 @@ public class JalviewFileChooser extends JFileChooser /* * Save dialog is opened until user picks a file format */ + /* if (!runner.isRegistered(overwriteCheck)) { // first call for this instance - runner.firstResponse(overwriteCheck); + runner.setFirstResponse(overwriteCheck); } else { // reset response flags runner.resetResponses(); } - - setDialogType(SAVE_DIALOG); +*/ + // runner.addResponse(overwriteCheck); +// setDialogType(SAVE_DIALOG); // Java 9,10,11 on OSX - clear selected file so name isn't auto populated this.setSelectedFile(null); - saveparent = parent; - - int value = showDialog(parent, MessageManager.getString("action.save")); - /** - * @j2sNative - */ + int value = super.showSaveDialog(parent);//, MessageManager.getString("action.save")); + if (!Jalview.isJS()) { - runner.firstRun(value); + runner.handleResponse(value); } return value; } @@ -538,9 +519,48 @@ public class JalviewFileChooser extends JFileChooser } @Override - public JalviewFileChooser addResponse(RunResponse action) + public DialogRunnerI addResponse(RunResponse action) { return runner.addResponse(action); } + /** + * JalviewJS signals file selection by a property change event + * for property "SelectedFile". This methods responds to that by + * running the response action for 'OK' in the dialog. + * + * @param evt + */ + @Override + public void propertyChange(PropertyChangeEvent evt) + { + // TODO other properties need runners... + switch (evt.getPropertyName()) + { + case "SelectedFile": + runner.handleResponse(APPROVE_OPTION); + break; + } + } + + @Override + public void approveSelection() + { + if (getDialogType() == SAVE_DIALOG && !Jalview.isJS()) + { + File selectedFile = getSelectedFile(); + if ((selectedFile != null) && selectedFile.exists()) + { + int confirm = JvOptionPane.showConfirmDialog(this, + MessageManager.getString("label.overwrite_existing_file"), + MessageManager.getString("label.file_already_exists"), JvOptionPane.YES_NO_OPTION); + + if (confirm != JvOptionPane.YES_OPTION) + { + return; + } + } + } + super.approveSelection(); + } } diff --git a/src/jalview/util/dialogrunner/DialogRunner.java b/src/jalview/util/dialogrunner/DialogRunner.java index 05a63c7..59f3c98 100644 --- a/src/jalview/util/dialogrunner/DialogRunner.java +++ b/src/jalview/util/dialogrunner/DialogRunner.java @@ -21,6 +21,7 @@ package jalview.util.dialogrunner; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -36,43 +37,32 @@ import java.util.Map; * the actual dialog that will be shown - which will also initiate the * response chain. */ -public class DialogRunner implements DialogRunnerI +public class DialogRunner implements DialogRunnerI { + private Map> callbacks = new HashMap<>(); - private Map> callbacks = new java.util.HashMap<>(); + private boolean firstRunWasCalled = false; - public T dialog; - - public DialogRunner(T ourDialog) + /** + * Constructor + */ + public DialogRunner() { - dialog = ourDialog; } /** - * clear all 'was ran' flags so responses can be called again, and firstRun will - * trigger response execution + * Reset so handleResponse will start response execution */ public void resetResponses() { - for (List lr : callbacks.values()) - { - for (RunResponse response : lr) - { - response.reset(); - } - } - responses.clear(); - if (defaultResponse != null) - { - defaultResponse.reset(); - } firstRunWasCalled = false; } @Override - public T addResponse(RunResponse action) + public DialogRunnerI addResponse(RunResponse action) { - return addResponse(false, action); + addResponse(false, action); + return this; } /** @@ -82,26 +72,26 @@ public class DialogRunner implements DialogRunnerI * @param action * @return */ - public T firstResponse(RunResponse action) + public DialogRunnerI setFirstResponse(RunResponse action) { return addResponse(true, action); } - protected T addResponse(boolean prePend, RunResponse action) + protected DialogRunnerI addResponse(boolean prePend, RunResponse action) { - List laction = callbacks.get(action.ourTrigger); - if (laction == null) + List actions = callbacks.get(action.getTrigger()); + if (actions == null) { - laction = new ArrayList<>(); - callbacks.put(action.ourTrigger, laction); + actions = new ArrayList<>(); + callbacks.put(action.getTrigger(), actions); } if (prePend) { - laction.add(0,action); + actions.add(0,action); } else { - laction.add(action); + actions.add(action); } - return dialog; + return this; } /** @@ -111,160 +101,89 @@ public class DialogRunner implements DialogRunnerI */ public boolean isRegistered(RunResponse action) { - List resp = callbacks.get(action.ourTrigger); - if (resp != null) - { - for (RunResponse r : resp) - { - if (r == action) - { - return true; - } - } - } - return false; - } - /** - * handle a response - * - * @param responseCode - */ - public void run(int responseCode) - { - run(new Response(responseCode)); - } - - public void run(String responseString) - { - run(new Response(responseString)); - } - - public void run(Object responseObj) - { - run(new Response(responseObj)); + List resp = callbacks.get(action.getTrigger()); + return resp != null && resp.contains(action); } /** - * start of response handling. + * Handles a response by running the chain of registered actions (if any). + * Answers the list of responses run (in order). * - * @param responseCode + * @param response */ - public void firstRun(int responseCode) - { - doFirstRun(new Response(responseCode)); - } - - public void firstRun(String responseString) + @Override + public List handleResponse(Object response) { - doFirstRun(new Response(responseString)); - } + List responsesRun = new ArrayList(); - public void firstRun(Object responseObj) - { - if (responseObj != null && !responseObj.equals(responseObj)) + /* + * this test is for NaN in Chrome + */ + if (response != null && !response.equals(response)) { - // NaN is an object in Chrome - catch this weirdness - // this so we don't cause issues later - return; + return responsesRun; } - doFirstRun(new Response(responseObj)); - } - - - boolean firstRunWasCalled = false; - - private void doFirstRun(Response response) - { + + /* + * failsafe check for illegal duplicate call(?) + */ if (firstRunWasCalled) { - return; +// return responsesRun; } firstRunWasCalled = true; - run(response); + + runResponse(response, responsesRun); + if (responsesRun.isEmpty()) + { + System.err.println("Did nothing for " + response); + } + + return responsesRun; } - private void run(Response response) + /** + * Runs any response handlers registered for the given response. If any + * response provides a return value, then the handler for that value is + * run next recursively. Handlers are only run once. + * + * @param response + * @param alreadyRun + */ + private void runResponse(Object response, List alreadyRun) { - if (response.objresp != null - && !response.objresp.equals(response.objresp)) + /* + * this test is for NaN in Chrome + */ + if (response != null && !response.equals(response)) { - // NaN is an object in Chrome - catch this weirdness - // this so we don't cause issues later return; } - responses.add(response); - List laction = response.isNull() ? null : callbacks.get(response); + List actions = response == null ? null : callbacks.get(response); - if (laction == null) + if (actions == null) { - if (defaultResponse != null) - { - defaultResponse.ourTrigger = response; - defaultResponse.wasRun = true; - defaultResponse.run(); - } - else - { - System.err.println("Doing nothing for " + response); - } + System.err.println("Doing nothing for " + response); return; } - boolean wasRun = false; - int num = 0; - for (RunResponse action : laction) + for (RunResponse action : actions) { - num++; - // find next action to execute - if (!action.wasRun) + if (!alreadyRun.contains(action)) { - System.err - .println("Executing action (" + num + ") for " + response); - wasRun = true; - action.wasRun = true; + action.setReturnValue(null); action.run(); - if (action.returned != null) + alreadyRun.add(action); + Object returnValue = action.getReturnValue(); + if (returnValue != null) { - run(action.returned); + /* + * RunResponse wants to chain another action + */ + runResponse(returnValue, alreadyRun); } break; } } - if (!wasRun) - { - System.err.println("Did nothing for " + response); - } - } - - List responses = new ArrayList<>(); - - RunResponse defaultResponse = null; - - /** - * Convenience wrapper for setting default response to a runnable - * - * @param runnable - */ - public void setDefaultResponse(Runnable runnable) - { - defaultResponse = new RunResponse(runnable) - { - @Override - public void run() - { - runnable.run(); - } - }; - } - - /** - * Default responses are called once, with ourTrigger set to the unHandled - * response received - * - * @param runnable - */ - public void setDefaultResponse(RunResponse runnable) - { - defaultResponse = runnable; } } diff --git a/src/jalview/util/dialogrunner/DialogRunnerI.java b/src/jalview/util/dialogrunner/DialogRunnerI.java index 758d421..6914656 100644 --- a/src/jalview/util/dialogrunner/DialogRunnerI.java +++ b/src/jalview/util/dialogrunner/DialogRunnerI.java @@ -20,13 +20,15 @@ */ package jalview.util.dialogrunner; +import java.util.List; + /** * functional pattern for blocking dialog response handling * * @author jprocter * */ -public interface DialogRunnerI +public interface DialogRunnerI { /** @@ -37,7 +39,19 @@ public interface DialogRunnerI * * * @param action - * @return the dialog + * @return + */ + DialogRunnerI addResponse(RunResponse action); + + /** + * Runs any registered handlers for the given response, and answers the list + * of responses run (if any) in order run + * + * @param response + * @return */ - T addResponse(RunResponse action); + default List handleResponse(Object response) + { + return null; + } } diff --git a/src/jalview/util/dialogrunner/Response.java b/src/jalview/util/dialogrunner/Response.java deleted file mode 100644 index f168e20..0000000 --- a/src/jalview/util/dialogrunner/Response.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) - * Copyright (C) $$Year-Rel$$ The Jalview Authors - * - * This file is part of Jalview. - * - * Jalview is free software: you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation, either version 3 - * of the License, or (at your option) any later version. - * - * Jalview is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty - * of MERCHANTABILITY or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Jalview. If not, see . - * The Jalview Authors are detailed in the 'AUTHORS' file. - */ -package jalview.util.dialogrunner; - -public class Response -{ - int type = 0; // int = 0, String = 1, Object = 2; - - int intresp; - - String stringresp; - - Object objresp; - - public Response(int response) - { - type = 0; - intresp = response; - } - - public Response(String response) - { - type = 1; - stringresp = response; - } - - public Response(Object response) - { - if (response instanceof String) - { - type = 1; - stringresp = (String) response; - return; - } - if (response instanceof Integer) - { - type = 0; - intresp = ((Integer) response).intValue(); - return; - } - objresp = response; - type = 2; - } - - @Override - public boolean equals(Object obj) - { - if (obj == null || !(obj instanceof Response)) - { - return false; - } - ; - if (((Response) obj).type == type) - { - switch (type) - { - case 0: - return ((((Response) obj).intresp) == intresp); - case 1: - return (((Response) obj).stringresp.equals(stringresp)); - case 2: - return (((Response) obj).objresp).equals(objresp); - } - } - return false; - } - - @Override - public int hashCode() - { - switch (type) - { - case 0: - return Integer.valueOf(intresp).hashCode(); - case 1: - return stringresp.hashCode(); - case 2: - return objresp.hashCode(); - } - return super.hashCode(); - } - - @Override - public String toString() - { - switch (type) - { - case 0: - return "DialogRunner int: " + intresp; - case 1: - return "DialogRunner str: '" + stringresp + "'"; - case 2: - return "DialogRunner obj: " + String.valueOf(objresp); - } - return "Unconfigured response."; - } - - /** - * null response - triggers the default response - * @return - */ - public boolean isNull() - { - return (type==2 && objresp==null) || (type==1 && (stringresp==null || stringresp.equals(""))); - } -} \ No newline at end of file diff --git a/src/jalview/util/dialogrunner/RunResponse.java b/src/jalview/util/dialogrunner/RunResponse.java index 3fd56c4..7cb7e08 100644 --- a/src/jalview/util/dialogrunner/RunResponse.java +++ b/src/jalview/util/dialogrunner/RunResponse.java @@ -32,43 +32,57 @@ public abstract class RunResponse implements Runnable /** * Response that triggers the Run method */ - public Response ourTrigger; + private Object trigger; /** * set by run() on exit */ - public Response returned = null; + private Object returnValue = null; /** * set by dialog runner */ - public boolean wasRun = false; + private boolean wasRun = false; - public RunResponse(int trigger) + public RunResponse(Object onTrigger) { - ourTrigger = new Response(trigger); + trigger = onTrigger; } - public RunResponse(Object trigger) + public Object getTrigger() { - ourTrigger = new Response(trigger); + return trigger; } - - public RunResponse(String trigger) - { - ourTrigger = new Response(trigger); - } - + public void reset() { wasRun = false; - returned = null; - + returnValue = null; } @Override public String toString() { - return "Runner for " + ourTrigger; + return "Runner for " + trigger; + } + + public Object getReturnValue() + { + return returnValue; + } + + public void setReturnValue(Object o) + { + returnValue = o; + } + + public boolean hasBeenRun() + { + return wasRun; + } + + public void setRun() + { + wasRun = true; } } diff --git a/test/jalview/util/dialogrunner/DialogRunnerTest.java b/test/jalview/util/dialogrunner/DialogRunnerTest.java index e69ae87..7d7e5e8 100644 --- a/test/jalview/util/dialogrunner/DialogRunnerTest.java +++ b/test/jalview/util/dialogrunner/DialogRunnerTest.java @@ -7,18 +7,24 @@ public class DialogRunnerTest { public class MockDialog implements DialogRunnerI { - DialogRunner runner = new DialogRunner<>(this); + DialogRunnerI runner = new DialogRunner(); @Override - public MockDialog response(RunResponse action) + public DialogRunnerI addResponse(RunResponse action) { - return runner.response(action); + return runner.addResponse(action); } public void doDialog(String resp) { - runner.firstRun(resp); + runner.handleResponse(resp); } + + @Override + public void handleResponse(Object response) { + // TODO Auto-generated method stub + + } } MockDialog dialog = new MockDialog(); @@ -27,11 +33,13 @@ public class DialogRunnerTest public void testDialogRunner() { RunResponse ok, cancel, help, ineed; - final Response ooh = new Response("OOOOoooOOOOH!"), - r_ok = new Response("OK"), r_cancel = new Response("CANCEL"), - r_done = new Response("DONE"), r_help = new Response("HELP"), - r_ddoit = new Response("DIDNT DOIT"), - r_needsb = new Response("I NEED SOMEBODY"); + final String ooh = "OOOOoooOOOOH!"; + final String r_ok = "OK"; + final String r_cancel = "CANCEL"; + final String r_done = "DONE"; + final String r_help = "HELP"; + final String r_ddoit = "DIDNT DOIT"; + final String r_needsb = "I NEED SOMEBODY"; ok = new RunResponse("OK") { @@ -39,7 +47,7 @@ public class DialogRunnerTest @Override public void run() { - returned = new Response("DONE"); + returned = "DONE"; } }; final RunResponse befok = new RunResponse("OK") @@ -48,7 +56,7 @@ public class DialogRunnerTest @Override public void run() { - returned = new Response("OK"); + returned = "OK"; } }; @@ -80,7 +88,7 @@ public class DialogRunnerTest Assert.assertFalse(dialog.runner.isRegistered(ok)); - dialog.response(ok).response(cancel).response(help).response(ineed); + dialog.addResponse(ok).addResponse(cancel).addResponse(help).addResponse(ineed); Assert.assertTrue(dialog.runner.isRegistered(ok)); @@ -114,7 +122,7 @@ public class DialogRunnerTest // TODO: test prepend and chained execution of tasks for a response. Assert.assertFalse(dialog.runner.isRegistered(befok)); - dialog.runner.firstResponse(befok); + dialog.runner.setFirstResponse(befok); Assert.assertTrue(dialog.runner.isRegistered(befok)); Assert.assertTrue(dialog.runner.isRegistered(ok)); diff --git a/test/jalview/util/dialogrunner/ResponseTest.java b/test/jalview/util/dialogrunner/ResponseTest.java deleted file mode 100644 index d722b1a..0000000 --- a/test/jalview/util/dialogrunner/ResponseTest.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) - * Copyright (C) $$Year-Rel$$ The Jalview Authors - * - * This file is part of Jalview. - * - * Jalview is free software: you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation, either version 3 - * of the License, or (at your option) any later version. - * - * Jalview is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty - * of MERCHANTABILITY or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Jalview. If not, see . - * The Jalview Authors are detailed in the 'AUTHORS' file. - */ -package jalview.util.dialogrunner; - -import org.testng.Assert; -import org.testng.annotations.Test; - -public class ResponseTest -{ - @Test - public void testResonse() { - Response intr=new Response(1),intrCopy=new Response(1); - Response strr=new Response("1"),strrcopy=new Response("1"); - Response objr=new Response(Double.valueOf(1d)); - Assert.assertTrue(intr.equals(intrCopy)); - Assert.assertTrue(strr.equals(strrcopy)); - Assert.assertFalse(intr.equals(strr)); - Assert.assertFalse(intr.equals(objr)); - Assert.assertFalse(strr.equals(objr)); - Assert.assertEquals(intr.toString(), "DialogRunner int: 1"); - Assert.assertEquals(strr.toString(), "DialogRunner str: '1'"); - Assert.assertEquals(objr.toString(), "DialogRunner obj: 1.0"); - } -} diff --git a/test/jalview/util/dialogrunner/RunResponseTest.java b/test/jalview/util/dialogrunner/RunResponseTest.java index ff86675..4942b32 100644 --- a/test/jalview/util/dialogrunner/RunResponseTest.java +++ b/test/jalview/util/dialogrunner/RunResponseTest.java @@ -14,11 +14,11 @@ public class RunResponseTest @Override public void run() { - returned = new Response("DONE"); + returned = "DONE"; } }; - Assert.assertEquals(rr.ourTrigger, new Response("OK")); - Assert.assertNotEquals(rr.ourTrigger, new Response("NOTOK")); + Assert.assertEquals(rr.ourTrigger, "OK"); + Assert.assertNotEquals(rr.ourTrigger, "NOTOK"); Assert.assertNull(rr.returned); Assert.assertFalse(rr.wasRun); // trivial .. @@ -26,12 +26,12 @@ public class RunResponseTest rr.run(); Assert.assertTrue(rr.wasRun); - Assert.assertEquals(rr.returned, new Response("DONE")); + Assert.assertEquals(rr.returned, "DONE"); rr.reset(); Assert.assertNull(rr.returned); Assert.assertFalse(rr.wasRun); - Assert.assertEquals(rr.toString(), "Runner for " + new Response("OK")); + Assert.assertEquals(rr.toString(), "Runner for " + "OK"); // just test the other constructors RunResponse rr12 = new RunResponse(12) @@ -39,7 +39,7 @@ public class RunResponseTest @Override public void run() { - returned = new Response("DONE"); + returned = "DONE"; } }; RunResponse rrpi = new RunResponse(new Double(3.142)) @@ -47,11 +47,10 @@ public class RunResponseTest @Override public void run() { - returned = new Response("DONE"); + returned = "DONE"; } }; - Assert.assertEquals(rr12.ourTrigger, new Response(12)); - Assert.assertEquals(rrpi.ourTrigger, - new Response(Double.valueOf(3.142))); + Assert.assertEquals(rr12.ourTrigger, Integer.valueOf(12)); + Assert.assertEquals(rrpi.ourTrigger, Double.valueOf(3.142)); } }