Search in sources :

Example 21 with VisualStyle

use of org.cytoscape.view.vizmap.VisualStyle in project cytoscape-impl by cytoscape.

the class FitLabelMappingGenerator method generateMap.

@Override
public <T> Map<T, V> generateMap(final Set<T> tableValues) {
    // Generate map for the current network view.
    final CyApplicationManager appMgr = servicesUtil.get(CyApplicationManager.class);
    final CyNetworkView networkView = appMgr.getCurrentNetworkView();
    // If current view is not available, simply return empty map.
    if (networkView == null)
        return Collections.emptyMap();
    // If given set is empty, return empty map.
    if (tableValues == null || tableValues.isEmpty())
        return Collections.emptyMap();
    // This only works with NAME column.
    final T testName = tableValues.iterator().next();
    if (testName instanceof String == false)
        throw new IllegalArgumentException("This generator only works with 'name' column.");
    final CyNetwork network = networkView.getModel();
    final CyTable nodeTable = networkView.getModel().getDefaultNodeTable();
    final VisualMappingManager vmMgr = servicesUtil.get(VisualMappingManager.class);
    final VisualStyle style = vmMgr.getCurrentVisualStyle();
    // Check label size mapping exists or not
    final VisualMappingFunction<?, Integer> fontSizeMapping = style.getVisualMappingFunction(BasicVisualLexicon.NODE_LABEL_FONT_SIZE);
    // Use default label width for checking.  TODO: should we use mapping?
    final Double maxLabelWidth = style.getDefaultValue(BasicVisualLexicon.NODE_LABEL_WIDTH);
    final Map<T, V> valueMap = new HashMap<T, V>();
    for (final T attrVal : tableValues) {
        final Collection<CyRow> rows = nodeTable.getMatchingRows(CyNetwork.NAME, attrVal);
        CyRow row = null;
        if (rows.isEmpty() == false)
            row = rows.iterator().next();
        else
            continue;
        final Long suid = row.get(CyIdentifiable.SUID, Long.class);
        final View<CyNode> nodeView = networkView.getNodeView(network.getNode(suid));
        if (nodeView == null)
            continue;
        final String labelText = nodeView.getVisualProperty(BasicVisualLexicon.NODE_LABEL);
        final int textLen = labelText.length();
        final int fontSize;
        if (fontSizeMapping == null)
            fontSize = style.getDefaultValue(BasicVisualLexicon.NODE_LABEL_FONT_SIZE);
        else
            fontSize = nodeView.getVisualProperty(BasicVisualLexicon.NODE_LABEL_FONT_SIZE);
        final Double width = fontSize * textLen * 0.7;
        if (maxLabelWidth > width)
            valueMap.put(attrVal, (V) width);
        else
            valueMap.put(attrVal, (V) maxLabelWidth);
    }
    return valueMap;
}
Also used : HashMap(java.util.HashMap) CyNetwork(org.cytoscape.model.CyNetwork) CyRow(org.cytoscape.model.CyRow) CyApplicationManager(org.cytoscape.application.CyApplicationManager) CyTable(org.cytoscape.model.CyTable) CyNode(org.cytoscape.model.CyNode) VisualMappingManager(org.cytoscape.view.vizmap.VisualMappingManager) VisualStyle(org.cytoscape.view.vizmap.VisualStyle) CyNetworkView(org.cytoscape.view.model.CyNetworkView)

Example 22 with VisualStyle

use of org.cytoscape.view.vizmap.VisualStyle in project cytoscape-impl by cytoscape.

the class VisualPropertySheet method createMenuItems.

