JvOptionPane.WARNING_MESSAGE);
}
}
- }).showOpenDialog(this);
+ });
+ chooser.showOpenDialog(this);
}
public TreePanel showNewickTree(NewickFile nf, String treeTitle)
}
};
- }).showOpenDialog(null);
+ });
+ chooser.showOpenDialog(null);
}
new FileLoader().LoadFile(viewport, selectedFile,
DataSourceType.FILE, format);
}
- }).showOpenDialog(this);
+ });
+ chooser.showOpenDialog(this);
}
/**
package jalview.gui;
+import jalview.bin.Jalview;
import jalview.util.dialogrunner.DialogRunner;
import jalview.util.dialogrunner.DialogRunnerI;
import jalview.util.dialogrunner.RunResponse;
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;
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<Object> ourOptions;
+
+ public JvOptionPane(final Component parent)
{
-
- this.parentComponent = parentComponent;
+ this.parentComponent = Jalview.isJS() ? this : parent;
}
public static int showConfirmDialog(Component parentComponent,
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)
{
return prefix;
}
- DialogRunner<JvOptionPane> runner = new DialogRunner(this);
-
- private List<Object> ourOptions;
/**
* create a new option dialog that can be used to register responses - along
* lines of showOptionDialog
if (!isInteractiveMode())
{
- runner.firstRun((int) getMockResponse());
+ runner.handleResponse(getMockResponse());
}
// two uses:
//
//
// 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,
{
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);
}
}
-
-
}
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;
* @author AMW
*
*/
-public class JalviewFileChooser extends JFileChooser
- implements PropertyChangeListener, DialogRunnerI
+public class JalviewFileChooser extends JFileChooser implements DialogRunnerI,
+ PropertyChangeListener
{
- DialogRunner<JalviewFileChooser> 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
}
JalviewFileChooser(String dir, String[] extensions, String[] descs,
- String selected, boolean allFiles)
+ String selected, boolean acceptAny)
{
super(safePath(dir));
if (extensions.length == descs.length)
{
formats.add(new String[] { extensions[i], descs[i] });
}
- init(formats, selected, allFiles);
+ init(formats, selected, acceptAny);
}
else
{
}
}
- @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)
@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;
}
* @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<String[]> formats, String selected, boolean allFiles)
+ void init(List<String[]> 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)
{
}
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
/*
* 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;
}
}
@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();
+ }
}
package jalview.util.dialogrunner;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
* the actual dialog that will be shown - which will also initiate the
* response chain.
*/
-public class DialogRunner<T extends DialogRunnerI> implements DialogRunnerI
+public class DialogRunner implements DialogRunnerI
{
+ private Map<Object, List<RunResponse>> callbacks = new HashMap<>();
- private Map<Response, List<RunResponse>> 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<RunResponse> 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;
}
/**
* @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<RunResponse> laction = callbacks.get(action.ourTrigger);
- if (laction == null)
+ List<RunResponse> 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;
}
/**
*/
public boolean isRegistered(RunResponse action)
{
- List<RunResponse> 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<RunResponse> 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<RunResponse> handleResponse(Object response)
{
- doFirstRun(new Response(responseString));
- }
+ List<RunResponse> responsesRun = new ArrayList<RunResponse>();
- 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<RunResponse> 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<RunResponse> laction = response.isNull() ? null : callbacks.get(response);
+ List<RunResponse> 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<Response> 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;
}
}
*/
package jalview.util.dialogrunner;
+import java.util.List;
+
/**
* functional pattern for blocking dialog response handling
*
* @author jprocter
*
*/
-public interface DialogRunnerI<T extends DialogRunnerI>
+public interface DialogRunnerI
{
/**
* </pre>
*
* @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<RunResponse> handleResponse(Object response)
+ {
+ return null;
+ }
}
+++ /dev/null
-/*
- * 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 <http://www.gnu.org/licenses/>.
- * 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
/**
* 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;
}
}
{
public class MockDialog implements DialogRunnerI
{
- DialogRunner<MockDialog> 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();
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")
{
@Override
public void run()
{
- returned = new Response("DONE");
+ returned = "DONE";
}
};
final RunResponse befok = new RunResponse("OK")
@Override
public void run()
{
- returned = new Response("OK");
+ returned = "OK";
}
};
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));
// 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));
+++ /dev/null
-/*
- * 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 <http://www.gnu.org/licenses/>.
- * 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");
- }
-}
@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 ..
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)
@Override
public void run()
{
- returned = new Response("DONE");
+ returned = "DONE";
}
};
RunResponse rrpi = new RunResponse(new Double(3.142))
@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));
}
}