Search in sources :

Example 51 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<>();

            int cannotDelete;

            ArrayList<DirectoryNode> nodes2Remove = new ArrayList<>(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 52 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");
        }
        // Safe filename
        String filename = DigestUtils.sha1Hex(preset.getName());
        FileObject presetFile = folder.getFileObject(filename, "xml");
        if (presetFile == null) {
            presetFile = folder.createData(filename, "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
        try (OutputStream outputStream = presetFile.getOutputStream()) {
            Source source = new DOMSource(document);
            Result result = new StreamResult(outputStream);
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.transform(source, result);
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) OutputStream(java.io.OutputStream) 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) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileObject(org.openide.filesystems.FileObject)

Example 53 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) {
                    Exceptions.printStackTrace(ex);
                }
            }
        }
    }
}
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 54 with FileObject

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

the class DesktopImportControllerUI method importFiles.

private void importFiles(final Reader[] readers, final FileImporter[] importers, FileObject[] fileObjects) {
    try {
        File[] files = new File[readers.length];
        Map<ImporterUI, List<FileImporter>> importerUIs = new HashMap<>();
        for (int i = 0; i < importers.length; i++) {
            FileImporter importer = importers[i];
            ImporterUI ui = controller.getUI(importer);
            if (ui != null) {
                List<FileImporter> l = importerUIs.get(ui);
                if (l == null) {
                    l = new ArrayList<>();
                    importerUIs.put(ui, l);
                }
                l.add(importer);
            }
            if (importer instanceof FileImporter.FileAware) {
                try (Reader reader = readers[i]) {
                    File file = null;
                    if (fileObjects != null) {
                        file = FileUtil.toFile(fileObjects[i]);
                    }
                    if (file == null) {
                        // There is no source file but the importer needs it, create temporary copy:
                        String fileName = "tmp_file" + 1;
                        String charset = "UTF-8";
                        if (fileObjects != null && fileObjects[i] != null) {
                            // Netbeans FileUtil.toFile bug returning null?? Try to recover:
                            fileName = fileObjects[i].getNameExt();
                            CharsetToolkit charsetToolkit = new CharsetToolkit(fileObjects[i].getInputStream());
                            charset = charsetToolkit.getCharset().name();
                        }
                        file = TempDirUtils.createTempDir().createFile(fileName);
                        try (FileOutputStream fos = new FileOutputStream(file)) {
                            FileUtil.copy(new ReaderInputStream(reader, charset), fos);
                        }
                    }
                    files[i] = file;
                    ((FileImporter.FileAware) importer).setFile(file);
                }
            }
        }
        for (Map.Entry<ImporterUI, List<FileImporter>> entry : importerUIs.entrySet()) {
            ImporterUI ui = entry.getKey();
            String title = NbBundle.getMessage(DesktopImportControllerUI.class, "DesktopImportControllerUI.file.ui.dialog.title", ui.getDisplayName());
            JPanel panel = ui.getPanel();
            FileImporter[] fi = (FileImporter[]) entry.getValue().toArray((FileImporter[]) Array.newInstance(entry.getValue().get(0).getClass(), 0));
            ui.setup(fi);
            if (panel != null) {
                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;
                }
            }
            if (ui instanceof ImporterUI.WithWizard) {
                boolean finishedOk = showWizard(ui, ((ImporterUI.WithWizard) ui).getWizardDescriptor());
                if (!finishedOk) {
                    ui.unsetup(false);
                    return;
                }
            }
            ui.unsetup(true);
        }
        final List<Container> results = new ArrayList<>();
        for (int i = 0; i < importers.length; i++) {
            doImport(results, readers[i], files[i], importers[i]);
        }
        executor.execute(null, new Runnable() {

            @Override
            public void run() {
                if (!results.isEmpty()) {
                    finishImport(results.toArray(new Container[0]));
                }
            }
        });
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    }
}
Also used : ImporterUI(org.gephi.io.importer.spi.ImporterUI) JPanel(javax.swing.JPanel) HashMap(java.util.HashMap) CharsetToolkit(org.gephi.utils.CharsetToolkit) FileImporter(org.gephi.io.importer.spi.FileImporter) ArrayList(java.util.ArrayList) Reader(java.io.Reader) Container(org.gephi.io.importer.api.Container) List(java.util.List) ArrayList(java.util.ArrayList) ChangeListener(javax.swing.event.ChangeListener) ValidationPanel(org.netbeans.validation.api.ui.ValidationPanel) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) ReaderInputStream(org.openide.util.io.ReaderInputStream) ChangeEvent(javax.swing.event.ChangeEvent) FileOutputStream(java.io.FileOutputStream) DialogDescriptor(org.openide.DialogDescriptor) FileObject(org.openide.filesystems.FileObject) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap)

