Search in sources :

Example 6 with FileChooserFilter

use of org.cytoscape.util.swing.FileChooserFilter in project cytoscape-impl by cytoscape.

the class SessionHandler method handleEvent.

@Override
public void handleEvent(final CyShutdownEvent e) {
    final CyNetworkManager netMgr = serviceRegistrar.getService(CyNetworkManager.class);
    // If there are no networks, just quit.
    if (netMgr.getNetworkSet().isEmpty() || e.forceShutdown())
        return;
    // Ask user whether to save current session or not.
    final String msg = "Do you want to save your session?";
    final String header = "Save Networks Before Quitting?";
    final Object[] options = { "Yes, save and quit", "No, just quit", "Cancel" };
    final int n = JOptionPane.showOptionDialog(desktop.getJFrame(), msg, header, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
    if (n == JOptionPane.NO_OPTION) {
        return;
    } else if (n == JOptionPane.YES_OPTION) {
        final CySessionManager sessionMgr = serviceRegistrar.getService(CySessionManager.class);
        final String sessionFileName = sessionMgr.getCurrentSessionFileName();
        final File file;
        if (sessionFileName == null || sessionFileName.isEmpty()) {
            FileChooserFilter filter = new FileChooserFilter("Session File", "cys");
            List<FileChooserFilter> filterCollection = new ArrayList<FileChooserFilter>(1);
            filterCollection.add(filter);
            final FileUtil fileUtil = serviceRegistrar.getService(FileUtil.class);
            file = fileUtil.getFile(desktop, "Save Session File", FileUtil.SAVE, filterCollection);
        } else {
            file = new File(sessionFileName);
        }
        if (file == null) {
            // just check the file again in case the file chooser dialoge task is canceled.
            e.abortShutdown("User canceled the shutdown request.");
            return;
        }
        final SynchronousTaskManager<?> syncTaskMgr = serviceRegistrar.getService(SynchronousTaskManager.class);
        final SaveSessionAsTaskFactory saveTaskFactory = serviceRegistrar.getService(SaveSessionAsTaskFactory.class);
        syncTaskMgr.execute(saveTaskFactory.createTaskIterator(file));
        return;
    } else {
        e.abortShutdown("User canceled the shutdown request.");
        return;
    }
}
Also used : SynchronousTaskManager(org.cytoscape.work.SynchronousTaskManager) FileChooserFilter(org.cytoscape.util.swing.FileChooserFilter) CyNetworkManager(org.cytoscape.model.CyNetworkManager) CySessionManager(org.cytoscape.session.CySessionManager) ArrayList(java.util.ArrayList) List(java.util.List) NetworkList(org.cytoscape.internal.io.networklist.NetworkList) File(java.io.File) FileUtil(org.cytoscape.util.swing.FileUtil) SaveSessionAsTaskFactory(org.cytoscape.task.write.SaveSessionAsTaskFactory)

Example 7 with FileChooserFilter

use of org.cytoscape.util.swing.FileChooserFilter in project cytoscape-impl by cytoscape.

the class FileUtilImpl method addFileExt.

private String addFileExt(final Collection<FileChooserFilter> filters, String fileName) {
    final Set<String> extSet = new LinkedHashSet<>();
    for (final FileChooserFilter filter : filters) {
        final String[] exts = filter.getExtensions();
        for (String ext : exts) extSet.add(ext);
    }
    // because we don't know which one should be chosen.
    if (extSet.size() == 1) {
        // Check file name has the extension
        final String upperName = fileName.toUpperCase();
        final String ext = extSet.iterator().next();
        if (!upperName.endsWith("." + ext.toUpperCase()))
            fileName = fileName + "." + ext;
    }
    return fileName;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FileChooserFilter(org.cytoscape.util.swing.FileChooserFilter)

Example 8 with FileChooserFilter

use of org.cytoscape.util.swing.FileChooserFilter in project cytoscape-impl by cytoscape.

the class FileUtilImpl method getFiles.

@Override
public File[] getFiles(final Component parent, final String title, final int loadSaveCustom, String startDir, final String customApproveText, final boolean multiselect, final Collection<FileChooserFilter> filters) {
    if (parent == null)
        throw new NullPointerException("\"parent\" must not be null.");
    final String osName = System.getProperty("os.name");
    final CyApplicationManager applicationManager = serviceRegistrar.getService(CyApplicationManager.class);
    if (osName.startsWith("Mac")) {
        // This is a Macintosh, use the AWT style file dialog
        final String fileDialogForDirectories = System.getProperty("apple.awt.fileDialogForDirectories");
        System.setProperty("apple.awt.fileDialogForDirectories", "false");
        try {
            final FileDialog chooser;
            if (parent instanceof Frame)
                chooser = new FileDialog((Frame) parent, title, loadSaveCustom);
            else if (parent instanceof Dialog)
                chooser = new FileDialog((Dialog) parent, title, loadSaveCustom);
            else if (parent instanceof JMenuItem) {
                JComponent jcomponent = (JComponent) ((JPopupMenu) parent.getParent()).getInvoker();
                chooser = new FileDialog((Frame) jcomponent.getTopLevelAncestor(), title, loadSaveCustom);
            } else {
                throw new IllegalArgumentException("Cannot (not implemented yet) create a dialog " + "own by a parent component of type: " + parent.getClass().getCanonicalName());
            }
            if (startDir != null)
                chooser.setDirectory(startDir);
            else
                chooser.setDirectory(applicationManager.getCurrentDirectory().getAbsolutePath());
            chooser.setModal(true);
            chooser.setFilenameFilter(new CombinedFilenameFilter(filters));
            chooser.setLocationRelativeTo(parent);
            chooser.setMultipleMode(multiselect);
            chooser.setVisible(true);
            if (chooser.getFile() != null) {
                final File[] results;
                if (loadSaveCustom == SAVE) {
                    String newFileName = chooser.getFile();
                    final String fileNameWithExt = addFileExt(filters, newFileName);
                    if (!fileNameWithExt.equals(newFileName)) {
                        final File file = new File(chooser.getDirectory() + File.separator + fileNameWithExt);
                        if (file.exists()) {
                            int answer = JOptionPane.showConfirmDialog(parent, "The file '" + file.getName() + "' already exists. Are you sure you want to overwrite it?", "File exists", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                            if (// Try again
                            answer == JOptionPane.NO_OPTION)
                                return getFiles(parent, title, loadSaveCustom, file.getParent(), customApproveText, multiselect, filters);
                        }
                        newFileName = fileNameWithExt;
                    }
                    results = new File[1];
                    results[0] = new File(chooser.getDirectory() + File.separator + newFileName);
                } else
                    results = chooser.getFiles();
                if (chooser.getDirectory() != null)
                    applicationManager.setCurrentDirectory(new File(chooser.getDirectory()));
                return results;
            }
        } finally {
            if (fileDialogForDirectories != null)
                System.setProperty("apple.awt.fileDialogForDirectories", fileDialogForDirectories);
        }
        return null;
    } else {
        // this is not a Mac, use the Swing based file dialog
        final JFileChooser chooser;
        if (startDir != null)
            chooser = new JFileChooser(new File(startDir));
        else
            chooser = new JFileChooser(applicationManager.getCurrentDirectory());
        // set multiple selection, if applicable
        chooser.setMultiSelectionEnabled(multiselect);
        // set the dialog title
        chooser.setDialogTitle(title);
        chooser.setAcceptAllFileFilterUsed(loadSaveCustom == LOAD);
        int i = 0;
        FileChooserFilter defaultFilter = null;
        for (final FileChooserFilter filter : filters) {
            // do it now!
            if (++i == filters.size() && defaultFilter == null)
                defaultFilter = filter;
            else // with "All ", make it the default.
            if (defaultFilter == null && filter.getDescription().startsWith("All "))
                defaultFilter = filter;
            chooser.addChoosableFileFilter(filter);
        }
        File[] results = null;
        File tmp = null;
        // set the dialog type
        if (loadSaveCustom == LOAD) {
            if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
                if (multiselect)
                    results = chooser.getSelectedFiles();
                else if ((tmp = chooser.getSelectedFile()) != null) {
                    results = new File[1];
                    results[0] = tmp;
                }
                if (filters != null && !filters.isEmpty()) {
                    boolean extensionFound = false;
                    for (int k = 0; k < results.length; ++k) {
                        String path = results[k].getPath();
                        for (final FileChooserFilter filter : filters) {
                            String[] filterExtensions = filter.getExtensions();
                            for (int t = 0; t < filterExtensions.length; ++t) {
                                if (filterExtensions[t].equals("") || path.endsWith("." + filterExtensions[t]))
                                    extensionFound = true;
                            }
                        }
                        if (!extensionFound) {
                            JOptionPane.showMessageDialog(chooser, "Cytoscape does not recognize files with suffix '" + path.substring(path.lastIndexOf(".")) + "' . Please choose another file.", "File extension incorrect", JOptionPane.WARNING_MESSAGE);
                            return null;
                        }
                        extensionFound = false;
                    }
                }
            }
        } else if (loadSaveCustom == SAVE) {
            if (chooser.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION) {
                if (multiselect) {
                    results = chooser.getSelectedFiles();
                } else if ((tmp = chooser.getSelectedFile()) != null) {
                    results = new File[1];
                    results[0] = tmp;
                }
                // not, so we need to do so ourselves:
                for (int k = 0; k < results.length; ++k) {
                    File file = results[k];
                    final String filePath = file.getAbsolutePath();
                    final String filePathWithExt = addFileExt(filters, filePath);
                    // Add an extension to the filename, if necessary and possible
                    if (!filePathWithExt.equals(filePath)) {
                        file = new File(filePathWithExt);
                        results[k] = file;
                    }
                    if (file.exists()) {
                        int answer = JOptionPane.showConfirmDialog(chooser, "The file '" + file.getName() + "' already exists. Are you sure you want to overwrite it?", "File exists", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                        if (// Try again
                        answer == JOptionPane.NO_OPTION)
                            return getFiles(parent, title, loadSaveCustom, file.getParent(), customApproveText, multiselect, filters);
                    }
                }
            }
        } else {
            if (chooser.showDialog(parent, customApproveText) == JFileChooser.APPROVE_OPTION) {
                if (multiselect)
                    results = chooser.getSelectedFiles();
                else if ((tmp = chooser.getSelectedFile()) != null) {
                    results = new File[1];
                    results[0] = tmp;
                }
            }
        }
        if (results != null && chooser.getCurrentDirectory().getPath() != null)
            applicationManager.setCurrentDirectory(chooser.getCurrentDirectory());
        return results;
    }
}
Also used : Frame(java.awt.Frame) JComponent(javax.swing.JComponent) FileChooserFilter(org.cytoscape.util.swing.FileChooserFilter) CyApplicationManager(org.cytoscape.application.CyApplicationManager) JFileChooser(javax.swing.JFileChooser) FileDialog(java.awt.FileDialog) Dialog(java.awt.Dialog) JMenuItem(javax.swing.JMenuItem) FileDialog(java.awt.FileDialog) File(java.io.File)

Example 9 with FileChooserFilter

use of org.cytoscape.util.swing.FileChooserFilter in project cytoscape-impl by cytoscape.

the class InstallAppsPanel method installFromFileButtonActionPerformed.

private void installFromFileButtonActionPerformed(ActionEvent evt) {
    List<String> sha1Checksums = new ArrayList<String>();
    for (App app : appManager.getApps()) {
        sha1Checksums.add(app.getSha512Checksum());
    }
    // Setup a the file filter for the open file dialog
    FileChooserFilter fileChooserFilter = new FileChooserFilter("Jar, Zip, and Karaf Kar Files (*.jar, *.zip, *.kar)", new String[] { "jar", "zip", "kar" });
    Collection<FileChooserFilter> fileChooserFilters = new LinkedList<FileChooserFilter>();
    fileChooserFilters.add(fileChooserFilter);
    // Show the dialog
    final File[] files = fileUtil.getFiles(parent, "Choose file(s)", FileUtil.LOAD, FileUtil.LAST_DIRECTORY, "Install", true, fileChooserFilters);
    if (files != null) {
        TaskIterator ti = new TaskIterator();
        ti.append(new InstallAppsFromFileTask(Arrays.asList(files), appManager, true));
        ti.append(new ShowInstalledAppsIfChangedTask(appManager, parent));
        taskManager.setExecutionContext(parent);
        taskManager.execute(ti);
    }
}
Also used : App(org.cytoscape.app.internal.manager.App) WebApp(org.cytoscape.app.internal.net.WebApp) TaskIterator(org.cytoscape.work.TaskIterator) InstallAppsFromFileTask(org.cytoscape.app.internal.task.InstallAppsFromFileTask) ArrayList(java.util.ArrayList) ShowInstalledAppsIfChangedTask(org.cytoscape.app.internal.task.ShowInstalledAppsIfChangedTask) File(java.io.File) LinkedList(java.util.LinkedList) FileChooserFilter(org.cytoscape.util.swing.FileChooserFilter)

Aggregations

FileChooserFilter (org.cytoscape.util.swing.FileChooserFilter)9 File (java.io.File)7 ArrayList (java.util.ArrayList)4 FileUtil (org.cytoscape.util.swing.FileUtil)2 TaskIterator (org.cytoscape.work.TaskIterator)2 Dialog (java.awt.Dialog)1 FileDialog (java.awt.FileDialog)1 Frame (java.awt.Frame)1 MalformedURLException (java.net.MalformedURLException)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 JComponent (javax.swing.JComponent)1 JFileChooser (javax.swing.JFileChooser)1 JMenuItem (javax.swing.JMenuItem)1 App (org.cytoscape.app.internal.manager.App)1 WebApp (org.cytoscape.app.internal.net.WebApp)1 InstallAppsFromFileTask (org.cytoscape.app.internal.task.InstallAppsFromFileTask)1 ShowInstalledAppsIfChangedTask (org.cytoscape.app.internal.task.ShowInstalledAppsIfChangedTask)1