From bf009101400340fedd14a58c84d0df4a33ce3ac0 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Fri, 17 Aug 2018 14:57:12 +0100 Subject: [PATCH] JAL-3048 adapt 'input from URL' dialog for JalviewJS --- resources/lang/Messages.properties | 2 +- src/jalview/gui/Desktop.java | 264 ++++++++++++------------------------ 2 files changed, 90 insertions(+), 176 deletions(-) diff --git a/resources/lang/Messages.properties b/resources/lang/Messages.properties index 184ae5c..2c4e964 100644 --- a/resources/lang/Messages.properties +++ b/resources/lang/Messages.properties @@ -417,7 +417,7 @@ label.input_alignment_from_url = Input Alignment From URL label.input_alignment = Input Alignment label.couldnt_import_as_vamsas_session = Couldn't import {0} as a new vamsas session. label.vamsas_document_import_failed = Vamsas Document Import Failed -label.couldnt_locate = Couldn't locate {0} +label.couldnt_locate = Couldn''t locate {0} label.url_not_found = URL not found label.new_sequence_url_link = New sequence URL link label.cannot_edit_annotations_in_wrapped_view = Cannot edit annotations in wrapped view diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index 6e155eb..ceea332 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -115,6 +115,7 @@ import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JProgressBar; +import javax.swing.JTextField; import javax.swing.KeyStroke; import javax.swing.SwingUtilities; import javax.swing.event.HyperlinkEvent; @@ -1146,10 +1147,9 @@ public class Desktop extends jalview.jbgui.GDesktop } /** - * DOCUMENT ME! + * Shows a dialog for input of a URL at which to retrieve alignment data * - * @param e - * DOCUMENT ME! + * @param viewport */ @Override public void inputURLMenuItem_actionPerformed(AlignViewport viewport) @@ -1159,46 +1159,101 @@ public class Desktop extends jalview.jbgui.GDesktop JLabel label = new JLabel( MessageManager.getString("label.input_file_url")); - JComboBox history = new JComboBox(); JPanel panel = new JPanel(new GridLayout(2, 1)); panel.add(label); - panel.add(history); - history.setPreferredSize(new Dimension(400, 20)); - history.setEditable(true); - history.addItem("http://www."); - - String historyItems = jalview.bin.Cache.getProperty("RECENT_URL"); - - StringTokenizer st; - - if (historyItems != null) + + /* + * the URL to fetch is + * Java: an editable combobox with history + * JS: (pending JAL-3038) a plain text field + */ + JComponent history; + String urlBase = "http://www."; + if (Jalview.isJS()) { - st = new StringTokenizer(historyItems, "\t"); - - while (st.hasMoreTokens()) + history = new JTextField(urlBase, 35); + } + else + { + JComboBox asCombo = new JComboBox<>(); + asCombo.setPreferredSize(new Dimension(400, 20)); + asCombo.setEditable(true); + asCombo.addItem(urlBase); + String historyItems = Cache.getProperty("RECENT_URL"); + if (historyItems != null) { - history.addItem(st.nextElement()); + for (String token : historyItems.split("\\t")) + { + asCombo.addItem(token); + } } + history = asCombo; } + panel.add(history); - // BH 2018 -- providing a callback for SwingJS - // dialogOption is just a simple way to provide - // context for the modal-like response. - // The only requirement is that desktop implement - // PropertyChangeListener, which is used already in Java - // for changes in input value and such within the dialogs. + Object[] options = new Object[] { MessageManager.getString("action.ok"), + MessageManager.getString("action.cancel") }; + RunResponse action = new RunResponse(JvOptionPane.OK_OPTION) { + @Override + public void run() + { + String url = Jalview.isJS() ? ((JTextField) history).getText() + : ((JComboBox) history).getSelectedItem() + .toString(); - 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 (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) + { + String msg = MessageManager.formatMessage("label.couldnt_locate", url); + JvOptionPane.showInternalMessageDialog(Desktop.desktop, msg, + MessageManager.getString("label.url_not_found"), + JvOptionPane.WARNING_MESSAGE); - // no code may follow this, as SwingJS will not block - // callback in JavaScript comes via a property change event, - // thus going into desktop.onDialogReturn(int) just the same as - // in Java. + return; + } + if (viewport != null) + { + new FileLoader().LoadFile(viewport, url, DataSourceType.URL, + format); + } + else + { + new FileLoader().LoadFile(url, DataSourceType.URL, format); + } + } + }}; + String dialogOption = MessageManager + .getString("label.input_alignment_from_url"); + JvOptionPane.newOptionDialog(desktop).response(action) + .showInternalDialog(panel, dialogOption, + JvOptionPane.YES_NO_CANCEL_OPTION, + JvOptionPane.PLAIN_MESSAGE, null, options, + MessageManager.getString("action.ok")); } /** @@ -2425,149 +2480,8 @@ public class Desktop extends jalview.jbgui.GDesktop * @author AMW */ public class MyDesktopPane extends JDesktopPane - implements Runnable, PropertyChangeListener + implements Runnable { - - public Object[] dialogData; - - // @Override - @Override - public void propertyChange(PropertyChangeEvent event) - { - // TODO this is obsolete with JAL-3048 - delete? - 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.JFileChooser": - switch (name) - { - case "SelectedFile": - // in JavaScript, this File object will have a _bytes property, - // because the file data has already been loaded - onDialogReturn(new Object[] { (File) val }); - return; - } - break; - } - System.out.println(event.getSource().getClass().getName() + " " - + event.getPropertyName() + ": " + event.getNewValue()); - } - - // JSCOmponent.DialogCaller interface - void onDialogReturn(Object value) - { - switch ((String) dialogData[0]) - { - case "SelectedFile": - case "runnable": - dialogData[0] = value; - ((Runnable) dialogData[1]).run(); - break; - default: - } - } - - // 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 "runnable": - dialogData[0] = Integer.valueOf(value); - ((Runnable) dialogData[1]).run(); - break; - 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; -- 1.7.10.2