KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > nbprefuse > layout > AggregateLayout


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  * AggregateLayout.java
21  *
22  * Created on April 3, 2006, 5:34 PM
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.modules.xml.nbprefuse.layout;
29
30 import java.awt.geom.Rectangle2D JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import prefuse.Visualization;
33 import prefuse.action.layout.Layout;
34 import prefuse.util.GraphicsLib;
35 import prefuse.visual.AggregateItem;
36 import prefuse.visual.AggregateTable;
37 import prefuse.visual.VisualItem;
38
39 /**
40  *
41  * @author Jeri Lockhart
42  */

43
44 /**
45  * Layout algorithm that computes a convex hull surrounding
46  * aggregate items and saves it in the "_polygon" field.
47  */

48 public class AggregateLayout extends Layout {
49     
50     private int m_margin = 5; // convex hull pixel margin
51
private double[] m_pts; // buffer for computing convex hulls
52
private Visualization m_vis;
53     
54     public AggregateLayout(Visualization vis, String JavaDoc aggrGroup) {
55         super(aggrGroup);
56         m_vis = vis;
57     }
58     
59     /**
60      * @see edu.berkeley.guir.prefuse.action.Action#run(edu.berkeley.guir.prefuse.ItemRegistry, double)
61      */

62     public void run(double frac) {
63         
64         AggregateTable aggr = (AggregateTable)m_vis.getGroup(m_group);
65         // do we have any to process?
66
int num = aggr.getTupleCount();
67         if ( num == 0 ) return;
68         
69         // update buffers
70
int maxsz = 0;
71         for ( Iterator JavaDoc aggrs = aggr.tuples(); aggrs.hasNext(); )
72             maxsz = Math.max(maxsz, 4*2*
73                     ((AggregateItem)aggrs.next()).getAggregateSize());
74         if ( m_pts == null || maxsz > m_pts.length ) {
75             m_pts = new double[maxsz];
76         }
77         
78         // compute and assign convex hull for each aggregate
79
Iterator JavaDoc aggrs = m_vis.visibleItems(m_group);
80         while ( aggrs.hasNext() ) {
81             AggregateItem aitem = (AggregateItem)aggrs.next();
82
83             int idx = 0;
84             if ( aitem.getAggregateSize() == 0 ) continue;
85             VisualItem item = null;
86             Iterator JavaDoc iter = aitem.items();
87             while ( iter.hasNext() ) {
88                 item = (VisualItem)iter.next();
89                 if ( item.isVisible() ) {
90                     addPoint(m_pts, idx, item, m_margin);
91                     idx += 2*4;
92                 }
93             }
94             // if no aggregates are visible, do nothing
95
if ( idx == 0 ) continue;
96
97             // compute convex hull
98
double[] nhull = GraphicsLib.convexHull(m_pts, idx);
99             
100             // prepare viz attribute array
101
float[] fhull = (float[])aitem.get(VisualItem.POLYGON);
102             if ( fhull == null || fhull.length < nhull.length )
103                 fhull = new float[nhull.length];
104             else if ( fhull.length > nhull.length )
105                 fhull[nhull.length] = Float.NaN;
106             
107             // copy hull values
108
for ( int j=0; j<nhull.length; j++ )
109                 fhull[j] = (float)nhull[j];
110             aitem.set(VisualItem.POLYGON, fhull);
111             aitem.setValidated(false); // force invalidation
112
}
113     }
114     
115     private static void addPoint(double[] pts, int idx,
116                                  VisualItem item, int growth)
117     {
118         Rectangle2D JavaDoc b = item.getBounds();
119         double minX = (b.getMinX())-growth, minY = (b.getMinY())-growth;
120         double maxX = (b.getMaxX())+growth, maxY = (b.getMaxY())+growth;
121         pts[idx] = minX; pts[idx+1] = minY;
122         pts[idx+2] = minX; pts[idx+3] = maxY;
123         pts[idx+4] = maxX; pts[idx+5] = minY;
124         pts[idx+6] = maxX; pts[idx+7] = maxY;
125     }
126     
127 } // end of class AggregateLayout
128
Popular Tags