Skip to content

Commit

Permalink
fix opening files using drap and drop
Browse files Browse the repository at this point in the history
  • Loading branch information
pgdurand committed Mar 4, 2021
1 parent 4386502 commit 031de47
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 127 deletions.
89 changes: 7 additions & 82 deletions src/bzh/plealog/blastviewer/actions/main/OpenFileAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,14 @@

import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Enumeration;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JComponent;

import com.plealog.genericapp.api.EZEnvironment;
import com.plealog.genericapp.api.file.EZFileManager;
import com.plealog.genericapp.api.log.EZLogger;

import bzh.plealog.bioinfo.api.data.searchresult.SRIteration;
import bzh.plealog.bioinfo.api.data.searchresult.SROutput;
import bzh.plealog.blastviewer.resources.BVMessages;
import bzh.plealog.blastviewer.util.BlastViewerOpener;
import bzh.plealog.blastviewer.util.FileLoadRunner;

/**
* This class implements the action to load a Blast XML results from a file.
Expand Down Expand Up @@ -63,82 +57,13 @@ public OpenFileAction(String name, Icon icon) {
super(name, icon);
}

private class Loader extends Thread {
private void doAction() {
//EZFileManager.useOSNativeFileDialog(true);
File[] fs = EZFileManager.chooseFilesForOpenAction(BVMessages
.getString("OpenFileAction.lbl"));
if (fs == null)// user canceled dlg box
return;

EZEnvironment.setWaitCursor();

EZLogger.info(BVMessages
.getString("OpenFileAction.msg1"));
SROutput sro, sroMaster=null;
int notLoadedFiles=0, ncount=0;
for (File f:fs) {
ncount++;
BlastViewerOpener.setHelperMessage(BVMessages
.getString("OpenFileAction.msg1")+ncount+"/"+fs.length);
sro = BlastViewerOpener.readBlastFile(f);
if (sro==null) {
notLoadedFiles++;
EZLogger.warn(
BVMessages.getString("OpenFileAction.err") +
f.getAbsolutePath());
continue;
}
if (sroMaster == null) {
sroMaster = sro;
}
else {
Enumeration<SRIteration> sriEnum = sro.enumerateIteration();
while(sriEnum.hasMoreElements()) {
sroMaster.addIteration(sriEnum.nextElement());
}
}
}

EZLogger.info(String.format(
BVMessages.getString("OpenFileAction.msg4"), fs.length));

if (sroMaster!=null) {
if(notLoadedFiles!=0) {
EZEnvironment.displayInfoMessage(EZEnvironment.getParentFrame(),
BVMessages.getString("OpenFileAction.msg2") );
}
BlastViewerOpener.setHelperMessage(BVMessages
.getString("FetchFromNcbiAction.msg4"));

JComponent viewer = BlastViewerOpener.prepareViewer(sroMaster);

BlastViewerOpener.displayInternalFrame(viewer, fs[0].getName(), null);
EZLogger.info(BVMessages.getString("OpenFileAction.msg5"));
}
else {
EZEnvironment.displayInfoMessage(EZEnvironment.getParentFrame(),
BVMessages.getString("OpenFileAction.msg3") );
}

System.gc();
}
public void run() {
try {
doAction();
} catch (Throwable t) {
EZLogger.warn(
BVMessages.getString("OpenFileAction.err") +
t.toString());
} finally {
BlastViewerOpener.cleanHelperMessage();
EZEnvironment.setDefaultCursor();
}
}
}

public void actionPerformed(ActionEvent event) {
new Loader().start();
//EZFileManager.useOSNativeFileDialog(true);
File[] fs = EZFileManager.chooseFilesForOpenAction(BVMessages
.getString("OpenFileAction.lbl"));
if (fs == null)// user canceled dlg box
return;
new FileLoadRunner(fs).start();
}

}
47 changes: 2 additions & 45 deletions src/bzh/plealog/blastviewer/util/BlastTransferHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
import javax.swing.JComponent;
import javax.swing.TransferHandler;

import bzh.plealog.blastviewer.BlastViewerPanel;
import bzh.plealog.blastviewer.resources.BVMessages;

import com.plealog.genericapp.api.EZEnvironment;
import com.plealog.genericapp.api.log.EZLogger;

Expand Down Expand Up @@ -61,7 +58,8 @@ public boolean importData(JComponent c, Transferable t) {
try {
lFiles = (List<?>) t.getTransferData(_fileFlavor);
EZEnvironment.setWaitCursor();
new Fetcher(lFiles).start();
//new Fetcher(lFiles).start();
new FileLoadRunner(lFiles.toArray(new File[0])).start();
} catch (Exception e) {
EZLogger.warn("DragNDrop error: " + e);
}
Expand Down Expand Up @@ -89,45 +87,4 @@ private boolean hasFileFlavor(DataFlavor[] flavors) {
return false;
}

/**
* Load files in an external thread to avoid a UI lock.
*/
private class Fetcher extends Thread {
private List<?> lFiles;

public Fetcher(List<?> lFiles) {
this.lFiles = lFiles;
}

private void doAction(){
BlastViewerPanel viewer;
File f;
String msg;
int i, size;

size = lFiles.size();
for (i = 0; i < size; i++) {
f = (File) lFiles.get(i);
msg = String.format(BVMessages.getString("BlastTransferHandler.msg1"), f.getName());
EZLogger.info(msg);
BlastViewerOpener.setHelperMessage(msg);
viewer = new BlastViewerPanel();
viewer.setContent(BlastViewerOpener.readBlastFile(f));
BlastViewerOpener.displayInternalFrame(viewer, f.getName(), null);
}
}
public void run() {
try {
doAction();
} catch (Throwable t) {
EZLogger.warn(BVMessages.getString("OpenFileAction.err")
+ t.toString());
} finally {
EZEnvironment.setDefaultCursor();
BlastViewerOpener.cleanHelperMessage();
}
}

}

}
107 changes: 107 additions & 0 deletions src/bzh/plealog/blastviewer/util/FileLoadRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* Copyright (C) 2003-2021 Patrick G. Durand
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You may obtain a copy of the License at
*
* https://www.gnu.org/licenses/agpl-3.0.txt
*
* This program 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 Affero General Public License for more details.
*/
package bzh.plealog.blastviewer.util;

