Search in sources :

Example 61 with CyApplicationManager

use of org.cytoscape.application.CyApplicationManager in project cytoscape-impl by cytoscape.

the class FileUtilImpl method getFolder.

@Override
public File getFolder(Component parent, String title, String startDir) {
    if (parent == null)
        throw new NullPointerException("\"parent\" must not be null.");
    final CyApplicationManager applicationManager = serviceRegistrar.getService(CyApplicationManager.class);
    final String osName = System.getProperty("os.name");
    if (osName.startsWith("Mac")) {
        final String property = System.getProperty("apple.awt.fileDialogForDirectories");
        System.setProperty("apple.awt.fileDialogForDirectories", "true");
        try {
            FileDialog chooser;
            if (parent instanceof Dialog)
                chooser = new FileDialog((Dialog) parent, title, FileDialog.LOAD);
            else if (parent instanceof Frame)
                chooser = new FileDialog((Frame) parent, title, FileDialog.LOAD);
            else
                throw new IllegalArgumentException("parent must be Dialog or Frame");
            if (startDir != null)
                chooser.setDirectory(startDir);
            else
                chooser.setDirectory(applicationManager.getCurrentDirectory().getAbsolutePath());
            chooser.setModal(true);
            chooser.setLocationRelativeTo(parent);
            chooser.setVisible(true);
            String file = chooser.getFile();
            String dir = chooser.getDirectory();
            if (file == null || dir == null) {
                return null;
            }
            return new File(dir + File.separator + file);
        } finally {
            if (property != null) {
                System.setProperty("apple.awt.fileDialogForDirectories", property);
            }
        }
    } 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());
        chooser.setDialogTitle(title);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        File result = null;
        if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
            result = chooser.getSelectedFile();
        }
        if (result != null && chooser.getCurrentDirectory().getPath() != null)
            applicationManager.setCurrentDirectory(chooser.getCurrentDirectory());
        return result;
    }
}
Also used : CyApplicationManager(org.cytoscape.application.CyApplicationManager) Frame(java.awt.Frame) JFileChooser(javax.swing.JFileChooser) FileDialog(java.awt.FileDialog) Dialog(java.awt.Dialog) FileDialog(java.awt.FileDialog) File(java.io.File)

Example 62 with CyApplicationManager

use of org.cytoscape.application.CyApplicationManager 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 63 with CyApplicationManager

use of org.cytoscape.application.CyApplicationManager in project cytoscape-impl by cytoscape.

the class NetworkViewMediator method findStylesWithMappedColumn.

private Set<VisualStyle> findStylesWithMappedColumn(final String columnName) {
    final Set<VisualStyle> styles = new HashSet<>();
    if (columnName != null) {
        final CyApplicationManager appMgr = serviceRegistrar.getService(CyApplicationManager.class);
        final RenderingEngineManager engineMgr = serviceRegistrar.getService(RenderingEngineManager.class);
        final RenderingEngine<CyNetwork> renderer = appMgr.getCurrentRenderingEngine();
        final VisualLexicon lexicon = renderer != null ? renderer.getVisualLexicon() : engineMgr.getDefaultVisualLexicon();
        final Set<VisualProperty<?>> properties = lexicon.getAllVisualProperties();
        final VisualMappingManager vmm = serviceRegistrar.getService(VisualMappingManager.class);
        for (final VisualStyle vs : vmm.getAllVisualStyles()) {
            for (final VisualProperty<?> vp : properties) {
                // Check VisualMappingFunction
                final VisualMappingFunction<?, ?> fn = vs.getVisualMappingFunction(vp);
                if (fn != null && fn.getMappingColumnName().equalsIgnoreCase(columnName)) {
                    styles.add(vs);
                    break;
                }
                // Check MappableVisualPropertyValue
                final Object defValue = vs.getDefaultValue(vp);
                if (defValue instanceof MappableVisualPropertyValue) {
                    ((MappableVisualPropertyValue) defValue).update();
                    styles.add(vs);
                    break;
                }
            }
        }
    }
    return styles;
}
Also used : BasicVisualLexicon(org.cytoscape.view.presentation.property.BasicVisualLexicon) VisualLexicon(org.cytoscape.view.model.VisualLexicon) RenderingEngineManager(org.cytoscape.view.presentation.RenderingEngineManager) CyNetwork(org.cytoscape.model.CyNetwork) CyApplicationManager(org.cytoscape.application.CyApplicationManager) MappableVisualPropertyValue(org.cytoscape.view.presentation.property.values.MappableVisualPropertyValue) VisualProperty(org.cytoscape.view.model.VisualProperty) VisualStyle(org.cytoscape.view.vizmap.VisualStyle) VisualMappingManager(org.cytoscape.view.vizmap.VisualMappingManager) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 64 with CyApplicationManager

