Skip to content

Commit

Permalink
Make Okay Button Context Aware
Browse files Browse the repository at this point in the history
  • Loading branch information
AvocadoMoon committed Nov 13, 2024
1 parent 7261ca7 commit 0e4b7dc
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.Enumeration;

public class MainPanel {
private static JFrame exportTableDialog;
public static JFrame exportTableDialog;

public final static ControlButtonsPanel controlButtonsPanel = new ControlButtonsPanel();
public final static N5ExportTable n5ExportTable = new N5ExportTable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
Expand All @@ -20,36 +21,35 @@ public class DataReductionGUI extends JPanel implements ActionListener {
private JComboBox<String> chosenImage;
private final JCheckBox selectRangeOfMeasurement = new JCheckBox("Select Measurement Range: ");
private final JCheckBox normalizeMeasurement = new JCheckBox("Normalize Measurement: ");


private final JDialog jDialog;
private final JOptionPane pane;
private final JDialog jDialog = new JDialog(MainPanel.exportTableDialog, true);
private final JButton okayButton = new JButton("Okay");
private final JButton cancelButton = new JButton("Cancel");
private File chosenFile;

private final ArrayList<SimResultsLoader> filesToOpen;

public int mainGUIReturnValue;
public int fileChooserReturnValue;

private final SelectSimRange selectSimRange;
private final RoiSelection roiSelection;
private final NormalizeGUI normalizeGUI;
private final SelectMeasurements selectMeasurements;

private boolean continueWithProcess = false;

private final Border lowerEtchedBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);

public DataReductionGUI(ArrayList<SimResultsLoader> filesToOpen, double simCSize, double simZSize, double simTSize){
this.filesToOpen = filesToOpen;
this.filesToOpen = filesToOpen;
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
okayButton.setEnabled(false);

pane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
jDialog = pane.createDialog("Measurement Script");
jDialog.setResizable(true);

selectSimRange = new SelectSimRange(jDialog, simCSize, simZSize, simTSize);
roiSelection = new RoiSelection();
roiSelection = new RoiSelection(this);
normalizeGUI = new NormalizeGUI(jDialog, simTSize);
selectMeasurements = new SelectMeasurements();
selectMeasurements = new SelectMeasurements(this);

JPanel imagesToMeasurePanel = new JPanel();
imagesToMeasurePanel.setLayout(new BoxLayout(imagesToMeasurePanel, BoxLayout.Y_AXIS));
Expand All @@ -63,28 +63,23 @@ public DataReductionGUI(ArrayList<SimResultsLoader> filesToOpen, double simCSize
add(displayOptionsPanel());
add(normalizeGUI);
add(selectSimRange);
add(okayCancelPanel());

setVisible(true);

okayButton.addActionListener(this);
cancelButton.addActionListener(this);
normalizeMeasurement.addActionListener(this);
selectRangeOfMeasurement.addActionListener(this);

jDialog.pack();
}
this.setBorder(new EmptyBorder(15, 12, 15, 12));

public DataReductionSubmission displayGUI(){
jDialog.add(this);
jDialog.setVisible(true);
mainGUIReturnValue = (Integer) pane.getValue();
if (mainGUIReturnValue == JOptionPane.OK_OPTION){
JFileChooser saveToFile = new JFileChooser();
saveToFile.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooserReturnValue = saveToFile.showDialog(this, "Save Results To File");
if (fileChooserReturnValue == JFileChooser.APPROVE_OPTION){
chosenFile = saveToFile.getSelectedFile();
MainPanel.controlButtonsPanel.enableCriticalButtons(false);
return createSubmission();
}
}
return null;
jDialog.setResizable(true);
jDialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
jDialog.setTitle("Measurement Script");
jDialog.pack();
}

public DataReductionSubmission createSubmission(){
Expand All @@ -97,6 +92,13 @@ public DataReductionSubmission createSubmission(){
selectSimRange.getRangeOfSim(), selectMeasurements.getChosenMeasurements());
}

private JPanel okayCancelPanel(){
JPanel jPanel = new JPanel();
jPanel.add(okayButton);
jPanel.add(cancelButton);
return jPanel;
}

private JPanel imageSelectionPanel(){
JPanel jPanel = new JPanel(new GridLayout(1, 2));
jPanel.add(new JLabel("Experimental"));
Expand Down Expand Up @@ -129,15 +131,39 @@ private JPanel displayOptionsPanel(){
return jPanel;
}

public void activateOkayButton(){
boolean selectedAMeasurement = !selectMeasurements.getChosenMeasurements().isEmpty();
boolean chosenExperimentImage = chosenImage.getSelectedItem() != null;
boolean roisIsSelected = !roiSelection.getImageROIList().isEmpty() && !roiSelection.getSimROIList().isEmpty();
okayButton.setEnabled(selectedAMeasurement && chosenExperimentImage && roisIsSelected);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(normalizeMeasurement)) {
normalizeGUI.setVisible(normalizeMeasurement.isSelected());
} else if (e.getSource().equals(selectRangeOfMeasurement)){
selectSimRange.setVisible(selectRangeOfMeasurement.isSelected());
} else if (e.getSource().equals(okayButton)) {
JFileChooser saveToFile = new JFileChooser();
saveToFile.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooserReturnValue = saveToFile.showDialog(this, "Save Results To File");
if (fileChooserReturnValue == JFileChooser.APPROVE_OPTION){
chosenFile = saveToFile.getSelectedFile();
MainPanel.controlButtonsPanel.enableCriticalButtons(false);
DataReductionWriter.createDataReductionProcess(createSubmission());
continueWithProcess = true;
jDialog.dispose();
}
} else if (e.getSource().equals(cancelButton)) {
jDialog.dispose();
}
}

public boolean shouldContinueWithProcess() {
return continueWithProcess;
}

public static class DataReductionSubmission{
public final boolean normalizeMeasurementsBool;
public final ArrayList<Roi> arrayOfSimRois;
Expand Down Expand Up @@ -176,7 +202,6 @@ public DataReductionSubmission(boolean normalizeMeasurementsBool, ArrayList<Roi>

public static void main(String[] args) {
DataReductionGUI dataReductionGUI = new DataReductionGUI(new ArrayList<>(), 0, 0, 0);
dataReductionGUI.displayGUI();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
class RoiSelection extends JPanel {
private final ROIDataModel imageTableModel = new ROIDataModel();
private final ROIDataModel simTableModel = new ROIDataModel();
private final DataReductionGUI parentGUI;

public RoiSelection(){
public RoiSelection(DataReductionGUI parentGUI){
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

JList<String> imageROITable = new JList<>(imageTableModel);
Expand All @@ -25,6 +26,7 @@ public RoiSelection(){
JFileChooser simROIFileChooser = new JFileChooser();
this.add(createROIInput(simROITable, simTableModel, simROIFileChooser, "Sim"));
this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "ROI Files"));
this.parentGUI = parentGUI;
}

private JPanel createROIInput(JList<String> jList, ROIDataModel roiDataModel,
Expand All @@ -43,6 +45,7 @@ public void actionPerformed(ActionEvent e) {
for (Roi roi : roiList){
roiDataModel.addRow(roi);
}
parentGUI.activateOkayButton();
jList.updateUI();
}
});
Expand All @@ -53,6 +56,7 @@ public void actionPerformed(ActionEvent e) {
for (int roiIndex: jList.getSelectedIndices()){
roiDataModel.removeRow(roiIndex);
}
parentGUI.activateOkayButton();
jList.updateUI();
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
package org.vcell.N5.reduction;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.util.ArrayList;

class SelectMeasurements extends JPanel{
class SelectMeasurements extends JPanel implements ListSelectionListener {
private final JList<String> chosenMeasurement;
private final MeasurementsDataModel measurementsDataModel = new MeasurementsDataModel();
private final DataReductionGUI parentGUI;

public SelectMeasurements(){
public SelectMeasurements(DataReductionGUI parentGUI){
setLayout(new GridLayout(1, 1));
this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Measurement Type"));
chosenMeasurement = new JList<>(measurementsDataModel);
chosenMeasurement.setVisibleRowCount(3);
chosenMeasurement.addListSelectionListener(this);
JScrollPane jScrollPane = new JScrollPane(chosenMeasurement);
this.add(jScrollPane);
this.parentGUI = parentGUI;
}

public ArrayList<AvailableMeasurements> getChosenMeasurements(){
return measurementsDataModel.getSelectedMeasurements(chosenMeasurement.getSelectedIndices());
}

@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getSource().equals(chosenMeasurement)){
parentGUI.activateOkayButton();
}
}

public enum AvailableMeasurements{
AVERAGE("Average"),
STD_DEV("Standard Deviation");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,11 @@ public void openN5FileDataset(ArrayList<SimResultsLoader> filesToOpen, boolean o
ArrayList<Double> dimensions = firstSim.getN5Dimensions();
if (dataReduction){
dataReductionGUI = new DataReductionGUI(filesToOpen, dimensions.get(2), dimensions.get(3), dimensions.get(4));
DataReductionGUI.DataReductionSubmission submission = dataReductionGUI.displayGUI();
if (submission != null){
DataReductionWriter.createDataReductionProcess(submission);
}
} else {
rangeSelector.displayRangeMenu(dimensions.get(2), dimensions.get(3), dimensions.get(4));
}
}
boolean dataReductionOkay = dataReduction && dataReductionGUI.mainGUIReturnValue == JOptionPane.OK_OPTION && dataReductionGUI.fileChooserReturnValue == JFileChooser.APPROVE_OPTION;
boolean dataReductionOkay = dataReduction && dataReductionGUI.shouldContinueWithProcess();
if (dataReductionOkay || !dataReduction){
controlButtonsPanel.allowCancel(true);
MainPanel.changeCursor(new Cursor(Cursor.WAIT_CURSOR));
Expand Down

0 comments on commit 0e4b7dc

Please sign in to comment.