use of javax.swing.filechooser.FileFilter in project pcgen by PCGen.
the class NotesView method exportFile.
//Note Import/Export methods
/**
* Exports a node out to a gmn file.
*
*@param node node to export to file
*/
private void exportFile(NotesTreeNode node) {
JFileChooser fLoad = new JFileChooser();
String sFile = SettingsHandler.getGMGenOption(OPTION_NAME_LASTFILE, "");
new File(sFile);
FileFilter ff = NotesPlugin.getFileType();
fLoad.addChoosableFileFilter(ff);
fLoad.setFileFilter(ff);
int returnVal = fLoad.showSaveDialog(this);
try {
if (returnVal == JFileChooser.APPROVE_OPTION) {
String fileName = fLoad.getSelectedFile().getName();
String dirName = fLoad.getSelectedFile().getParent();
String extension = EXTENSION;
if (fileName.indexOf(extension) < 0) {
fileName += extension;
}
File expFile = new File(dirName + File.separator + fileName);
if (expFile.exists()) {
int choice = JOptionPane.showConfirmDialog(this, "File Exists, Overwrite?", "File Exists", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (choice == JOptionPane.NO_OPTION) {
return;
}
}
SettingsHandler.setGMGenOption(OPTION_NAME_LASTFILE, expFile.toString());
writeNotesFile(expFile, node);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Error Writing File");
Logging.errorPrint("Error Writing to file: " + e.getMessage(), e);
}
}
use of javax.swing.filechooser.FileFilter 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 javax.swing.filechooser.FileFilter 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 javax.swing.filechooser.FileFilter in project cayenne by apache.
the class CayenneModelerController method processDropAction.
@SuppressWarnings("unchecked")
private boolean processDropAction(Transferable transferable) {
List<File> fileList;
try {
fileList = (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor);
} catch (Exception e) {
return false;
}
if (fileList != null) {
File transferFile = fileList.get(0);
if (transferFile.isFile()) {
FileFilter filter = FileFilters.getApplicationFilter();
if (filter.accept(transferFile)) {
ActionEvent e = new ActionEvent(transferFile, ActionEvent.ACTION_PERFORMED, "OpenProject");
Application.getInstance().getActionManager().getAction(OpenProjectAction.class).actionPerformed(e);
return true;
}
}
}
return false;
}
Aggregations