use of org.cytoscape.application.CyApplicationManager in project cytoscape-impl by cytoscape.

the class NetworkViewMediator method handleEvent.

// // Event Handlers ////
@Override
public void handleEvent(SetCurrentNetworkViewEvent e) {
    if (loadingSession)
        return;
    final CyNetworkView view = e.getNetworkView();
    final CyApplicationManager appMgr = serviceRegistrar.getService(CyApplicationManager.class);
    final RenderingEngine<CyNetwork> currentEngine = appMgr.getCurrentRenderingEngine();
    // Set current RenderingEngine
    if (view != null) {
        final CyNetworkViewManager netViewMgr = serviceRegistrar.getService(CyNetworkViewManager.class);
        if (netViewMgr.getNetworkViewSet().contains(view)) {
            if (currentEngine == null || currentEngine.getViewModel() != view)
                appMgr.setCurrentRenderingEngine(presentationMap.get(view));
        }
    } else if (view == null && currentEngine != null) {
        appMgr.setCurrentRenderingEngine(null);
    }
}
Also used : CyApplicationManager(org.cytoscape.application.CyApplicationManager) CyNetworkViewManager(org.cytoscape.view.model.CyNetworkViewManager) CyNetwork(org.cytoscape.model.CyNetwork) CyNetworkView(org.cytoscape.view.model.CyNetworkView)

Example 65 with CyApplicationManager

use of org.cytoscape.application.CyApplicationManager in project cytoscape-impl by cytoscape.

the class NetworkViewMediator method render.

/**
 * Create a visualization container and add presentation to it.
 */
private final void render(final CyNetworkView view) {
    invokeOnEDT(() -> {
        // If already registered in this manager, do not render.
        if (getNetworkViewMainPanel().isRendered(view))
            return;
        NetworkViewRenderer renderer = null;
        final String rendererId = view.getRendererId();
        final CyApplicationManager appMgr = serviceRegistrar.getService(CyApplicationManager.class);
        if (rendererId != null)
            renderer = appMgr.getNetworkViewRenderer(rendererId);
        if (renderer == null)
            renderer = appMgr.getDefaultNetworkViewRenderer();
        final RenderingEngineFactory<CyNetwork> engineFactory = renderer.getRenderingEngineFactory(NetworkViewRenderer.DEFAULT_CONTEXT);
        final RenderingEngineFactory<CyNetwork> thumbnailFactory = renderer.getRenderingEngineFactory(NetworkViewRenderer.THUMBNAIL_CONTEXT);
        final RenderingEngine<CyNetwork> renderingEngine = getNetworkViewMainPanel().addNetworkView(view, engineFactory, thumbnailFactory);
        presentationMap.put(view, renderingEngine);
        final boolean isCurrentView = view.equals(appMgr.getCurrentNetworkView());
        new Thread(() -> {
            serviceRegistrar.getService(RenderingEngineManager.class).addRenderingEngine(renderingEngine);
            if (isCurrentView)
                appMgr.setCurrentRenderingEngine(renderingEngine);
        }).start();
    });
}
Also used : CyApplicationManager(org.cytoscape.application.CyApplicationManager) CyNetwork(org.cytoscape.model.CyNetwork) NetworkViewRenderer(org.cytoscape.application.NetworkViewRenderer)

Aggregations

CyApplicationManager (org.cytoscape.application.CyApplicationManager)80 CyNetwork (org.cytoscape.model.CyNetwork)40 CyNetworkView (org.cytoscape.view.model.CyNetworkView)30 CyNetworkViewManager (org.cytoscape.view.model.CyNetworkViewManager)18 VisualMappingManager (org.cytoscape.view.vizmap.VisualMappingManager)16 CyNetworkManager (org.cytoscape.model.CyNetworkManager)14 CyEventHelper (org.cytoscape.event.CyEventHelper)12 CyTable (org.cytoscape.model.CyTable)12 ArrayList (java.util.ArrayList)11 CyServiceRegistrar (org.cytoscape.service.util.CyServiceRegistrar)10 CyNode (org.cytoscape.model.CyNode)9 VisualStyle (org.cytoscape.view.vizmap.VisualStyle)9 Test (org.junit.Test)9 HashSet (java.util.HashSet)8 CyNetworkTableManager (org.cytoscape.model.CyNetworkTableManager)8 CyRootNetworkManager (org.cytoscape.model.subnetwork.CyRootNetworkManager)8 TaskIterator (org.cytoscape.work.TaskIterator)8 HashMap (java.util.HashMap)7 CyGroupManager (org.cytoscape.group.CyGroupManager)7 CyTableManager (org.cytoscape.model.CyTableManager)7