Example 55 with FileObject

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

the class CommandLineProcessor method process.

@Override
public void process(Env env, Map values) throws CommandException {
    List<String> filenameList = new ArrayList<>();
    Object obj = values.get(openOption);
    if (obj != null) {
        filenameList.addAll(Arrays.asList((String[]) obj));
    }
    obj = values.get(openOption2);
    if (obj != null) {
        filenameList.addAll(Arrays.asList((String[]) obj));
    }
    try {
        for (int i = 0; i < filenameList.size(); i++) {
            File file = new File(filenameList.get(i));
            if (!file.isAbsolute()) {
                file = new File(env.getCurrentDirectory(), filenameList.get(i));
            }
            FileObject fileObject = FileUtil.toFileObject(file);
            if (!file.exists()) {
                NotifyDescriptor.Message msg = new NotifyDescriptor.Message(NbBundle.getMessage(CommandLineProcessor.class, "CommandLineProcessor.fileNotFound", file.getName()), NotifyDescriptor.WARNING_MESSAGE);
                DialogDisplayer.getDefault().notify(msg);
                return;
            }
            if (fileObject.hasExt(GEPHI_EXTENSION)) {
                ProjectControllerUI pc = Lookup.getDefault().lookup(ProjectControllerUI.class);
                try {
                    pc.openProject(file);
                } catch (Exception ew) {
                    Exceptions.printStackTrace(ew);
                    NotifyDescriptor.Message msg = new NotifyDescriptor.Message(NbBundle.getMessage(CommandLineProcessor.class, "CommandLineProcessor.openGephiError"), NotifyDescriptor.WARNING_MESSAGE);
                    DialogDisplayer.getDefault().notify(msg);
                }
                return;
            } else {
                ImportControllerUI importController = Lookup.getDefault().lookup(ImportControllerUI.class);
                if (importController.getImportController().isFileSupported(FileUtil.toFile(fileObject))) {
                    importController.importFile(fileObject);
                } else {
                    NotifyDescriptor.Message msg = new NotifyDescriptor.Message(NbBundle.getMessage(CommandLineProcessor.class, "CommandLineProcessor.fileNotSupported"), NotifyDescriptor.WARNING_MESSAGE);
                    DialogDisplayer.getDefault().notify(msg);
                }
            }
        }
    } catch (OutOfMemoryError ex) {
        System.gc();
        NotifyDescriptor nd = new NotifyDescriptor.Message(MEMORY_ERROR, NotifyDescriptor.ERROR_MESSAGE);
        DialogDisplayer.getDefault().notify(nd);
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
        NotifyDescriptor nd = new NotifyDescriptor.Message("CommandLineParsing " + ex.getMessage(), NotifyDescriptor.ERROR_MESSAGE);
        DialogDisplayer.getDefault().notify(nd);
    }
}
Also used : ArrayList(java.util.ArrayList) CommandException(org.netbeans.api.sendopts.CommandException) NotifyDescriptor(org.openide.NotifyDescriptor) ImportControllerUI(org.gephi.desktop.importer.api.ImportControllerUI) FileObject(org.openide.filesystems.FileObject) FileObject(org.openide.filesystems.FileObject) File(java.io.File) ProjectControllerUI(org.gephi.desktop.project.api.ProjectControllerUI)

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