Skip to content

Commit

Permalink
Initial Attempt At Loading Factory and Event Listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
AvocadoMoon committed Sep 27, 2024
1 parent b1b657c commit b565b21
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 88 deletions.
107 changes: 107 additions & 0 deletions view-simulation-results/src/main/java/org/vcell/N5/LoadingFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package org.vcell.N5;

import ij.ImagePlus;
import org.vcell.N5.UI.ImageIntoMemory;
import org.vcell.N5.UI.N5ExportTable;
import org.vcell.N5.UI.SimLoadingEventCreator;
import org.vcell.N5.UI.SimLoadingListener;

import javax.swing.*;
import javax.swing.event.EventListenerList;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class LoadingFactory implements SimLoadingEventCreator{
private static final EventListenerList eventListenerList = new EventListenerList();

public void openN5FileDataset(ArrayList<SimResultsLoader> filesToOpen, boolean openInMemory){
N5ExportTable.enableCriticalButtons(false);
N5ExportTable.exportTableDialog.setCursor(new Cursor(Cursor.WAIT_CURSOR));
Thread openN5FileDataset = new Thread(() -> {
try{
for(SimResultsLoader simResultsLoader: filesToOpen){
simResultsLoader.createS3ClientAndReader();
ImageIntoMemory imageIntoMemory;
if (openInMemory){
ArrayList<Double> dimensions = simResultsLoader.getN5Dimensions();
imageIntoMemory = new ImageIntoMemory(dimensions.get(2), dimensions.get(3), dimensions.get(4), simResultsLoader);
imageIntoMemory.displayRangeMenu();
} else{
ImagePlus imagePlus = simResultsLoader.getImgPlusFromN5File();
imagePlus.show();
}

}
} catch (Exception ex) {
N5ExportTable.exportTableDialog.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
N5ExportTable.enableCriticalButtons(true);
throw new RuntimeException(ex);
} finally {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (!openInMemory) {
N5ExportTable.exportTableDialog.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
N5ExportTable.enableCriticalButtons(true);
}
}
});
}
});
openN5FileDataset.setName("Open N5 File");
openN5FileDataset.start();
}

public void openLocalN5FS(ArrayList<SimResultsLoader> filesToOpen){
N5ExportTable.enableCriticalButtons(true);
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setAcceptAllFileFilterUsed(false);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
N5ExportTable.exportTableDialog.setCursor(new Cursor(Cursor.WAIT_CURSOR));
Thread openN5FileDataset = new Thread(() -> {
try{
for(SimResultsLoader simResultsLoader: filesToOpen){
simResultsLoader.setSelectedLocalFile(file);
ImagePlus imagePlus = simResultsLoader.getImgPlusFromLocalN5File();
imagePlus.show();
}
} catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
N5ExportTable.exportTableDialog.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
N5ExportTable.enableCriticalButtons(true);
}
});
}
});
openN5FileDataset.start();
}
}

@Override
public void addSimLoadingListener(SimLoadingListener simLoadingListener) {
eventListenerList.add(SimLoadingListener.class, simLoadingListener);
}

@Override
public void notifySimIsLoading(SimResultsLoader simResultsLoader) {
for (SimLoadingListener simLoadingListener: eventListenerList.getListeners(SimLoadingListener.class)){
simLoadingListener.simIsLoading(simResultsLoader.rowNumber);
}
}

