Search in sources :

Example 56 with FileObject

use of org.openide.filesystems.FileObject in project gephi by gephi.

the class ImportUtils method getArchivedFile.

public static FileObject getArchivedFile(final FileObject fileObject) {
    if (!isArchiveFile(fileObject)) {
        return fileObject;
    }
    FileObject result = fileObject;
    // ZIP and JAR archives
    if (FileUtil.isArchiveFile(fileObject)) {
        FileObject[] children = FileUtil.getArchiveRoot(fileObject).getChildren();
        if (children.length > 0) {
            result = children[0];
        }
    } else {
        // GZ or BZIP2 archives
        boolean isGz = fileObject.getExt().equalsIgnoreCase("gz");
        boolean isBzip = fileObject.getExt().equalsIgnoreCase("bz2");
        if (isGz || isBzip) {
            try {
                String[] splittedFileName = fileObject.getName().split("\\.");
                if (splittedFileName.length < 2) {
                    return fileObject;
                }
                String fileExt1 = splittedFileName[splittedFileName.length - 1];
                String fileExt2 = splittedFileName[splittedFileName.length - 2];
                File tempFile;
                if (fileExt1.equalsIgnoreCase("tar")) {
                    String fname = fileObject.getName().replaceAll("\\.tar$", "");
                    fname = fname.replace(fileExt2, "");
                    tempFile = File.createTempFile(fname, "." + fileExt2);
                    // Untar & unzip
                    if (isGz) {
                        tempFile = getGzFile(fileObject, tempFile, true);
                    } else {
                        tempFile = getBzipFile(fileObject, tempFile, true);
                    }
                } else {
                    String fname = fileObject.getName();
                    fname = fname.replace(fileExt1, "");
                    tempFile = File.createTempFile(fname, "." + fileExt1);
                    // Unzip
                    if (isGz) {
                        tempFile = getGzFile(fileObject, tempFile, false);
                    } else {
                        tempFile = getBzipFile(fileObject, tempFile, false);
                    }
                }
                tempFile.deleteOnExit();
                tempFile = FileUtil.normalizeFile(tempFile);
                result = FileUtil.toFileObject(tempFile);
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }
    if (result == null) {
        // Never return null if the archive is empty, broken or anything
        result = fileObject;
    }
    return result;
}
Also used : FileObject(org.openide.filesystems.FileObject) IOException(java.io.IOException) File(java.io.File)

Example 57 with FileObject

use of org.openide.filesystems.FileObject in project gephi by gephi.

the class ImportControllerImpl method importFile.

@Override
public Container importFile(File file) throws FileNotFoundException {
    FileObject fileObject = FileUtil.toFileObject(file);
    if (fileObject != null) {
        // Unzip and return content file
        fileObject = ImportUtils.getArchivedFile(fileObject);
        file = FileUtil.toFile(fileObject);
        FileImporterBuilder builder = getMatchingImporter(fileObject);
        if (fileObject != null && builder != null) {
            Container c = importFile(fileObject.getInputStream(), builder.buildImporter(), file);
            return c;
        }
    }
    return null;
}
Also used : FileImporterBuilder(org.gephi.io.importer.spi.FileImporterBuilder) Container(org.gephi.io.importer.api.Container) FileObject(org.openide.filesystems.FileObject)

Example 58 with FileObject

use of org.openide.filesystems.FileObject in project gephi by gephi.

the class SaveTask method run.

@Override
public void run() {
    Progress.start(progressTicket);
    Progress.setDisplayName(progressTicket, NbBundle.getMessage(SaveTask.class, "SaveTask.name"));
    File writeFile = null;
    try {
        String tempFileName = file.getName() + "_temp" + System.currentTimeMillis();
        writeFile = new File(file.getParent(), tempFileName);
        FileOutputStream outputStream = null;
        ZipOutputStream zipOut = null;
        BufferedOutputStream bos = null;
        DataOutputStream dos = null;
        try {
            // Stream
            int zipLevel = NbPreferences.forModule(SaveTask.class).getInt(ZIP_LEVEL_PREFERENCE, 9);
            outputStream = new FileOutputStream(writeFile);
            zipOut = new ZipOutputStream(outputStream);
            zipOut.setLevel(zipLevel);
            bos = new BufferedOutputStream(zipOut);
            dos = new DataOutputStream(bos);
            // Providers and workspace
            Collection<WorkspacePersistenceProvider> providers = PersistenceProviderUtils.getPersistenceProviders();
            Workspace[] workspaces = project.getLookup().lookup(WorkspaceProviderImpl.class).getWorkspaces();
            // Setup progress
            Progress.switchToDeterminate(progressTicket, 1 + (1 + providers.size()) * workspaces.length);
            // Write Project
            writeProject(dos, zipOut);
            Progress.progress(progressTicket);
            // Write Workspace files
            for (Workspace ws : workspaces) {
                writeWorkspace(ws, dos, zipOut);
                Progress.progress(progressTicket);
                for (WorkspacePersistenceProvider provider : providers) {
                    if (provider instanceof WorkspaceXMLPersistenceProvider) {
                        writeWorkspaceChildrenXML(ws, (WorkspaceXMLPersistenceProvider) provider, dos, zipOut);
                    } else if (provider instanceof WorkspaceBytesPersistenceProvider) {
                        writeWorkspaceChildrenBytes(ws, (WorkspaceBytesPersistenceProvider) provider, dos, zipOut);
                    }
                    Progress.progress(progressTicket);
                    if (cancel) {
                        break;
                    }
                }
                if (cancel) {
                    break;
                }
            }
            Progress.switchToIndeterminate(progressTicket);
            zipOut.finish();
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException ex1) {
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException ex1) {
                }
            }
            if (zipOut != null) {
                try {
                    zipOut.close();
                } catch (IOException ex1) {
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException ex1) {
                }
            }
        }
        Progress.finish(progressTicket);
        // Rename file
        if (!cancel && writeFile.exists()) {
            // Delete original file
            if (file.exists()) {
                file.delete();
            }
            FileObject tempFileObject = FileUtil.toFileObject(writeFile);
            FileLock lock = tempFileObject.lock();
            tempFileObject.rename(lock, getFileNameWithoutExt(file), getFileExtension(file));
            lock.releaseLock();
        }
    } catch (Exception ex) {
        if (ex instanceof GephiFormatException) {
            throw (GephiFormatException) ex;
        }
        throw new GephiFormatException(SaveTask.class, ex);
    } finally {
        if (writeFile != null && writeFile.exists()) {
            FileObject tempFileObject = FileUtil.toFileObject(writeFile);
            try {
                tempFileObject.delete();
            } catch (IOException ex) {
            }
        }
    }
    Progress.finish(progressTicket);
}
Also used : WorkspaceProviderImpl(org.gephi.project.impl.WorkspaceProviderImpl) DataOutputStream(java.io.DataOutputStream) WorkspacePersistenceProvider(org.gephi.project.spi.WorkspacePersistenceProvider) IOException(java.io.IOException) IOException(java.io.IOException) WorkspaceXMLPersistenceProvider(org.gephi.project.spi.WorkspaceXMLPersistenceProvider) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) WorkspaceBytesPersistenceProvider(org.gephi.project.spi.WorkspaceBytesPersistenceProvider) FileLock(org.openide.filesystems.FileLock) FileObject(org.openide.filesystems.FileObject) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Workspace(org.gephi.project.api.Workspace)

