From 86ce8158b364b43431b6384c342abd1d50d49f76 Mon Sep 17 00:00:00 2001 From: Mike Kucera Date: Wed, 2 May 2018 16:22:10 -0400 Subject: [PATCH] Column namespaces in layout menu. Refs #4174 --- .../internal/layout/ui/DynamicLayoutMenu.java | 33 ++++++++++++++--- .../layout/ui/LayoutMenuPopulator.java | 2 +- .../view/BrowserTableHeaderRenderer.java | 35 +------------------ 3 files changed, 31 insertions(+), 39 deletions(-) diff --git a/swing-application-impl/src/main/java/org/cytoscape/internal/layout/ui/DynamicLayoutMenu.java b/swing-application-impl/src/main/java/org/cytoscape/internal/layout/ui/DynamicLayoutMenu.java index b5ebabae24..be9cb81d4e 100644 --- a/swing-application-impl/src/main/java/org/cytoscape/internal/layout/ui/DynamicLayoutMenu.java +++ b/swing-application-impl/src/main/java/org/cytoscape/internal/layout/ui/DynamicLayoutMenu.java @@ -32,17 +32,22 @@ import java.util.List; import java.util.Set; +import javax.swing.Icon; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.event.MenuEvent; import javax.swing.event.MenuListener; import org.cytoscape.application.CyApplicationManager; +import org.cytoscape.application.swing.CyColumnPresentation; +import org.cytoscape.application.swing.CyColumnPresentationManager; import org.cytoscape.model.CyColumn; import org.cytoscape.model.CyNetwork; import org.cytoscape.model.CyNode; import org.cytoscape.model.CyTable; import org.cytoscape.model.CyTableUtil; +import org.cytoscape.service.util.CyServiceRegistrar; +import org.cytoscape.util.swing.IconManager; import org.cytoscape.view.layout.CyLayoutAlgorithm; import org.cytoscape.view.model.CyNetworkView; import org.cytoscape.view.model.View; @@ -64,7 +69,8 @@ public class DynamicLayoutMenu extends JMenu implements MenuListener { private final boolean usesNodeAttrs; private final boolean usesEdgeAttrs; private final boolean supportsSelectedOnly; - private final CyApplicationManager appMgr; + + private final CyServiceRegistrar serviceRegistrar; // There is a memory leak on Mac, the Mac LAF holds on to this menu // for too long, allow the network to be garbage collected if this happens. @@ -79,13 +85,13 @@ public class DynamicLayoutMenu extends JMenu implements MenuListener { * * @param layout DOCUMENT ME! */ - public DynamicLayoutMenu(CyLayoutAlgorithm layout, CyNetwork network, boolean enabled, CyApplicationManager appMgr, DialogTaskManager tm, + public DynamicLayoutMenu(CyLayoutAlgorithm layout, CyNetwork network, boolean enabled, CyServiceRegistrar serviceRegistrar, DialogTaskManager tm, boolean usesNodeAttrs, boolean usesEdgeAttrs, boolean supportsSelectedOnly) { super(layout.toString()); this.layout = layout; this.networkRef = new WeakReference<>(network); this.tm = tm; - this.appMgr = appMgr; + this.serviceRegistrar = serviceRegistrar; addMenuListener(this); setEnabled(enabled); this.supportsSelectedOnly = supportsSelectedOnly; @@ -135,7 +141,7 @@ private void addAttributeMenus(JMenu parent, CyTable attributes, Set> t parent.add(new LayoutAttributeMenuItem(UNWEIGHTED, selectedOnly)); for (final CyColumn column : attributes.getColumns()) if (typeSet.contains(column.getType())) - parent.add(new LayoutAttributeMenuItem(column.getName(), selectedOnly)); + parent.add(new LayoutAttributeMenuItem(column, selectedOnly)); } private void addSelectedOnlyMenus(JMenu parent) { @@ -172,8 +178,27 @@ public LayoutAttributeMenuItem(String label, boolean selectedOnly) { this.selectedOnly = selectedOnly; } + public LayoutAttributeMenuItem(CyColumn column, boolean selectedOnly) { + addActionListener(this); + this.selectedOnly = selectedOnly; + String name = column.getNameOnly(); + String namespace = column.getNamespace(); + CyColumnPresentationManager presentationMgr = serviceRegistrar.getService(CyColumnPresentationManager.class); + CyColumnPresentation pres = presentationMgr.getColumnPresentation(namespace); + Icon icon = pres.getNamespaceIcon(); + if(icon != null) { + setIcon(IconManager.resizeIcon(icon, 16)); + } + if(namespace == null) { + setText(name); + } else { + setText(namespace + " " + name); + } + } + public void actionPerformed(ActionEvent e) { // NOTE: We iterate over all selected networks, not just the current network! + CyApplicationManager appMgr = serviceRegistrar.getService(CyApplicationManager.class); List views = appMgr.getSelectedNetworkViews(); for (final CyNetworkView netView : views) { Set> nodeViews; diff --git a/swing-application-impl/src/main/java/org/cytoscape/internal/layout/ui/LayoutMenuPopulator.java b/swing-application-impl/src/main/java/org/cytoscape/internal/layout/ui/LayoutMenuPopulator.java index aad8e83ec2..17a06ee2f4 100644 --- a/swing-application-impl/src/main/java/org/cytoscape/internal/layout/ui/LayoutMenuPopulator.java +++ b/swing-application-impl/src/main/java/org/cytoscape/internal/layout/ui/LayoutMenuPopulator.java @@ -129,7 +129,7 @@ public void menuSelected(MenuEvent e) { boolean usesSelected = (layout.getSupportsSelectedOnly() && someSelected); if (usesNodeAttrs || usesEdgeAttrs || usesSelected) { - JMenu newMenu = new DynamicLayoutMenu(layout,network,enableMenuItem,appMgr, + JMenu newMenu = new DynamicLayoutMenu(layout,network,enableMenuItem,serviceRegistrar, tm,usesNodeAttrs,usesEdgeAttrs,usesSelected); menuMap.put(layout, newMenu); gravityTracker.addMenu(newMenu, gravity); diff --git a/table-browser-impl/src/main/java/org/cytoscape/browser/internal/view/BrowserTableHeaderRenderer.java b/table-browser-impl/src/main/java/org/cytoscape/browser/internal/view/BrowserTableHeaderRenderer.java index 27dea0595b..d4b2a3aff6 100644 --- a/table-browser-impl/src/main/java/org/cytoscape/browser/internal/view/BrowserTableHeaderRenderer.java +++ b/table-browser-impl/src/main/java/org/cytoscape/browser/internal/view/BrowserTableHeaderRenderer.java @@ -31,15 +31,11 @@ import java.awt.BorderLayout; import java.awt.Component; import java.awt.Font; -import java.awt.Graphics2D; -import java.awt.Image; -import java.awt.image.BufferedImage; import java.util.List; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.Icon; -import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTable; @@ -148,38 +144,9 @@ private Icon getNamespaceIcon(String namespace) { if(icon == null) return null; - return resizeIcon(icon, 16); + return IconManager.resizeIcon(icon, 16); } - - private static Icon resizeIcon(Icon icon, int max) { - final int height = icon.getIconHeight(), width = icon.getIconWidth(); - if(width <= 16 && height <= 16) - return icon; - - // calculate new height and width - final int newHeight, newWidth; - - if(height > width) { - newHeight = max; - newWidth = (int)((float)width/(float)height * max); - } else { - newWidth = max; - newHeight = (int)((float)height/(float)width * max); - } - - - BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); - Graphics2D g = img.createGraphics(); - icon.paintIcon(null, g, 0, 0); - g.dispose(); - - Image resizedImage = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH); - ImageIcon resizedIcon = new ImageIcon(resizedImage); - return resizedIcon; - } - - @Override public Component getTableCellRendererComponent(final JTable table, final Object value, boolean isSelected, boolean hasFocus, int row, int col) {