private void createMenuItems() {
    final JPopupMenu rootMenu = getVpsMenu();
    // Remove previous menu items
    menuItemMap.clear();
    final int length = rootMenu.getSubElements() != null ? rootMenu.getSubElements().length : 0;
    for (int i = 0; i < length; i++) rootMenu.remove(i);
    // Add new menu items
    final VisualLexicon lexicon = model.getVisualLexicon();
    final VisualProperty<?> rootVp = model.getRootVisualProperty();
    final VisualLexiconNode rootNode = lexicon.getVisualLexiconNode(rootVp);
    // Menu Items for showing/hiding each VP Sheet Item
    // ------------------------------------------------
    // -- Visual Properties --
    final Queue<VisualLexiconNode> queue = new PriorityQueue<>(50, new Comparator<VisualLexiconNode>() {

        @Override
        public int compare(final VisualLexiconNode n1, final VisualLexiconNode n2) {
            final Collator collator = Collator.getInstance(Locale.getDefault());
            collator.setStrength(Collator.PRIMARY);
            return collator.compare(VisualPropertySheetItemModel.createTitle(n1.getVisualProperty()), VisualPropertySheetItemModel.createTitle(n2.getVisualProperty()));
        }
    });
    queue.addAll(rootNode.getChildren());
    final Map<VisualLexiconNode, JComponent> menuMap = new HashMap<>();
    menuMap.put(rootNode, rootMenu);
    final VisualStyle style = model.getVisualStyle();
    final Set<VisualProperty<?>> disabledProps = new HashSet<>();
    // final Set<VisualPropertyDependency<?>> depSet = style.getAllVisualPropertyDependencies();
    // for (final VisualPropertyDependency<?> dep : depSet) {
    // // To do the same as in Cytoscape v2.8, we only care about these dependencies
    // if (!dep.getIdString().equals("arrowColorMatchesEdge") && !dep.getIdString().equals("nodeSizeLocked"))
    // continue; // TODO: revisit these requirements and remove this workaround.
    // In general, the user should not be able to lock the child properties of an enabled dependency.
    // if (dep.isDependencyEnabled())
    // disabledProps.addAll(dep.getVisualProperties());
    // else
    // disabledProps.add(dep.getParentVisualProperty());
    // }
    final Set<VisualLexiconNode> nextNodes = new HashSet<>();
    while (!queue.isEmpty()) {
        final VisualLexiconNode curNode = queue.poll();
        final VisualProperty<?> vp = curNode.getVisualProperty();
        if (vp.getTargetDataType() == model.getTargetDataType()) {
            final Collection<VisualLexiconNode> children = curNode.getChildren();
            nextNodes.addAll(children);
            if (PropertySheetUtil.isCompatible(vp)) {
                VisualLexiconNode parentNode = curNode.getParent();
                boolean leaf = children.isEmpty();
                final VisualPropertySheetItem<?> vpSheetItem = getItem(vp);
                final String label = vpSheetItem != null ? vpSheetItem.getModel().getTitle() : VisualPropertySheetItemModel.createTitle(vp);
                if (!leaf) {
                    // Non-leaf visual property...
                    final JMenuItem nonLeafMenu = new JMenu(label);
                    menuMap.put(curNode, nonLeafMenu);
                    menuMap.get(parentNode).add(nonLeafMenu);
                    // So this VP can also be added as a child of itself
                    parentNode = curNode;
                }
                if (vpSheetItem != null) {
                    final JCheckBoxMenuItem mi = new JCheckBoxMenuItem(label, vpSheetItem.isVisible());
                    mi.addActionListener(evt -> {
                        // Show/hide the Visual Property Sheet Item
                        setVisible(vpSheetItem, !vpSheetItem.isVisible());
                    });
                    menuMap.get(parentNode).add(mi);
                    menuItemMap.put(vpSheetItem, mi);
                    if (parentNode == curNode)
                        menuMap.get(parentNode).add(new JSeparator());
                    // Should this visual property be disabled?
                    if (disabledProps.contains(vp))
                        mi.setEnabled(false);
                }
            }
        }
        queue.addAll(nextNodes);
        nextNodes.clear();
    }
    // -- Visual Property Dependencies --
    final TreeSet<VisualPropertyDependency<?>> depTreeSet = new TreeSet<>((VisualPropertyDependency<?> d1, VisualPropertyDependency<?> d2) -> {
        final Collator collator = Collator.getInstance(Locale.getDefault());
        collator.setStrength(Collator.PRIMARY);
        return collator.compare(d1.getDisplayName(), d2.getDisplayName());
    });
    depTreeSet.addAll(style.getAllVisualPropertyDependencies());
    for (final VisualPropertyDependency<?> dep : depTreeSet) {
        final VisualPropertySheetItem<?> vpSheetItem = getItem(dep);
        if (vpSheetItem != null) {
            final JCheckBoxMenuItem mi = new JCheckBoxMenuItem(dep.getDisplayName(), vpSheetItem.isVisible());
            mi.addActionListener(evt -> {
                // Show/hide the Visual Property Sheet Item
                setVisible(vpSheetItem, !vpSheetItem.isVisible());
            });
            final VisualLexiconNode parentNode = lexicon.getVisualLexiconNode(dep.getParentVisualProperty());
            JComponent parentMenu = menuMap.get(parentNode);
            if (// just add it directly to the popup menu
            parentMenu == null)
                parentMenu = rootMenu;
            parentMenu.add(mi);
            menuItemMap.put(vpSheetItem, mi);
        }
    }
    // ------------------------------------------------
    if (menuMap.size() > 1) {
        rootMenu.add(new JSeparator());
        {
            final JMenuItem mi = new JMenuItem("Show All");
            mi.addActionListener(evt -> {
                for (Entry<VisualPropertySheetItem<?>, JCheckBoxMenuItem> entry : menuItemMap.entrySet()) setVisible(entry.getKey(), true);
            });
            rootMenu.add(mi);
        }
        {
            final JMenuItem mi = new JMenuItem("Hide All");
            mi.addActionListener(evt -> {
                for (Entry<VisualPropertySheetItem<?>, JCheckBoxMenuItem> entry : menuItemMap.entrySet()) setVisible(entry.getKey(), false);
            });
            rootMenu.add(mi);
        }
        getVpsBtn().setEnabled(true);
    } else {
        getVpsBtn().setEnabled(false);
    }
}
Also used : SortedSet(java.util.SortedSet) PriorityQueue(java.util.PriorityQueue) VisualPropertyDependency(org.cytoscape.view.vizmap.VisualPropertyDependency) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem) IconManager(org.cytoscape.util.swing.IconManager) Locale(java.util.Locale) CyNetwork(org.cytoscape.model.CyNetwork) Map(java.util.Map) MouseAdapter(java.awt.event.MouseAdapter) Font(java.awt.Font) Collection(java.util.Collection) Set(java.util.Set) JMenu(javax.swing.JMenu) VisualLexicon(org.cytoscape.view.model.VisualLexicon) NavigableSet(java.util.NavigableSet) BorderFactory(javax.swing.BorderFactory) GridBagConstraints(java.awt.GridBagConstraints) ComponentEvent(java.awt.event.ComponentEvent) Box(javax.swing.Box) Dimension(java.awt.Dimension) PopupMenuEvent(javax.swing.event.PopupMenuEvent) LookAndFeelUtil(org.cytoscape.util.swing.LookAndFeelUtil) JSeparator(javax.swing.JSeparator) Entry(java.util.Map.Entry) Queue(java.util.Queue) GridBagLayout(java.awt.GridBagLayout) JPanel(javax.swing.JPanel) VisualStyle(org.cytoscape.view.vizmap.VisualStyle) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) SwingUtilities(javax.swing.SwingUtilities) VisualLexiconNode(org.cytoscape.view.model.VisualLexiconNode) JMenuItem(javax.swing.JMenuItem) Collator(java.text.Collator) BoxLayout(javax.swing.BoxLayout) VisualProperty(org.cytoscape.view.model.VisualProperty) JComponent(javax.swing.JComponent) LookAndFeelUtil.isAquaLAF(org.cytoscape.util.swing.LookAndFeelUtil.isAquaLAF) JButton(javax.swing.JButton) Iterator(java.util.Iterator) JPopupMenu(javax.swing.JPopupMenu) PopupMenuListener(javax.swing.event.PopupMenuListener) MouseEvent(java.awt.event.MouseEvent) ComponentAdapter(java.awt.event.ComponentAdapter) JScrollPane(javax.swing.JScrollPane) JLabel(javax.swing.JLabel) ServicesUtil(org.cytoscape.view.vizmap.gui.internal.util.ServicesUtil) GroupLayout(javax.swing.GroupLayout) PropertySheetUtil(org.cytoscape.view.vizmap.gui.util.PropertySheetUtil) Comparator(java.util.Comparator) Collections(java.util.Collections) VisualLexicon(org.cytoscape.view.model.VisualLexicon) HashMap(java.util.HashMap) JSeparator(javax.swing.JSeparator) Collator(java.text.Collator) Entry(java.util.Map.Entry) TreeSet(java.util.TreeSet) VisualStyle(org.cytoscape.view.vizmap.VisualStyle) JMenuItem(javax.swing.JMenuItem) HashSet(java.util.HashSet) VisualLexiconNode(org.cytoscape.view.model.VisualLexiconNode) JComponent(javax.swing.JComponent) PriorityQueue(java.util.PriorityQueue) JPopupMenu(javax.swing.JPopupMenu) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem) VisualPropertyDependency(org.cytoscape.view.vizmap.VisualPropertyDependency) VisualProperty(org.cytoscape.view.model.VisualProperty) JMenu(javax.swing.JMenu)

