use of org.cytoscape.io.CyFileFilter in project cytoscape-impl by cytoscape.
the class SessionWriterImpl method zipProperties.
/**
* Writes the cytoscape.props file to the session zip.
*/
private void zipProperties() throws Exception {
for (CyProperty<?> cyProps : session.getProperties()) {
if (cancelled)
return;
String filename = null;
CyFileFilter filter = null;
Class<?> type = cyProps.getPropertyType();
if (Bookmarks.class.isAssignableFrom(type)) {
filename = BOOKMARKS_FILE;
filter = bookmarksFilter;
} else if (Properties.class.isAssignableFrom(type)) {
filename = cyProps.getName() + PROPERTIES_EXT;
filter = propertiesFilter;
} else {
// filename = cyProps.getName();
// TODO: how to get the file extension and the file filter for unknown CyProperty types?
logger.error("Cannot save CyProperty \"" + cyProps.getName() + "\": type \"" + type + "\" is not supported");
continue;
}
zos.putNextEntry(new ZipEntry(sessionDir + PROPERTIES_FOLDER + filename));
CyWriter propertiesWriter = propertyWriterMgr.getWriter(cyProps.getProperties(), filter, zos);
propertiesWriter.run(taskMonitor);
zos.closeEntry();
propertiesWriter = null;
}
}
use of org.cytoscape.io.CyFileFilter in project cytoscape-impl by cytoscape.
the class SupportedFileTypesManager method getSupportedFileTypes.
private List<FileChooserFilter> getSupportedFileTypes(final DataCategory category, final Set<CyFileFilterProvider> factories) {
List<FileChooserFilter> types = new ArrayList<FileChooserFilter>();
Set<String> allExtensions = new HashSet<String>();
for (final CyFileFilterProvider factory : factories) {
CyFileFilter filter = factory.getFileFilter();
// this is a hack to exclude internal session table format
if (filter.getExtensions().contains("cytable") || filter.getDataCategory() != category)
continue;
String description = filter.getDescription();
Set<String> filterExtensions = filter.getExtensions();
String[] extensions = new String[filterExtensions.size()];
int index = 0;
for (String extension : filterExtensions) {
allExtensions.add(extension);
extensions[index] = extension;
index++;
}
types.add(new FileChooserFilter(description, extensions));
}
if (types.isEmpty())
return types;
Collections.sort(types, new Comparator<FileChooserFilter>() {
@Override
public int compare(FileChooserFilter o1, FileChooserFilter o2) {
return o1.getDescription().compareTo(o2.getDescription());
}
});
String description = String.format("All %1$s files", category.getDisplayName().toLowerCase());
types.add(new FileChooserFilter(description, new ArrayList<String>(allExtensions).toArray(new String[allExtensions.size()])));
return types;
}
use of org.cytoscape.io.CyFileFilter in project cytoscape-impl by cytoscape.
the class AbstractCyWriter method addOrReplaceExtension.
/**
* Adds or replaces the extension of the given File with one suitable for the selected export file format
* @param file The input file
* @return a File with an extension suitable for the selected export file format
*/
protected final File addOrReplaceExtension(final File file) {
final CyFileFilter filter = getFileFilter(getExportFileFormat());
if (filter == null)
return file;
final Iterator<String> extensions = filter.getExtensions().iterator();
if (!extensions.hasNext())
return file;
final String filterExtension = extensions.next();
String fileName = file.getAbsolutePath();
final String fileExtension = FilenameUtils.getExtension(fileName);
if (!filterExtension.trim().equals(fileExtension.trim()))
fileName = FilenameUtils.removeExtension(fileName) + "." + filterExtension;
return new File(fileName);
}
use of org.cytoscape.io.CyFileFilter in project cytoscape-impl by cytoscape.
the class AbstractCyWriter method fileExtensionIsOk.
/**
* Returns whether the given File's extension is acceptable for the selected file format
* @param file The input file
* @return true if the extension is accepted for the selected file format, and false otherwise
*/
protected final boolean fileExtensionIsOk(final File file) {
final String exportFileFormat = getExportFileFormat();
if (exportFileFormat == null)
return true;
final CyFileFilter filter = getFileFilter(exportFileFormat);
if (filter == null)
return true;
return filter.getExtensions().contains(FilenameUtils.getExtension(file.getName()));
}
use of org.cytoscape.io.CyFileFilter in project cytoscape-impl by cytoscape.
the class CySessionWriter method run.
/**
* The method that will actually write the specified session to the specified
* file.
* @param tm The {@link org.cytoscape.work.TaskMonitor} provided by the TaskManager execution environment.
*/
@Override
public final void run(final TaskMonitor tm) throws Exception {
final CySessionWriterManager writerMgr = serviceRegistrar.getService(CySessionWriterManager.class);
final List<CyFileFilter> filters = writerMgr.getAvailableWriterFilters();
if (filters == null || filters.size() < 1)
throw new NullPointerException("No Session file filters found");
if (filters.size() > 1)
throw new IllegalArgumentException("Found too many session filters.");
String filepath = outputFile.getAbsolutePath();
if (!filepath.toLowerCase().endsWith(".cys")) {
filepath += ".cys";
outputFile = new File(filepath);
}
if (!canWrite(outputFile)) {
throw new IOException("Session not saved. Cytoscape does not have write permission to save the session at \"" + outputFile.getParentFile().getAbsolutePath() + "\". Choose another folder or change the folder permissions.");
}
// Write to a temporary file first, to prevent the original file from being damaged, in case there is any error
createTempFile();
try {
writer = writerMgr.getWriter(session, filters.get(0), tmpFile);
if (writer == null)
throw new NullPointerException("No CyWriter found for specified file type.");
writer.run(tm);
} catch (final Exception e) {
deleteFile(tmpFile);
throw e;
}
if (cancelled) {
deleteFile(tmpFile);
return;
}
// If the main task is successfully executed, this task will move the temp file to the actual output path
replaceOutputFile();
}
Aggregations