@Override
public void notifySimIsDoneLoading(SimResultsLoader simResultsLoader) {
for (SimLoadingListener simLoadingListener: eventListenerList.getListeners(SimLoadingListener.class)){
simLoadingListener.simFinishedLoading(simResultsLoader.rowNumber);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class N5ImageHandler implements Command {
public static N5ExportTable exportTable;
public static String exportedMetaDataPath = System.getProperty("user.home") + "/.vcell/exportMetaData.json";
private static ExportDataRepresentation.FormatExportDataRepresentation exampleJSONData;
public final static LoadingFactory loadingFactory = new LoadingFactory();

@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.google.gson.GsonBuilder;
import ij.ImagePlus;
import ij.plugin.ContrastEnhancer;
import ij.plugin.frame.ContrastAdjuster;
import net.imglib2.cache.img.CachedCellImg;
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.type.numeric.real.DoubleType;
Expand All @@ -24,14 +23,10 @@
import org.janelia.saalfeldlab.n5.imglib2.N5Utils;
import org.janelia.saalfeldlab.n5.s3.N5AmazonS3Reader;
import org.scijava.log.Logger;
import org.vcell.N5.UI.N5ExportTable;
import org.vcell.N5.UI.ImageIntoMemory;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
Expand All @@ -57,9 +52,14 @@ public class SimResultsLoader {

private static final Logger logger = N5ImageHandler.getLogger(SimResultsLoader.class);
public static AmazonS3ClientBuilder s3ClientBuilder;
public int rowNumber;

public SimResultsLoader(){

}
public SimResultsLoader(String stringURI, String userSetFileName, int rowNumber){
this(stringURI, userSetFileName);
this.rowNumber = rowNumber;
}
public SimResultsLoader(String stringURI, String userSetFileName){
uri = URI.create(stringURI);
Expand Down Expand Up @@ -164,77 +164,6 @@ public ArrayList getN5Dimensions(){
return n5AmazonS3Reader.getAttribute(dataSetChosen, "dimensions", ArrayList.class);
}


public static void openN5FileDataset(ArrayList<SimResultsLoader> filesToOpen, boolean openInMemory){
N5ExportTable.enableCriticalButtons(false);
N5ExportTable.exportTableDialog.setCursor(new Cursor(Cursor.WAIT_CURSOR));
Thread openN5FileDataset = new Thread(() -> {
try{
for(SimResultsLoader simResultsLoader: filesToOpen){
simResultsLoader.createS3ClientAndReader();
ImageIntoMemory imageIntoMemory;
if (openInMemory){
ArrayList<Double> dimensions = simResultsLoader.getN5Dimensions();
imageIntoMemory = new ImageIntoMemory(dimensions.get(2), dimensions.get(3), dimensions.get(4), simResultsLoader);
imageIntoMemory.displayRangeMenu();
} else{
ImagePlus imagePlus = simResultsLoader.getImgPlusFromN5File();
imagePlus.show();
}

}
} catch (Exception ex) {
N5ExportTable.exportTableDialog.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
N5ExportTable.enableCriticalButtons(true);
throw new RuntimeException(ex);
} finally {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (!openInMemory) {
N5ExportTable.exportTableDialog.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
N5ExportTable.enableCriticalButtons(true);
}
}
});
}
});
openN5FileDataset.setName("Open N5 File");
openN5FileDataset.start();
}

public static void openLocalN5FS(ArrayList<SimResultsLoader> filesToOpen){
N5ExportTable.enableCriticalButtons(true);
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setAcceptAllFileFilterUsed(false);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
N5ExportTable.exportTableDialog.setCursor(new Cursor(Cursor.WAIT_CURSOR));
Thread openN5FileDataset = new Thread(() -> {
try{
for(SimResultsLoader simResultsLoader: filesToOpen){
simResultsLoader.setSelectedLocalFile(file);
ImagePlus imagePlus = simResultsLoader.getImgPlusFromLocalN5File();
imagePlus.show();
}
} catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
N5ExportTable.exportTableDialog.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
N5ExportTable.enableCriticalButtons(true);
}
});
}
});
openN5FileDataset.start();
}
}