Example 23 with VisualStyle

use of org.cytoscape.view.vizmap.VisualStyle in project cytoscape-impl by cytoscape.

the class VizMapperProxy method handleEvent.

// --- Cytoscape EVENTS ---
@Override
public void handleEvent(final VisualStyleAddedEvent e) {
    synchronized (lock) {
        if (!cytoscapeStarted || ignoreStyleEvents)
            return;
    }
    final VisualStyle vs = e.getVisualStyleAdded();
    boolean changed = false;
    synchronized (lock) {
        changed = visualStyles.add(vs);
    }
    if (changed && !loadingSession)
        sendNotification(VISUAL_STYLE_ADDED, vs);
}
Also used : VisualStyle(org.cytoscape.view.vizmap.VisualStyle)

Example 24 with VisualStyle

use of org.cytoscape.view.vizmap.VisualStyle in project cytoscape-impl by cytoscape.

the class CreateLegendTask method run.

@Override
public void run(final TaskMonitor monitor) throws Exception {
    // Should be executed in EDT!
    invokeOnEDT(() -> {
        final VisualMappingManager vmMgr = servicesUtil.get(VisualMappingManager.class);
        final VisualStyle selectedStyle = vmMgr.getCurrentVisualStyle();
        final LegendDialog ld = new LegendDialog(selectedStyle, servicesUtil);
        ld.showDialog(null);
    });
}
Also used : LegendDialog(org.cytoscape.view.vizmap.gui.internal.view.legend.LegendDialog) VisualMappingManager(org.cytoscape.view.vizmap.VisualMappingManager) VisualStyle(org.cytoscape.view.vizmap.VisualStyle)

