Search in sources :

Example 21 with FileObject

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

the class DesktopImportControllerUI method importFile.

@Override
public void importFile(FileObject fileObject) {
    try {
        final FileImporter importer = controller.getFileImporter(FileUtil.toFile(fileObject));
        if (importer == null) {
            NotifyDescriptor.Message msg = new NotifyDescriptor.Message(NbBundle.getMessage(getClass(), "DesktopImportControllerUI.error_no_matching_file_importer"), NotifyDescriptor.WARNING_MESSAGE);
            DialogDisplayer.getDefault().notify(msg);
            return;
        }
        //MRU
        MostRecentFiles mostRecentFiles = Lookup.getDefault().lookup(MostRecentFiles.class);
        mostRecentFiles.addFile(fileObject.getPath());
        ImporterUI ui = controller.getUI(importer);
        if (ui != null) {
            String title = NbBundle.getMessage(DesktopImportControllerUI.class, "DesktopImportControllerUI.file.ui.dialog.title", ui.getDisplayName());
            JPanel panel = ui.getPanel();
            FileImporter[] fi = (FileImporter[]) Array.newInstance(importer.getClass(), 1);
            fi[0] = importer;
            ui.setup(fi);
            final DialogDescriptor dd = new DialogDescriptor(panel, title);
            if (panel instanceof ValidationPanel) {
                ValidationPanel vp = (ValidationPanel) panel;
                vp.addChangeListener(new ChangeListener() {

                    @Override
                    public void stateChanged(ChangeEvent e) {
                        dd.setValid(!((ValidationPanel) e.getSource()).isProblem());
                    }
                });
            }
            Object result = DialogDisplayer.getDefault().notify(dd);
            if (!result.equals(NotifyDescriptor.OK_OPTION)) {
                ui.unsetup(false);
                return;
            }
            ui.unsetup(true);
        }
        LongTask task = null;
        if (importer instanceof LongTask) {
            task = (LongTask) importer;
        }
        //Execute task
        fileObject = getArchivedFile(fileObject);
        final String containerSource = fileObject.getNameExt();
        final InputStream stream = fileObject.getInputStream();
        String taskName = NbBundle.getMessage(DesktopImportControllerUI.class, "DesktopImportControllerUI.taskName", containerSource);
        executor.execute(task, new Runnable() {

            @Override
            public void run() {
                try {
                    Container container = controller.importFile(stream, importer);
                    if (container != null) {
                        container.setSource(containerSource);
                        finishImport(container);
                    }
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        }, taskName, errorHandler);
    } catch (Exception ex) {
        Logger.getLogger("").log(Level.WARNING, "", ex);
    }
}
Also used : ImporterUI(org.gephi.io.importer.spi.ImporterUI) MostRecentFiles(org.gephi.desktop.mrufiles.api.MostRecentFiles) JPanel(javax.swing.JPanel) GZIPInputStream(java.util.zip.GZIPInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileImporter(org.gephi.io.importer.spi.FileImporter) ValidationPanel(org.netbeans.validation.api.ui.ValidationPanel) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) NotifyDescriptor(org.openide.NotifyDescriptor) Container(org.gephi.io.importer.api.Container) ChangeEvent(javax.swing.event.ChangeEvent) LongTask(org.gephi.utils.longtask.spi.LongTask) DialogDescriptor(org.openide.DialogDescriptor) ChangeListener(javax.swing.event.ChangeListener) FileObject(org.openide.filesystems.FileObject)

Example 22 with FileObject

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

the class PresetUtils method loadPresets.

private void loadPresets() {
    FileObject folder = FileUtil.getConfigFile("previewpresets");
    if (folder != null) {
        for (FileObject child : folder.getChildren()) {
            if (child.isValid() && child.hasExt("xml")) {
                try {
                    InputStream stream = child.getInputStream();
                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder builder = factory.newDocumentBuilder();
                    Document document = builder.parse(stream);
                    PreviewPreset preset = readXML(document);
                    addPreset(preset);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) InputStream(java.io.InputStream) FileObject(org.openide.filesystems.FileObject) Document(org.w3c.dom.Document) PreviewPreset(org.gephi.preview.api.PreviewPreset)

Example 23 with FileObject

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

the class PresetUtils method savePreset.

public void savePreset(PreviewPreset preset) {
    int exist = -1;
    for (int i = 0; i < presets.size(); i++) {
        PreviewPreset p = presets.get(i);
        if (p.getName().equals(preset.getName())) {
            exist = i;
            break;
        }
    }
    if (exist == -1) {
        addPreset(preset);
    } else {
        presets.set(exist, preset);
    }
    try {
        //Create file if dont exist
        FileObject folder = FileUtil.getConfigFile("previewpresets");
        if (folder == null) {
            folder = FileUtil.getConfigRoot().createFolder("previewpresets");
        }
        FileObject presetFile = folder.getFileObject(preset.getName(), "xml");
        if (presetFile == null) {
            presetFile = folder.createData(preset.getName(), "xml");
        }
        //Create doc
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        final Document document = documentBuilder.newDocument();
        document.setXmlVersion("1.0");
        document.setXmlStandalone(true);
        //Write doc
        writeXML(document, preset);
        //Write XML file
        Source source = new DOMSource(document);
        Result result = new StreamResult(FileUtil.toFile(presetFile));
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(source, result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileObject(org.openide.filesystems.FileObject) Document(org.w3c.dom.Document) PreviewPreset(org.gephi.preview.api.PreviewPreset) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 24 with FileObject

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

the class DirectoryChooserUI method deleteAction.

// remove multiple directories
private void deleteAction() {
    // fixed #97079 to be able to delete one or more folders
    final TreePath[] nodePath = tree.getSelectionPaths();
    if (nodePath == null) {
        return;
    }
    String message = "";
    if (nodePath.length == 1) {
        File file = ((DirectoryNode) nodePath[0].getLastPathComponent()).getFile();
        // Don't do anything if it's a special file
        if (!canWrite(file)) {
            return;
        }
        message = MessageFormat.format(getBundle().getString("MSG_Delete"), file.getName());
    } else {
        message = MessageFormat.format(getBundle().getString("MSG_Delete_Multiple"), nodePath.length);
    }
    int answer = JOptionPane.showConfirmDialog(fileChooser, message, getBundle().getString("MSG_Confirm"), JOptionPane.YES_NO_OPTION);
    if (answer == JOptionPane.YES_OPTION) {
        RequestProcessor.getDefault().post(new Runnable() {

            DirectoryNode node;

            ArrayList<File> list = new ArrayList<File>();

            int cannotDelete;

            ArrayList<DirectoryNode> nodes2Remove = new ArrayList<DirectoryNode>(nodePath.length);

            @Override
            public void run() {
                if (!EventQueue.isDispatchThread()) {
                    // first pass, out of EQ thread, deletes files
                    setCursor(fileChooser, Cursor.WAIT_CURSOR);
                    cannotDelete = 0;
                    for (int i = 0; i < nodePath.length; i++) {
                        DirectoryNode nodeToDelete = (DirectoryNode) nodePath[i].getLastPathComponent();
                        try {
                            FileObject fo = FileUtil.toFileObject(nodeToDelete.getFile());
                            fo.delete();
                            nodes2Remove.add(nodeToDelete);
                        } catch (IOException ignore) {
                            cannotDelete++;
                            if (canWrite(nodeToDelete.getFile())) {
                                list.add(nodeToDelete.getFile());
                            }
                        }
                    }
                    // send to second pass
                    EventQueue.invokeLater(this);
                } else {
                    // second pass, in EQ thread
                    for (DirectoryNode curNode : nodes2Remove) {
                        model.removeNodeFromParent(curNode);
                    }
                    setCursor(fileChooser, Cursor.DEFAULT_CURSOR);
                    if (cannotDelete > 0) {
                        String message = "";
                        if (cannotDelete == 1) {
                            message = cannotDelete + " " + getBundle().getString("MSG_Sing_Delete");
                        } else {
                            message = cannotDelete + " " + getBundle().getString("MSG_Plur_Delete");
                        }
                        setSelected((File[]) list.toArray(new File[list.size()]));
                        JOptionPane.showConfirmDialog(fileChooser, message, getBundle().getString("MSG_Confirm"), JOptionPane.OK_OPTION);
                    } else {
                        setSelected(new File[] { null });
                        setFileName("");
                    }
                }
            }
        });
    }
}
Also used : IOException(java.io.IOException) TreePath(javax.swing.tree.TreePath) FileObject(org.openide.filesystems.FileObject) File(java.io.File)

Example 25 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)

Aggregations

FileObject (org.openide.filesystems.FileObject)34 File (java.io.File)15 IOException (java.io.IOException)9 NotifyDescriptor (org.openide.NotifyDescriptor)6 ImportControllerUI (org.gephi.desktop.importer.api.ImportControllerUI)5 ActionEvent (java.awt.event.ActionEvent)4 InputStream (java.io.InputStream)4 JPanel (javax.swing.JPanel)4 DocumentBuilder (javax.xml.parsers.DocumentBuilder)4 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)4 ProjectControllerUI (org.gephi.desktop.project.api.ProjectControllerUI)4 Document (org.w3c.dom.Document)4 ActionListener (java.awt.event.ActionListener)3 ArrayList (java.util.ArrayList)3 JFileChooser (javax.swing.JFileChooser)3 MostRecentFiles (org.gephi.desktop.mrufiles.api.MostRecentFiles)3 FileImporter (org.gephi.io.importer.spi.FileImporter)3 DialogFileFilter (org.gephi.ui.utils.DialogFileFilter)3 ClassPath (org.netbeans.api.java.classpath.ClassPath)3 DialogDescriptor (org.openide.DialogDescriptor)3