KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > util > FilteredRowManager


1 package prefuse.data.util;
2
3 import prefuse.data.CascadedTable;
4 import prefuse.data.Table;
5 import prefuse.data.column.IntColumn;
6 import prefuse.util.collections.IntIntSortedMap;
7 import prefuse.util.collections.IntIntTreeMap;
8
9 /**
10  * RowManager that additionally manages mappings between the managed
11  * rows and those of a parent table.
12  *
13  * @author <a HREF="http://jheer.org">jeffrey heer</a>
14  */

15 public class FilteredRowManager extends RowManager {
16
17     protected IntColumn m_childToParent;
18     protected IntIntSortedMap m_parentToChild;
19     
20     /**
21      * Create a new FilteredRowManager.
22      * @param table the table to manage
23      */

24     public FilteredRowManager(Table table) {
25         super(table);
26         m_childToParent = new IntColumn(table.getRowCount());
27         m_parentToChild = new IntIntTreeMap(false);
28         clear();
29     }
30     
31     /**
32      * @see prefuse.data.util.RowManager#clear()
33      */

34     public void clear() {
35         super.clear();
36         m_parentToChild.clear();
37         for ( int i=0; i<m_childToParent.getRowCount(); ++i ) {
38             m_childToParent.setInt(-1, i);
39         }
40     }
41     
42     /**
43      * Add a new row backed by the given parent row.
44      * @param parentRow the backing parent row
45      * @return the index of the newly added row
46      */

47     public int addRow(int parentRow) {
48         int r = super.addRow();
49         put(r, parentRow);
50         return r;
51     }
52     
53     /**
54      * @see prefuse.data.util.RowManager#releaseRow(int)
55      */

56     public boolean releaseRow(int row) {
57         if ( super.releaseRow(row) ) {
58             remove(row);
59             return true;
60         } else {
61             return false;
62         }
63     }
64     
65     /**
66      * @see prefuse.data.util.RowManager#getColumnRow(int, int)
67      */

68     public int getColumnRow(int row, int col) {
69         return ((CascadedTable)m_table).getParentTable()
70                     .getColumnRow(getParentRow(row), col);
71     }
72     
73     /**
74      * @see prefuse.data.util.RowManager#getTableRow(int, int)
75      */

76     public int getTableRow(int columnRow, int col) {
77         return getChildRow(columnRow);
78     }
79     
80     // ------------------------------------------------------------------------
81

82     /**
83      * Given a row managed by this manager, return the corresponding row
84      * in the parent table.
85      * @param childRow a row managed by this manager
86      * @return the parent table row
87      */

88     public int getParentRow(int childRow) {
89         if ( childRow >= m_childToParent.getRowCount() ) {
90             return -1;
91         } else {
92             return m_childToParent.getInt(childRow);
93         }
94     }
95
96     /**
97      * Given a row in the parent table, return the corresponding row managed
98      * by this manager.
99      * @param parentRow a row in the parent table
100      * @return the managed row corresponding to the parent row
101      */

102     public int getChildRow(int parentRow) {
103         int val = m_parentToChild.get(parentRow);
104         return ( val == Integer.MIN_VALUE ? -1 : val );
105     }
106     
107     /**
108      * Add a mapping between the given managed row and parent row.
109      * @param childRow a row managed by this manager
110      * @param parentRow a row in the parent table
111      */

112     public void put(int childRow, int parentRow) {
113         // ensure capacity of IntColumn
114
if ( childRow >= m_childToParent.getRowCount() )
115             m_childToParent.setMaximumRow(childRow+1);
116         
117         // add mapping
118
m_childToParent.setInt(parentRow, childRow);
119         m_parentToChild.put(parentRow, childRow);
120     }
121     
122     /**
123      * Remove a mapping between the given managed row and the corresponding
124      * parent row.
125      * @param childRow a row managed by this manager
126      */

127     public void remove(int childRow) {
128         int parentRow = m_childToParent.getInt(childRow);
129         m_childToParent.setInt(-1, childRow);
130         m_parentToChild.remove(parentRow);
131     }
132
133 } // end of class FilteredRowManager
134
Popular Tags