From: hansonr Date: Thu, 28 Jun 2018 02:00:44 +0000 (+0100) Subject: JAL-3032 DND enabled; additional io changes for File vs. String X-Git-Tag: Develop-2_11_2_0-d20201215~24^2~68^2~624 X-Git-Url: http://source.jalview.org/gitweb/?p=jalview.git;a=commitdiff_plain;h=c945c9641de88cad01b09b3db19e45667dfe751e JAL-3032 DND enabled; additional io changes for File vs. String --- diff --git a/src/jalview/api/AlignViewControllerI.java b/src/jalview/api/AlignViewControllerI.java index a7ec69e..58a58e9 100644 --- a/src/jalview/api/AlignViewControllerI.java +++ b/src/jalview/api/AlignViewControllerI.java @@ -96,7 +96,7 @@ public interface AlignViewControllerI * @return true if parsing resulted in something being imported to the view or * dataset */ - public boolean parseFeaturesFile(String file, DataSourceType sourceType, + public boolean parseFeaturesFile(Object file, DataSourceType sourceType, boolean relaxedIdMatching); /** diff --git a/src/jalview/controller/AlignViewController.java b/src/jalview/controller/AlignViewController.java index d992e4e..91a69eb 100644 --- a/src/jalview/controller/AlignViewController.java +++ b/src/jalview/controller/AlignViewController.java @@ -349,7 +349,7 @@ public class AlignViewController implements AlignViewControllerI } @Override - public boolean parseFeaturesFile(String file, DataSourceType protocol, + public boolean parseFeaturesFile(Object file, DataSourceType protocol, boolean relaxedIdMatching) { boolean featuresAdded = false; diff --git a/src/jalview/ext/jmol/JmolParser.java b/src/jalview/ext/jmol/JmolParser.java index 2a510a2..fd483ad 100644 --- a/src/jalview/ext/jmol/JmolParser.java +++ b/src/jalview/ext/jmol/JmolParser.java @@ -59,13 +59,14 @@ public class JmolParser extends StructureFile implements JmolStatusListener { Viewer viewer = null; - public JmolParser(boolean immediate, String inFile, + public JmolParser(boolean immediate, Object inFile, DataSourceType sourceType) throws IOException { + // BH 2018 File or String for filename super(immediate, inFile, sourceType); } - public JmolParser(String inFile, DataSourceType sourceType) + public JmolParser(Object inFile, DataSourceType sourceType) throws IOException { super(inFile, sourceType); @@ -133,6 +134,7 @@ public class JmolParser extends StructureFile implements JmolStatusListener * params -o (output to sysout) -n (nodisplay) -x (exit when finished) * see http://wiki.jmol.org/index.php/Jmol_Application */ + viewer = (Viewer) JmolViewer.allocateViewer(null, null, null, null, null, "-x -o -n", this); // ensure the 'new' (DSSP) not 'old' (Ramachandran) SS method is used diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 26c5548..2587866 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -4359,13 +4359,14 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, * Try to load a features file onto the alignment. * * @param file - * contents or path to retrieve file + * contents or path to retrieve file or a File object * @param sourceType * access mode of file (see jalview.io.AlignFile) * @return true if features file was parsed correctly. */ - public boolean parseFeaturesFile(String file, DataSourceType sourceType) + public boolean parseFeaturesFile(Object file, DataSourceType sourceType) { + // BH 2018 return avc.parseFeaturesFile(file, sourceType, Cache.getDefault("RELAXEDSEQIDMATCHING", false)); @@ -4412,8 +4413,10 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, // Java's Transferable for native dnd evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); Transferable t = evt.getTransferable(); + + final AlignFrame thisaf = this; - final List files = new ArrayList<>(); + final List files = new ArrayList<>(); List protocols = new ArrayList<>(); try @@ -4441,20 +4444,22 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, * Object[] { String,SequenceI} */ ArrayList filesmatched = new ArrayList<>(); - ArrayList filesnotmatched = new ArrayList<>(); + ArrayList filesnotmatched = new ArrayList<>(); for (int i = 0; i < files.size(); i++) { - String file = files.get(i).toString(); + // BH 2018 + Object file = files.get(i); + String fileName = file.toString(); String pdbfn = ""; - DataSourceType protocol = FormatAdapter.checkProtocol(file); + DataSourceType protocol = (file instanceof File ? DataSourceType.FILE : FormatAdapter.checkProtocol(fileName)); if (protocol == DataSourceType.FILE) { - File fl = new File(file); + File fl = (file instanceof File ? (File) file : new File(fileName)); pdbfn = fl.getName(); } else if (protocol == DataSourceType.URL) { - URL url = new URL(file); + URL url = new URL(fileName); pdbfn = url.getFile(); } if (pdbfn.length() > 0) @@ -4476,7 +4481,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, } if (mtch != null) { - FileFormatI type = null; + FileFormatI type; try { type = new IdentifyFile().identify(file, protocol); @@ -4567,7 +4572,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, { return; } - for (String fn : filesnotmatched) + for (Object fn : filesnotmatched) { loadJalviewDataFile(fn, null, null, null); } @@ -4594,9 +4599,10 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, * @param file * either a filename or a URL string. */ - public void loadJalviewDataFile(String file, DataSourceType sourceType, + public void loadJalviewDataFile(Object file, DataSourceType sourceType, FileFormatI format, SequenceI assocSeq) { + // BH 2018 was String file try { if (sourceType == null) diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index e82bd9c..39098f2 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -63,6 +63,7 @@ import java.awt.datatransfer.ClipboardOwner; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.dnd.DnDConstants; +import java.awt.dnd.DropTarget; import java.awt.dnd.DropTargetDragEvent; import java.awt.dnd.DropTargetDropEvent; import java.awt.dnd.DropTargetEvent; @@ -113,6 +114,7 @@ import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JProgressBar; +import javax.swing.JTextArea; import javax.swing.KeyStroke; import javax.swing.SwingUtilities; import javax.swing.event.HyperlinkEvent; @@ -421,7 +423,6 @@ public class Desktop extends jalview.jbgui.GDesktop checkURLLinks(); - this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this)); // Spawn a thread that shows the splashscreen SwingUtilities.invokeLater(new Runnable() @@ -464,6 +465,8 @@ public class Desktop extends jalview.jbgui.GDesktop } // end BH 2018 ignore + this.setDropTarget(new java.awt.dnd.DropTarget(desktop, this)); + this.addWindowListener(new WindowAdapter() { @Override @@ -1062,7 +1065,7 @@ public class Desktop extends jalview.jbgui.GDesktop // Java's Transferable for native dnd evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); Transferable t = evt.getTransferable(); - List files = new ArrayList<>(); + List files = new ArrayList<>(); List protocols = new ArrayList<>(); try @@ -1080,13 +1083,15 @@ public class Desktop extends jalview.jbgui.GDesktop { for (int i = 0; i < files.size(); i++) { - String file = files.get(i).toString(); + // BH 2018 File or String + Object file = files.get(i); + String fileName = file.toString(); DataSourceType protocol = (protocols == null) ? DataSourceType.FILE : protocols.get(i); FileFormatI format = null; - if (file.endsWith(".jar")) + if (fileName.endsWith(".jar")) { format = FileFormat.Jalview; @@ -1096,7 +1101,7 @@ public class Desktop extends jalview.jbgui.GDesktop format = new IdentifyFile().identify(file, protocol); } - new FileLoader().LoadFile(file, protocol, format); + new FileLoader().LoadFile(null, file, protocol, format); } } catch (Exception ex) @@ -1740,7 +1745,7 @@ public class Desktop extends jalview.jbgui.GDesktop ArrayList fileLoadingPanels = new ArrayList<>(); - public void startLoading(final String fileName) + public void startLoading(final Object fileName) { if (fileLoadingCount == 0) { @@ -3504,10 +3509,29 @@ public class Desktop extends jalview.jbgui.GDesktop * - the payload from the drop event * @throws Exception */ - public static void transferFromDropTarget(List files, + public static void transferFromDropTarget(List files, List protocols, DropTargetDropEvent evt, Transferable t) throws Exception { + + // BH 2018 changed List to List to allow for File from SwingJS + +// DataFlavor[] flavors = t.getTransferDataFlavors(); +// for (int i = 0; i < flavors.length; i++) { +// if (flavors[i].isFlavorJavaFileListType()) { +// evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); +// List list = (List) t.getTransferData(flavors[i]); +// for (int j = 0; j < list.size(); j++) { +// File file = (File) list.get(j); +// byte[] data = getDroppedFileBytes(file); +// fileName.setText(file.getName() + " - " + data.length + " " + evt.getLocation()); +// JTextArea target = (JTextArea) ((DropTarget) evt.getSource()).getComponent(); +// target.setText(new String(data)); +// } +// dtde.dropComplete(true); +// return; +// } +// DataFlavor uriListFlavor = new DataFlavor( "text/uri-list;class=java.lang.String"), urlFlavour = null; @@ -3557,7 +3581,7 @@ public class Desktop extends jalview.jbgui.GDesktop for (Object file : (List) t .getTransferData(DataFlavor.javaFileListFlavor)) { - files.add(((File) file).toString()); + files.add(file); protocols.add(DataSourceType.FILE); } } @@ -3663,13 +3687,14 @@ public class Desktop extends jalview.jbgui.GDesktop // resolve any .lnk files in the file drop for (int f = 0; f < files.size(); f++) { - String source = files.get(f).toLowerCase(); + String source = files.get(f).toString().toLowerCase(); if (protocols.get(f).equals(DataSourceType.FILE) && (source.endsWith(".lnk") || source.endsWith(".url") || source.endsWith(".site"))) { try { - File lf = new File(files.get(f)); + Object obj = files.get(f); + File lf = (obj instanceof File ? (File) obj : new File((String) obj)); // process link file to get a URL Cache.log.debug("Found potential link file: " + lf); WindowsShortcut wscfile = new WindowsShortcut(lf); diff --git a/src/jalview/io/AlignFile.java b/src/jalview/io/AlignFile.java index 497f0a5..cea2870 100755 --- a/src/jalview/io/AlignFile.java +++ b/src/jalview/io/AlignFile.java @@ -112,7 +112,7 @@ public abstract class AlignFile extends FileParse * @param sourceType * What type of file to read from (File, URL, Pasted String) */ - public AlignFile(String dataObject, DataSourceType sourceType) + public AlignFile(Object dataObject, DataSourceType sourceType) throws IOException { this(true, dataObject, sourceType); @@ -130,9 +130,10 @@ public abstract class AlignFile extends FileParse * What type of file to read from (File, URL) * @throws IOException */ - public AlignFile(boolean parseImmediately, String dataObject, + public AlignFile(boolean parseImmediately, Object dataObject, DataSourceType sourceType) throws IOException { + // BH allows File or String super(dataObject, sourceType); initData(); if (parseImmediately) diff --git a/src/jalview/io/AnnotationFile.java b/src/jalview/io/AnnotationFile.java index e578a45..6b02022 100755 --- a/src/jalview/io/AnnotationFile.java +++ b/src/jalview/io/AnnotationFile.java @@ -646,7 +646,7 @@ public class AnnotationFile String refSeqId = null; - public boolean annotateAlignmentView(AlignViewportI viewport, String file, + public boolean annotateAlignmentView(AlignViewportI viewport, Object file, DataSourceType protocol) { ColumnSelection colSel = viewport.getColumnSelection(); @@ -677,23 +677,23 @@ public class AnnotationFile } public boolean readAnnotationFile(AlignmentI al, HiddenColumns hidden, - String file, DataSourceType sourceType) + Object file, DataSourceType sourceType) { BufferedReader in = null; try { if (sourceType == DataSourceType.FILE) { - in = new BufferedReader(new FileReader(file)); + in = FileLoader.getBuffereReader(file); } else if (sourceType == DataSourceType.URL) { - URL url = new URL(file); + URL url = new URL(file.toString()); in = new BufferedReader(new InputStreamReader(url.openStream())); } else if (sourceType == DataSourceType.PASTE) { - in = new BufferedReader(new StringReader(file)); + in = new BufferedReader(new StringReader(file.toString())); } else if (sourceType == DataSourceType.CLASSLOADER) { diff --git a/src/jalview/io/AppletFormatAdapter.java b/src/jalview/io/AppletFormatAdapter.java index bc2465a..4c25634 100755 --- a/src/jalview/io/AppletFormatAdapter.java +++ b/src/jalview/io/AppletFormatAdapter.java @@ -174,7 +174,7 @@ public class AppletFormatAdapter if (isParseWithJMOL) { // needs a File option - alignFile = new JmolParser(inFile, sourceType); + alignFile = new JmolParser(selectedFile == null ? inFile : selectedFile, sourceType); } else { @@ -421,15 +421,22 @@ public class AppletFormatAdapter return null; } + /** * Determines the protocol (i.e DataSourceType.{FILE|PASTE|URL}) for the input * data + * + * BH 2018 allows File or String, and can return RELATIVE_URL * - * @param data + * @param dataObject File or String * @return the protocol for the input data */ - public static DataSourceType checkProtocol(String data) + public static DataSourceType checkProtocol(Object dataObject) { + if(dataObject instanceof File) + return DataSourceType.FILE; + + String data = dataObject.toString(); DataSourceType protocol = DataSourceType.PASTE; String ft = data.toLowerCase().trim(); if (ft.indexOf("http:") == 0 || ft.indexOf("https:") == 0 diff --git a/src/jalview/io/FeaturesFile.java b/src/jalview/io/FeaturesFile.java index 169da5a..0b46197 100755 --- a/src/jalview/io/FeaturesFile.java +++ b/src/jalview/io/FeaturesFile.java @@ -104,11 +104,11 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI /** * Constructor which does not parse the file immediately * - * @param file + * @param file File or String filename * @param paste * @throws IOException */ - public FeaturesFile(String file, DataSourceType paste) + public FeaturesFile(Object file, DataSourceType paste) throws IOException { super(false, file, paste); @@ -131,7 +131,7 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI * @param type * @throws IOException */ - public FeaturesFile(boolean parseImmediately, String file, + public FeaturesFile(boolean parseImmediately, Object file, DataSourceType type) throws IOException { super(parseImmediately, file, type); diff --git a/src/jalview/io/FileLoader.java b/src/jalview/io/FileLoader.java index 74c82eb..eda3899 100755 --- a/src/jalview/io/FileLoader.java +++ b/src/jalview/io/FileLoader.java @@ -41,8 +41,14 @@ import jalview.structure.StructureSelectionManager; import jalview.util.MessageManager; import jalview.ws.utils.UrlDownloadClient; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Vector; @@ -92,19 +98,15 @@ public class FileLoader implements Runnable this.raiseGUI = raiseGUI; } - public void LoadFile(AlignViewport viewport, String file, + public void LoadFile(AlignViewport viewport, Object file, DataSourceType protocol, FileFormatI format) { this.viewport = viewport; - LoadFile(file, protocol, format); - } - - public void LoadFile(AlignViewport viewport, File selectedFile, - DataSourceType protocol, FileFormatI format) - { - this.viewport = viewport; - this.selectedFile = selectedFile; - LoadFile(selectedFile.getPath(), protocol, format); + if (file instanceof File) { + this.selectedFile = (File) file; + file = selectedFile.getPath(); + } + LoadFile(file.toString(), protocol, format); } public void LoadFile(String file, DataSourceType protocol, @@ -613,4 +615,21 @@ public class FileLoader implements Runnable return tempStructFile.toString(); } + /** + * + * @param file a File, or a String which is a name of a file + * @return + * @throws FileNotFoundException + */ + @SuppressWarnings("unused") + public static BufferedReader getBuffereReader(Object file) throws FileNotFoundException { + if (file instanceof String) + return new BufferedReader(new FileReader((String) file)); + + byte[] bytes = /** @j2sNative file._bytes || */ null; + if (bytes != null) + return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes))); + return new BufferedReader(new FileReader((File) file)); + } + } diff --git a/src/jalview/io/FileParse.java b/src/jalview/io/FileParse.java index bb03b35..4df9165 100755 --- a/src/jalview/io/FileParse.java +++ b/src/jalview/io/FileParse.java @@ -40,8 +40,6 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.zip.GZIPInputStream; -import javajs.util.Rdr; - /** * implements a random access wrapper around a particular datasource, for * passing to identifyFile and AlignFile objects. @@ -59,8 +57,12 @@ public class FileParse public File inFile = null; - public byte[] bytes; // from JavaScript + private byte[] bytes; // from JavaScript + public byte[] getBytes() + { + return bytes; + } /** * a viewport associated with the current file operation. May be null. May * move to different object. @@ -308,31 +310,29 @@ public class FileParse * Create a datasource for input to Jalview. See AppletFormatAdapter for the * types of sources that are handled. * - * @param fileStr - * - datasource locator/content + * @param file + * - datasource locator/content as File or String * @param sourceType * - protocol of source * @throws MalformedURLException * @throws IOException */ - public FileParse(String fileStr, DataSourceType sourceType) - throws MalformedURLException, IOException - { - - this(null, fileStr, sourceType, false); - } - - public FileParse(File file, DataSourceType sourceType) + public FileParse(Object file, DataSourceType sourceType) throws MalformedURLException, IOException { - - this(file, file.getPath(), sourceType, true); + if (file instanceof File) + { + parse((File) file, ((File) file).getPath(), sourceType, true); + } + else + { + parse(null, file.toString(), sourceType, false); + } } - private FileParse(File file, String fileStr, DataSourceType sourceType, + private void parse(File file, String fileStr, DataSourceType sourceType, boolean isFileObject) throws MalformedURLException, IOException { - /** * @j2sNative * @@ -349,7 +349,7 @@ public class FileParse { // this will be from JavaScript inFile = file; - dataIn = new BufferedReader(new java.io.InputStreamReader(new ByteArrayInputStream(bytes))); + dataIn = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes))); dataName = fileStr; } else if (checkFileSource(fileStr)) @@ -375,18 +375,16 @@ public class FileParse } else if (sourceType == DataSourceType.RELATIVE_URL) { - String data = null; /** * BH 2018 hack for no support for access-origin * * @j2sNative * - * data = $.ajax({url:fileStr, async:false}).responseText; + * this.bytes = swingjs.JSToolkit.getFileAsBytes(fileStr); * */ - dataIn = Rdr.getBR(data); - + dataIn = new BufferedReader(new java.io.InputStreamReader(new ByteArrayInputStream(bytes))); dataName = fileStr; } diff --git a/src/jalview/io/IdentifyFile.java b/src/jalview/io/IdentifyFile.java index e2786ae..8fa76ec 100755 --- a/src/jalview/io/IdentifyFile.java +++ b/src/jalview/io/IdentifyFile.java @@ -31,9 +31,18 @@ import java.io.IOException; */ public class IdentifyFile { + + public FileFormatI identify(Object file, DataSourceType protocol) throws FileFormatException + { + // BH 2018 + return (file instanceof File ? identify((File) file, protocol) : identify((String) file, protocol)); + + } + public FileFormatI identify(File file, DataSourceType sourceType) throws FileFormatException { + // BH 2018 String emessage = "UNIDENTIFIED FILE PARSING ERROR"; FileParse parser = null; try @@ -466,4 +475,5 @@ public class IdentifyFile } } + } diff --git a/src/jalview/io/JPredFile.java b/src/jalview/io/JPredFile.java index 026c879..a944f38 100755 --- a/src/jalview/io/JPredFile.java +++ b/src/jalview/io/JPredFile.java @@ -68,6 +68,8 @@ public class JPredFile extends AlignFile /** * Creates a new JPredFile object. * + * BH allows File or String + * * @param inFile * DOCUMENT ME! * @param sourceType @@ -76,7 +78,7 @@ public class JPredFile extends AlignFile * @throws IOException * DOCUMENT ME! */ - public JPredFile(String inFile, DataSourceType sourceType) + public JPredFile(Object inFile, DataSourceType sourceType) throws IOException { super(inFile, sourceType); diff --git a/src/jalview/io/StructureFile.java b/src/jalview/io/StructureFile.java index 3025907..778c683 100644 --- a/src/jalview/io/StructureFile.java +++ b/src/jalview/io/StructureFile.java @@ -68,7 +68,7 @@ public abstract class StructureFile extends AlignFile private boolean pdbIdAvailable; - public StructureFile(String inFile, DataSourceType sourceType) + public StructureFile(Object inFile, DataSourceType sourceType) throws IOException { super(inFile, sourceType); @@ -98,7 +98,7 @@ public abstract class StructureFile extends AlignFile } - public StructureFile(boolean parseImmediately, String dataObject, + public StructureFile(boolean parseImmediately, Object dataObject, DataSourceType sourceType) throws IOException { super(parseImmediately, dataObject, sourceType); diff --git a/src/jalview/io/TCoffeeScoreFile.java b/src/jalview/io/TCoffeeScoreFile.java index fc0c913..7e963d5 100644 --- a/src/jalview/io/TCoffeeScoreFile.java +++ b/src/jalview/io/TCoffeeScoreFile.java @@ -123,9 +123,10 @@ public class TCoffeeScoreFile extends AlignFile Integer fWidth; - public TCoffeeScoreFile(String inFile, DataSourceType fileSourceType) + public TCoffeeScoreFile(Object inFile, DataSourceType fileSourceType) throws IOException { + // BH 2018 allows File or String super(inFile, fileSourceType); } diff --git a/src/jalview/jbgui/GDesktop.java b/src/jalview/jbgui/GDesktop.java index a4afb74..11a558c 100755 --- a/src/jalview/jbgui/GDesktop.java +++ b/src/jalview/jbgui/GDesktop.java @@ -115,6 +115,7 @@ public class GDesktop extends JFrame */ public GDesktop() { + super(); try { jbInit(); diff --git a/swingjs/SwingJS-site.zip b/swingjs/SwingJS-site.zip index 1f777fe1..c13c99c 100644 Binary files a/swingjs/SwingJS-site.zip and b/swingjs/SwingJS-site.zip differ