1 19 20 package org.netbeans.modules.xml.nbprefuse; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.MouseEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.beans.PropertyChangeSupport ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import javax.swing.Action ; 31 import org.netbeans.modules.refactoring.api.RefactoringElement; 32 import org.netbeans.modules.xml.nbprefuse.util.GraphUtilities; 33 import org.openide.filesystems.FileObject; 34 import prefuse.Visualization; 35 import prefuse.data.tuple.TupleSet; 36 import prefuse.util.ui.UILib; 37 import prefuse.visual.AggregateItem; 38 import prefuse.visual.NodeItem; 39 import prefuse.visual.VisualItem; 40 41 45 public class FindUsagesFocusControl extends NbFocusControl{ 46 47 protected SelectionMode selectionMode = SelectionMode.SINGLE; 48 protected PropertyChangeSupport pcSupport; 49 50 public enum SelectionMode {SINGLE, MULTIPLE}; 51 52 53 57 public FindUsagesFocusControl() { 58 super(); 59 initialize(); 60 61 } 62 63 68 public FindUsagesFocusControl(String focusGroup) { 69 super(focusGroup); 70 initialize(); 71 } 72 73 79 public FindUsagesFocusControl(int clicks) { 80 super(clicks); 81 initialize(); 82 } 83 84 91 public FindUsagesFocusControl(String focusGroup, int clicks) { 92 super(focusGroup, clicks); 93 initialize(); 94 } 95 96 103 public FindUsagesFocusControl(int clicks, String act) { 104 super(clicks, act); 105 initialize(); 106 } 107 108 116 public FindUsagesFocusControl(String focusGroup, int clicks, String act) { 117 super(focusGroup, clicks, act); 118 initialize(); 119 } 120 121 public void itemPressed(VisualItem item, MouseEvent e) { 122 super.itemPressed(item, e); 123 if (e.isPopupTrigger()) { 124 selectNode(item,e); 126 } 127 } 128 129 public void itemReleased(VisualItem item, MouseEvent e) { 130 super.itemReleased(item, e); 131 if (e.isPopupTrigger()) { 132 selectNode(item,e); 134 } 135 } 136 137 private void selectNode(VisualItem item, MouseEvent e) { 138 Visualization vis = item.getVisualization(); 140 NodeItem nodeItem = null; 141 if (item instanceof NodeItem){ 142 nodeItem = NodeItem.class.cast(item); 143 } else { 144 return; 145 } 146 selectNode(e, nodeItem, vis); 147 runActivity(vis); 148 } 149 150 153 public void itemClicked(VisualItem item, MouseEvent e) { 154 if ( UILib.isButtonPressed(e, button) && 155 e.getClickCount() == ccount) { 156 Visualization vis = item.getVisualization(); 157 if (ccount == 1){ 158 selectNode(item,e); 159 } else if (ccount == 2){ 161 int aggregateItemFileGroup = -1; 164 if (item.canGetBoolean(AnalysisConstants.IS_FILE_GROUP_AGGREGATE) && 165 item.getBoolean(AnalysisConstants.IS_FILE_GROUP_AGGREGATE) && 166 item.canGetInt(AnalysisConstants.ID)){ 167 aggregateItemFileGroup = item.getInt(AnalysisConstants.ID); 168 } 169 if ((item.canGetBoolean(AnalysisConstants.IS_FILE_NODE) 170 && item.getBoolean(AnalysisConstants.IS_FILE_NODE) ) 171 || 172 aggregateItemFileGroup > -1) { 173 NodeItem fileNode = null; 176 if (item instanceof AggregateItem){ 177 fileNode = findFileNode(aggregateItemFileGroup, item); 178 } else { 179 fileNode = (NodeItem)item; 180 } 181 GraphUtilities.expandCollapseFileNode(fileNode); 182 } 184 191 210 211 else if (item.canGet(AnalysisConstants.REFACTORING_ELEMENT, RefactoringElement.class)) { 212 RefactoringElement comp = (RefactoringElement) item.get(AnalysisConstants.REFACTORING_ELEMENT); 213 214 } 217 218 if (item instanceof NodeItem){ 219 selectNode(e, NodeItem.class.cast(item), vis); 220 } 221 runActivity(vis); 222 223 224 } } 226 } 227 228 234 private NodeItem findFileNode(final int aggregateItemFileGroup, final VisualItem item) { 235 NodeItem fileNode = null; 236 AggregateItem agIt = AggregateItem.class.cast(item); 237 Iterator agItems = agIt.items(); 238 while(agItems.hasNext()){ 239 VisualItem agItem = (VisualItem)agItems.next(); 240 int fileGroup = -1; 241 if (agItem.canGetInt(AnalysisConstants.FILE_NODE_FILE_GROUP)) { 242 fileGroup = agItem.getInt(AnalysisConstants.FILE_NODE_FILE_GROUP); 243 if (fileGroup == aggregateItemFileGroup && 244 agItem.canGetBoolean(AnalysisConstants.IS_EXPANDED)){ 245 fileNode = NodeItem.class.cast(agItem); 246 break; 247 } 248 } 249 } 250 return fileNode; 251 } 252 253 private void selectNode(final MouseEvent e, final NodeItem nodeItem, final Visualization vis) { 254 TupleSet ts = vis.getFocusGroup(Visualization.FOCUS_ITEMS); 255 NodeItem curSelected = null; 256 List <NodeItem> selected = getNodeItemList(ts); 257 if (!selected.isEmpty()){ 258 curSelected = selected.get(0); 260 } 262 Object curComp = getGraphNodeUserObject(curSelected); 265 Object newComp = getGraphNodeUserObject(nodeItem); 266 267 270 271 boolean ctrl = e.isControlDown(); 272 if ( !ctrl) { 273 pcSupport.firePropertyChange( 274 AnalysisViewer.PROP_GRAPH_NODE_SELECTION_CHANGE, 275 curComp, newComp); curFocus = nodeItem; 277 ts.setTuple(nodeItem); 278 281 } else if ( ts.containsTuple(nodeItem) ) { 282 pcSupport.firePropertyChange( 283 AnalysisViewer.PROP_GRAPH_NODE_SELECTION_CHANGE, 284 newComp, null); ts.removeTuple(nodeItem); 286 if (nodeItem instanceof NodeItem && 292 nodeItem.canSetBoolean(AnalysisConstants.MOUSEOVER)){ 293 nodeItem.setBoolean(AnalysisConstants.MOUSEOVER, false); 294 } 295 296 299 } else { 300 if (selectionMode == SelectionMode.MULTIPLE){ 301 ts.addTuple(nodeItem); 304 } else { 305 pcSupport.firePropertyChange( 310 AnalysisViewer.PROP_GRAPH_NODE_SELECTION_CHANGE, 311 curComp, newComp); curFocus = nodeItem; 313 ts.setTuple(nodeItem); 314 } 316 } 317 } 318 319 331 335 336 337 private void initialize() { 338 pcSupport = new PropertyChangeSupport (this); 339 } 340 341 348 public void addGraphNodeSelectionChangeListener(PropertyChangeListener l){ 349 pcSupport.addPropertyChangeListener( 350 AnalysisViewer.PROP_GRAPH_NODE_SELECTION_CHANGE, l); 351 } 352 353 354 355 359 private List <NodeItem> getNodeItemList(TupleSet ts){ 360 ArrayList <NodeItem> list = null; 361 int count = ts.getTupleCount(); 362 if (count > 0){ 363 list = new ArrayList <NodeItem>(count); 364 } else { 365 return Collections.emptyList(); 366 } 367 Iterator it = ts.tuples(); 368 while (it.hasNext()){ 369 list.add((NodeItem)it.next()); 371 } 372 return list; 373 } 374 375 376 381 private Object getGraphNodeUserObject(NodeItem item ){ 382 if (item == null){ 383 return null; 384 } 385 386 FileObject fo = (FileObject) item.get(AnalysisConstants.FILE_OBJECT); 387 if (fo != null) { 388 return fo; 389 } 390 return item.get(AnalysisConstants.USER_OBJECT); 391 } 392 } 393 | Popular Tags |