KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > refactoring > ui > views > WhereUsedView


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * WhereUsedView.java
22  *
23  * Created on October 25, 2005, 2:09 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.refactoring.ui.views;
30
31 import java.awt.BorderLayout JavaDoc;
32 import java.awt.Color JavaDoc;
33 import java.awt.Dimension JavaDoc;
34 import java.awt.event.MouseEvent JavaDoc;
35 import java.beans.PropertyChangeEvent JavaDoc;
36 import java.beans.PropertyChangeListener JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Map JavaDoc;
42 import javax.swing.JPanel JavaDoc;
43 import javax.swing.tree.DefaultTreeModel JavaDoc;
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 /**
106  *
107  * @author Jeri Lockhart
108  */

109 public class WhereUsedView implements View, PropertyChangeListener JavaDoc {
110     
111     private Model JavaDoc model;
112     private Referenceable query;
113     private Graph graph;
114     private Display display = null;
115     private JPanel JavaDoc displayPanel;
116     private boolean isPrimitive;
117     private boolean usePacer = false; // slow in slow out pacer for initial
118
// graph animation
119
private DefaultTreeModel JavaDoc defaultTreeModel;
120 //
121
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 JavaDoc) {
129                 this.model = (Model JavaDoc) ref;
130             } else {
131                 throw new IllegalArgumentException JavaDoc("Expect Component or Model");
132             }
133         }
134     }
135   
136     
137     
138     /**
139      * Implement View
140      *
141      */

142     
143 // public JPanel getDisplayPanel() {
144
// return displayPanel;
145
// }
146
public Referenceable getQueryComponent() {
147          return query;
148      }
149      
150      
151     public Object JavaDoc[] createModels( ){
152         return null;
153     }
154     
155     /**
156      * Create the graph and the tree model
157      *
158      *
159      */

160     public Object JavaDoc[] createModels(FindUsageResult fuResult ){
161         defaultTreeModel = null;
162         Graph graph = this.createGraph(fuResult, true);
163         return new Object JavaDoc[] {defaultTreeModel, graph};
164     }
165     
166     
167     /**
168      *
169      *
170      */

171     private Graph createGraph(FindUsageResult fuResult, boolean createDefaultTreeModel){
172         usePacer = true;
173         graph = null;
174         Object JavaDoc[] 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 JavaDoc)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             // initialize display
200
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); // default value -1
210
at.addColumn(AnalysisConstants.IS_FILE_GROUP_AGGREGATE, boolean.class, false);
211                 
212                 List JavaDoc<NodeItem> fileNodes = new ArrayList JavaDoc<NodeItem>();
213                 aggrCount = createAggregates(viz, at, fileNodes);
214                 
215                 AnalysisUtilities.expandCollapseFileNodes(fileNodes);
216             }
217             
218             // size the AnalysisViewer to the available space in the main
219
// parent panel
220
// if width is < 1, use preferred size
221
Dimension JavaDoc dim = viewer.getPanel().getBounds().getSize();
222 // System.out.println("AnalysisView dimensions:" + dim.toString());
223
Dimension JavaDoc 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           // display.setSize(740, 500);
233
//viewer.getPanel().setSize(display.getSize());
234
this.displayPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
235             displayPanel.add(display, BorderLayout.CENTER);
236             viewer.addDisplayPanel(displayPanel);
237             
238             
239             // initialize renderers
240

