use of edu.cmu.tetrad.algcomparison.simulation.LoadContinuousDataAndGraphs in project tetrad by cmu-phil.
the class SimulationEditor method getFileMenu.
private void getFileMenu(final JMenu fileMenu, final Simulation simulation, final GraphSelectionEditor graphEditor, final DataEditor dataEditor, final JTabbedPane tabbedPane, final String[] simulationItems) {
JMenuItem loadSimulation = new JMenuItem("Load Simulation");
JMenuItem saveSimulation = new JMenuItem("Save Simulation");
loadSimulation.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
String sessionSaveLocation = Preferences.userRoot().get("fileSaveLocation", "");
chooser.setCurrentDirectory(new File(sessionSaveLocation));
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int ret1 = chooser.showOpenDialog(JOptionUtils.centeringComp());
if (!(ret1 == JFileChooser.APPROVE_OPTION)) {
return;
}
File file = chooser.getSelectedFile();
if (file == null) {
return;
}
// Check to make sure the directory has the right structure.
File[] files = file.listFiles();
if (files == null) {
JOptionPane.showMessageDialog((SimulationEditor.this), "That wasn't a directory");
return;
}
boolean correctStructure = isCorrectStructure(files);
if (!correctStructure) {
int count = 0;
File thisOne = null;
for (File _file : files) {
File[] _files = _file.listFiles();
if (_files == null) {
continue;
}
if (isCorrectStructure(_files)) {
count++;
thisOne = _file;
}
}
if (thisOne == null) {
JOptionPane.showMessageDialog((SimulationEditor.this), "That file was not a simulation, and none of its subdirectories was either. " + "\nNeed a directory with a 'data' subdirectory, a 'graph' subdirectory, " + "\nand a 'parameters.txt' file.");
return;
}
if (count > 1) {
JOptionPane.showMessageDialog((SimulationEditor.this), "More than one subdirectory of that directory was a simulation; please select " + "\none of the subdirectories.");
return;
}
file = thisOne;
}
edu.cmu.tetrad.algcomparison.simulation.Simulation _simulation = new LoadContinuousDataAndGraphs(file.getPath());
_simulation.createData(simulation.getParams());
if (_simulation.getNumDataModels() > 0) {
Graph trueGraph = _simulation.getTrueGraph(0);
edu.cmu.tetrad.graph.GraphUtils.circleLayout(trueGraph, 225, 200, 150);
List<Graph> graphs = new ArrayList<>();
for (int i = 0; i < _simulation.getNumDataModels(); i++) {
graphs.add(_simulation.getTrueGraph(i));
}
graphEditor.replace(graphs);
DataWrapper wrapper = new DataWrapper(new Parameters());
DataModelList list = new DataModelList();
for (int i = 0; i < _simulation.getNumDataModels(); i++) {
list.add(_simulation.getDataModel(i));
}
wrapper.setDataModelList(list);
tabbedPane.setComponentAt(2, new DataEditor(wrapper, false, JTabbedPane.LEFT));
}
String graphPref = null;
String simPref = null;
if (_simulation.getParameters().contains("graphsDropdownPreference")) {
graphPref = (String) simulation.getParams().get("graphsDropdownPreference");
}
if (_simulation.getParameters().contains("simulationsDropdownPreference")) {
simPref = (String) simulation.getParams().get("simulationsDropdownPreference");
}
if (graphPref != null) {
graphsDropdown.setSelectedItem(graphPref);
System.out.println("Set pre-loaded Graph: " + graphPref);
}
if (simPref != null) {
simulationsDropdown.setSelectedItem(simPref);
System.out.println("Set pre-loaded sim: " + simPref);
}
simulation.setSimulation(_simulation, simulation.getParams());
resetPanel(simulation, graphItems, simulationItems, tabbedPane);
}
private boolean isCorrectStructure(File[] files) {
boolean hasDataDir = false;
boolean hasGraphDir = false;
boolean hasParametersFile = false;
for (File _file : files) {
if (_file.isDirectory() && _file.getName().equals("data")) {
hasDataDir = true;
}
if (_file.isDirectory() && _file.getName().equals("graph")) {
hasGraphDir = true;
}
if (_file.isFile() && _file.getName().equals("parameters.txt")) {
hasParametersFile = true;
}
}
return hasDataDir && hasGraphDir && hasParametersFile;
}
});
saveSimulation.addActionListener((e) -> {
JFileChooser chooser = new JFileChooser();
String sessionSaveLocation = Preferences.userRoot().get("fileSaveLocation", "");
chooser.setCurrentDirectory(new File(sessionSaveLocation));
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int ret1 = chooser.showSaveDialog(JOptionUtils.centeringComp());
if (!(ret1 == JFileChooser.APPROVE_OPTION)) {
return;
}
final File selectedFile = chooser.getSelectedFile();
if (selectedFile == null) {
return;
}
// if (file.listFiles().length != 0) {
// JOptionPane.showMessageDialog((SimulationEditor.this),
// "That wasn't a a new or empty directory; try typing a name for the directory\n" +
// "or creating an empty directory.");
// return;
// }
new Comparison().saveToFiles(selectedFile.getAbsolutePath(), simulation.getSimulation(), simulation.getParams());
Preferences.userRoot().put("fileSaveLocation", selectedFile.getParent());
});
fileMenu.addSeparator();
fileMenu.add(loadSimulation);
fileMenu.add(saveSimulation);
}
Aggregations