From: hansonr Date: Sun, 24 Jun 2018 10:28:43 +0000 (+0100) Subject: asynchronous modal dialogs using PropertyChangeListener for JavaScript X-Git-Tag: Release_2_11_4_0~45^2~18^2~650 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=129b565f498688b03a8f89c3dbb35569d5464e39;p=jalview.git asynchronous modal dialogs using PropertyChangeListener for JavaScript --- diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index c08c64e..3ebbb2d 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -1168,8 +1168,8 @@ public class Desktop extends jalview.jbgui.GDesktop // for viewing JLabel label = new JLabel( MessageManager.getString("label.input_file_url")); - final JComboBox history = new JComboBox(); + JComboBox history = new JComboBox(); JPanel panel = new JPanel(new GridLayout(2, 1)); panel.add(label); panel.add(history); @@ -1191,66 +1191,20 @@ public class Desktop extends jalview.jbgui.GDesktop } } - int reply = JvOptionPane.showInternalConfirmDialog(desktop, panel, - MessageManager.getString("label.input_alignment_from_url"), - JvOptionPane.OK_CANCEL_OPTION); + // BH 2018 -- providing a callback for SwingJS + String dialogOption = "label.input_alignment_from_url"; + desktop.dialogData = new Object[] { dialogOption, viewport, history }; + desktop.onDialogReturn( + JvOptionPane.showInternalConfirmDialog(desktop, panel, + MessageManager.getString(dialogOption), + JvOptionPane.OK_CANCEL_OPTION)); - if (reply != JvOptionPane.OK_OPTION) - { - return; - } - - String url = history.getSelectedItem().toString(); - - if (url.toLowerCase().endsWith(".jar")) - { - if (viewport != null) - { - new FileLoader().LoadFile(viewport, url, DataSourceType.URL, - FileFormat.Jalview); - } - else - { - new FileLoader().LoadFile(url, DataSourceType.URL, - FileFormat.Jalview); - } - } - else - { - FileFormatI format = null; - try - { - format = new IdentifyFile().identify(url, DataSourceType.URL); - } catch (FileFormatException e) - { - // TODO revise error handling, distinguish between - // URL not found and response not valid - } + // no code may follow this, as SwingJS will not block + // callback in JavaScript comes via a property change event - if (format == null) - { - JvOptionPane.showInternalMessageDialog(Desktop.desktop, - MessageManager.formatMessage("label.couldnt_locate", - new Object[] - { url }), - MessageManager.getString("label.url_not_found"), - JvOptionPane.WARNING_MESSAGE); - - return; - } - - if (viewport != null) - { - new FileLoader().LoadFile(viewport, url, DataSourceType.URL, - format); - } - else - { - new FileLoader().LoadFile(url, DataSourceType.URL, format); - } - } } + /** * Opens the CutAndPaste window for the user to paste an alignment in to * @@ -2472,9 +2426,148 @@ public class Desktop extends jalview.jbgui.GDesktop * * @author AMW */ - public class MyDesktopPane extends JDesktopPane implements Runnable + public class MyDesktopPane extends JDesktopPane + implements Runnable, PropertyChangeListener { + public Object[] dialogData; + + // @Override + @Override + public void propertyChange(PropertyChangeEvent event) + { + Object val = event.getNewValue(); + String name = event.getPropertyName(); + System.out.println(name); + switch (event.getSource().getClass().getName()) + { + case "javax.swing.JOptionPane": + switch (name) + { + case "inputValue": + onDialogReturn(val); + return; + case "value": + if (val instanceof Integer) + { + onDialogReturn(((Integer) val).intValue()); + } + else + { + onDialogReturn(val); + } + return; + } + break; + case "javax.swing.ColorChooserDialog": + switch (name) + { + case "SelectedColor": + onDialogReturn(val); + return; + } + break; + case "javax.swing.JFileChooser": + switch (name) + { + case "SelectedFile": + File file = (File) val; + byte[] array = (val == null ? null + : /** @j2sNative file._bytes || */ + null); + onDialogReturn("fileName is '" + file.getName() + "'\n\n" + + new String(array)); + return; + } + break; + } + System.out.println(event.getSource().getClass().getName() + " " + + event.getPropertyName() + ": " + event.getNewValue()); + } + + // JSCOmponent.DialogCaller interface + private void onDialogReturn(Object value) + { + System.out.println("not implemented"); + } + + // JSCOmponent.DialogCaller interface + void onDialogReturn(int value) + { + if (value != Math.floor(value)) + { + // in JavaScript, this will be NaN, oddly enough + return; + } + + switch ((String) dialogData[0]) + { + case "label.input_alignment_from_url": + // reconstruct the parameter data + int reply = value; + AlignViewport viewport = (AlignViewport) dialogData[1]; + JComboBox history = (JComboBox) dialogData[2]; + // the rest of this is unchangaed + if (reply != JvOptionPane.OK_OPTION) + { + return; + } + + String url = history.getSelectedItem().toString(); + + if (url.toLowerCase().endsWith(".jar")) + { + if (viewport != null) + { + new FileLoader().LoadFile(viewport, url, DataSourceType.URL, + FileFormat.Jalview); + } + else + { + new FileLoader().LoadFile(url, DataSourceType.URL, + FileFormat.Jalview); + } + } + else + { + FileFormatI format = null; + try + { + format = new IdentifyFile().identify(url, DataSourceType.URL); + } catch (FileFormatException e) + { + // TODO revise error handling, distinguish between + // URL not found and response not valid + } + + if (format == null) + { + JvOptionPane.showInternalMessageDialog(Desktop.desktop, + MessageManager.formatMessage("label.couldnt_locate", + new Object[] + { url }), + MessageManager.getString("label.url_not_found"), + JvOptionPane.WARNING_MESSAGE); + + return; + } + + if (viewport != null) + { + new FileLoader().LoadFile(viewport, url, DataSourceType.URL, + format); + } + else + { + new FileLoader().LoadFile(url, DataSourceType.URL, format); + } + } + + break; + } + + } + private static final float ONE_MB = 1048576f; boolean showMemoryUsage = false;