241             // draw aggregates as polygons with curved edges
242
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, // palette size
260
0.17f, // saturation 23%
261
1.0f, // brightness 100%
262
null
263 // new AnalysisConstants.HSBHues[] {
264
// AnalysisConstants.HSBHues.GREEN,
265
// AnalysisConstants.HSBHues.PINK,
266
// AnalysisConstants.HSBHues.RED}
267
),
268                 ColorLib.getGrayscalePalette(
269                         aggrCount // palette size
270
),
271                 ColorLib.getCoolPalette(
272                         aggrCount // palette size
273
),
274                 ColorLib.getHotPalette(
275                         aggrCount // palette size
276
),
277                 ColorLib.getInterpolatedPalette(
278                         aggrCount, // palette size
279
new Color JavaDoc(204, 255, 255).getRGB(),
280                         new Color JavaDoc(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 // System.out.println("layout.getLayoutBounds() " + layout.getLayoutBounds()); // null
310
// Rectangle2D rect = layout.getLayoutBounds(viz);
311
// layout.setLayoutBounds(new Rectangle(rect.getBounds().width-10, rect.getBounds().height-10));
312

313 // ActionList update = new ActionList(Activity.INFINITY);
314
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             // remove SubtreeDragControl from Display - it doesn't
366
// work well with the AggregateItems
367
// display.addControlListener(new SubtreeDragControl());
368
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)); // "tooltip"
375
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             // FindUsagesFocusControl default is SINGLE selection
384
display.addControlListener(selectionFocusControl); // one click
385
display.addControlListener(schemaViewSearchFocusControl); // double click
386
display.addControlListener(
387                     new MouseoverActionControl(AnalysisConstants.ACTION_UPDATE_REPAINT));// mouseover
388
display.addControlListener(
389                     new NeighborHighlightControl(AnalysisConstants.ACTION_UPDATE_REPAINT)); //NOI18N
390
display.addControlListener(new PopupMouseControl());
391             display.addControlListener(new NodeExpansionMouseControl(viz,
392                     AnalysisConstants.ACTION_UPDATE_AGGREGATE_LAYOUT_REPAINT));
393             //////////////////////////////////////////////////////////////////
394
display.addControlListener(new ControlAdapter(){
395                 public void mouseClicked(MouseEvent JavaDoc 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 // VisualItem.FILLCOLOR, palettes[paletteCount]);
407
ActionList drawAggr = new ActionList();
408                         drawAggr.add(aFill);
409                         drawAggr.add(repaintAction);
410                         viz.putAction("drawAggr", drawAggr);
411                         viz.run("drawAggr");
412             String JavaDoc 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 // StatusDisplayer.getDefault().setStatusText(paletteName);
431
}
432                 }
433             });
434             
435             if (usePacer) {
436                 // animated transition
437
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 PolarLocationAnimator(
442
// AnalysisConstants.GRAPH_GROUP));
443
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 JavaDoc e ) {
456             ErrorManager.getDefault().notify(e);
457         }
458         return wasShown;
459     }
460     
461     
462     /**
463      * Create visual aggregates for each file node group
464      * returns the number of aggregates created
465      *
466      */

467     private int createAggregates(Visualization vis, AggregateTable at, List JavaDoc<NodeItem> fileNodes){
468         Map JavaDoc<Integer JavaDoc,AggregateItem> fileMap = new HashMap JavaDoc<Integer JavaDoc,AggregateItem>();
469         Iterator JavaDoc nodes = vis.getGroup(AnalysisConstants.GRAPH_GROUP_NODES).tuples();
470         while(nodes.hasNext()){
471             Integer JavaDoc fileGroup = null;
472             NodeItem n = NodeItem.class.cast(nodes.next());
473             // skip the query node
474
if (n.getBoolean(AnalysisConstants.IS_QUERY_NODE)){
475                 continue;
476             }
477             // schema component nodes will have a positive FILE_GROUP number
478
if (n.canGetInt(AnalysisConstants.FILE_GROUP)){
479                 fileGroup = (Integer JavaDoc)n.getInt(AnalysisConstants.FILE_GROUP);
480                 if (fileGroup == -1){
481                     // file nodes will have a positive FILE_NODE_FILE_GROUP
482
if (n.canGetInt(AnalysisConstants.FILE_NODE_FILE_GROUP)){
483                         fileNodes.add(n);
484                         fileGroup =
485                                 (Integer JavaDoc)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     /**
510      * Should the SchemaColumnView make the Column
511      * that the View is shown in as wide as possible?
512      * @return boolean true if View should be shown
513      * in a column as wide as the available horizontal space
514      * in the column view
515      */

516     public boolean getMaximizeWidth(){
517         return true;
518     }
519     
520     
521     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
522         if (evt.getPropertyName().equals(
523                 RefactoringPanel.NODE_SELECTION_CHANGE)){
524             // In the FindUsages explorer, a new node has been selected
525
Object JavaDoc 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             // get the current focus set
534
TupleSet ts = vis.getFocusGroup(vis.FOCUS_ITEMS);
535 // setUnselectNodeStroke(ts);
536
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 JavaDoc it = allItems.tuples();
543             while (it.hasNext()){
544                 Tuple n = (Tuple)it.next();
545                // Component sc = (Component)n.get(AnalysisConstants.XAM_COMPONENT);
546
Object JavaDoc 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     /**
558      * Switch to a different graph
559      * @param newGraph the new Graph to use for the Visualization
560      */

561     public void setGraph(Graph newGraph){
562         this.graph = newGraph;
563     }
564     
565    
566     
567     
568 }
569
Popular Tags