// 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);
}
}
- 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
*
*
* @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;