use of org.vcell.util.gui.VCFileChooser in project vcell by virtualcell.
the class ChooseFile method showGeometryModelXMLFileChooser.
// // Check structure sized and select a simulation to export to SBML
// if (isSbml) {
// // get user choice of structure and its size and computes absolute sizes of compartments using the StructureSizeSolver.
// Structure[] structures = bioModel.getModel().getStructures();
// // get the nonspatial simulationContexts corresponding to names in applicableAppNameList
// // This is needed in ApplnSelectionAndStructureSizeInputPanel
//
// SimulationContext[] applicableSimContexts = applicableSimContext.toArray(new SimulationContext[applicableSimContext.size()]);
//
// String strucName = null;
// double structSize = 1.0;
// int structSelection = -1;
// int option = JOptionPane.CANCEL_OPTION;
// Simulation chosenSimulation = null;
//
// ApplnSelectionAndStructureSizeInputPanel applnStructInputPanel = null;
// while (structSelection < 0) {
// applnStructInputPanel = new ApplnSelectionAndStructureSizeInputPanel();
// applnStructInputPanel.setSimContexts(applicableSimContexts);
// applnStructInputPanel.setStructures(structures);
// applnStructInputPanel.setPreferredSize(new java.awt.Dimension(350, 400));
// applnStructInputPanel.setMaximumSize(new java.awt.Dimension(350, 400));
// option = DialogUtils.showComponentOKCancelDialog(currentWindow, applnStructInputPanel, "Select Application and Specify Structure Size to Export:");
// structSelection = applnStructInputPanel.getStructSelectionIndex();
// if (option == JOptionPane.CANCEL_OPTION || option == JOptionPane.CLOSED_OPTION) {
// break;
// } else if (option == JOptionPane.OK_OPTION && structSelection < 0) {
// DialogUtils.showErrorDialog(currentWindow, "Please select a structure and set its size");
// }
// }
//
// if (option == JOptionPane.OK_OPTION) {
// applnStructInputPanel.applyStructureNameAndSizeValues();
// strucName = applnStructInputPanel.getSelectedStructureName();
// chosenSimContext = applnStructInputPanel.getSelectedSimContext();
// hashTable.put("selectedSimContext", chosenSimContext);
//
// GeometryContext geoContext = chosenSimContext.getGeometryContext();
// if (!fileFilter.getDescription().equals(FileFilters.FILE_FILTER_SBML_31_SPATIAL.getDescription())) {
// // calculate structure Sizes only if appln is not spatial
// structSize = applnStructInputPanel.getStructureSize();
// // Invoke StructureSizeEvaluator to compute absolute sizes of compartments if all sizes are not set
// if ( (geoContext.isAllSizeSpecifiedNull() && geoContext.isAllVolFracAndSurfVolSpecifiedNull()) ||
// ((strucName == null || structSize <= 0.0) && (geoContext.isAllSizeSpecifiedNull() && geoContext.isAllVolFracAndSurfVolSpecified())) ||
// (!geoContext.isAllSizeSpecifiedPositive() && geoContext.isAllVolFracAndSurfVolSpecifiedNull()) ||
// (!geoContext.isAllSizeSpecifiedPositive() && !geoContext.isAllVolFracAndSurfVolSpecified()) ||
// (geoContext.isAllSizeSpecifiedNull() && !geoContext.isAllVolFracAndSurfVolSpecified()) ) {
// DialogUtils.showErrorDialog(currentWindow, "Cannot export to SBML without compartment sizes being set. This can be automatically " +
// " computed if the absolute size of at least one compartment and the relative sizes (Surface-to-volume-ratio/Volume-fraction) " +
// " of all compartments are known. Sufficient information is not available to perform this computation." +
// "\n\nThis can be fixed by going back to the application '" + chosenSimContext.getName() + "' and setting structure sizes in the 'StructureMapping' tab.");
// throw UserCancelException.CANCEL_XML_TRANSLATION;
// }
// if (!geoContext.isAllSizeSpecifiedPositive() && geoContext.isAllVolFracAndSurfVolSpecified()) {
// Structure chosenStructure = chosenSimContext.getModel().getStructure(strucName);
// StructureMapping chosenStructMapping = chosenSimContext.getGeometryContext().getStructureMapping(chosenStructure);
// StructureSizeSolver.updateAbsoluteStructureSizes(chosenSimContext, chosenStructure, structSize, chosenStructMapping.getSizeParameter().getUnitDefinition());
// }
// } else {
// if (!geoContext.isAllUnitSizeParameterSetForSpatial()) {
// DialogUtils.showErrorDialog(currentWindow, "Cannot export to SBML without compartment size ratios being set." +
// "\n\nThis can be fixed by going back to the application '" + chosenSimContext.getName() + "' and setting structure" +
// " size ratios in the 'StructureMapping' tab.");
// throw UserCancelException.CANCEL_XML_TRANSLATION;
// }
// }
//
// // Select simulation whose overrides need to be exported
// // If simContext doesn't have simulations, don't pop up simulationSelectionPanel
// Simulation[] sims = bioModel.getSimulations(chosenSimContext);
// // display only those simulations that have overrides in the simulationSelectionPanel.
// Vector<Simulation> orSims = new Vector<Simulation>();
// for (int s = 0; (sims != null) && (s < sims.length); s++) {
// if (sims[s].getMathOverrides().hasOverrides()) {
// orSims.addElement(sims[s]);
// }
// }
// Simulation[] overriddenSims = (Simulation[])BeanUtils.getArray(orSims, Simulation.class);
// if (overriddenSims.length > 0) {
// SimulationSelectionPanel simSelectionPanel = new SimulationSelectionPanel();
// simSelectionPanel.setPreferredSize(new java.awt.Dimension(600, 400));
// simSelectionPanel.setMaximumSize(new java.awt.Dimension(600, 400));
// simSelectionPanel.setSimulations(overriddenSims);
// int simOption = DialogUtils.showComponentOKCancelDialog(currentWindow, simSelectionPanel, "Select Simulation whose overrides should be exported:");
// if (simOption == JOptionPane.OK_OPTION) {
// chosenSimulation = simSelectionPanel.getSelectedSimulation();
// if (chosenSimulation != null) {
// hashTable.put("selectedSimulation", chosenSimulation);
// }
// } else if (simOption == JOptionPane.CANCEL_OPTION || simOption == JOptionPane.CLOSED_OPTION) {
// // User did not choose a simulation whose overrides are required to be exported.
// // Without that information, cannot export successfully into SBML,
// // Hence canceling the entire export to SBML operation.
// throw UserCancelException.CANCEL_XML_TRANSLATION;
// }
// }
// } else if (option == JOptionPane.CANCEL_OPTION || option == JOptionPane.CLOSED_OPTION) {
// // User did not choose to set size for any structure.
// // Without that information, cannot export successfully into SBML,
// // Hence canceling the entire export to SBML operation.
// throw UserCancelException.CANCEL_XML_TRANSLATION;
// }
//
// if (chosenSimulation != null) {
// // rename file to contain exported simulation.
// String ext = FilenameUtils.getExtension(selectedFileName);
// String base = FilenameUtils.getBaseName(selectedFileName);
// String path = FilenameUtils.getPath(selectedFileName);
// base += "_" + TokenMangler.mangleToSName(chosenSimulation.getName());
// selectedFileName = path + base + ext;
// selectedFile.renameTo(new File(selectedFileName));
// }
// resetPreferredFilePath(selectedFile, userPreferences);
// return selectedFile;
// }
/**
* Export to Geometry
* Creation date: (5/31/2004 6:04:14 PM)
* @param hashTable java.util.Hashtable
* @param clientWorker cbit.vcell.desktop.controls.ClientWorker
*/
private File showGeometryModelXMLFileChooser(Hashtable<String, Object> hashTable) throws java.lang.Exception {
Geometry geom = (Geometry) hashTable.get("documentToExport");
JFrame currentWindow = (JFrame) hashTable.get("currentWindow");
UserPreferences userPreferences = (UserPreferences) hashTable.get("userPreferences");
TopLevelWindowManager topLevelWindowManager = (TopLevelWindowManager) hashTable.get("topLevelWindowManager");
Component comp = (Component) hashTable.get("component");
comp = (comp == null ? (currentWindow == null ? (topLevelWindowManager == null ? null : topLevelWindowManager.getComponent()) : currentWindow) : comp);
File defaultPath = userPreferences.getCurrentDialogPath();
VCFileChooser fileChooser = new VCFileChooser(defaultPath);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setMultiSelectionEnabled(false);
// fileChooser.addChoosableFileFilter(FileFilters.FILE_FILTER_VCML);
fileChooser.addChoosableFileFilter(FileFilters.FILE_FILTER_PDF);
if (geom.getDimension() > 0) {
fileChooser.addChoosableFileFilter(FileFilters.FILE_FILTER_AVS);
fileChooser.addChoosableFileFilter(FileFilters.FILE_FILTER_STL);
fileChooser.addChoosableFileFilter(FileFilters.FILE_FILTER_PLY);
}
// remove all selector
fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
// set the default file filter...
fileChooser.setFileFilter(FileFilters.FILE_FILTER_VCML);
fileChooser.setSelectedFile(new java.io.File(TokenMangler.fixTokenStrict(geom.getName())));
fileChooser.setDialogTitle("Export Virtual Cell Geometry As...");
if (fileChooser.showSaveDialog(comp) != JFileChooser.APPROVE_OPTION) {
// user didn't choose save
throw UserCancelException.CANCEL_FILE_SELECTION;
} else {
File selectedFile = fileChooser.getSelectedFile();
javax.swing.filechooser.FileFilter fileFilter = fileChooser.getFileFilter();
if (selectedFile == null) {
// no file selected (no name given)
throw UserCancelException.CANCEL_FILE_SELECTION;
} else {
// /
String selectedFileName = recordAndRemoveExtension(selectedFile.getPath());
String n = selectedFile.getPath().toLowerCase();
if (fileFilter == FileFilters.FILE_FILTER_VCML && !n.endsWith(".vcml")) {
selectedFile = new File(selectedFileName + ".vcml");
} else if (fileFilter == FileFilters.FILE_FILTER_PDF && !n.endsWith(".pdf")) {
selectedFile = new File(selectedFileName + ".pdf");
}
if (fileFilter == null) {
throw new Exception("No file save type was selected.");
}
checkForOverwrites(selectedFile, comp, userPreferences);
// put the filter in the hash so the export task knows what to do...
hashTable.put("fileFilter", fileFilter);
resetPreferredFilePath(selectedFile, userPreferences);
return selectedFile;
}
}
}
use of org.vcell.util.gui.VCFileChooser in project vcell by virtualcell.
the class ChooseFile method showMathModelXMLFileChooser.
/**
* Insert the method's description here.
* Creation date: (5/31/2004 6:04:14 PM)
* @param hashTable java.util.Hashtable
* @param clientWorker cbit.vcell.desktop.controls.ClientWorker
*/
private File showMathModelXMLFileChooser(Hashtable<String, Object> hashTable) throws java.lang.Exception {
// for mathmodels:
MathModel mathModel = (MathModel) hashTable.get("documentToExport");
JFrame currentWindow = (JFrame) hashTable.get("currentWindow");
UserPreferences userPreferences = (UserPreferences) hashTable.get("userPreferences");
TopLevelWindowManager topLevelWindowManager = (TopLevelWindowManager) hashTable.get("topLevelWindowManager");
if (topLevelWindowManager == null) {
throw new RuntimeException("toplLevelWindowManager required");
}
File defaultPath = userPreferences.getCurrentDialogPath();
VCFileChooser fileChooser = new VCFileChooser(defaultPath);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setMultiSelectionEnabled(false);
// fileChooser.addChoosableFileFilter(FileFilters.FILE_FILTER_SBML_21); // Can export Mathmodel to L2V1 ??
fileChooser.addChoosableFileFilter(FileFilters.FILE_FILTER_SBML_23);
fileChooser.addChoosableFileFilter(FileFilters.FILE_FILTER_SBML_24);
// fileChooser.addChoosableFileFilter(FileFilters.FILE_FILTER_CELLML);
fileChooser.addChoosableFileFilter(FileFilters.FILE_FILTER_MATLABV6);
fileChooser.addChoosableFileFilter(FileFilters.FILE_FILTER_PDF);
fileChooser.addChoosableFileFilter(FileFilters.FILE_FILTER_SMOLDYN_INPUT);
// remove all selector
fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
// set the default file filter...
fileChooser.setFileFilter(FileFilters.FILE_FILTER_VCML);
fileChooser.setSelectedFile(new java.io.File(TokenMangler.fixTokenStrict(mathModel.getName())));
fileChooser.setDialogTitle("Export Virtual Cell MathModel As...");
if (fileChooser.showSaveDialog(currentWindow) != JFileChooser.APPROVE_OPTION) {
// user didn't choose save
throw UserCancelException.CANCEL_FILE_SELECTION;
} else {
File selectedFile = fileChooser.getSelectedFile();
javax.swing.filechooser.FileFilter fileFilter = fileChooser.getFileFilter();
if (selectedFile == null) {
// no file selected (no name given)
throw UserCancelException.CANCEL_FILE_SELECTION;
} else {
// /
String selectedFileName = recordAndRemoveExtension(selectedFile.getPath());
String n = selectedFile.getPath().toLowerCase();
if (fileFilter == FileFilters.FILE_FILTER_CELLML && !n.endsWith(".xml")) {
selectedFile = new File(selectedFileName + ".xml");
} else if (fileFilter == FileFilters.FILE_FILTER_VCML && !n.endsWith(".vcml")) {
selectedFile = new File(selectedFileName + ".vcml");
} else if ((fileFilter == FileFilters.FILE_FILTER_SBML_23 || (fileFilter == FileFilters.FILE_FILTER_SBML_24)) && !n.endsWith(".xml")) {
selectedFile = new File(selectedFileName + ".xml");
} else if (fileFilter == FileFilters.FILE_FILTER_MATLABV6 && !n.endsWith(".m")) {
selectedFile = new File(selectedFileName + ".m");
} else if (fileFilter == FileFilters.FILE_FILTER_PDF && !n.endsWith(".pdf")) {
selectedFile = new File(selectedFileName + ".pdf");
} else if (fileFilter == FileFilters.FILE_FILTER_SMOLDYN_INPUT && !(n.endsWith(SMOLDYN_INPUT_FILE_EXTENSION) || (n.endsWith(".txt")))) {
selectedFile = new File(selectedFileName + SMOLDYN_INPUT_FILE_EXTENSION);
}
checkForOverwrites(selectedFile, topLevelWindowManager.getComponent(), userPreferences);
// put the filter in the hash so the export task knows what to do...
hashTable.put("fileFilter", fileFilter);
// only non-spatial math models can be exported to CellML.
if (fileFilter == FileFilters.FILE_FILTER_CELLML && mathModel.getMathDescription().getGeometry().getDimension() != 0) {
throw new Exception("No non-spatial applications, can only export to this format the math from a non-spatial application");
} else // Select a spatial stochastic simulation to export
if (fileFilter.getDescription().equals(FileFilters.FILE_FILTER_SMOLDYN_INPUT.getDescription())) {
Simulation[] sims = mathModel.getSimulations();
if (sims == null || sims.length == 0) {
DialogUtils.showWarningDialog(topLevelWindowManager.getComponent(), "MathModel '" + mathModel.getName() + "' has no simulations to export");
throw UserCancelException.CANCEL_GENERIC;
}
String[][] rowData = new String[sims.length][2];
for (int i = 0; i < sims.length; i++) {
rowData[i][0] = sims[i].getName();
rowData[i][1] = sims[i].getSolverTaskDescription().getSolverDescription().getDisplayLabel();
}
int[] selectedRow = PopupGenerator.showComponentOKCancelTableList(topLevelWindowManager.getComponent(), "Select a simulation to export", new String[] { "Simulation Name", "Solver" }, rowData, ListSelectionModel.SINGLE_SELECTION);
if (selectedRow == null) {
throw UserCancelException.CANCEL_GENERIC;
}
String chosenSimulationName = rowData[selectedRow[0]][0];
Simulation chosenSimulation = mathModel.getSimulation(chosenSimulationName);
if (chosenSimulation != null) {
hashTable.put("selectedSimulation", chosenSimulation);
} else {
throw new Exception("Couldn't match selection '" + chosenSimulationName + "' with simulation");
}
}
resetPreferredFilePath(selectedFile, userPreferences);
return selectedFile;
}
}
}
use of org.vcell.util.gui.VCFileChooser in project vcell by virtualcell.
the class BNGWindowManager method saveOutput.
/**
* Comment
*/
public void saveOutput(String bngOutputStr) {
// Ask user for save location
File defaultPath = getUserPreferences().getCurrentDialogPath();
VCFileChooser fileChooser = new VCFileChooser(defaultPath);
fileChooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
fileChooser.setMultiSelectionEnabled(false);
// remove all selector
fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
fileChooser.setSelectedFile(new java.io.File(getBngOutputPanel().getSelectedOutputFileName()));
fileChooser.setDialogTitle("Save Selected BNG Output Format ...");
if (fileChooser.showSaveDialog(getBngOutputPanel()) != JFileChooser.APPROVE_OPTION) {
// user didn't choose save
throw UserCancelException.CANCEL_FILE_SELECTION;
} else {
File selectedFile = fileChooser.getSelectedFile();
if (selectedFile == null) {
// no file selected (no name given)
throw UserCancelException.CANCEL_FILE_SELECTION;
} else {
// we have a file selection, check for overwrites
if (selectedFile.exists()) {
String answer = PopupGenerator.showWarningDialog(this, getUserPreferences(), UserMessage.warn_OverwriteFile, selectedFile.getAbsolutePath());
if (answer.equals(UserMessage.OPTION_CANCEL)) {
throw UserCancelException.CANCEL_FILE_SELECTION;
}
}
// write out text into file using FileOutputStream and PrintWriter
java.io.FileOutputStream outputStream = null;
String selectedFileName = selectedFile.getPath();
try {
outputStream = new java.io.FileOutputStream(selectedFileName);
} catch (java.io.IOException e) {
e.printStackTrace(System.out);
throw new RuntimeException("Error opening file '" + selectedFileName + " to write BioNetGen output" + ": " + e.getMessage());
}
PrintWriter bngOutputFile = new PrintWriter(outputStream);
bngOutputFile.print(bngOutputStr);
bngOutputFile.close();
}
}
}
use of org.vcell.util.gui.VCFileChooser in project vcell by virtualcell.
the class BNGWindowManager method uploadBNGLFile.
/**
* Comment
*/
public String uploadBNGLFile() throws java.io.FileNotFoundException, java.io.IOException {
// BNG input file (.bngl) contents
String bngInputStr = null;
// Ask user for upload location
File defaultPath = getUserPreferences().getCurrentDialogPath();
VCFileChooser fileChooser = new VCFileChooser(defaultPath);
fileChooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
fileChooser.setMultiSelectionEnabled(false);
// remove all selector
fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
// set default file filter
fileChooser.addChoosableFileFilter(org.vcell.util.gui.exporter.FileFilters.FILE_FILTER_BNGL);
fileChooser.setFileFilter(org.vcell.util.gui.exporter.FileFilters.FILE_FILTER_BNGL);
// Set file chooser dialog title
fileChooser.setDialogTitle("Upload Selected BNG file ...");
if (fileChooser.showOpenDialog(getBngOutputPanel()) != JFileChooser.APPROVE_OPTION) {
// user didn't choose Open
throw UserCancelException.CANCEL_FILE_SELECTION;
} else {
File selectedFile = fileChooser.getSelectedFile();
if (selectedFile == null) {
// no file selected (no name given)
throw UserCancelException.CANCEL_FILE_SELECTION;
} else {
long selectedFileLength = selectedFile.length();
// Check if file exists
if (!selectedFile.exists()) {
throw new RuntimeException("File " + selectedFile.getPath() + " not found");
}
// Check if file has .bngl extension
if (!selectedFile.getPath().endsWith(".bngl")) {
DialogUtils.showErrorDialog(getComponent(), "File " + selectedFile.getPath() + " is not a .bngl file");
throw new RuntimeException("File " + selectedFile.getPath() + " is not a .bngl file");
}
// Read characters from file into character array and transfer into string buffer.
StringBuffer stringBuffer = new StringBuffer();
FileInputStream fis = null;
fis = new FileInputStream(selectedFile);
InputStreamReader reader = new InputStreamReader(fis);
try (BufferedReader br = new BufferedReader(reader)) {
char[] charArray = new char[10000];
while (true) {
int numRead = br.read(charArray, 0, charArray.length);
if (numRead > 0) {
stringBuffer.append(charArray, 0, numRead);
} else if (numRead == -1) {
break;
}
}
}
if (stringBuffer.length() != selectedFileLength) {
System.out.println("<<<SYSOUT ALERT>>> Reading from bng file: read " + stringBuffer.length() + " of " + selectedFileLength + " bytes of input file");
}
bngInputStr = stringBuffer.toString();
}
return bngInputStr;
}
}
use of org.vcell.util.gui.VCFileChooser in project vcell by virtualcell.
the class BNGWindowManager method saveBNGLFile.
/**
* prompt user for filename, then write String to it
* @param bngl String to write out
* @throws IOException
*/
public void saveBNGLFile(String bngl) throws IOException {
File defaultPath = getUserPreferences().getCurrentDialogPath();
VCFileChooser fileChooser = new VCFileChooser(defaultPath);
fileChooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
fileChooser.setMultiSelectionEnabled(false);
// remove all selector
fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
// set default file filter
fileChooser.addChoosableFileFilter(org.vcell.util.gui.exporter.FileFilters.FILE_FILTER_BNGL);
fileChooser.setFileFilter(org.vcell.util.gui.exporter.FileFilters.FILE_FILTER_BNGL);
// Set file chooser dialog title
fileChooser.setDialogTitle("Save BNG file ...");
if (fileChooser.showSaveDialog(getBngOutputPanel()) != JFileChooser.APPROVE_OPTION) {
throw UserCancelException.CANCEL_FILE_SELECTION;
} else {
File selectedFile = fileChooser.getSelectedFile();
String path = selectedFile.getAbsolutePath();
if (path.indexOf('.') == -1) {
path += org.vcell.util.gui.exporter.FileFilters.FILE_FILTER_BNGL.getPrimaryExtension();
selectedFile = new File(path);
}
if (selectedFile.exists()) {
final int answer = JOptionPane.showConfirmDialog(getComponent(), selectedFile + " exists. Overwrite?", "Confirm Replace", JOptionPane.YES_NO_OPTION);
if (answer != JOptionPane.YES_OPTION) {
return;
}
}
try (FileWriter fw = new FileWriter(path)) {
fw.write(bngl);
}
}
}
Aggregations