1 7 8 package org.jdesktop.swing.decorator; 9 10 22 public abstract class Filter { 23 private final int column; private FilterPipeline pipeline = null; 25 protected ComponentAdapter adapter = null; 26 int order = -1; 28 31 public Filter() { 32 this(0); 33 } 34 35 40 public Filter(int col) { 41 column = col; 42 init(); 43 } 44 45 49 protected abstract void init(); 50 51 54 protected abstract void reset(); 55 56 59 protected abstract void generateMappingFromPrevious(); 60 61 64 protected abstract void filter(); 65 66 73 public void refresh() { 74 refresh(true); 75 } 76 77 89 protected void refresh(boolean reset) { 90 if (reset) { 91 reset(); 92 } 93 94 filter(); 95 generateMappingFromPrevious(); 96 97 if (pipeline != null) { 99 if (pipeline.contains(this)) { 100 pipeline.filterChanged(this); 101 return; 102 } 103 } 104 if (adapter != null) { 105 adapter.refresh(); 106 } 107 } 108 109 115 final void assign(ComponentAdapter adapter) { 116 if (adapter == null) { 117 throw new IllegalArgumentException ("null adapter"); 118 } 119 120 if (this.adapter == null) { 121 this.adapter = adapter; 122 } 123 else if (this.adapter != adapter){ 124 throw new IllegalStateException ("Already bound to another adapter"); 125 } 126 } 127 128 134 final void assign(FilterPipeline pipeline) { 135 140 141 if ((this.pipeline == null) || (pipeline == null)) { 142 this.pipeline = pipeline; 143 } 144 else if (this.pipeline != pipeline) { 145 throw new IllegalStateException ("Already bound to another pipeline"); 146 } 147 } 148 149 158 protected FilterPipeline getPipeline() { 159 return pipeline; 160 } 161 162 167 public int getColumnIndex() { 168 return column; } 170 171 public String getColumnName() { 172 if (adapter == null) { 173 return "Column " + column; } 175 else { 176 int viewColumnIndex = adapter.modelToView(getColumnIndex()); 177 return viewColumnIndex < 0 ? 178 "" : adapter.getColumnName(viewColumnIndex); 179 } 180 } 181 182 187 188 195 public int convertRowIndexToModel(int row) { 196 if ((order == 0) || (pipeline == null)) { 197 return translateToPreviousFilter(row); 198 } 199 else { 200 return pipeline.convertRowIndexToModel(this, translateToPreviousFilter(row)); 201 } 202 } 203 204 211 public int convertRowIndexToView(int row) { 212 if ((order == 0) || (pipeline == null)) { 213 return translateFromPreviousFilter(row); 214 } 215 else { 216 return translateFromPreviousFilter(pipeline.convertRowIndexToView(this, row)); 217 } 218 } 219 220 227 public abstract int getSize(); 228 229 240 protected abstract int translateFromPreviousFilter(int row); 241 242 253 protected abstract int translateToPreviousFilter(int row); 254 255 262 public Object getValueAt(int row, int column) { 263 if ((order == 0) || (pipeline == null)) { 264 return adapter.getValueAt(translateToPreviousFilter(row), adapter.modelToView(column)); 265 } 266 else { 267 return pipeline.getInputValueFor(this, translateToPreviousFilter(row), column); 268 } 269 } 270 271 279 public void setValueAt(Object aValue, int row, int column) { 280 if ((order == 0) || (pipeline == null)) { 281 adapter.setValueAt(aValue, translateToPreviousFilter(row), adapter.modelToView(column)); 282 } 283 else { 284 pipeline.setInputValueFor(aValue, this, translateToPreviousFilter(row), column); 285 } 286 } 287 288 public boolean isCellEditable(int row, int column) { 289 if ((order == 0) || (pipeline == null)) { 290 return adapter.isCellEditable(translateToPreviousFilter(row), adapter.modelToView(column)); 291 } 292 else { 293 return pipeline.isInputEditableFor(this, translateToPreviousFilter(row), column); 294 } 295 } 296 297 302 protected int getInputSize() { 303 return pipeline == null ? adapter == null ? 304 0 : adapter.getRowCount() : pipeline.getInputSize(this); 305 } 306 307 314 protected Object getInputValue(int row, int column) { 315 if (pipeline != null) { 316 return pipeline.getInputValueFor(this, row, column); 317 } 318 else if (adapter != null) { 319 return adapter.getValueAt(row, adapter.modelToView(column)); 320 } 321 322 return null; 323 } 324 325 } 326 327 | Popular Tags |