1 package prefuse.data.tuple; 2 3 import java.beans.PropertyChangeListener ; 4 import java.util.Comparator ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 8 import javax.swing.event.SwingPropertyChangeSupport ; 9 10 import prefuse.data.Schema; 11 import prefuse.data.Table; 12 import prefuse.data.Tuple; 13 import prefuse.data.event.EventConstants; 14 import prefuse.data.event.TupleSetListener; 15 import prefuse.data.expression.Expression; 16 import prefuse.data.expression.Predicate; 17 import prefuse.data.util.FilterIteratorFactory; 18 import prefuse.data.util.Sort; 19 import prefuse.data.util.SortedTupleIterator; 20 import prefuse.util.collections.CopyOnWriteArrayList; 21 22 29 public abstract class AbstractTupleSet implements TupleSet { 30 31 34 public Iterator tuples(Predicate filter) { 35 if ( filter == null ) { 36 return tuples(); 37 } else { 38 return FilterIteratorFactory.tuples(this, filter); 39 } 40 } 41 42 45 public Iterator tuples(Predicate filter, Sort sort) { 46 if ( sort == null ) { 47 return tuples(filter); 48 } else { 49 Comparator c = sort.getComparator(this); 50 return new SortedTupleIterator(tuples(filter),getTupleCount(),c); 51 } 52 } 53 54 55 57 private CopyOnWriteArrayList m_tupleListeners; 58 59 62 public void addTupleSetListener(TupleSetListener tsl) { 63 if ( m_tupleListeners == null ) 64 m_tupleListeners = new CopyOnWriteArrayList(); 65 if ( !m_tupleListeners.contains(tsl) ) 66 m_tupleListeners.add(tsl); 67 } 68 69 72 public void removeTupleSetListener(TupleSetListener tsl) { 73 if ( m_tupleListeners != null ) { 74 m_tupleListeners.remove(tsl); 75 } 76 } 77 78 87 protected void fireTupleEvent(Table t, int start, int end, int type) { 88 if ( m_tupleListeners != null && m_tupleListeners.size() > 0 ) { 89 Object [] lstnrs = m_tupleListeners.getArray(); 90 Tuple[] tuples = new Tuple[end-start+1]; 91 for ( int i=0, r=start; r <= end; ++r, ++i ) { 92 tuples[i] = t.getTuple(r); 93 } 94 for ( int i=0; i<lstnrs.length; ++i ) { 95 TupleSetListener tsl = (TupleSetListener)lstnrs[i]; 96 if ( type == EventConstants.INSERT ) { 97 tsl.tupleSetChanged(this, tuples, EMPTY_ARRAY); 98 } else { 99 tsl.tupleSetChanged(this, EMPTY_ARRAY, tuples); 100 } 101 } 102 } 103 } 104 105 112 protected void fireTupleEvent(Tuple t, int type) { 113 if ( m_tupleListeners != null && m_tupleListeners.size() > 0 ) { 114 Object [] lstnrs = m_tupleListeners.getArray(); 115 Tuple[] ts = new Tuple[] {t}; 116 for ( int i=0; i<lstnrs.length; ++i ) { 117 TupleSetListener tsl = (TupleSetListener)lstnrs[i]; 118 if ( type == EventConstants.INSERT ) { 119 tsl.tupleSetChanged(this, ts, EMPTY_ARRAY); 120 } else { 121 tsl.tupleSetChanged(this, EMPTY_ARRAY, ts); 122 } 123 } 124 } 125 } 126 127 132 protected void fireTupleEvent(Tuple[] added, Tuple[] removed) { 133 if ( m_tupleListeners != null && m_tupleListeners.size() > 0 ) { 134 Object [] lstnrs = m_tupleListeners.getArray(); 135 added = added==null ? EMPTY_ARRAY : added; 136 removed = removed==null ? EMPTY_ARRAY : removed; 137 for ( int i=0; i<lstnrs.length; ++i ) { 138 TupleSetListener tsl = (TupleSetListener)lstnrs[i]; 139 tsl.tupleSetChanged(this, added, removed); 140 } 141 } 142 } 143 144 146 150 public boolean isAddColumnSupported() { 151 return false; 152 } 153 154 157 public void addColumns(Schema schema) { 158 if ( isAddColumnSupported() ) { 159 for ( int i=0; i<schema.getColumnCount(); ++i ) { 160 try { 161 addColumn(schema.getColumnName(i), 162 schema.getColumnType(i), 163 schema.getDefault(i)); 164 } catch ( IllegalArgumentException iae ) {} 165 } 166 } else { 167 throw new UnsupportedOperationException (); 168 } 169 } 170 171 175 public void addColumn(String name, Class type, Object defaultValue) { 176 throw new UnsupportedOperationException (); 177 } 178 179 183 public void addColumn(String name, Class type) { 184 throw new UnsupportedOperationException (); 185 } 186 187 191 public void addColumn(String name, Expression expr) { 192 throw new UnsupportedOperationException (); 193 } 194 195 199 public void addColumn(String name, String expr) { 200 throw new UnsupportedOperationException (); 201 } 202 203 205 private HashMap m_props; 206 private SwingPropertyChangeSupport m_propSupport; 207 208 211 public void addPropertyChangeListener(PropertyChangeListener lstnr) { 212 if ( lstnr == null ) return; 213 if ( m_propSupport == null ) 214 m_propSupport = new SwingPropertyChangeSupport (this); 215 m_propSupport.addPropertyChangeListener(lstnr); 216 } 217 218 221 public void addPropertyChangeListener(String key, 222 PropertyChangeListener lstnr) 223 { 224 if ( lstnr == null ) return; 225 if ( m_propSupport == null ) 226 m_propSupport = new SwingPropertyChangeSupport (this); 227 m_propSupport.addPropertyChangeListener(key, lstnr); 228 } 229 230 233 public void removePropertyChangeListener(PropertyChangeListener lstnr) { 234 if ( lstnr == null ) return; 235 if ( m_propSupport == null ) return; 236 m_propSupport.removePropertyChangeListener(lstnr); 237 } 238 239 242 public void removePropertyChangeListener(String key, 243 PropertyChangeListener lstnr) 244 { 245 if ( lstnr == null ) return; 246 if ( m_propSupport == null ) return; 247 m_propSupport.removePropertyChangeListener(key, lstnr); 248 } 249 250 253 public void putClientProperty(String key, Object value) { 254 Object prev = null; 255 if ( m_props == null && value == null ) { 256 return; 258 } else if ( value == null ) { 259 prev = m_props.remove(key); 260 } else { 261 if ( m_props == null ) 262 m_props = new HashMap (2); 263 prev = m_props.put(key, value); 264 } 265 if ( m_propSupport != null ) 266 m_propSupport.firePropertyChange(key, prev, value); 267 } 268 269 272 public Object getClientProperty(String key) { 273 return ( m_props == null ? null : m_props.get(key) ); 274 } 275 276 } | Popular Tags |