Search in sources :

Example 6 with CyFileFilter

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;
    }
}
Also used : CyFileFilter(org.cytoscape.io.CyFileFilter) ZipEntry(java.util.zip.ZipEntry) CyWriter(org.cytoscape.io.write.CyWriter) Properties(java.util.Properties)

Example 7 with CyFileFilter

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;
}
Also used : ArrayList(java.util.ArrayList) FileChooserFilter(org.cytoscape.util.swing.FileChooserFilter) CyFileFilterProvider(org.cytoscape.io.CyFileFilterProvider) CyFileFilter(org.cytoscape.io.CyFileFilter) HashSet(java.util.HashSet)

Example 8 with CyFileFilter

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);
}
Also used : CyFileFilter(org.cytoscape.io.CyFileFilter) File(java.io.File)

Example 9 with CyFileFilter

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()));
}
Also used : CyFileFilter(org.cytoscape.io.CyFileFilter)

Example 10 with CyFileFilter

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();
}
Also used : CyFileFilter(org.cytoscape.io.CyFileFilter) IOException(java.io.IOException) CySessionWriterManager(org.cytoscape.io.write.CySessionWriterManager) File(java.io.File) IOException(java.io.IOException)

Aggregations

CyFileFilter (org.cytoscape.io.CyFileFilter)10 ArrayList (java.util.ArrayList)5 File (java.io.File)3 IOException (java.io.IOException)3 Before (org.junit.Before)3 BufferedInputStream (java.io.BufferedInputStream)2 CyApplicationManager (org.cytoscape.application.CyApplicationManager)2 CyWriter (org.cytoscape.io.write.CyWriter)2 RenderingEngine (org.cytoscape.view.presentation.RenderingEngine)2 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 ZipEntry (java.util.zip.ZipEntry)1 CyFileFilterProvider (org.cytoscape.io.CyFileFilterProvider)1 CyNetworkViewWriterManager (org.cytoscape.io.write.CyNetworkViewWriterManager)1 CySessionWriterManager (org.cytoscape.io.write.CySessionWriterManager)1 PresentationWriterManager (org.cytoscape.io.write.PresentationWriterManager)1 CyNetwork (org.cytoscape.model.CyNetwork)1