1 19 20 28 29 package org.netbeans.modules.xml.refactoring.ui.views; 30 31 import java.awt.BorderLayout ; 32 import java.awt.Color ; 33 import java.awt.Dimension ; 34 import java.awt.event.MouseEvent ; 35 import java.beans.PropertyChangeEvent ; 36 import java.beans.PropertyChangeListener ; 37 import java.util.ArrayList ; 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 import java.util.Map ; 42 import javax.swing.JPanel ; 43 import javax.swing.tree.DefaultTreeModel ; 44 import org.netbeans.modules.xml.nbprefuse.View; 45 import org.netbeans.modules.xml.refactoring.FindUsageResult; 46 import org.netbeans.modules.xml.nbprefuse.AnalysisConstants; 47 import org.netbeans.modules.xml.nbprefuse.AnalysisViewer; 48 import org.netbeans.modules.xml.refactoring.ui.j.ui.RefactoringPanel; 49 import org.netbeans.modules.xml.nbprefuse.layout.AggregateLayout; 50 import org.netbeans.modules.xml.nbprefuse.layout.NbFruchtermanReingoldLayout; 51 import org.netbeans.modules.xml.nbprefuse.AggregateDragControl; 52 import org.netbeans.modules.xml.nbprefuse.EdgeFillColorAction; 53 import org.netbeans.modules.xml.nbprefuse.EdgeStrokeColorAction; 54 import org.netbeans.modules.xml.nbprefuse.FindUsagesFocusControl; 55 import org.netbeans.modules.xml.nbprefuse.MouseoverActionControl; 56 import org.netbeans.modules.xml.nbprefuse.NodeExpansionMouseControl; 57 import org.netbeans.modules.xml.nbprefuse.NodeFillColorAction; 58 import org.netbeans.modules.xml.nbprefuse.NodeStrokeColorAction; 59 import org.netbeans.modules.xml.nbprefuse.NodeTextColorAction; 60 import org.netbeans.modules.xml.nbprefuse.PopupMouseControl; 61 import org.netbeans.modules.xml.refactoring.ui.readers.WhereUsedReader; 62 import org.netbeans.modules.xml.nbprefuse.render.CompositionEdgeRenderer; 63 import org.netbeans.modules.xml.nbprefuse.render.FindUsagesRendererFactory; 64 import org.netbeans.modules.xml.nbprefuse.render.GeneralizationEdgeRenderer; 65 import org.netbeans.modules.xml.nbprefuse.render.NbLabelRenderer; 66 import org.netbeans.modules.xml.nbprefuse.render.ReferenceEdgeRenderer; 67 import org.netbeans.modules.xml.refactoring.Usage; 68 import org.netbeans.modules.xml.refactoring.ui.util.AnalysisUtilities; 69 import org.netbeans.modules.xml.xam.Component; 70 import org.netbeans.modules.xml.xam.Model; 71 import org.netbeans.modules.xml.xam.Referenceable; 72 import org.openide.ErrorManager; 73 import org.openide.filesystems.FileObject; 74 import org.openide.util.NbBundle; 75 import prefuse.Constants; 76 import prefuse.Display; 77 import prefuse.Visualization; 78 import prefuse.action.ActionList; 79 import prefuse.action.RepaintAction; 80 import prefuse.action.animate.ColorAnimator; 81 import prefuse.action.animate.QualityControlAnimator; 82 import prefuse.action.animate.VisibilityAnimator; 83 import prefuse.action.assignment.ColorAction; 84 import prefuse.action.assignment.DataColorAction; 85 import prefuse.activity.SlowInSlowOutPacer; 86 import prefuse.controls.ControlAdapter; 87 import prefuse.controls.NeighborHighlightControl; 88 import prefuse.controls.PanControl; 89 import prefuse.controls.ToolTipControl; 90 import prefuse.controls.WheelZoomControl; 91 import prefuse.controls.ZoomControl; 92 import prefuse.data.Graph; 93 import prefuse.data.Tuple; 94 import prefuse.data.tuple.TupleSet; 95 import prefuse.render.EdgeRenderer; 96 import prefuse.render.PolygonRenderer; 97 import prefuse.render.Renderer; 98 import prefuse.util.ColorLib; 99 import prefuse.visual.AggregateItem; 100 import prefuse.visual.AggregateTable; 101 import prefuse.visual.NodeItem; 102 import prefuse.visual.VisualItem; 103 104 105 109 public class WhereUsedView implements View, PropertyChangeListener { 110 111 private Model model; 112 private Referenceable query; 113 private Graph graph; 114 private Display display = null; 115 private JPanel displayPanel; 116 private boolean isPrimitive; 117 private boolean usePacer = false; private DefaultTreeModel defaultTreeModel; 120 private static int paletteCount = 0; 122 123 public WhereUsedView(Referenceable ref){ 124 if (ref != null){ 125 this.query = ref; 126 if (ref instanceof Component) { 127 this.model = ((Component)ref).getModel(); 128 } else if (ref instanceof Model ) { 129 this.model = (Model ) ref; 130 } else { 131 throw new IllegalArgumentException ("Expect Component or Model"); 132 } 133 } 134 } 135 136 137 138 142 143 public Referenceable getQueryComponent() { 147 return query; 148 } 149 150 151 public Object [] createModels( ){ 152 return null; 153 } 154 155 160 public Object [] createModels(FindUsageResult fuResult ){ 161 defaultTreeModel = null; 162 Graph graph = this.createGraph(fuResult, true); 163 return new Object [] {defaultTreeModel, graph}; 164 } 165 166 167 171 private Graph createGraph(FindUsageResult fuResult, boolean createDefaultTreeModel){ 172 usePacer = true; 173 graph = null; 174 Object [] models = new WhereUsedReader().loadGraph(fuResult, query, 175 createDefaultTreeModel, isPrimitive, model); 176 if (models != null && models.length >0){ 177 graph = (Graph)models[0]; 178 if (createDefaultTreeModel && models.length>1) { 179 defaultTreeModel = (DefaultTreeModel )models[1]; 180 } 181 } 182 return graph; 183 184 } 185 186 public boolean showView(AnalysisViewer viewer) { 187 boolean wasShown = false; 188 189 final Visualization viz = new Visualization(); 190 if (graph == null){ 191 ErrorManager.getDefault().log(ErrorManager.ERROR, 192 NbBundle.getMessage(WhereUsedView.class, 193 "LBL_Graph_Not_Created_Error")); 194 return wasShown; 195 } 196 197 try { 198 199 this.display = new Display(); 201 display.setBackground(Color.WHITE); 202 viz.addGraph(AnalysisConstants.GRAPH_GROUP, graph); 203 display.setVisualization(viz); 204 205 int aggrCount = 0; 206 if (graph.getNodeCount() > 1){ 207 AggregateTable at = viz.addAggregates(AnalysisConstants.GRAPH_GROUP_AGGR); 208 at.addColumn(VisualItem.POLYGON, float[].class); 209 at.addColumn(AnalysisConstants.ID, int.class,-1); at.addColumn(AnalysisConstants.IS_FILE_GROUP_AGGREGATE, boolean.class, false); 211 212 List <NodeItem> fileNodes = new ArrayList <NodeItem>(); 213 aggrCount = createAggregates(viz, at, fileNodes); 214 215 AnalysisUtilities.expandCollapseFileNodes(fileNodes); 216 } 217 218 Dimension dim = viewer.getPanel().getBounds().getSize(); 222 Dimension displayDim = display.getBounds().getSize(); 224 if (dim.width < 1 || dim.height < 1){ 225 display.setSize(dim.width < 1?AnalysisConstants.DISPLAY_PREFERRED_WIDTH: 226 dim.width,dim.height<1?AnalysisConstants.DISPLAY_PREFERRED_HEIGHT:dim.height); 227 viewer.getPanel().setSize(display.getSize()); 228 } else if (!dim.equals(displayDim)) { 229 display.setSize(dim.width, dim.height); 230 } 231 232 this.displayPanel = new JPanel (new BorderLayout ()); 235 displayPanel.add(display, BorderLayout.CENTER); 236 viewer.addDisplayPanel(displayPanel); 237 238 239 241 Renderer polyR = new PolygonRenderer(Constants.POLY_TYPE_CURVE); 243 ((PolygonRenderer)polyR).setCurveSlack(0.15f); 244 245 viz.setRendererFactory(new FindUsagesRendererFactory( 246 new NbLabelRenderer(), 247 new NbLabelRenderer(), 248 new GeneralizationEdgeRenderer(), 249 new CompositionEdgeRenderer(), 250 new ReferenceEdgeRenderer(), 251 new EdgeRenderer(), 252 polyR 253 )); 254 255 256 257 final int[][] palettes = new int[][]{ 258 AnalysisUtilities.getHSBPalette( 259 aggrCount+1, 0.17f, 1.0f, null 263 ), 268 ColorLib.getGrayscalePalette( 269 aggrCount ), 271 ColorLib.getCoolPalette( 272 aggrCount ), 274 ColorLib.getHotPalette( 275 aggrCount ), 277 ColorLib.getInterpolatedPalette( 278 aggrCount, new Color (204, 255, 255).getRGB(), 280 new Color (255, 204, 255).getRGB() 281 ) 282 }; 283 284 final RepaintAction repaintAction = new RepaintAction(); 285 viz.putAction(AnalysisConstants.ACTION_REPAINT, repaintAction); 286 287 ColorAction aggrFill = new DataColorAction( 288 AnalysisConstants.GRAPH_GROUP_AGGR, 289 AnalysisConstants.ID, 290 Constants.NOMINAL, 291 VisualItem.FILLCOLOR, palettes[0]); 292 293 NodeFillColorAction nFill = new NodeFillColorAction(); 294 NodeTextColorAction nText = new NodeTextColorAction(); 295 NodeStrokeColorAction nStroke = new NodeStrokeColorAction(); 296 EdgeStrokeColorAction eStroke = new EdgeStrokeColorAction(); 297 EdgeFillColorAction eFill = new EdgeFillColorAction(); 298 299 ActionList draw = new ActionList(); 300 draw.add(nFill); 301 draw.add(nText); 302 draw.add(nStroke); 303 draw.add(eStroke); 304 draw.add(eFill); 305 draw.add(aggrFill); 306 viz.putAction(AnalysisConstants.ACTION_DRAW, draw); 307 308 309 313 ActionList update = new ActionList(viz); 315 update.add(nFill); 316 update.add(nText); 317 update.add(nStroke); 318 update.add(eStroke); 319 update.add(eFill); 320 update.add(aggrFill); 321 viz.putAction(AnalysisConstants.ACTION_UPDATE, update); 322 323 ActionList updateRepaint = new ActionList(viz); 324 updateRepaint.add(nFill); 325 updateRepaint.add(nText); 326 updateRepaint.add(nStroke); 327 updateRepaint.add(eStroke); 328 updateRepaint.add(eFill); 329 updateRepaint.add(aggrFill); 330 updateRepaint.add(repaintAction); 331 viz.putAction(AnalysisConstants.ACTION_UPDATE_REPAINT, updateRepaint); 332 333 AggregateLayout aggregateLayout = new AggregateLayout(viz, 334 AnalysisConstants.GRAPH_GROUP_AGGR); 335 viz.putAction(AnalysisConstants.ACTION_AGGREGATE_LAYOUT, aggregateLayout); 336 337 338 ActionList updateAggregateLayoutRepaint = new ActionList(viz); 339 updateAggregateLayoutRepaint.add(nFill); 340 updateAggregateLayoutRepaint.add(nText); 341 updateAggregateLayoutRepaint.add(nStroke); 342 updateAggregateLayoutRepaint.add(eStroke); 343 updateAggregateLayoutRepaint.add(eFill); 344 updateAggregateLayoutRepaint.add(aggrFill); 345 updateAggregateLayoutRepaint.add(aggregateLayout); 346 updateAggregateLayoutRepaint.add(repaintAction); 347 viz.putAction(AnalysisConstants.ACTION_UPDATE_AGGREGATE_LAYOUT_REPAINT, 348 updateAggregateLayoutRepaint); 349 350 ActionList layout = new ActionList(); 351 layout.add(new NbFruchtermanReingoldLayout( 352 AnalysisConstants.GRAPH_GROUP)); 353 layout.add(aggregateLayout); 354 layout.add(repaintAction); 355 viz.putAction(AnalysisConstants.ACTION_LAYOUT, layout); 356 357 ActionList layoutR = new ActionList(); 358 layoutR.add(new NbFruchtermanReingoldLayout( 359 AnalysisConstants.GRAPH_GROUP)); 360 layoutR.add(aggregateLayout); 361 layoutR.add(repaintAction); 362 viz.putAction(AnalysisConstants.ACTION_LAYOUT_REPAINT, layoutR); 363 364 365 display.setHighQuality(true); 369 display.addControlListener(new AggregateDragControl()); 370 display.addControlListener(new PanControl()); 371 display.addControlListener(new ZoomControl()); 372 display.addControlListener(new WheelZoomControl()); 373 display.addControlListener(new ToolTipControl( 374 AnalysisConstants.TOOLTIP)); FindUsagesFocusControl selectionFocusControl = 376 new FindUsagesFocusControl(1, 377 AnalysisConstants.ACTION_UPDATE_REPAINT); 378 FindUsagesFocusControl schemaViewSearchFocusControl = 379 new FindUsagesFocusControl(2, 380 AnalysisConstants.ACTION_UPDATE_AGGREGATE_LAYOUT_REPAINT); 381 selectionFocusControl.addGraphNodeSelectionChangeListener( 382 viewer); 383 display.addControlListener(selectionFocusControl); display.addControlListener(schemaViewSearchFocusControl); display.addControlListener( 387 new MouseoverActionControl(AnalysisConstants.ACTION_UPDATE_REPAINT)); display.addControlListener( 389 new NeighborHighlightControl(AnalysisConstants.ACTION_UPDATE_REPAINT)); display.addControlListener(new PopupMouseControl()); 391 display.addControlListener(new NodeExpansionMouseControl(viz, 392 AnalysisConstants.ACTION_UPDATE_AGGREGATE_LAYOUT_REPAINT)); 393 display.addControlListener(new ControlAdapter(){ 395 public void mouseClicked(MouseEvent evt){ 396 if ( evt.isAltDown()){ 397 paletteCount++; 398 if (paletteCount > palettes.length-1){ 399 paletteCount = 0; 400 } 401 ColorAction aFill = new DataColorAction( 402 AnalysisConstants.GRAPH_GROUP_AGGR, 403 AnalysisConstants.ID, 404 Constants.NOMINAL, 405 VisualItem.FILLCOLOR, palettes[1]); 406 ActionList drawAggr = new ActionList(); 408 drawAggr.add(aFill); 409 drawAggr.add(repaintAction); 410 viz.putAction("drawAggr", drawAggr); 411 viz.run("drawAggr"); 412 String paletteName = null; 413 switch (paletteCount){ 414 case 0: 415 paletteName = "HSB Palette: Saturation 10f, Brightness 150f"; 416 break; 417 case 1: 418 paletteName = "Grayscale Palette"; 419 break; 420 case 2: 421 paletteName = "Cool Palette"; 422 break; 423 case 3: 424 paletteName = "Hot Palette"; 425 break; 426 case 4: 427 paletteName = "Interpolated (204,255,255) to (255,204,255)"; 428 break; 429 } 430 } 432 } 433 }); 434 435 if (usePacer) { 436 ActionList animate = new ActionList(1500, 40); 438 animate.setPacingFunction(new SlowInSlowOutPacer()); 439 animate.add(new QualityControlAnimator()); 440 animate.add(new VisibilityAnimator()); 441 animate.add(new ColorAnimator()); 444 animate.add(repaintAction); 445 viz.putAction(AnalysisConstants.ACTION_ANIMATE, animate); 446 viz.runAfter(AnalysisConstants.ACTION_LAYOUT_REPAINT, AnalysisConstants.ACTION_ANIMATE); 447 usePacer(false); 448 } 449 viz.runAfter(AnalysisConstants.ACTION_DRAW, AnalysisConstants.ACTION_LAYOUT_REPAINT); 450 451 viz.run(AnalysisConstants.ACTION_DRAW); 452 wasShown = true; 453 454 455 } catch ( Exception e ) { 456 ErrorManager.getDefault().notify(e); 457 } 458 return wasShown; 459 } 460 461 462 467 private int createAggregates(Visualization vis, AggregateTable at, List <NodeItem> fileNodes){ 468 Map <Integer ,AggregateItem> fileMap = new HashMap <Integer ,AggregateItem>(); 469 Iterator nodes = vis.getGroup(AnalysisConstants.GRAPH_GROUP_NODES).tuples(); 470 while(nodes.hasNext()){ 471 Integer fileGroup = null; 472 NodeItem n = NodeItem.class.cast(nodes.next()); 473 if (n.getBoolean(AnalysisConstants.IS_QUERY_NODE)){ 475 continue; 476 } 477 if (n.canGetInt(AnalysisConstants.FILE_GROUP)){ 479 fileGroup = (Integer )n.getInt(AnalysisConstants.FILE_GROUP); 480 if (fileGroup == -1){ 481 if (n.canGetInt(AnalysisConstants.FILE_NODE_FILE_GROUP)){ 483 fileNodes.add(n); 484 fileGroup = 485 (Integer )n.getInt(AnalysisConstants.FILE_NODE_FILE_GROUP); 486 } 487 } 488 } 489 if (fileGroup != null){ 490 AggregateItem ai = null; 491 if (fileMap.containsKey(fileGroup)){ 492 ai = fileMap.get(fileGroup); 493 } else { 494 ai = (AggregateItem)at.addItem(); 495 ai.setInt(AnalysisConstants.ID, fileGroup.intValue()); 496 ai.setBoolean(AnalysisConstants.IS_FILE_GROUP_AGGREGATE,true); 497 fileMap.put(fileGroup,ai); 498 } 499 ai.addItem(n); 500 } 501 } 502 return fileMap.size(); 503 } 504 505 public void usePacer(boolean use) { 506 this.usePacer = use; 507 } 508 509 516 public boolean getMaximizeWidth(){ 517 return true; 518 } 519 520 521 public void propertyChange(PropertyChangeEvent evt) { 522 if (evt.getPropertyName().equals( 523 RefactoringPanel.NODE_SELECTION_CHANGE)){ 524 Object newVal = evt.getNewValue(); 526 if (newVal instanceof Usage) { 527 newVal = ((Usage)newVal).getComponent(); 528 } 529 if (display == null || graph == null){ 530 return; 531 } 532 Visualization vis = display.getVisualization(); 533 TupleSet ts = vis.getFocusGroup(vis.FOCUS_ITEMS); 535 ts.clear(); 537 if (newVal == null){ 538 vis.run(AnalysisConstants.ACTION_UPDATE_REPAINT); 539 return; 540 } 541 TupleSet allItems = vis.getGroup(AnalysisConstants.GRAPH_GROUP_NODES); 542 Iterator it = allItems.tuples(); 543 while (it.hasNext()){ 544 Tuple n = (Tuple)it.next(); 545 Object sc = n.get(AnalysisConstants.USER_OBJECT); 547 FileObject fo = (FileObject)n.get(AnalysisConstants.FILE_OBJECT); 548 if (sc == newVal || fo == newVal) { 549 ts.setTuple(n); 550 break; 551 } 552 } 553 vis.run(AnalysisConstants.ACTION_UPDATE_REPAINT); 554 } 555 } 556 557 561 public void setGraph(Graph newGraph){ 562 this.graph = newGraph; 563 } 564 565 566 567 568 } 569 | Popular Tags |