KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > action > assignment > DataShapeAction


1 package prefuse.action.assignment;
2
3 import java.util.Map JavaDoc;
4
5 import prefuse.Constants;
6 import prefuse.data.tuple.TupleSet;
7 import prefuse.util.DataLib;
8 import prefuse.visual.VisualItem;
9
10 /**
11  * <p>
12  * Assignment Action that assigns shape values for a group of items based upon
13  * a data field. Shape values are simple integer codes that indicate to
14  * appropriate renderer instances what shape should be drawn. The default
15  * list of shape values is included in the {@link prefuse.Constants} class,
16  * all beginning with the prefix <code>SHAPE</code>. Of course, clients can
17  * always create their own shape codes that are handled by a custom Renderer.
18  * </p>
19  *
20  * <p>The data field will be assumed to be nominal, and shapes will
21  * be assigned to unique values in the order they are encountered. Note that
22  * if the number of unique values is greater than
23  * {@link prefuse.Constants#SHAPE_COUNT} (when no palette is given) or
24  * the length of a specified palette, then duplicate shapes will start
25  * being assigned.</p>
26  *
27  * <p>This Action only sets the shape field of the VisualItem. For this value
28  * to have an effect, a renderer instance that takes this shape value
29  * into account must be used (e.g., {@link prefuse.render.ShapeRenderer}).
30  * </p>
31  *
32  * @author <a HREF="http://jheer.org">jeffrey heer</a>
33  */

34 public class DataShapeAction extends ShapeAction {
35     
36     protected static final int NO_SHAPE = Integer.MIN_VALUE;
37     
38     protected String JavaDoc m_dataField;
39     protected int[] m_palette;
40     
41     protected Map JavaDoc m_ordinalMap;
42     
43     
44     /**
45      * Create a new DataShapeAction.
46      * @param group the data group to process
47      * @param field the data field to base shape assignments on
48      */

49     public DataShapeAction(String JavaDoc group, String JavaDoc field) {
50         super(group, NO_SHAPE);
51         m_dataField = field;
52     }
53     
54     /**
55      * Create a new DataShapeAction.
56      * @param group the data group to process
57      * @param field the data field to base shape assignments on
58      * @param palette a palette of shape values to use for the encoding.
59      * By default, shape values are assumed to be one of the integer SHAPE
60      * codes included in the {@link prefuse.Constants} class.
61      */

62     public DataShapeAction(String JavaDoc group, String JavaDoc field, int[] palette) {
63         super(group, NO_SHAPE);
64         m_dataField = field;
65         m_palette = palette;
66     }
67     
68     // ------------------------------------------------------------------------
69

70     /**
71      * Returns the data field used to encode shape values.
72      * @return the data field that is mapped to shape values
73      */

74     public String JavaDoc getDataField() {
75         return m_dataField;
76     }
77     
78     /**
79      * Set the data field used to encode shape values.
80      * @param field the data field to map to shape values
81      */

82     public void setDataField(String JavaDoc field) {
83         m_dataField = field;
84     }
85     
86     /**
87      * This operation is not supported by the DataShapeAction type.
88      * Calling this method will result in a thrown exception.
89      * @see prefuse.action.assignment.ShapeAction#setDefaultShape(int)
90      * @throws UnsupportedOperationException
91      */

92     public void setDefaultShape(int defaultShape) {
93         throw new UnsupportedOperationException JavaDoc();
94     }
95     
96     // ------------------------------------------------------------------------
97

98     /**
99      * @see prefuse.action.EncoderAction#setup()
100      */

101     protected void setup() {
102         TupleSet ts = m_vis.getGroup(m_group);
103         m_ordinalMap = DataLib.ordinalMap(ts, m_dataField);
104     }
105     
106     /**
107      * @see prefuse.action.assignment.ShapeAction#getShape(prefuse.visual.VisualItem)
108      */

109     public int getShape(VisualItem item) {
110         // check for any cascaded rules first
111
int shape = super.getShape(item);
112         if ( shape != NO_SHAPE ) {
113             return shape;
114         }
115         
116         // otherwise perform data-driven assignment
117
Object JavaDoc v = item.get(m_dataField);
118         int idx = ((Integer JavaDoc)m_ordinalMap.get(v)).intValue();
119
120         if ( m_palette == null ) {
121             return idx % Constants.SHAPE_COUNT;
122         } else {
123             return m_palette[idx % m_palette.length];
124         }
125     }
126     
127 } // end of class DataShapeAction
128
Popular Tags