Skip to content

Commit

Permalink
Column namespaces in layout menu. Refs #4174
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekucera committed May 2, 2018
1 parent b097129 commit 86ce815
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 39 deletions.
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -135,7 +141,7 @@ private void addAttributeMenus(JMenu parent, CyTable attributes, Set<Class<?>> 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) {
Expand Down Expand Up @@ -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<CyNetworkView> views = appMgr.getSelectedNetworkViews();
for (final CyNetworkView netView : views) {
Set<View<CyNode>> nodeViews;
Expand Down
Expand Up @@ -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);
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 86ce815

Please sign in to comment.