import java.io.File;
import java.util.Enumeration;

import javax.swing.JComponent;

import com.plealog.genericapp.api.EZEnvironment;
import com.plealog.genericapp.api.log.EZLogger;

import bzh.plealog.bioinfo.api.data.searchresult.SRIteration;
import bzh.plealog.bioinfo.api.data.searchresult.SROutput;
import bzh.plealog.blastviewer.resources.BVMessages;

/**
* Utility class to open BLAST and PLAST data files.
*
* @author Patrick G. Durand
*/
public class FileLoadRunner extends Thread {
private File[] fs;

public FileLoadRunner(File[] fs) {
this.fs = fs;
}
private void doAction() {
EZEnvironment.setWaitCursor();

EZLogger.info(BVMessages
.getString("OpenFileAction.msg1"));
SROutput sro, sroMaster=null;
int notLoadedFiles=0, ncount=0;
for (File f:fs) {
ncount++;
BlastViewerOpener.setHelperMessage(BVMessages
.getString("OpenFileAction.msg1")+ncount+"/"+fs.length);
sro = BlastViewerOpener.readBlastFile(f);
if (sro==null) {
notLoadedFiles++;
EZLogger.warn(
BVMessages.getString("OpenFileAction.err") +
f.getAbsolutePath());
continue;
}
if (sroMaster == null) {
sroMaster = sro;
}
else {
Enumeration<SRIteration> sriEnum = sro.enumerateIteration();
while(sriEnum.hasMoreElements()) {
sroMaster.addIteration(sriEnum.nextElement());
}
}
}

EZLogger.info(String.format(
BVMessages.getString("OpenFileAction.msg4"), fs.length));

if (sroMaster!=null) {
if(notLoadedFiles!=0) {
EZEnvironment.displayInfoMessage(EZEnvironment.getParentFrame(),
BVMessages.getString("OpenFileAction.msg2") );
}
BlastViewerOpener.setHelperMessage(BVMessages
.getString("FetchFromNcbiAction.msg4"));

JComponent viewer = BlastViewerOpener.prepareViewer(sroMaster);

BlastViewerOpener.displayInternalFrame(viewer, fs[0].getName(), null);
EZLogger.info(BVMessages.getString("OpenFileAction.msg5"));
}
else {
EZEnvironment.displayInfoMessage(EZEnvironment.getParentFrame(),
BVMessages.getString("OpenFileAction.msg3") );
}

System.gc();
}
public void run() {
try {
doAction();
} catch (Throwable t) {
EZLogger.warn(
BVMessages.getString("OpenFileAction.err") +
t.toString());
} finally {
BlastViewerOpener.cleanHelperMessage();
EZEnvironment.setDefaultCursor();
}
}
}

0 comments on commit 031de47

Please sign in to comment.