Search in sources :

Example 6 with FileObject

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

the class LayoutPresetPersistence method loadPresets.

private void loadPresets() {
    FileObject folder = FileUtil.getConfigFile("layoutpresets");
    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);
                    Preset preset = new Preset(document);
                    addPreset(preset);
                } catch (Exception e) {
                    Logger.getLogger("").log(Level.SEVERE, "Error while reading preset file", e);
                }
            }
        }
    }
}
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) IOException(java.io.IOException)

Example 7 with FileObject

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

the class LayoutPresetPersistence method savePreset.

public void savePreset(String name, Layout layout) {
    Preset preset = addPreset(new Preset(name, layout));
    FileOutputStream fos = null;
    try {
        //Create file if dont exist
        FileObject folder = FileUtil.getConfigFile("layoutpresets");
        if (folder == null) {
            folder = FileUtil.getConfigRoot().createFolder("layoutpresets");
        }
        File presetFile = new File(FileUtil.toFile(folder), name + ".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
        preset.writeXML(document);
        //Write XML file
        fos = new FileOutputStream(presetFile);
        Source source = new DOMSource(document);
        Result result = new StreamResult(fos);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(source, result);
    } catch (Exception e) {
        Logger.getLogger("").log(Level.SEVERE, "Error while writing preset file", e);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException 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) IOException(java.io.IOException) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) IOException(java.io.IOException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) FileObject(org.openide.filesystems.FileObject) File(java.io.File)

Example 8 with FileObject

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

the class VectorialFileExporterUI method action.

@Override
public void action() {
    final String LAST_PATH = "VectorialFileExporterUI_Last_Path";
    final String LAST_PATH_DEFAULT = "VectorialFileExporterUI_Last_Path_Default";
    final ExportControllerUI exportController = Lookup.getDefault().lookup(ExportControllerUI.class);
    if (exportController == null) {
        return;
    }
    //Get last directory
    String lastPathDefault = NbPreferences.forModule(VectorialFileExporterUI.class).get(LAST_PATH_DEFAULT, null);
    String lastPath = NbPreferences.forModule(VectorialFileExporterUI.class).get(LAST_PATH, lastPathDefault);
    //Options panel
    FlowLayout layout = new FlowLayout(FlowLayout.RIGHT);
    JPanel optionsPanel = new JPanel(layout);
    final JButton optionsButton = new JButton(NbBundle.getMessage(VectorialFileExporterUI.class, "VectorialFileExporterUI_optionsButton_name"));
    optionsPanel.add(optionsButton);
    optionsButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            ExporterUI exporterUI = exportController.getExportController().getUI(selectedExporter);
            if (exporterUI != null) {
                JPanel panel = exporterUI.getPanel();
                exporterUI.setup(selectedExporter);
                DialogDescriptor dd = new DialogDescriptor(panel, NbBundle.getMessage(VectorialFileExporterUI.class, "VectorialFileExporterUI_optionsDialog_title", selectedBuilder.getName()));
                TopDialog topDialog = new TopDialog(dialog, dd.getTitle(), dd.isModal(), dd, dd.getClosingOptions(), dd.getButtonListener());
                topDialog.setVisible(true);
                Object result = (dd.getValue() != null) ? dd.getValue() : NotifyDescriptor.CLOSED_OPTION;
                //                    Object result = DialogDisplayer.getDefault().notify(dd);
                exporterUI.unsetup(result == NotifyDescriptor.OK_OPTION);
            }
        }
    });
    //Graph Settings Panel
    final JPanel southPanel = new JPanel(new BorderLayout());
    southPanel.add(optionsPanel, BorderLayout.NORTH);
    //Optionable file chooser
    final JFileChooser chooser = new JFileChooser(lastPath) {

        @Override
        protected JDialog createDialog(Component parent) throws HeadlessException {
            dialog = super.createDialog(parent);
            Component c = dialog.getContentPane().getComponent(0);
            if (c != null && c instanceof JComponent) {
                Insets insets = ((JComponent) c).getInsets();
                southPanel.setBorder(BorderFactory.createEmptyBorder(insets.top, insets.left, insets.bottom, insets.right));
            }
            dialog.getContentPane().add(southPanel, BorderLayout.SOUTH);
            return dialog;
        }

        @Override
        public void approveSelection() {
            if (canExport(this)) {
                super.approveSelection();
            }
        }
    };
    chooser.setDialogTitle(NbBundle.getMessage(VectorialFileExporterUI.class, "VectorialFileExporterUI_filechooser_title"));
    chooser.addPropertyChangeListener(JFileChooser.FILE_FILTER_CHANGED_PROPERTY, new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            DialogFileFilter fileFilter = (DialogFileFilter) evt.getNewValue();
            //Options panel enabling
            selectedBuilder = getExporter(fileFilter);
            if (selectedBuilder != null) {
                selectedExporter = selectedBuilder.buildExporter();
            }
            if (selectedExporter != null && exportController.getExportController().getUI(selectedExporter) != null) {
                optionsButton.setEnabled(true);
            } else {
                optionsButton.setEnabled(false);
            }
            //Selected file extension change
            if (selectedFile != null && fileFilter != null) {
                String fileName = selectedFile.getName();
                String directoryPath = chooser.getCurrentDirectory().getAbsolutePath();
                if (fileName.lastIndexOf(".") != -1) {
                    fileName = fileName.substring(0, fileName.lastIndexOf("."));
                    fileName = fileName.concat(fileFilter.getExtensions().get(0));
                    selectedFile = new File(directoryPath, fileName);
                    chooser.setSelectedFile(selectedFile);
                }
            }
        }
    });
    chooser.addPropertyChangeListener(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY, new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getNewValue() != null) {
                selectedFile = (File) evt.getNewValue();
            }
        }
    });
    //File filters
    DialogFileFilter defaultFilter = null;
    for (VectorFileExporterBuilder vectorFileExporter : Lookup.getDefault().lookupAll(VectorFileExporterBuilder.class)) {
        for (FileType fileType : vectorFileExporter.getFileTypes()) {
            DialogFileFilter dialogFileFilter = new DialogFileFilter(fileType.getName());
            dialogFileFilter.addExtensions(fileType.getExtensions());
            if (defaultFilter == null) {
                defaultFilter = dialogFileFilter;
            }
            chooser.addChoosableFileFilter(dialogFileFilter);
        }
    }
    chooser.setAcceptAllFileFilterUsed(false);
    chooser.setFileFilter(defaultFilter);
    selectedFile = new File(chooser.getCurrentDirectory(), "Untitled" + defaultFilter.getExtensions().get(0));
    chooser.setSelectedFile(selectedFile);
    //Show
    int returnFile = chooser.showSaveDialog(null);
    if (returnFile == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        file = FileUtil.normalizeFile(file);
        FileObject fileObject = FileUtil.toFileObject(file);
        //Save last path
        NbPreferences.forModule(VectorialFileExporterUI.class).put(LAST_PATH, file.getAbsolutePath());
        //Do
        exportController.exportFile(fileObject, selectedExporter);
    }
    dialog = null;
}
Also used : JPanel(javax.swing.JPanel) VectorFileExporterBuilder(org.gephi.io.exporter.spi.VectorFileExporterBuilder) FlowLayout(java.awt.FlowLayout) Insets(java.awt.Insets) ExporterUI(org.gephi.io.exporter.spi.ExporterUI) PropertyChangeListener(java.beans.PropertyChangeListener) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) BorderLayout(java.awt.BorderLayout) FileObject(org.openide.filesystems.FileObject) JComponent(javax.swing.JComponent) Component(java.awt.Component) DialogFileFilter(org.gephi.ui.utils.DialogFileFilter) PropertyChangeEvent(java.beans.PropertyChangeEvent) JComponent(javax.swing.JComponent) ExportControllerUI(org.gephi.desktop.io.export.ExportControllerUI) ActionListener(java.awt.event.ActionListener) JFileChooser(javax.swing.JFileChooser) FileType(org.gephi.io.exporter.api.FileType) DialogDescriptor(org.openide.DialogDescriptor) FileObject(org.openide.filesystems.FileObject) File(java.io.File)

