1 19 20 28 29 package org.netbeans.modules.xml.nbprefuse; 30 31 32 import java.awt.event.ActionEvent ; 33 import java.awt.event.ActionListener ; 34 import java.awt.event.MouseEvent ; 35 import java.util.Iterator ; 36 import javax.swing.JMenuItem ; 37 import javax.swing.JPopupMenu ; 38 import org.netbeans.modules.xml.nbprefuse.util.GraphUtilities; 39 import org.openide.util.NbBundle; 40 import prefuse.Visualization; 41 import prefuse.controls.ControlAdapter; 42 import prefuse.visual.AggregateItem; 43 import prefuse.visual.NodeItem; 44 import prefuse.visual.VisualItem; 45 46 50 public final class NodeExpansionMouseControl extends ControlAdapter { 51 52 private JPopupMenu popup; 53 private VisualItem vItem; 54 private JMenuItem menuItem; 55 56 public NodeExpansionMouseControl(final Visualization viz, final String activity) { 57 popup = new JPopupMenu (); 58 menuItem = new JMenuItem (); 59 menuItem.addActionListener(new ActionListener () { 60 public void actionPerformed(ActionEvent e){ 61 if (vItem != null && vItem instanceof VisualItem){ 62 NodeItem fileNode = null; 63 if (vItem.canGetBoolean(AnalysisConstants.IS_FILE_GROUP_AGGREGATE) && 64 vItem.getBoolean(AnalysisConstants.IS_FILE_GROUP_AGGREGATE)){ 65 66 fileNode = findFileNode(vItem.getInt(AnalysisConstants.ID), 67 AggregateItem.class.cast(vItem)); 68 } else { 69 fileNode = NodeItem.class.cast(vItem); 70 } 71 if (fileNode.canGetBoolean(AnalysisConstants.IS_FILE_NODE) && 72 fileNode.getBoolean(AnalysisConstants.IS_FILE_NODE)){ 73 GraphUtilities.expandCollapseFileNode(fileNode); 74 if (activity != null){ 75 viz.run(activity); 76 } 77 } 78 } 79 } 80 }); 81 popup.add(menuItem); 82 83 } 84 85 public void itemReleased(VisualItem gi, MouseEvent e) { 86 87 super.itemReleased(gi, e); 88 vItem = gi; 89 maybeShowPopup(e); 90 } 91 92 public void itemPressed(VisualItem gi, MouseEvent e) { 93 94 super.itemPressed(gi, e); 95 vItem = gi; 96 maybeShowPopup(e); 97 } 98 99 100 101 private void maybeShowPopup(MouseEvent e) { 102 if (e.isPopupTrigger()) { 103 NodeItem fileNode = null; 104 if (vItem.canGetBoolean(AnalysisConstants.IS_FILE_GROUP_AGGREGATE) && 105 vItem.getBoolean(AnalysisConstants.IS_FILE_GROUP_AGGREGATE)){ 106 fileNode = findFileNode(vItem.getInt(AnalysisConstants.ID), 107 AggregateItem.class.cast(vItem)); 108 } 109 if (vItem.canGetBoolean(AnalysisConstants.IS_FILE_NODE) && 110 vItem.getBoolean(AnalysisConstants.IS_FILE_NODE)){ 111 fileNode = NodeItem.class.cast(vItem); 112 } 113 if (fileNode == null){ 114 return; 115 } 116 boolean isExpanded = fileNode.getBoolean(AnalysisConstants.IS_EXPANDED); 117 menuItem.setText(NbBundle.getMessage(NodeExpansionMouseControl.class,(isExpanded?"LBL_collapse":"LBL_expand"))); 118 popup.show(e.getComponent(), 119 e.getX(), e.getY()); 120 } 121 } 122 123 129 private NodeItem findFileNode(final int aggregateItemFileGroup, final VisualItem item ) { 130 NodeItem fileNode = null; 131 AggregateItem agIt = AggregateItem.class.cast(item); 132 Iterator agItems = agIt.items(); 133 while(agItems.hasNext()){ 134 VisualItem agItem = (VisualItem)agItems.next(); 135 int fileGroup = -1; 136 if (agItem.canGetInt(AnalysisConstants.FILE_NODE_FILE_GROUP)) { 137 fileGroup = agItem.getInt(AnalysisConstants.FILE_NODE_FILE_GROUP); 138 if (fileGroup == aggregateItemFileGroup && 139 agItem.canGetBoolean(AnalysisConstants.IS_EXPANDED)){ 140 fileNode = NodeItem.class.cast(agItem); 141 break; 142 } 143 } 144 } 145 return fileNode; 146 } 147 148 } 149 150 151 | Popular Tags |