Example 25 with VisualStyle

use of org.cytoscape.view.vizmap.VisualStyle in project cytoscape-impl by cytoscape.

the class GenerateNetworkViewsTask method createNetworkView.

private void createNetworkView(final CyNetwork network) {
    final VisualMappingManager vmManager = serviceRegistrar.getService(VisualMappingManager.class);
    // get the current style before registering the views!
    final VisualStyle curStyle = vmManager.getCurrentVisualStyle();
    final CyNetworkView view = viewReader.buildCyNetworkView(network);
    final VisualStyle viewStyle = vmManager.getVisualStyle(view);
    serviceRegistrar.getService(CyNetworkViewManager.class).addNetworkView(view, false);
    // This allows the CyNetworkReader implementation to set the desired style itself.
    if (viewStyle != null && !viewStyle.equals(vmManager.getDefaultVisualStyle())) {
        viewStyle.apply(view);
    } else {
        vmManager.setVisualStyle(curStyle, view);
        curStyle.apply(view);
    }
    if (!view.isSet(BasicVisualLexicon.NETWORK_CENTER_X_LOCATION) && !view.isSet(BasicVisualLexicon.NETWORK_CENTER_Y_LOCATION) && !view.isSet(BasicVisualLexicon.NETWORK_CENTER_Z_LOCATION))
        view.fitContent();
    results.add(view);
}
Also used : CyNetworkViewManager(org.cytoscape.view.model.CyNetworkViewManager) VisualMappingManager(org.cytoscape.view.vizmap.VisualMappingManager) VisualStyle(org.cytoscape.view.vizmap.VisualStyle) CyNetworkView(org.cytoscape.view.model.CyNetworkView)

Aggregations

VisualStyle (org.cytoscape.view.vizmap.VisualStyle)100 CyNetworkView (org.cytoscape.view.model.CyNetworkView)42 VisualMappingManager (org.cytoscape.view.vizmap.VisualMappingManager)37 CyNetwork (org.cytoscape.model.CyNetwork)35 CyNode (org.cytoscape.model.CyNode)30 CyEdge (org.cytoscape.model.CyEdge)24 CyEventHelper (org.cytoscape.event.CyEventHelper)14 HashSet (java.util.HashSet)13 VisualProperty (org.cytoscape.view.model.VisualProperty)12 DiscreteMapping (org.cytoscape.view.vizmap.mappings.DiscreteMapping)12 Paint (java.awt.Paint)11 HashMap (java.util.HashMap)11 CyApplicationManager (org.cytoscape.application.CyApplicationManager)11 CyNetworkViewManager (org.cytoscape.view.model.CyNetworkViewManager)11 Color (java.awt.Color)10 ArrayList (java.util.ArrayList)10 RenderingEngineManager (org.cytoscape.view.presentation.RenderingEngineManager)9 BasicVisualLexicon (org.cytoscape.view.presentation.property.BasicVisualLexicon)9 ContinuousMapping (org.cytoscape.view.vizmap.mappings.ContinuousMapping)9 View (org.cytoscape.view.model.View)8