Skip to content

Commit

Permalink
finished hooking up saving and loading of analysis settings
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkb777 committed Aug 29, 2017
1 parent d91b30c commit 85a7b4c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 23 deletions.
35 changes: 18 additions & 17 deletions src/main/java/com/bwc/ora/OraUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public class OraUtils {
public static final ActionListener testAnalysisActionListener = evt -> {
try {
//read in image and ready for analysis
OraUtils.loadOctFromTiffFile(new File(OraUtils.class.getClassLoader().getResource("KS_10238_OS_L_7_90_05_529disp_reg_fr1-25_AL21p35.tif").toURI()), Oct.getInstance());
OraUtils.loadOctFromTiffFile(new File(OraUtils.class.getClassLoader().getResource("KS_10238_OS_L_7_90_05_529disp_reg_fr1-25_AL21p35.tif").toURI()),
Oct.getInstance());
Collections.getInstance().resetCollectionsForNewAnalysis();
ModelsCollection.getInstance().resetSettingsToDefault();
} catch (IOException | URISyntaxException ex) {
Expand All @@ -56,7 +57,7 @@ public class OraUtils {
public static final ActionListener newAnalysisActionListener = evt -> {
try {
//read in image and ready for analysis
File tiffFile = OraUtils.selectFile(null, JFileChooser.FILES_ONLY, "Log OCT", "TIFF file", "tiff", "tif");
File tiffFile = OraUtils.selectFile(true, null, JFileChooser.FILES_ONLY, "Log OCT", "TIFF file", "tiff", "tif");
if (tiffFile != null) {
OraUtils.loadOctFromTiffFile(tiffFile, Oct.getInstance());
Collections.getInstance().resetCollectionsForNewAnalysis();
Expand All @@ -71,7 +72,7 @@ public class OraUtils {
public static final ActionListener exportAnalysisActionListener = evt -> {
try {
//read in image and ready for analysis
File exportDir = OraUtils.selectFile(null, JFileChooser.DIRECTORIES_ONLY, "Analysis export directory", null);
File exportDir = OraUtils.selectFile(true, null, JFileChooser.DIRECTORIES_ONLY, "Analysis export directory", null);
if (exportDir != null) {
ExportWriter.exportAnalysis(exportDir);
}
Expand All @@ -87,7 +88,8 @@ public static void loadOctFromTiffFile(File octFile, Oct oct) throws IOException
oct.setLogOctImage(TiffReader.readTiffImage(octFile));
}

public static File selectFile(Component parent, int selectorType, String selectDescription, String extensionDescription, String... extentions) {
public static File selectFile(boolean openDialog, Component parent, int selectorType, String selectDescription, String extensionDescription,
String... extentions) {
File prevLocation = fc.getSelectedFile() != null ? fc.getSelectedFile().getParentFile() : null;

fc = new JFileChooser(prevLocation);
Expand All @@ -99,13 +101,11 @@ public static File selectFile(Component parent, int selectorType, String selectD
fc.setAcceptAllFileFilterUsed(false);
fc.setApproveButtonText("Select");
fc.setDialogTitle("Select " + selectDescription + "...");
if (fc.getSelectedFile() != null && fc.getSelectedFile().isFile()) {
fc.setCurrentDirectory(fc.getSelectedFile().getParentFile());
}
int returnVal = fc.showOpenDialog(parent);
int returnVal = openDialog ? fc.showOpenDialog(parent) : fc.showSaveDialog(parent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
if (selectorType == JFileChooser.FILES_ONLY) {
return loadFile(fc.getSelectedFile());
System.out.println("pwd: "+fc.getCurrentDirectory());
return openDialog ? loadFile(fc.getSelectedFile()) : fc.getSelectedFile();
} else if (selectorType == JFileChooser.DIRECTORIES_ONLY) {
return loadDir(fc.getSelectedFile());
} else {
Expand All @@ -117,13 +117,12 @@ public static File selectFile(Component parent, int selectorType, String selectD
}

private static File loadFile(File file) {
if (file.exists()) {
if (!file.isFile()) {
throw new IllegalArgumentException("Supplied input file " + file.getAbsolutePath() + " is not a file!");
}
} else {
if (!file.exists()) {
throw new IllegalArgumentException("Supplied input file does not exist @ " + file.getAbsolutePath() + ".");
}
if (!file.isFile()) {
throw new IllegalArgumentException("Supplied input file " + file.getAbsolutePath() + " is not a file!");
}
return file;
}

Expand Down Expand Up @@ -168,7 +167,9 @@ public void mouseClicked(MouseEvent e) {
));
lrpCollection.setSelectedIndex(0);
lrpPanel.removeMouseListener(this);
if (buttonToEnable != null) buttonToEnable.setEnabled(true);
if (buttonToEnable != null) {
buttonToEnable.setEnabled(true);
}
}
}
});
Expand Down Expand Up @@ -208,8 +209,8 @@ public static void setLrpsForAnalysis() {
}

boolean illegalPositionLrps = lrps.stream()
.anyMatch(lrp -> lrp.getLrpCenterXPosition() - ((lrpSettings.getLrpWidth() - 1) / 2) < 0
|| lrp.getLrpCenterXPosition() + ((lrpSettings.getLrpWidth() - 1) / 2) >= octWidth);
.anyMatch(lrp -> lrp.getLrpCenterXPosition() - ((lrpSettings.getLrpWidth() - 1) / 2) < 0
|| lrp.getLrpCenterXPosition() + ((lrpSettings.getLrpWidth() - 1) / 2) >= octWidth);

if (illegalPositionLrps) {
throw new IllegalArgumentException("Combination of LRP width, the number of LRPs and LRP " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ModelsCollection {
private final LrpSettings lrpSettings = new LrpSettings();
private final OctSettings octSettings = new OctSettings();
private final DisplaySettings displaySettings = new DisplaySettings();
private final ScaleBar scaleBar = new ScaleBar("scale_bars", 100);
private transient final ScaleBar scaleBar = new ScaleBar("scale_bars", 100);
private final AnalysisSettings analysisSettings = new AnalysisSettings();

private ModelsCollection() {
Expand Down
47 changes: 44 additions & 3 deletions src/main/java/com/bwc/ora/io/SettingsIOUtils.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,60 @@
package com.bwc.ora.io;

import com.bwc.ora.OraUtils;
import com.bwc.ora.collections.Collections;
import com.bwc.ora.collections.ModelsCollection;
import com.bwc.ora.models.Oct;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.io.*;

public class SettingsIOUtils {

public static final String SETTINGS_FILE_EXTENSION = "oraconf";
public static final String ORA_ANALYSIS_SETTINGS_FILE_EXTENSION = "oasf";

public static final ActionListener loadAnalysisSettingsActionListener = evt -> {
try {
//read in settings file for analysis
File osfFile = OraUtils.selectFile(true, null, JFileChooser.FILES_ONLY, "ORA Analysis Settings", ORA_ANALYSIS_SETTINGS_FILE_EXTENSION + " file",
ORA_ANALYSIS_SETTINGS_FILE_EXTENSION);
if (osfFile != null) {
SettingsIOUtils.loadSettings(osfFile);
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "ORA Analysis Setting loading failed,"
+ " reason: " + ex.getMessage(), "Loading error!", JOptionPane.ERROR_MESSAGE);
}
};

public static final ActionListener saveAnalysisSettingsActionListener = evt -> {
try {
//read in settings file for analysis
File osfFile = OraUtils.selectFile(false, null, JFileChooser.FILES_ONLY, "Save Analysis Settings", ORA_ANALYSIS_SETTINGS_FILE_EXTENSION + " file",
ORA_ANALYSIS_SETTINGS_FILE_EXTENSION);
if (osfFile != null) {
if (!osfFile.getName().endsWith(ORA_ANALYSIS_SETTINGS_FILE_EXTENSION)) {
osfFile = new File(osfFile.getParentFile(), osfFile.getName() + "." + ORA_ANALYSIS_SETTINGS_FILE_EXTENSION);
}
SettingsIOUtils.saveCurrentSettings(osfFile);
} else {
throw new IOException("No file entered");
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "ORA Analysis Setting saving failed,"
+ " reason: " + ex.getMessage(), "Save error!", JOptionPane.ERROR_MESSAGE);
}
};

public static void saveCurrentSettings(File analysisSettingsFile) throws IOException {
try (PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(analysisSettingsFile)))) {
System.out.println("Saving to " + analysisSettingsFile.getAbsolutePath());
try (PrintWriter pw = new PrintWriter(analysisSettingsFile)) {
Gson jsonFormatter = new GsonBuilder().setPrettyPrinting().create();
pw.print(jsonFormatter.toJson(ModelsCollection.getInstance()));
String json = jsonFormatter.toJson(ModelsCollection.getInstance());
System.out.println("Settings JSON:\n" + json);
pw.println(json);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/bwc/ora/models/LrpSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class LrpSettings {

private transient final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

private final Integer[] lrpWidthOptions = new Integer[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 };
private final int[] lrpSmoothingRange = new int[] { 0, 49 };
private transient final Integer[] lrpWidthOptions = new Integer[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 };
private transient final int[] lrpSmoothingRange = new int[] { 0, 49 };
private int lrpWidth = 5;
public static final String PROP_LRP_WIDTH = "lrpWidth";
private int lrpHeight = 300;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/bwc/ora/views/menus/SettingsMenu.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bwc.ora.views.menus;

import com.bwc.ora.collections.ModelsCollection;
import com.bwc.ora.io.SettingsIOUtils;
import com.bwc.ora.models.DisplaySettings;

import javax.swing.*;
Expand All @@ -14,6 +15,8 @@ public class SettingsMenu extends JMenu {
private JCheckBoxMenuItem displayFileNameCheckBox = new JCheckBoxMenuItem("Display OCT File Name", true);
private JCheckBoxMenuItem displayLrpPeaksCheckBox = new JCheckBoxMenuItem("Display LRP Peaks", true);
private JCheckBoxMenuItem showFwhmCheckBox = new JCheckBoxMenuItem("Display FWHM on LRP", false);
private JMenuItem loadAnalysisSettingsMenuItem = new JMenuItem("Load Analysis Settings");
private JMenuItem saveAnalysisSettingsMenuItem = new JMenuItem("Save Analysis Settings");

public SettingsMenu() {
setText("Settings");
Expand All @@ -30,8 +33,17 @@ private void initSettingsMenu() {
add(displayScaleBarsCheckBox);
add(displayLrpPeaksCheckBox);
add(showFwhmCheckBox);
addSeparator();
add(saveAnalysisSettingsMenuItem);
add(loadAnalysisSettingsMenuItem);

connectSettingsToModel();
registerItemListeners();
}

private void registerItemListeners() {
saveAnalysisSettingsMenuItem.addActionListener(SettingsIOUtils.saveAnalysisSettingsActionListener);
loadAnalysisSettingsMenuItem.addActionListener(SettingsIOUtils.loadAnalysisSettingsActionListener);
}

private void connectSettingsToModel() {
Expand Down

0 comments on commit 85a7b4c

Please sign in to comment.