Search in sources :

Example 16 with JFileChooser

use of javax.swing.JFileChooser in project gephi by gephi.

the class GraphFileExporterUI method action.

@Override
public void action() {
    final String LAST_PATH = "GraphFileExporterUI_Last_Path";
    final String LAST_PATH_DEFAULT = "GraphFileExporterUI_Last_Path_Default";
    final ExportControllerUI exportController = Lookup.getDefault().lookup(ExportControllerUI.class);
    if (exportController == null) {
        return;
    }
    //Get last directory
    String lastPathDefault = NbPreferences.forModule(GraphFileExporterUI.class).get(LAST_PATH_DEFAULT, null);
    String lastPath = NbPreferences.forModule(GraphFileExporterUI.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(GraphFileExporterUI.class, "GraphFileExporterUI_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(GraphFileExporterUI.class, "GraphFileExporterUI_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);
    GraphFileExporterUIPanel graphSettings = new GraphFileExporterUIPanel();
    graphSettings.setVisibleOnlyGraph(visibleOnlyGraph);
    southPanel.add(graphSettings, BorderLayout.CENTER);
    //Optionable file chooser
    final JFileChooser chooser = new JFileChooser(lastPath) {

        @Override
        protected JDialog createDialog(Component parent) throws HeadlessException {
            dialog = super.createDialog(parent);
            dialog.setSize(640, 480);
            dialog.setResizable(true);
            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(GraphFileExporterUI.class, "GraphFileExporterUI_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 (selectedBuilder != 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 (GraphFileExporterBuilder graphFileExporter : Lookup.getDefault().lookupAll(GraphFileExporterBuilder.class)) {
        for (FileType fileType : graphFileExporter.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(GraphFileExporterUI.class).put(LAST_PATH, file.getAbsolutePath());
        //Save variable
        visibleOnlyGraph = graphSettings.isVisibleOnlyGraph();
        //Do
        selectedExporter.setExportVisible(visibleOnlyGraph);
        exportController.exportFile(fileObject, selectedExporter);
    }
    dialog = null;
}
Also used : JPanel(javax.swing.JPanel) GraphFileExporterBuilder(org.gephi.io.exporter.spi.GraphFileExporterBuilder) 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) GraphFileExporterUIPanel(org.gephi.desktop.io.export.GraphFileExporterUIPanel) 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 17 with JFileChooser

use of javax.swing.JFileChooser 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 18 with JFileChooser

use of javax.swing.JFileChooser in project gephi by gephi.

the class ProjectControllerUIImpl method saveAsProject.

@Override
public void saveAsProject() {
    final String LAST_PATH = "SaveAsProject_Last_Path";
    final String LAST_PATH_DEFAULT = "SaveAsProject_Last_Path_Default";
    DialogFileFilter filter = new DialogFileFilter(NbBundle.getMessage(ProjectControllerUIImpl.class, "SaveAsProject_filechooser_filter"));
    filter.addExtension(".gephi");
    //Get last directory
    String lastPathDefault = NbPreferences.forModule(ProjectControllerUIImpl.class).get(LAST_PATH_DEFAULT, null);
    String lastPath = NbPreferences.forModule(ProjectControllerUIImpl.class).get(LAST_PATH, lastPathDefault);
    //File chooser
    final JFileChooser chooser = new JFileChooser(lastPath);
    chooser.addChoosableFileFilter(filter);
    int returnFile = chooser.showSaveDialog(null);
    if (returnFile == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        //Save last path
        NbPreferences.forModule(ProjectControllerUIImpl.class).put(LAST_PATH, file.getAbsolutePath());
        //File management
        try {
            if (!file.getPath().endsWith(".gephi")) {
                file = new File(file.getPath() + ".gephi");
            }
            if (!file.exists()) {
                if (!file.createNewFile()) {
                    String failMsg = NbBundle.getMessage(ProjectControllerUIImpl.class, "SaveAsProject_SaveFailed", new Object[] { file.getPath() });
                    JOptionPane.showMessageDialog(null, failMsg);
                    return;
                }
            } else {
                String overwriteMsg = NbBundle.getMessage(ProjectControllerUIImpl.class, "SaveAsProject_Overwrite", new Object[] { file.getPath() });
                if (JOptionPane.showConfirmDialog(null, overwriteMsg) != JOptionPane.OK_OPTION) {
                    return;
                }
            }
            file = FileUtil.normalizeFile(file);
            final String SaveAsFileName = file.getName();
            //File exist now, Save project
            Project project = controller.getCurrentProject();
            saveProject(project, file);
            //Modifying Title bar
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    JFrame frame = (JFrame) WindowManager.getDefault().getMainWindow();
                    String title = frame.getTitle();
                    title = title.substring(0, title.indexOf(" - ")) + " - " + SaveAsFileName;
                    frame.setTitle(title);
                }
            });
        } catch (Exception e) {
            Logger.getLogger("").log(Level.WARNING, "", e);
        }
    }
}
Also used : Project(org.gephi.project.api.Project) JFileChooser(javax.swing.JFileChooser) JFrame(javax.swing.JFrame) DialogFileFilter(org.gephi.ui.utils.DialogFileFilter) File(java.io.File)

Example 19 with JFileChooser

use of javax.swing.JFileChooser in project gephi by gephi.

the class ProjectControllerUIImpl method openFile.