Example 9 with FileObject

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

the class ImportControllerImpl method getFileImporter.

@Override
public FileImporter getFileImporter(File file) {
    FileObject fileObject = FileUtil.toFileObject(file);
    //Unzip and return content file
    fileObject = getArchivedFile(fileObject);
    FileImporterBuilder builder = getMatchingImporter(fileObject);
    if (fileObject != null && builder != null) {
        FileImporter fi = builder.buildImporter();
        return fi;
    }
    return null;
}
Also used : FileImporterBuilder(org.gephi.io.importer.spi.FileImporterBuilder) FileImporter(org.gephi.io.importer.spi.FileImporter) FileObject(org.openide.filesystems.FileObject)

Example 10 with FileObject

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

the class WelcomeTopComponent method initAction.

private void initAction() {
    openAction = new AbstractAction("", ImageUtilities.loadImageIcon("org/gephi/desktop/welcome/resources/gephifile20.png", false)) {

        @Override
        public void actionPerformed(ActionEvent e) {
            JXHyperlink link = (JXHyperlink) e.getSource();
            File file = (File) link.getClientProperty(LINK_PATH);
            FileObject fileObject = FileUtil.toFileObject(file);
            if (fileObject.hasExt(GEPHI_EXTENSION)) {
                ProjectControllerUI pc = Lookup.getDefault().lookup(ProjectControllerUI.class);
                try {
                    pc.openProject(file);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    NotifyDescriptor.Message msg = new NotifyDescriptor.Message(NbBundle.getMessage(WelcomeTopComponent.class, "WelcomeTopComponent.openGephiError"), NotifyDescriptor.WARNING_MESSAGE);
                    DialogDisplayer.getDefault().notify(msg);
                }
            } else {
                ImportControllerUI importController = Lookup.getDefault().lookup(ImportControllerUI.class);
                if (importController.getImportController().isFileSupported(FileUtil.toFile(fileObject))) {
                    importController.importFile(fileObject);
                }
            }
            closeDialog();
        }
    };
    newProjectLink.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            ProjectControllerUI pc = Lookup.getDefault().lookup(ProjectControllerUI.class);
            pc.newProject();
            closeDialog();
        }
    });
    openFileLink.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            ProjectControllerUI pc = Lookup.getDefault().lookup(ProjectControllerUI.class);
            pc.openFile();
            closeDialog();
        }
    });
}
Also used : ActionEvent(java.awt.event.ActionEvent) JXHyperlink(org.jdesktop.swingx.JXHyperlink) NotifyDescriptor(org.openide.NotifyDescriptor) ActionListener(java.awt.event.ActionListener) ImportControllerUI(org.gephi.desktop.importer.api.ImportControllerUI) FileObject(org.openide.filesystems.FileObject) AbstractAction(javax.swing.AbstractAction) File(java.io.File) ProjectControllerUI(org.gephi.desktop.project.api.ProjectControllerUI)

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