Example 59 with FileObject

use of org.openide.filesystems.FileObject in project Universal-G-Code-Sender by winder.

the class ActionRegistrationService method createAndLocalizeFullMenu.

/**
 * Creates a folder path in the netbeans filesystem and sets a localized
 * display name or each level of the path.
 */
public FileObject createAndLocalizeFullMenu(String path, String localizedPath) throws IOException {
    FileObject root = FileUtil.getConfigRoot();
    String[] paths = path.split("/");
    String[] names = localizedPath.split("/");
    if (paths.length != names.length) {
        throw new IllegalArgumentException("Path length must equal localized path length: " + path + ", " + localizedPath);
    }
    if (!paths[0].equals(names[0])) {
        throw new IllegalArgumentException("Path and localized path must be in the same top level directory. Found: " + paths[0] + " and " + names[0]);
    }
    String fullPath = paths[0];
    FileObject in = FileUtil.createFolder(root, fullPath);
    for (int i = 1; i < paths.length; i++) {
        fullPath = fullPath + "/" + paths[i];
        in = FileUtil.createFolder(root, fullPath);
        in.setAttribute("displayName", names[i]);
        in.refresh();
    }
    return in;
}
Also used : FileObject(org.openide.filesystems.FileObject)

Example 60 with FileObject