@Override
public void openFile() {
    final String LAST_PATH = "OpenFile_Last_Path";
    final String LAST_PATH_DEFAULT = "OpenFile_Last_Path_Default";
    //Get last directory
    String lastPathDefault = NbPreferences.forModule(ProjectControllerUIImpl.class).get(LAST_PATH_DEFAULT, null);
    String lastPath = NbPreferences.forModule(ProjectControllerUIImpl.class).get(LAST_PATH, lastPathDefault);
    //Init dialog
    final JFileChooser chooser = new JFileChooser(lastPath);
    chooser.setMultiSelectionEnabled(true);
    DialogFileFilter gephiFilter = new DialogFileFilter(NbBundle.getMessage(ProjectControllerUIImpl.class, "OpenProject_filechooser_filter"));
    gephiFilter.addExtension(".gephi");
    DialogFileFilter graphFilter = new DialogFileFilter(NbBundle.getMessage(getClass(), "OpenFile_filechooser_graphfilter"));
    graphFilter.addExtension(".gephi");
    ImportControllerUI importController = Lookup.getDefault().lookup(ImportControllerUI.class);
    for (FileType fileType : importController.getImportController().getFileTypes()) {
        DialogFileFilter dialogFileFilter = new DialogFileFilter(fileType.getName());
        dialogFileFilter.addExtensions(fileType.getExtensions());
        chooser.addChoosableFileFilter(dialogFileFilter);
        graphFilter.addExtensions(fileType.getExtensions());
    }
    DialogFileFilter zipFileFilter = new DialogFileFilter(NbBundle.getMessage(getClass(), "OpenFile_filechooser_zipfilter"));
    zipFileFilter.addExtensions(new String[] { ".zip", ".gz", ".bz2" });
    chooser.addChoosableFileFilter(zipFileFilter);
    chooser.addChoosableFileFilter(gephiFilter);
    chooser.addChoosableFileFilter(graphFilter);
    //Open dialog
    int returnFile = chooser.showOpenDialog(null);
    if (returnFile == JFileChooser.APPROVE_OPTION) {
        File[] files = chooser.getSelectedFiles();
        FileObject[] fileObjects = new FileObject[files.length];
        int i = 0;
        File gephiFile = null;
        for (File file : files) {
            file = FileUtil.normalizeFile(file);
            FileObject fileObject = FileUtil.toFileObject(file);
            fileObjects[i++] = fileObject;
            if (fileObject.getExt().equalsIgnoreCase("gephi")) {
                if (gephiFile != null) {
                    NotifyDescriptor.Message msg = new NotifyDescriptor.Message(NbBundle.getMessage(ProjectControllerUIImpl.class, "ProjectControllerUI.error.multipleGephi"), NotifyDescriptor.ERROR_MESSAGE);
                    DialogDisplayer.getDefault().notify(msg);
                    return;
                } else {
                    gephiFile = file;
                }
            }
            //Save last path
            NbPreferences.forModule(ProjectControllerUIImpl.class).put(LAST_PATH, file.getAbsolutePath());
        }
        if (gephiFile != null) {
            //Project
            if (controller.getCurrentProject() != null) {
                if (!closeCurrentProject()) {
                    return;
                }
            }
            try {
                loadProject(gephiFile);
            } catch (Exception ew) {
                ew.printStackTrace();
                NotifyDescriptor.Message msg = new NotifyDescriptor.Message(NbBundle.getMessage(ProjectControllerUIImpl.class, "OpenProject.defaulterror"), NotifyDescriptor.WARNING_MESSAGE);
                DialogDisplayer.getDefault().notify(msg);
            }
        } else {
            //Import
            importController.importFiles(fileObjects);
        }
    }
}
Also used : NotifyDescriptor(org.openide.NotifyDescriptor) JFileChooser(javax.swing.JFileChooser) FileType(org.gephi.io.importer.api.FileType) ImportControllerUI(org.gephi.desktop.importer.api.ImportControllerUI) FileObject(org.openide.filesystems.FileObject) DialogFileFilter(org.gephi.ui.utils.DialogFileFilter) File(java.io.File)

Example 20 with JFileChooser

use of javax.swing.JFileChooser in project android-classyshark by google.

the class ClassySharkPanel method openArchive.

@Override
public void openArchive() {
    final JFileChooser fc = new JFileChooser();
    fc.setFileFilter(new FileFilter() {

        @Override
        public boolean accept(File f) {
            return FileChooserUtils.acceptFile(f);
        }

        @Override
        public String getDescription() {
            return FileChooserUtils.getFileChooserDescription();
        }
    });
    fc.setCurrentDirectory(CurrentFolderConfig.INSTANCE.getCurrentDirectory());
    int returnVal = fc.showOpenDialog(this);
    toolbar.setText("");
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File resultFile = fc.getSelectedFile();
        CurrentFolderConfig.INSTANCE.setCurrentDirectory(fc.getCurrentDirectory());
        RecentArchivesConfig.INSTANCE.addArchive(resultFile.getName(), fc.getCurrentDirectory());
        displayArchive(resultFile);
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) FileFilter(javax.swing.filechooser.FileFilter) File(java.io.File)

Aggregations

JFileChooser (javax.swing.JFileChooser)273 File (java.io.File)157 IOException (java.io.IOException)55 FileFilter (javax.swing.filechooser.FileFilter)40 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)29 ActionEvent (java.awt.event.ActionEvent)25 ActionListener (java.awt.event.ActionListener)20 JButton (javax.swing.JButton)18 FileOutputStream (java.io.FileOutputStream)17 JPanel (javax.swing.JPanel)15 Preferences (java.util.prefs.Preferences)12 Component (java.awt.Component)11 Point (java.awt.Point)11 JMenuItem (javax.swing.JMenuItem)11 FileNotFoundException (java.io.FileNotFoundException)10 JLabel (javax.swing.JLabel)10 JFrame (javax.swing.JFrame)9 ResourceBundle (java.util.ResourceBundle)8 BorderLayout (java.awt.BorderLayout)7 FileDialog (java.awt.FileDialog)7