1 19 20 28 29 package org.netbeans.modules.xml.schema.refactoring.query.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 org.netbeans.modules.xml.nbprefuse.AggregateDragControl; 44 import org.netbeans.modules.xml.nbprefuse.AnalysisConstants; 45 import org.netbeans.modules.xml.nbprefuse.AnalysisViewer; 46 import org.netbeans.modules.xml.nbprefuse.EdgeFillColorAction; 47 import org.netbeans.modules.xml.nbprefuse.EdgeStrokeColorAction; 48 import org.netbeans.modules.xml.nbprefuse.FindUsagesFocusControl; 49 import org.netbeans.modules.xml.nbprefuse.MouseoverActionControl; 50 import org.netbeans.modules.xml.nbprefuse.NodeExpansionMouseControl; 51 import org.netbeans.modules.xml.nbprefuse.NodeFillColorAction; 52 import org.netbeans.modules.xml.nbprefuse.NodeStrokeColorAction; 53 import org.netbeans.modules.xml.nbprefuse.NodeTextColorAction; 54 import org.netbeans.modules.xml.nbprefuse.PopupMouseControl; 55 import org.netbeans.modules.xml.nbprefuse.View; 56 import org.netbeans.modules.xml.nbprefuse.layout.AggregateLayout; 57 import org.netbeans.modules.xml.nbprefuse.layout.NbFruchtermanReingoldLayout; 58 import org.netbeans.modules.xml.nbprefuse.render.CompositionEdgeRenderer; 59 import org.netbeans.modules.xml.nbprefuse.render.FindUsagesRendererFactory; 60 import org.netbeans.modules.xml.nbprefuse.render.GeneralizationEdgeRenderer; 61 import org.netbeans.modules.xml.nbprefuse.render.NbLabelRenderer; 62 import org.netbeans.modules.xml.nbprefuse.render.ReferenceEdgeRenderer; 63 import org.netbeans.modules.xml.refactoring.ui.util.AnalysisUtilities; 64 import org.netbeans.modules.xml.schema.model.SchemaComponent; 65 import org.netbeans.modules.xml.schema.model.SchemaComponentReference; 66 import org.netbeans.modules.xml.schema.model.SchemaModel; 67 import org.netbeans.modules.xml.schema.model.SchemaModelFactory; 68 import org.netbeans.modules.xml.schema.refactoring.ui.QueryUtilities; 69 import org.netbeans.modules.xml.xam.Component; 70 import org.netbeans.modules.xml.xam.Named; 71 import org.openide.ErrorManager; 72 import org.openide.filesystems.FileObject; 73 import org.openide.util.NbBundle; 74 import prefuse.Constants; 75 import prefuse.Display; 76 import prefuse.Visualization; 77 import prefuse.action.ActionList; 78 import prefuse.action.RepaintAction; 79 import prefuse.action.animate.ColorAnimator; 80 import prefuse.action.animate.QualityControlAnimator; 81 import prefuse.action.animate.VisibilityAnimator; 82 import prefuse.action.assignment.ColorAction; 83 import prefuse.action.assignment.DataColorAction; 84 import prefuse.activity.SlowInSlowOutPacer; 85 import prefuse.controls.ControlAdapter; 86 import prefuse.controls.NeighborHighlightControl; 87 import prefuse.controls.PanControl; 88 import prefuse.controls.ToolTipControl; 89 import prefuse.controls.WheelZoomControl; 90 import prefuse.controls.ZoomControl; 91 import prefuse.data.Graph; 92 import prefuse.data.Tuple; 93 import prefuse.data.tuple.TupleSet; 94 import prefuse.render.EdgeRenderer; 95 import prefuse.render.PolygonRenderer; 96 import prefuse.render.Renderer; 97 import prefuse.util.ColorLib; 98 import prefuse.visual.AggregateItem; 99 import prefuse.visual.AggregateTable; 100 import prefuse.visual.NodeItem; 101 import prefuse.visual.VisualItem; 102 103 107 public class WhereUsedView implements View, PropertyChangeListener { 108 109 private SchemaModel model; 110 private SchemaComponent query; 111 private Display display = null; 112 private JPanel displayPanel; 113 private boolean isPrimitive; 114 private Graph graph; 115 private boolean usePacer = false; private int resizeCounter = 0; 122 private static int paletteCount = 0; 123 124 125 126 public WhereUsedView(SchemaComponentReference ref){ 127 if (ref != null){ 128 this.query = (SchemaComponent)ref.get(); 129 this.isPrimitive = query.getModel() == 130 SchemaModelFactory.getDefault().getPrimitiveTypesModel(); 131 this.model = 132 (this.isPrimitive? 133 SchemaModelFactory.getDefault().getPrimitiveTypesModel(): 134 query.getModel()); 135 } 136 } 137 141 public WhereUsedView(SchemaModel model, 142 SchemaComponent query, 143 boolean isPrimitive 144 ) { 145 this.model = model; 146 this.query = query; 147 this.isPrimitive = isPrimitive; 148 } 149 150 151 155 public WhereUsedView(SchemaModel model, SchemaComponent query) { 156 this(model, query, false); 157 } 158 159 160 164 165 public JPanel getDisplayPanel() { 166 return displayPanel; 167 } 168 public Named getQueryComponent() { 169 return (Named)query; 170 } 171 172 177 public Object [] createModels(){ 178 return null; 179 } 180 181 182 public boolean showView(AnalysisViewer viewer) { 183 boolean wasShown = false; 184 185 186 if (resizeCounter == 0){ 189 resizeCounter++; 190 } else if (resizeCounter == 1 || resizeCounter == 2){ 191 resizeCounter++; 192 return wasShown; 193 } 194 final Visualization viz = new Visualization(); 195 if (graph == null){ 196 ErrorManager.getDefault().log(ErrorManager.ERROR, 197 NbBundle.getMessage(WhereUsedView.class, 198 "LBL_Graph_Not_Created_Error")); 199 return wasShown; 200 } 201 202 try { 203 204 this.display = new Display(); 206 display.setBackground(Color.WHITE); 207 viz.addGraph(AnalysisConstants.GRAPH_GROUP, graph); 208 display.setVisualization(viz); 209 210 int aggrCount = 0; 211 if (graph.getNodeCount() > 1){ 212 AggregateTable at = viz.addAggregates(AnalysisConstants.GRAPH_GROUP_AGGR); 213 at.addColumn(VisualItem.POLYGON, float[].class); 214 at.addColumn(AnalysisConstants.ID, int.class,-1); at.addColumn(AnalysisConstants.IS_FILE_GROUP_AGGREGATE, boolean.class, false); 216 217 List <NodeItem> fileNodes = new ArrayList <NodeItem>(); 218 aggrCount = createAggregates(viz, at, fileNodes); 219 220 AnalysisUtilities.expandCollapseFileNodes(fileNodes); 221 } 222 223 Dimension dim = viewer.getPanel().getBounds().getSize(); 227 Dimension displayDim = display.getBounds().getSize(); 229 if (dim.width < 1 || dim.height < 1){ 230 display.setSize(dim.width < 1?AnalysisConstants.DISPLAY_PREFERRED_WIDTH: 231 dim.width,dim.height<1?AnalysisConstants.DISPLAY_PREFERRED_HEIGHT:dim.height); 232 viewer.getPanel().setSize(display.getSize()); 233 } else if (!dim.equals(displayDim)) { 234 display.setSize(dim.width, dim.height); 235 } 236 237 this.displayPanel = new JPanel (new BorderLayout ()); 238 displayPanel.add(display, BorderLayout.CENTER); 239 viewer.addDisplayPanel(displayPanel); 240 241 242 244 Renderer polyR = new PolygonRenderer(Constants.POLY_TYPE_CURVE); 246 ((PolygonRenderer)polyR).setCurveSlack(0.15f); 247 248 viz.setRendererFactory(new FindUsagesRendererFactory( 249 new NbLabelRenderer(), 250 new NbLabelRenderer(), 251 new GeneralizationEdgeRenderer(), 252 new CompositionEdgeRenderer(), 253 new ReferenceEdgeRenderer(), 254 new EdgeRenderer(), 255 polyR 256 )); 257 258 259 260 final int[][] palettes = new int[][]{ 261 QueryUtilities.getHSBPalette( 262 aggrCount+1, 0.17f, 1.0f, null 266 ), 271 ColorLib.getGrayscalePalette( 272 aggrCount ), 274 ColorLib.getCoolPalette( 275 aggrCount ), 277 ColorLib.getHotPalette( 278 aggrCount ), 280 ColorLib.getInterpolatedPalette( 281 aggrCount, new Color (204, 255, 255).getRGB(), 283 new Color (255, 204, 255).getRGB() 284 ) 285 }; 286 287 final RepaintAction repaintAction = new RepaintAction(); 288 viz.putAction(AnalysisConstants.ACTION_REPAINT, repaintAction); 289 290 ColorAction aggrFill = new DataColorAction( 291 AnalysisConstants.GRAPH_GROUP_AGGR, 292 AnalysisConstants.ID, 293 Constants.NOMINAL, 294 VisualItem.FILLCOLOR, palettes[0]); 295 296 NodeFillColorAction nFill = new NodeFillColorAction(); 297 NodeTextColorAction nText = new NodeTextColorAction(); 298 NodeStrokeColorAction nStroke = new NodeStrokeColorAction(); 299 EdgeStrokeColorAction eStroke = new EdgeStrokeColorAction(); 300 EdgeFillColorAction eFill = new EdgeFillColorAction(); 301 302 ActionList draw = new ActionList(); 303 draw.add(nFill); 304 draw.add(nText); 305 draw.add(nStroke); 306 draw.add(eStroke); 307 draw.add(eFill); 308 draw.add(aggrFill); 309 viz.putAction(AnalysisConstants.ACTION_DRAW, draw); 310 311 312 316 ActionList update = new ActionList(viz); 318 update.add(nFill); 319 update.add(nText); 320 update.add(nStroke); 321 update.add(eStroke); 322 update.add(eFill); 323 update.add(aggrFill); 324 viz.putAction(AnalysisConstants.ACTION_UPDATE, update); 325 326 ActionList updateRepaint = new ActionList(viz); 327 updateRepaint.add(nFill); 328 updateRepaint.add(nText); 329 updateRepaint.add(nStroke); 330 updateRepaint.add(eStroke); 331 updateRepaint.add(eFill); 332 updateRepaint.add(aggrFill); 333 updateRepaint.add(repaintAction); 334 viz.putAction(AnalysisConstants.ACTION_UPDATE_REPAINT, updateRepaint); 335 336 AggregateLayout aggregateLayout = new AggregateLayout(viz, 337 AnalysisConstants.GRAPH_GROUP_AGGR); 338 viz.putAction(AnalysisConstants.ACTION_AGGREGATE_LAYOUT, aggregateLayout); 339 340 341 ActionList updateAggregateLayoutRepaint = new ActionList(viz); 342 updateAggregateLayoutRepaint.add(nFill); 343 updateAggregateLayoutRepaint.add(nText); 344 updateAggregateLayoutRepaint.add(nStroke); 345 updateAggregateLayoutRepaint.add(eStroke); 346 updateAggregateLayoutRepaint.add(eFill); 347 updateAggregateLayoutRepaint.add(aggrFill); 348 updateAggregateLayoutRepaint.add(aggregateLayout); 349 updateAggregateLayoutRepaint.add(repaintAction); 350 viz.putAction(AnalysisConstants.ACTION_UPDATE_AGGREGATE_LAYOUT_REPAINT, 351 updateAggregateLayoutRepaint); 352 353 ActionList layout = new ActionList(); 354 layout.add(new NbFruchtermanReingoldLayout( 355 AnalysisConstants.GRAPH_GROUP)); 356 layout.add(aggregateLayout); 357 layout.add(repaintAction); 358 viz.putAction(AnalysisConstants.ACTION_LAYOUT, layout); 359 360 ActionList layoutR = new ActionList(); 361 layoutR.add(new NbFruchtermanReingoldLayout( 362 AnalysisConstants.GRAPH_GROUP)); 363 layoutR.add(aggregateLayout); 364 layoutR.add(repaintAction); 365 viz.putAction(AnalysisConstants.ACTION_LAYOUT_REPAINT, layoutR); 366 367 368 display.setHighQuality(true); 372 display.addControlListener(new AggregateDragControl()); 373 display.addControlListener(new PanControl()); 374 display.addControlListener(new ZoomControl()); 375 display.addControlListener(new WheelZoomControl()); 376 display.addControlListener(new ToolTipControl( 377 AnalysisConstants.TOOLTIP)); FindUsagesFocusControl selectionFocusControl = 379 new FindUsagesFocusControl(1, 380 AnalysisConstants.ACTION_UPDATE_REPAINT); 381 FindUsagesFocusControl schemaViewSearchFocusControl = 382 new FindUsagesFocusControl(2, 383 AnalysisConstants.ACTION_UPDATE_AGGREGATE_LAYOUT_REPAINT); 384 selectionFocusControl.addGraphNodeSelectionChangeListener( 385 viewer); 386 display.addControlListener(selectionFocusControl); display.addControlListener(schemaViewSearchFocusControl); display.addControlListener( 390 new MouseoverActionControl(AnalysisConstants.ACTION_UPDATE_REPAINT)); display.addControlListener( 392 new NeighborHighlightControl(AnalysisConstants.ACTION_UPDATE_REPAINT)); display.addControlListener(new PopupMouseControl()); 394 display.addControlListener(new NodeExpansionMouseControl(viz, 395 AnalysisConstants.ACTION_UPDATE_AGGREGATE_LAYOUT_REPAINT)); 396 display.addControlListener(new ControlAdapter(){ 398 public void mouseClicked(MouseEvent evt){ 399 if ( evt.isAltDown()){ 400 paletteCount++; 401 if (paletteCount > palettes.length-1){ 402 paletteCount = 0; 403 } 404 ColorAction aFill = new DataColorAction( 405 AnalysisConstants.GRAPH_GROUP_AGGR, 406 AnalysisConstants.ID, 407 Constants.NOMINAL, 408 VisualItem.FILLCOLOR, palettes[1]); 409 ActionList drawAggr = new ActionList(); 411 drawAggr.add(aFill); 412 drawAggr.add(repaintAction); 413 viz.putAction("drawAggr", drawAggr); 414 viz.run("drawAggr"); 415 String paletteName = null; 416 switch (paletteCount){ 417 case 0: 418 paletteName = "HSB Palette: Saturation 10f, Brightness 150f"; 419 break; 420 case 1: 421 paletteName = "Grayscale Palette"; 422 break; 423 case 2: 424 paletteName = "Cool Palette"; 425 break; 426 case 3: 427 paletteName = "Hot Palette"; 428 break; 429 case 4: 430 paletteName = "Interpolated (204,255,255) to (255,204,255)"; 431 break; 432 } 433 } 435 } 436 }); 437 438 if (usePacer) { 439 ActionList animate = new ActionList(1500, 40); 441 animate.setPacingFunction(new SlowInSlowOutPacer()); 442 animate.add(new QualityControlAnimator()); 443 animate.add(new VisibilityAnimator()); 444 animate.add(new ColorAnimator()); 447 animate.add(repaintAction); 448 viz.putAction(AnalysisConstants.ACTION_ANIMATE, animate); 449 viz.runAfter(AnalysisConstants.ACTION_LAYOUT_REPAINT, AnalysisConstants.ACTION_ANIMATE); 450 } 451 viz.runAfter(AnalysisConstants.ACTION_DRAW, AnalysisConstants.ACTION_LAYOUT_REPAINT); 452 453 viz.run(AnalysisConstants.ACTION_DRAW); 454 wasShown = true; 455 456 457 } catch ( Exception e ) { 458 ErrorManager.getDefault().notify(e); 459 } 460 return wasShown; 461 } 462 463 464 469 private int createAggregates(Visualization vis, AggregateTable at, List <NodeItem> fileNodes){ 470 Map <Integer ,AggregateItem> fileMap = new HashMap <Integer ,AggregateItem>(); 471 Iterator nodes = vis.getGroup(AnalysisConstants.GRAPH_GROUP_NODES).tuples(); 472 while(nodes.hasNext()){ 473 Integer fileGroup = null; 474 NodeItem n = NodeItem.class.cast(nodes.next()); 475 if (n.getBoolean(AnalysisConstants.IS_QUERY_NODE)){ 477 continue; 478 } 479 if (n.canGetInt(AnalysisConstants.FILE_GROUP)){ 481 fileGroup = (Integer )n.getInt(AnalysisConstants.FILE_GROUP); 482 if (fileGroup == -1){ 483 if (n.canGetInt(AnalysisConstants.FILE_NODE_FILE_GROUP)){ 485 fileNodes.add(n); 486 fileGroup = 487 (Integer )n.getInt(AnalysisConstants.FILE_NODE_FILE_GROUP); 488 } 489 } 490 } 491 if (fileGroup != null){ 492 AggregateItem ai = null; 493 if (fileMap.containsKey(fileGroup)){ 494 ai = fileMap.get(fileGroup); 495 } else { 496 ai = (AggregateItem)at.addItem(); 497 ai.setInt(AnalysisConstants.ID, fileGroup.intValue()); 498 ai.setBoolean(AnalysisConstants.IS_FILE_GROUP_AGGREGATE,true); 499 fileMap.put(fileGroup,ai); 500 } 501 ai.addItem(n); 502 } 503 } 504 return fileMap.size(); 505 } 506 507 public void usePacer(boolean use) { 508 this.usePacer = use; 509 } 510 511 518 public boolean getMaximizeWidth(){ 519 return true; 520 } 521 522 public static final String 524 NODE_SELECTION_CHANGE = "node-selection-change"; 526 public void propertyChange(PropertyChangeEvent evt) { 527 if (evt.getPropertyName().equals( 528 NODE_SELECTION_CHANGE)){ 529 Object newVal = evt.getNewValue(); 531 if (display == null || graph == null){ 532 return; 533 } 534 Visualization vis = display.getVisualization(); 535 TupleSet ts = vis.getFocusGroup(vis.FOCUS_ITEMS); 537 ts.clear(); 539 if (newVal == null){ 540 vis.run(AnalysisConstants.ACTION_UPDATE_REPAINT); 541 return; 542 } 543 TupleSet allItems = vis.getGroup(AnalysisConstants.GRAPH_GROUP_NODES); 544 Iterator it = allItems.tuples(); 545 while (it.hasNext()){ 546 Tuple n = (Tuple)it.next(); 547 if (n.canGet(AnalysisConstants.XAM_COMPONENT, 548 Component.class)){ 549 Component sc = (Component)n.get( 550 AnalysisConstants.XAM_COMPONENT); 551 if (sc == newVal){ 552 ts.setTuple(n); 553 break; 555 } 556 } else if (n.canGet(AnalysisConstants.FILE_OBJECT, 557 FileObject.class)){ 558 FileObject fo = (FileObject)n.get( 559 AnalysisConstants.FILE_OBJECT); 560 if (fo == newVal){ 561 ts.setTuple(n); 562 break; 564 } 565 } 566 } 567 vis.run(AnalysisConstants.ACTION_UPDATE_REPAINT); 568 } 569 } 570 571 575 public void setGraph(Graph newGraph){ 576 this.graph = newGraph; 577 } 578 579 580 585 600 601 } 602 | Popular Tags |