void setDataSetChosen(String dataSetChosen) {
this.dataSetChosen = dataSetChosen;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.awt.event.FocusEvent;
import java.io.IOException;

public class ImageIntoMemory extends EventListenerList implements ActionListener {
public class ImageIntoMemory implements ActionListener {
public int startC;
public int endC;
public int startT;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package org.vcell.N5.UI;

import ij.ImagePlus;
import net.imglib2.cache.img.CachedCellImg;
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.type.numeric.real.DoubleType;
import org.janelia.saalfeldlab.n5.N5FSReader;
import org.scijava.log.Logger;
import org.vcell.N5.ExportDataRepresentation;
import org.vcell.N5.N5ImageHandler;
import org.vcell.N5.SimCacheLoader;
import org.vcell.N5.SimResultsLoader;

import javax.swing.*;
Expand All @@ -24,15 +18,14 @@
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class N5ExportTable implements ActionListener, ListSelectionListener {
public class N5ExportTable implements ActionListener, ListSelectionListener, SimLoadingListener {
public static JDialog exportTableDialog;
private N5ExportTableModel n5ExportTableModel;
private ParameterTableModel parameterTableModel;
Expand Down Expand Up @@ -363,7 +356,7 @@ public void actionPerformed(ActionEvent e) {
SimResultsLoader simResultsLoader = new SimResultsLoader(uri, n5ExportTableModel.getRowData(row).savedFileName);
filesToOpen.add(simResultsLoader);
}
SimResultsLoader.openN5FileDataset(filesToOpen, e.getSource().equals(openInMemory));
N5ImageHandler.loadingFactory.openN5FileDataset(filesToOpen, e.getSource().equals(openInMemory));
} else if (e.getSource().equals(copyLink)) {
ExportDataRepresentation.SimulationExportDataRepresentation selectedRow = n5ExportTableModel.getRowData(exportListTable.getSelectedRow());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Expand All @@ -381,7 +374,7 @@ public void actionPerformed(ActionEvent e) {
SimResultsLoader simResultsLoader = new SimResultsLoader(uri, n5ExportTableModel.getRowData(row).savedFileName);
filesToOpen.add(simResultsLoader);
}
SimResultsLoader.openLocalN5FS(filesToOpen);
N5ImageHandler.loadingFactory.openLocalN5FS(filesToOpen);
}
else if (e.getSource().equals(includeExampleExports)){
if(includeExampleExports.isSelected()){
Expand Down Expand Up @@ -429,6 +422,16 @@ public void valueChanged(ListSelectionEvent e) {
parameterTable.updateUI();
}

@Override
public void simIsLoading(int itemRow) {

}

@Override
public void simFinishedLoading(int itemRow) {

}


static class ParameterTableModel extends AbstractTableModel{

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.vcell.N5.UI;

import org.vcell.N5.N5ImageHandler;
import org.vcell.N5.SimResultsLoader;

import javax.swing.*;
Expand Down Expand Up @@ -95,7 +96,7 @@ public String getS3URL(){
@Override
public void actionPerformed(ActionEvent e) {
SimResultsLoader simResultsLoader = new SimResultsLoader(getS3URL(), "");
SimResultsLoader.openN5FileDataset(new ArrayList<SimResultsLoader>(){{add(simResultsLoader);}}, N5ExportTable.openInMemory.isSelected());
N5ImageHandler.loadingFactory.openN5FileDataset(new ArrayList<SimResultsLoader>(){{add(simResultsLoader);}}, N5ExportTable.openInMemory.isSelected());
this.setVisible(false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.vcell.N5.UI;

import org.vcell.N5.SimResultsLoader;

import javax.swing.event.EventListenerList;

public interface SimLoadingEventCreator {
public void addSimLoadingListener(SimLoadingListener simLoadingListener);

public void notifySimIsLoading(SimResultsLoader simResultsLoader);

public void notifySimIsDoneLoading(SimResultsLoader simResultsLoader);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.vcell.N5.UI;

import java.util.EventListener;

public interface SimLoadingListener extends EventListener {

public void simIsLoading(int itemRow);

public void simFinishedLoading(int itemRow);

}

0 comments on commit b565b21

Please sign in to comment.