KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > display > RenderingQueue


1 package prefuse.util.display;
2
3 import java.util.Arrays JavaDoc;
4
5 import prefuse.util.ArrayLib;
6 import prefuse.visual.VisualItem;
7 import prefuse.visual.sort.ItemSorter;
8
9 /**
10  * A helper class representing rendering and picking queues. This functionality
11  * is listed separately to keep the Display implementation a bit cleaner.
12  * Fields are public and used directly by a single Display instance.
13  *
14  * @author <a HREF="http://jheer.org">jeffrey heer</a>
15  */

16 public class RenderingQueue {
17
18     private static final int DEFAULT_SIZE = 256;
19     
20     public ItemSorter sort = new ItemSorter();
21     
22     // rendering queue
23
public VisualItem[] ritems = new VisualItem[DEFAULT_SIZE];
24     public int[] rscores = new int[DEFAULT_SIZE];
25     public int rsize = 0;
26     
27     // picking queue
28
public VisualItem[] pitems = new VisualItem[DEFAULT_SIZE];
29     public int[] pscores = new int[DEFAULT_SIZE];
30     public int psize = 0;
31     public boolean psorted = false;
32     
33     // buffer queues for use in sorting, these prevent continual re-allocation
34
transient static VisualItem[] items_buf;
35     transient static int[] scores_buf;
36     
37     /**
38      * Clear both rendering and picking queues.
39      */

40     public void clear() {
41         Arrays.fill(ritems, 0, rsize, null);
42         Arrays.fill(pitems, 0, psize, null);
43         rsize = 0;
44         psize = 0;
45     }
46     
47     /**
48      * Add an item to the rendering queue.
49      * @param item the item to add
50      */

51     public void addToRenderQueue(VisualItem item) {
52         if ( ritems.length == rsize ) {
53             int capacity = (3*ritems.length)/2 + 1;
54             VisualItem[] q = new VisualItem[capacity];
55             int[] s = new int[capacity];
56             System.arraycopy(ritems, 0, q, 0, rsize);
57             System.arraycopy(rscores, 0, s, 0, rsize);
58             ritems = q;
59             rscores = s;
60         }
61         ritems[rsize] = item;
62         rscores[rsize++] = (sort != null ? sort.score(item) : 0);
63     }
64     
65     /**
66      * Add an item to the picking queue.
67      * @param item the item to add
68      */

69     public void addToPickingQueue(VisualItem item) {
70         if ( pitems.length == psize ) {
71             int capacity = (3*pitems.length)/2 + 1;
72             VisualItem[] q = new VisualItem[capacity];
73             int[] s = new int[capacity];
74             System.arraycopy(pitems, 0, q, 0, psize);
75             System.arraycopy(pscores, 0, s, 0, psize);
76             pitems = q;
77             pscores = s;
78         }
79         pitems[psize] = item;
80         pscores[psize++] = (sort != null ? sort.score(item) : 0);
81         psorted = false;
82     }
83     
84     /**
85      * Sort the rendering queue.
86      */

87     public void sortRenderQueue() {
88         sort(ritems, rscores, rsize);
89     }
90     
91     /**
92      * Sort the picking queue.
93      */

94     public void sortPickingQueue() {
95         sort(pitems, pscores, psize);
96         psorted = true;
97     }
98     
99     /**
100      * Sort a queue of items based upon an array of ordering scores.
101      */

102     private void sort(VisualItem[] items, int[] scores, int size) {
103         if ( sort == null ) return;
104         // first check buffer queues
105
if ( items_buf == null || items_buf.length < size ) {
106             items_buf = new VisualItem[items.length];
107             scores_buf = new int[scores.length];
108         }
109         // now sort
110
ArrayLib.sort(scores, items, scores_buf, items_buf, 0, size);
111     }
112     
113 } // end of class RenderingQueue
114
Popular Tags