use of org.openide.filesystems.FileObject in project Universal-G-Code-Sender by winder.

the class MacroService method reInitActions.

public void reInitActions() {
    String menuPath = "Menu/Machine/Macros";
    String actionCategory = "Macro";
    String localCategory = Localization.getString("platform.menu.macros");
    String localized = String.format("Menu/%s/%s", Localization.getString("platform.menu.machine"), Localization.getString("platform.menu.macros"));
    try {
        FileObject root = FileUtil.getConfigRoot();
        // Clear out the menu items.
        FileUtil.createFolder(root, menuPath).delete();
        FileUtil.createFolder(root, menuPath);
        String actionPath = "/Actions/" + actionCategory;
        FileUtil.createFolder(root, actionPath).delete();
        // FileObject actionsObject = FileUtil.createFolder(root, actionPath);
        ActionRegistrationService ars = Lookup.getDefault().lookup(ActionRegistrationService.class);
        BackendAPI backend = CentralLookup.getDefault().lookup(BackendAPI.class);
        Settings settings = backend.getSettings();
        int numMacros = settings.getNumMacros();
        for (int i = 0; i < numMacros; i++) {
            Macro m = settings.getMacro(i);
            ars.registerAction(MacroAction.class.getCanonicalName() + "." + m.getName(), m.getName(), actionCategory, localCategory, null, menuPath, localized, new MacroAction(settings, backend, i));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : BackendAPI(com.willwinder.universalgcodesender.model.BackendAPI) Macro(com.willwinder.universalgcodesender.types.Macro) ActionRegistrationService(com.willwinder.ugs.nbp.lib.services.ActionRegistrationService) FileObject(org.openide.filesystems.FileObject) Settings(com.willwinder.universalgcodesender.utils.Settings)

Aggregations

FileObject (org.openide.filesystems.FileObject)79 File (java.io.File)27 IOException (java.io.IOException)16 ArrayList (java.util.ArrayList)9 NotifyDescriptor (org.openide.NotifyDescriptor)7 Project (org.netbeans.api.project.Project)6 DataObject (org.openide.loaders.DataObject)6 ActionEvent (java.awt.event.ActionEvent)5 PropertyChangeEvent (java.beans.PropertyChangeEvent)5 PropertyChangeListener (java.beans.PropertyChangeListener)5 InputStream (java.io.InputStream)5 JFileChooser (javax.swing.JFileChooser)5 JPanel (javax.swing.JPanel)5 ImportControllerUI (org.gephi.desktop.importer.api.ImportControllerUI)5 DialogFileFilter (org.gephi.ui.utils.DialogFileFilter)5 DialogDescriptor (org.openide.DialogDescriptor)5 ActionListener (java.awt.event.ActionListener)4 DocumentBuilder (javax.xml.parsers.DocumentBuilder)4 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)4 BorderLayout (java.awt.BorderLayout)3