Skip to content

Commit

Permalink
Basic Table in GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
AvocadoMoon committed Jan 19, 2024
1 parent 6713daa commit 6ef459f
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 20 deletions.
30 changes: 14 additions & 16 deletions src/main/java/org/vcell/vcellfiji/N5ImageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -152,6 +153,7 @@ protected void done() {
// https://stackoverflow.com/questions/16937997/java-swingworker-thread-to-update-main-gui
// Why swing updating does not work


}

private void enableCriticalButtons(boolean enable) {
Expand All @@ -160,14 +162,15 @@ private void enableCriticalButtons(boolean enable) {
vGui.localFiles.setEnabled(enable);
vGui.remoteFiles.setEnabled(enable);
vGui.mostRecentExport.setEnabled(enable);
vGui.exportTableButton.setEnabled(enable);
}

@Override
public void run() {
this.vGui = new N5ViewerGUI();
this.vGui.localFileDialog.addActionListener(this);

this.vGui.okayButton.addActionListener(this);
// this.vGui.exportTableButton.addActionListener(this);

this.vGui.remoteFileSelection.submitS3Info.addActionListener(this);
this.vGui.mostRecentExport.addActionListener(this);
Expand Down Expand Up @@ -335,21 +338,16 @@ public static void main(String[] args) {
n5ImageHandler.run();
}

public HashMap<String, Object> getJsonData(){
try{
File jsonFile = new File(System.getProperty("user.home") + "/.vcell", "exportMetaData.json");
if (jsonFile.exists() && jsonFile.length() != 0){
HashMap<String, Object> jsonHashMap;
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Type type = new TypeToken<HashMap<String, Object>>() {}.getType();
jsonHashMap = gson.fromJson(new FileReader(jsonFile.getAbsolutePath()), type);
return jsonHashMap;
}
return null;
}
catch (Exception e){
logService.error("Failed to read export metadata JSON:", e);
return null;
public static HashMap<String, Object> getJsonData() throws FileNotFoundException {
File jsonFile = new File(System.getProperty("user.home") + "/.vcell", "exportMetaData.json");
if (jsonFile.exists() && jsonFile.length() != 0){
HashMap<String, Object> jsonHashMap;
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Type type = new TypeToken<HashMap<String, Object>>() {}.getType();
jsonHashMap = gson.fromJson(new FileReader(jsonFile.getAbsolutePath()), type);
return jsonHashMap;
}
return null;

}
}
118 changes: 114 additions & 4 deletions src/main/java/org/vcell/vcellfiji/UI/N5ViewerGUI.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package org.vcell.vcellfiji.UI;

import org.vcell.vcellfiji.N5ImageHandler;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class N5ViewerGUI extends JFrame implements ActionListener {
public JButton localFiles;
Expand All @@ -16,11 +22,15 @@ public class N5ViewerGUI extends JFrame implements ActionListener {
private JScrollPane resultsScrollPane;
public JButton remoteFiles;
public JButton okayButton;
public JButton exportTableButton;
public JCheckBox openMemoryCheckBox;
private JPanel datasetListPanel;
private JLabel datasetLabel;
private JLabel openInMemory;
public int jFileChooserResult;
private JDialog exportTableDialog;
private JPanel exportTablePanel;
private DefaultTableModel exportTableModel;

public JButton mostRecentExport;

Expand All @@ -41,7 +51,7 @@ public N5ViewerGUI() {
mainPanelConstraints.gridy = 0;
localFiles = new JButton();
localFiles.setText("Local Files");
mainPanel.add(localFiles, mainPanelConstraints);
// mainPanel.add(localFiles, mainPanelConstraints);

mainPanelConstraints.gridy = 0;
mainPanelConstraints.gridx = 1;
Expand All @@ -57,12 +67,18 @@ public N5ViewerGUI() {

mainPanelConstraints.gridy = 0;
mainPanelConstraints.gridx = 3;
exportTableButton = new JButton();
exportTableButton.setText("Export Table");
mainPanel.add(exportTableButton, mainPanelConstraints);

mainPanelConstraints.gridy = 0;
mainPanelConstraints.gridx = 4;
openInMemory = new JLabel();
openInMemory.setText("Open Image in Memory");
mainPanel.add(openInMemory, mainPanelConstraints);

mainPanelConstraints.gridy = 0;
mainPanelConstraints.gridx = 4;
mainPanelConstraints.gridx = 5;
openMemoryCheckBox = new JCheckBox();
mainPanel.add(openMemoryCheckBox, mainPanelConstraints);

Expand All @@ -82,7 +98,7 @@ public N5ViewerGUI() {
resultsScrollPane = new JScrollPane(datasetList);
datasetListPanel.add(resultsScrollPane, datasetConstraints);

mainPanelConstraints.gridwidth = 4;
mainPanelConstraints.gridwidth = 5;
mainPanelConstraints.gridy = 1;
mainPanelConstraints.ipady = 40;
mainPanelConstraints.gridx = 0;
Expand All @@ -94,7 +110,7 @@ public N5ViewerGUI() {
mainPanelConstraints = new GridBagConstraints();
mainPanelConstraints.gridy = 2;
mainPanelConstraints.gridx = 0;
mainPanelConstraints.gridwidth = 4;
mainPanelConstraints.gridwidth = 5;
okayButton = new JButton();
okayButton.setText("Open Dataset");
mainPanel.add(okayButton, mainPanelConstraints);
Expand All @@ -103,6 +119,7 @@ public N5ViewerGUI() {
localFiles.addActionListener(this);

remoteFiles.addActionListener(this);
exportTableButton.addActionListener(this);

// listener for if credentials or endpoint is used

Expand Down Expand Up @@ -135,7 +152,100 @@ public void actionPerformed(ActionEvent e) {
jFileChooserResult = localFileDialog.showOpenDialog(thisJFrame);
} else if (e.getSource() == remoteFiles) {
remoteFileSelection.setVisible(true);
} else if (e.getSource() == exportTableButton) {
displayExportTable();
}
}

public void refreshDataList(){

}

public void updateTableModel(){
try{
HashMap<String, Object> jsonData = N5ImageHandler.getJsonData();
if (jsonData != null){
List<String> set = (ArrayList<String>) jsonData.get("jobIDs");
String lastElement = exportTableModel.getRowCount() == 0 ? null: (String) exportTableModel.getValueAt(0,0);
for(int i = set.size() - 1; i > -1; i--){
if(lastElement != null && lastElement.equals(set.get(i))){
break;
}
addRowFromJson(jsonData, set.get(i));
}
}
exportTableModel.fireTableDataChanged();
}
catch (Exception e){
throw new RuntimeException(e);
}
}

public void initalizeTableData(){
HashMap<String, Object> jsonData = null;
try {
jsonData = N5ImageHandler.getJsonData();
if (jsonData != null){
List<String> set = (ArrayList<String>) jsonData.get("jobIDs");
for (String s : set) {
addRowFromJson(jsonData, s);
}
}
exportTableModel.fireTableDataChanged();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}

public void addRowFromJson(HashMap<String, Object> jsonData, String s){
exportTableModel.addRow(new Object[]{""});
}

public void displayExportTable(){
if (exportTableDialog == null){
exportTablePanel = new JPanel();

DefaultTableModel defaultTableModel = new DefaultTableModel();
defaultTableModel.addColumn("Test");
defaultTableModel.addColumn("Test2");
Object[] testRow = {"k", "b"};
defaultTableModel.addRow(testRow);

JTable jTable = new JTable(defaultTableModel);
JScrollPane jScrollPane = new JScrollPane(jTable);

jScrollPane.setSize(500, 400);
jScrollPane.setPreferredSize(new Dimension(500, 400));
jScrollPane.setMinimumSize(new Dimension(500, 400));

JLabel jLabel = new JLabel("Recent Exports. List is volatile save important export metadata elsewhere.");
JButton refresh = new JButton("Refresh List");
JButton open = new JButton("Open");

JPanel topBar = new JPanel();
topBar.setLayout(new FlowLayout());
refresh.addActionListener(this);
open.addActionListener(this);
topBar.add(jLabel);
topBar.add(open);
topBar.add(refresh);

exportTablePanel.setLayout(new BorderLayout());
exportTablePanel.add(topBar, BorderLayout.NORTH);
exportTablePanel.add(jScrollPane);

exportTablePanel.setPreferredSize(new Dimension(800, 500));
JOptionPane pane = new JOptionPane(exportTablePanel, JOptionPane.PLAIN_MESSAGE, 0, null, new Object[] {"Close"});
exportTableDialog = pane.createDialog("VCell Exports");
exportTableDialog.setModal(false);
exportTableDialog.setResizable(true);
exportTableDialog.setVisible(true);
}
else {
exportTableDialog.setVisible(true);
}
}


}

0 comments on commit 6ef459f

Please sign in to comment.