001: /*
002: * $Id: FilterTest.java,v 1.27 2006/12/15 13:15:47 kleopatra Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: */
007:
008: package org.jdesktop.swingx.decorator;
009:
010: import java.awt.event.ActionEvent;
011: import java.text.Collator;
012: import java.util.Collections;
013: import java.util.Comparator;
014: import java.util.List;
015:
016: import javax.swing.AbstractAction;
017: import javax.swing.Action;
018: import javax.swing.table.DefaultTableModel;
019: import javax.swing.table.TableModel;
020:
021: import org.jdesktop.swingx.InteractiveTestCase;
022: import org.jdesktop.swingx.JXFrame;
023: import org.jdesktop.swingx.JXTable;
024: import org.jdesktop.swingx.test.PipelineReport;
025: import org.jdesktop.test.AncientSwingTeam;
026:
027: public class FilterTest extends InteractiveTestCase {
028:
029: public FilterTest() {
030: super ("FilterTest");
031: }
032:
033: private TableModel tableModel;
034: protected ComponentAdapter directModelAdapter;
035: private PipelineReport pipelineReport;
036:
037: //----------------------- testing sorter notification
038: //----------------------- will happen only if in pipeline so we do it here
039: /**
040: * test notification from sorter after setSortkey.
041: * Guarantee refresh notification with same direction, columnIndex
042: * but different Comparator.
043: */
044: public void testSorterSortKeyComparatorRefresh() {
045: FilterPipeline pipeline = new FilterPipeline();
046: pipeline.assign(directModelAdapter);
047: // create a sorter for column 0, ascending,
048: // without explicit comparator
049: Sorter sorter = new ShuttleSorter();
050: pipeline.setSorter(sorter);
051: pipeline.addPipelineListener(pipelineReport);
052: // create sortKey with other sort direction on column
053: SortKey sortKey = new SortKey(SortOrder.ASCENDING, sorter
054: .getColumnIndex(), Collator.getInstance());
055: sorter.setSortKey(sortKey);
056: // sanity: sorter and sortKey synched
057: SorterTest.assertSorterSortKeySynched(sortKey, sorter);
058: assertEquals(1, pipelineReport
059: .getEventCount(PipelineEvent.CONTENTS_CHANGED));
060: }
061:
062: /**
063: * test notification from sorter after setSortkey.
064: * Guarantee refresh notification with same columnIndex.
065: */
066: public void testSorterSortKeyRefresh() {
067: FilterPipeline pipeline = new FilterPipeline();
068: pipeline.assign(directModelAdapter);
069: // create a sorter for column 0, ascending,
070: // without explicit comparator
071: Sorter sorter = new ShuttleSorter();
072: pipeline.setSorter(sorter);
073: pipeline.addPipelineListener(pipelineReport);
074: // create sortKey with other sort direction on column
075: SortKey sortKey = new SortKey(SortOrder.DESCENDING, sorter
076: .getColumnIndex());
077: sorter.setSortKey(sortKey);
078: // sanity: sorter and sortKey synched
079: SorterTest.assertSorterSortKeySynched(sortKey, sorter);
080: assertEquals(1, pipelineReport
081: .getEventCount(PipelineEvent.CONTENTS_CHANGED));
082: }
083:
084: /**
085: * test notification from sorter after setSortkey.
086: * Guarantee exactly one refresh notification.
087: */
088: public void testSorterSortKeyOneRefresh() {
089: FilterPipeline pipeline = new FilterPipeline();
090: pipeline.assign(directModelAdapter);
091: // create a sorter for column 0, ascending,
092: // without explicit comparator
093: Sorter sorter = new ShuttleSorter();
094: pipeline.setSorter(sorter);
095: pipeline.addPipelineListener(pipelineReport);
096: // create sortKey with other sort direction on column
097: SortKey sortKey = new SortKey(SortOrder.DESCENDING, sorter
098: .getColumnIndex() + 1);
099: sorter.setSortKey(sortKey);
100: // sanity: sorter and sortKey synched
101: SorterTest.assertSorterSortKeySynched(sortKey, sorter);
102: assertEquals(1, pipelineReport
103: .getEventCount(PipelineEvent.CONTENTS_CHANGED));
104: }
105:
106: /**
107: * test notification from sorter after setSortkey.
108: * Guarantee no refresh notification on same.
109: */
110: public void testSorterSortKeyNoRefresh() {
111: FilterPipeline pipeline = new FilterPipeline();
112: pipeline.assign(directModelAdapter);
113: // create a sorter for column 0, ascending,
114: // without explicit comparator
115: Comparator comparator = Collator.getInstance();
116: Sorter sorter = new ShuttleSorter(0, true, comparator);
117: pipeline.setSorter(sorter);
118: pipeline.addPipelineListener(pipelineReport);
119: // create sortKey with other sort direction on column
120: SortKey sortKey = new SortKey(SortOrder.ASCENDING, sorter
121: .getColumnIndex(), sorter.getComparator());
122: sorter.setSortKey(sortKey);
123: // sanity: sorter and sortKey synched
124: SorterTest.assertSorterSortKeySynched(sortKey, sorter);
125: assertFalse("sorter must not refresh without state change",
126: pipelineReport.hasEvents());
127: }
128:
129: //---------------------------------SortController/Sorter in FilterPipeline
130:
131: public void testSortControllerToggleWithComparator() {
132: FilterPipeline pipeline = createAssignedPipeline(true);
133: SortController controller = pipeline.getSortController();
134: // controller.toggleSortOrder(0, Collator.getInstance());
135: }
136:
137: /**
138: * creates and returns a FilterPipeline assigned to directModelAdapter.
139: * Registers pipelineReport if withReport.
140: *
141: * @param withReport flag to indicate if pipelineReport should be registered.
142: * @return FilterPipeline
143: */
144: private FilterPipeline createAssignedPipeline(boolean withReport) {
145: FilterPipeline pipeline = new FilterPipeline();
146: pipeline.assign(directModelAdapter);
147: if (withReport) {
148: pipeline.addPipelineListener(pipelineReport);
149: }
150: return pipeline;
151: }
152:
153: /**
154: * Guarantee that Pipeline's Sorter and SortController are in synch
155: * after setting properties of SortController.
156: *
157: */
158: public void testSortControllerToggleUpdatesSameSorter() {
159: FilterPipeline pipeline = createAssignedPipeline(false);
160: int column = 2;
161: pipeline.setSorter(new ShuttleSorter(column, false));
162: Sorter sorter = pipeline.getSorter();
163: pipeline.addPipelineListener(pipelineReport);
164: SortController controller = pipeline.getSortController();
165: controller.toggleSortOrder(column);
166: assertFalse("toggleSortOrder must have initialized sortKey",
167: controller.getSortKeys().isEmpty());
168: // we assume that there's exactly one sortkey created!
169: SortKey sortKey = controller.getSortKeys().get(0);
170: assertTrue(pipeline.getSorter().isAscending());
171: assertEquals(column, sortKey.getColumn());
172: assertSame(sorter, pipeline.getSorter());
173: SorterTest.assertSorterSortKeySynched(sortKey, pipeline
174: .getSorter());
175: assertEquals(1, pipelineReport
176: .getEventCount(PipelineEvent.CONTENTS_CHANGED));
177: }
178:
179: /**
180: * Guarantee that Pipeline's Sorter and SortController are in synch
181: * after setting properties of SortController.
182: *
183: */
184: public void testSortControllerToggleUpdatesSorter() {
185: FilterPipeline pipeline = createAssignedPipeline(false);
186: int column = 2;
187: pipeline.setSorter(new ShuttleSorter(column, false));
188: pipeline.addPipelineListener(pipelineReport);
189: SortController controller = pipeline.getSortController();
190: int newColumn = column - 1;
191: controller.toggleSortOrder(newColumn);
192: assertFalse("toggleSortOrder must have initialized sortKey",
193: controller.getSortKeys().isEmpty());
194: // we assume that there's exactly one sortkey created!
195: SortKey sortKey = controller.getSortKeys().get(0);
196: assertTrue(pipeline.getSorter().isAscending());
197: SorterTest.assertSorterSortKeySynched(sortKey, pipeline
198: .getSorter());
199: assertEquals(1, pipelineReport
200: .getEventCount(PipelineEvent.CONTENTS_CHANGED));
201: }
202:
203: /**
204: * Guarantee that Pipeline's Sorter is correctly initialized
205: * after setting properties of SortController.
206: *
207: */
208: public void testSortControllerToggleInitSorter() {
209: FilterPipeline pipeline = createAssignedPipeline(true);
210: int column = 2;
211: SortController controller = pipeline.getSortController();
212: controller.toggleSortOrder(column);
213: assertFalse("toggleSortOrder must have initialized sortKey",
214: controller.getSortKeys().isEmpty());
215: // we assume that there's exactly one sortkey created!
216: SortKey sortKey = controller.getSortKeys().get(0);
217: SorterTest.assertSorterSortKeySynched(sortKey, pipeline
218: .getSorter());
219: assertEquals(1, pipelineReport
220: .getEventCount(PipelineEvent.CONTENTS_CHANGED));
221: }
222:
223: /**
224: * Guarantee that Pipeline's Sorter and SortController are in synch
225: * after setting properties of SortController.
226: *
227: */
228: public void testSortControllerResetRemovesSorter() {
229: FilterPipeline pipeline = createAssignedPipeline(false);
230: int column = 2;
231: pipeline.setSorter(new ShuttleSorter(column, true));
232: SortController controller = pipeline.getSortController();
233: controller.setSortKeys(Collections.EMPTY_LIST);
234: assertNull(pipeline.getSorter());
235: }
236:
237: /**
238: * Guarantee that Pipeline's Sorter and SortController are in synch
239: * after setting properties of SortController.
240: *
241: */
242: public void testSortControllerSortKeysUpdatesSameSorter() {
243: FilterPipeline pipeline = createAssignedPipeline(false);
244: pipeline.setSorter(new ShuttleSorter());
245: Sorter sorter = pipeline.getSorter();
246: pipeline.addPipelineListener(pipelineReport);
247: SortController controller = pipeline.getSortController();
248: SortKey sortKey = new SortKey(SortOrder.DESCENDING, sorter
249: .getColumnIndex());
250: controller.setSortKeys(Collections.singletonList(sortKey));
251: assertSame(sorter, pipeline.getSorter());
252: SorterTest.assertSorterSortKeySynched(sortKey, pipeline
253: .getSorter());
254: assertEquals(1, pipelineReport
255: .getEventCount(PipelineEvent.CONTENTS_CHANGED));
256: }
257:
258: /**
259: * Guarantee that Pipeline's Sorter and SortController are in synch
260: * after setting properties of SortController.
261: *
262: */
263: public void testSortControllerSortKeysUpdatesSorter() {
264: FilterPipeline pipeline = createAssignedPipeline(false);
265: int column = 2;
266: pipeline.setSorter(new ShuttleSorter(column, true));
267: pipeline.addPipelineListener(pipelineReport);
268: SortController controller = pipeline.getSortController();
269: int newColumn = column - 1;
270: SortKey sortKey = new SortKey(SortOrder.DESCENDING, newColumn);
271: controller.setSortKeys(Collections.singletonList(sortKey));
272: SorterTest.assertSorterSortKeySynched(sortKey, pipeline
273: .getSorter());
274: assertEquals(1, pipelineReport
275: .getEventCount(PipelineEvent.CONTENTS_CHANGED));
276: }
277:
278: /**
279: * Guarantee that Pipeline's Sorter is correctly initialized
280: * after setting properties of SortController.
281: *
282: */
283: public void testSortControllerSortKeysInitSorter() {
284: FilterPipeline pipeline = createAssignedPipeline(true);
285: int column = 1;
286: SortController controller = pipeline.getSortController();
287: SortKey sortKey = new SortKey(SortOrder.DESCENDING, column,
288: Collator.getInstance());
289: controller.setSortKeys(Collections.singletonList(sortKey));
290: SorterTest.assertSorterSortKeySynched(sortKey, pipeline
291: .getSorter());
292: SortKey createdKey = SortKey.getFirstSortKeyForColumn(
293: controller.getSortKeys(), column);
294: SorterTest.assertSorterSortKeySynched(createdKey, pipeline
295: .getSorter());
296:
297: assertEquals(1, pipelineReport
298: .getEventCount(PipelineEvent.CONTENTS_CHANGED));
299: }
300:
301: /**
302: * initial addition of SortController
303: * (== basically renamed Jesse's RowSorter).
304: * no active sorter.
305: */
306: public void testSortControllerWithoutSorter() {
307: FilterPipeline pipeline = createAssignedPipeline(false);
308: SortController controller = pipeline.getSortController();
309: assertNotNull(controller);
310: // test all method if nothing is sorted
311: assertEquals(SortOrder.UNSORTED, controller.getSortOrder(0));
312: assertNotNull(controller.getSortKeys());
313: }
314:
315: /**
316: * initial addition of SortController
317: * Guarantee that SortController getters are in synch with Sorter.
318: */
319: public void testSortControllerWithSorter() {
320: FilterPipeline pipeline = createAssignedPipeline(false);
321: int column = 2;
322: pipeline.setSorter(new ShuttleSorter(column, true));
323: SortController controller = pipeline.getSortController();
324: assertNotNull(controller);
325: // test all method if sorter ascending sorter on column
326: assertEquals(SortOrder.ASCENDING, controller
327: .getSortOrder(column));
328: List<? extends SortKey> sortKeys = controller.getSortKeys();
329: assertNotNull(sortKeys);
330: assertEquals(1, sortKeys.size());
331: SortKey sortKey = sortKeys.get(0);
332: assertEquals(SortOrder.ASCENDING, sortKey.getSortOrder());
333: assertEquals(column, sortKey.getColumn());
334: // sanity: doesn't effect unsorted column
335: assertEquals(SortOrder.UNSORTED, controller
336: .getSortOrder(column - 1));
337: }
338:
339: public void testSortOrderChangedEvent() {
340: FilterPipeline pipeline = createAssignedPipeline(true);
341:
342: pipeline.setSorter(new ShuttleSorter());
343: assertEquals(1, pipelineReport
344: .getEventCount(PipelineEvent.CONTENTS_CHANGED));
345: // expect 2 events: one for sortOrderChanged, one for contentsChanged
346: // not yet implemented - has implications on other tests, so go for
347: // one type of events only ...
348: // assertEquals(1, pipelineReport.getEventCount(PipelineEvent.SORT_ORDER_CHANGED));
349: // PipelineEvent event = pipelineReport.getLastEvent(PipelineEvent.SORT_ORDER_CHANGED);
350: // assertEquals(PipelineEvent.SORT_ORDER_CHANGED, event.getType());
351: }
352:
353: //----------------------------------
354:
355: /**
356: * reported on swingx-dev mailing list:
357: * chained filters must AND - as they did.
358: * Currently (10/2005) they OR ?.
359: *
360: * Hmm, can't reproduce - JW.
361: */
362: public void testAndFilter() {
363: PatternFilter first = new PatternFilter("a", 0, 0);
364: PatternFilter second = new PatternFilter("b", 0, 1);
365: // FilterPipeline pipeline = new FilterPipeline(new Filter[] {first, second});
366: FilterPipeline pipeline = new FilterPipeline(first, second);
367: pipeline.assign(directModelAdapter);
368: assertTrue(pipeline.getOutputSize() > 0);
369: for (int i = 0; i < pipeline.getOutputSize(); i++) {
370: boolean firstMatch = first.getPattern().matcher(
371: pipeline.getValueAt(i, 0).toString()).find();
372: boolean secondMatch = second.getPattern().matcher(
373: pipeline.getValueAt(i, 1).toString()).find();
374: assertTrue(firstMatch);
375: assertEquals("both matchers must find", firstMatch,
376: secondMatch);
377: }
378:
379: }
380:
381: /**
382: * Issue ??-swingx
383: * pipeline should auto-flush on assigning adapter.
384: *
385: */
386: public void testFlushOnAssign() {
387: Filter filter = new PatternFilter(".*", 0, 0);
388: FilterPipeline pipeline = new FilterPipeline(
389: new Filter[] { filter });
390: pipeline.assign(directModelAdapter);
391: assertEquals("pipeline output size must be model count",
392: directModelAdapter.getRowCount(), pipeline
393: .getOutputSize());
394: // JW PENDING: remove necessity to explicitly flush...
395: Object value = pipeline.getValueAt(0, 0);
396: assertEquals(
397: "value access via sorter must return the same as via pipeline",
398: value, pipeline.getValueAt(0, 0));
399:
400: }
401:
402: /**
403: * test notification on setSorter: must fire on change only.
404: *
405: */
406: public void testPipelineEventOnSameSorter() {
407: FilterPipeline pipeline = new FilterPipeline();
408: pipeline.assign(directModelAdapter);
409: pipeline.addPipelineListener(pipelineReport);
410: Sorter sorter = new ShuttleSorter();
411: pipeline.setSorter(sorter);
412: assertEquals("pipeline must have fired on setSorter", 1,
413: pipelineReport.getEventCount());
414: pipelineReport.clear();
415: pipeline.setSorter(sorter);
416: assertEquals("pipeline must not have fired on same setSorter",
417: 0, pipelineReport.getEventCount());
418:
419: }
420:
421: /**
422: * test notification on setSorter.
423: *
424: */
425: public void testPipelineEventOnSetSorter() {
426: FilterPipeline pipeline = new FilterPipeline();
427: pipeline.assign(directModelAdapter);
428: pipeline.addPipelineListener(pipelineReport);
429: pipeline.setSorter(new ShuttleSorter());
430: assertEquals("pipeline must have fired on setSorter", 1,
431: pipelineReport.getEventCount());
432: pipelineReport.clear();
433: pipeline.setSorter(null);
434: assertEquals("pipeline must have fired on setSorter null", 1,
435: pipelineReport.getEventCount());
436:
437: }
438:
439: /**
440: * test notification on setSorter: must fire if assigned only.
441: *
442: */
443: public void testPipelineEventOnSetSorterUnassigned() {
444: FilterPipeline pipeline = new FilterPipeline();
445: pipeline.addPipelineListener(pipelineReport);
446: pipeline.setSorter(new ShuttleSorter());
447: assertEquals("pipeline must not fire if unassigned", 0,
448: pipelineReport.getEventCount());
449: pipelineReport.clear();
450: pipeline.setSorter(null);
451: assertEquals("pipeline must not fire if unassigned", 0,
452: pipelineReport.getEventCount());
453:
454: }
455:
456: /**
457: * Issue #45-swingx:
458: * interpose should throw if trying to interpose to a pipeline
459: * with a differnt ComponentAdapter.
460: *
461: */
462: public void testSetSorterDiffComponentAdapter() {
463: FilterPipeline pipeline = new FilterPipeline();
464: pipeline.assign(directModelAdapter);
465: Sorter sorter = new ShuttleSorter();
466: sorter.assign(new DirectModelAdapter(new DefaultTableModel(10,
467: 5)));
468: try {
469: pipeline.setSorter(sorter);
470: fail("interposing with a different adapter must throw an IllegalStateException");
471: } catch (IllegalStateException ex) {
472:
473: } catch (Exception e) {
474: fail("interposing with a different adapter must throw an "
475: + "IllegalStatetException instead of " + e);
476: }
477:
478: }
479:
480: /**
481: *
482: * Issue #46-swingx:
483: *
484: * need to clarify the behaviour of an empty pipeline.
485: * I would expect 0 filters to result in an open pipeline
486: * (nothing filtered). The implementation treats this case as a
487: * closed pipeline (everything filtered).
488: *
489: * Arguably it could be decided either way, but returning a
490: * outputsize > 0 and null instead of the adapter value for
491: * all rows is a bug.
492: *
493: * Fixed by adding an pass-all filter internally.
494: *
495: */
496: public void testEmptyPipeline() {
497: int sortColumn = 0;
498: FilterPipeline pipeline = new FilterPipeline();
499: pipeline.assign(directModelAdapter);
500: assertEquals("size must be number of rows in adapter",
501: directModelAdapter.getRowCount(), pipeline
502: .getOutputSize());
503: Object value = pipeline.getValueAt(0, sortColumn);
504: assertEquals(directModelAdapter.getValueAt(0, sortColumn),
505: value);
506: }
507:
508: /**
509: * sorter in empty pipeline must behave in the same way as
510: * an identical sorter in the pipeline's filter chain.
511: *
512: */
513: public void testSorterInEmptyPipeline() {
514: int sortColumn = 0;
515: // prepare the reference pipeline
516: Filter[] sorters = new Filter[] { new ShuttleSorter() };
517: FilterPipeline sortedPipeline = new FilterPipeline(sorters);
518: sortedPipeline.assign(directModelAdapter);
519: Object sortedValue = sortedPipeline.getValueAt(0, sortColumn);
520: // prepare the empty pipeline with associated sorter
521: FilterPipeline pipeline = new FilterPipeline();
522: pipeline.assign(directModelAdapter);
523: Sorter sorter = new ShuttleSorter();
524: pipeline.setSorter(sorter);
525: assertEquals("sorted values must be equal", sortedValue,
526: pipeline.getValueAt(0, sortColumn));
527:
528: }
529:
530: /**
531: * sorter.getValueAt must be same as pipeline.getValueAt.
532: *
533: */
534: public void testSorterInPipeline() {
535: Filter filter = createDefaultPatternFilter(0);
536: FilterPipeline pipeline = new FilterPipeline(
537: new Filter[] { filter });
538: pipeline.assign(directModelAdapter);
539: Sorter sorter = new ShuttleSorter();
540: pipeline.setSorter(sorter);
541: assertEquals(
542: "value access via sorter must return the same as via pipeline",
543: pipeline.getValueAt(0, 0), sorter.getValueAt(0, 0));
544:
545: }
546:
547: /**
548: * unassigned filter/-pipeline must have size 0.
549: *
550: */
551: public void testUnassignedFilter() {
552: Filter filter = createDefaultPatternFilter(0);
553: assertEquals(0, filter.getSize());
554: Filter[] filters = new Filter[] { filter };
555: FilterPipeline pipeline = new FilterPipeline(filters);
556: assertEquals(0, pipeline.getOutputSize());
557: }
558:
559: public void testUnassignedEmptyFilter() {
560: FilterPipeline filters = new FilterPipeline();
561: assertEquals(0, filters.getOutputSize());
562: }
563:
564: /**
565: * JW: test paranoia?
566: *
567: */
568: public void testDirectComponentAdapterAccess() {
569: FilterPipeline pipeline = createPipeline();
570: pipeline.assign(directModelAdapter);
571: assertTrue("pipeline must have filtered values", pipeline
572: .getOutputSize() < directModelAdapter.getRowCount());
573: }
574:
575: /**
576: * order of filters must be retained.
577: *
578: */
579: public void testFilterOrder() {
580: Filter filterZero = createDefaultPatternFilter(0);
581: Filter filterTwo = createDefaultPatternFilter(2);
582: Sorter sorter = new ShuttleSorter();
583: assertEquals("order < 0", -1, sorter.order);
584: Filter[] filters = new Filter[] { filterZero, filterTwo, sorter };
585: new FilterPipeline(filters);
586: assertOrder(filterZero, filters);
587: }
588:
589: /**
590: * FilterPipeline allows maximal one sorter per column.
591: *
592: */
593: public void testDuplicatedSortColumnException() {
594: Filter[] filters = new Filter[] { new ShuttleSorter(),
595: new ShuttleSorter() };
596: try {
597: new FilterPipeline(filters);
598: fail("trying to sort one column more than once must throw IllegalArgumentException");
599:
600: } catch (IllegalArgumentException e) {
601: // that's what we expect
602: } catch (Exception e) {
603: fail("trying to sort one column more than once must throw IllegalArgumentException"
604: + "instead of " + e);
605: }
606: }
607:
608: /**
609: * A filter can be bound to maximally one pipeline.
610: */
611: public void testAssignFilterPipelineBoundFilterException() {
612: Filter filter = createDefaultPatternFilter(0);
613: assertEquals("order < 0", -1, filter.order);
614: Filter[] filters = new Filter[] { filter };
615: new FilterPipeline(filters);
616: try {
617: new FilterPipeline(filters);
618: fail("sharing filters are not allowed - must throw IllegalArgumentException");
619: } catch (IllegalArgumentException e) {
620: // that's what we expect
621: } catch (Exception e) {
622: fail("exception must be illegalArgument instead of " + e);
623: }
624: }
625:
626: /**
627: * early binding of pipeline to filters.
628: *
629: */
630: public void testAssignFilterPipeline() {
631: Filter filter = createDefaultPatternFilter(0);
632: Filter[] filters = new Filter[] { filter };
633: FilterPipeline pipeline = new FilterPipeline(filters);
634: assertEquals("assigned to pipeline", pipeline, filter
635: .getPipeline());
636: JXTable table = new JXTable(tableModel);
637: table.setFilters(pipeline);
638: assertEquals("assigned to table's componentadapter", table,
639: filter.adapter.getComponent());
640: }
641:
642: private void assertOrder(Filter filter, Filter[] filters) {
643: int position = getFilterPosition(filter, filters);
644: assertEquals("order equals position in array", position,
645: filter.order);
646: }
647:
648: protected void assertOrders(Filter[] filters) {
649: for (int i = 0; i < filters.length; i++) {
650: assertEquals("order must be equal to filter position", i,
651: filters[i].order);
652: }
653: }
654:
655: private int getFilterPosition(Filter filter, Filter[] filters) {
656: for (int i = 0; i < filters.length; i++) {
657: if (filters[i].equals(filter)) {
658: return i;
659: }
660: }
661: return -1;
662: }
663:
664: /**
665: * returns a pipeline with two default patternfilters on
666: * column 0, 2 and an ascending sorter on column 0.
667: */
668: protected FilterPipeline createPipeline() {
669: Filter filterZero = createDefaultPatternFilter(0);
670: Filter filterTwo = createDefaultPatternFilter(2);
671: Sorter sorter = new ShuttleSorter();
672: Filter[] filters = new Filter[] { filterZero, filterTwo, sorter };
673: FilterPipeline pipeline = new FilterPipeline(filters);
674: return pipeline;
675: }
676:
677: /** returns a PatternFilter for occurences of "e" in column.
678: *
679: * @param column
680: * @return a PatternFilter for occurences of "e" in column
681: */
682: protected Filter createDefaultPatternFilter(int column) {
683: Filter filterZero = new PatternFilter("e", 0, column);
684: return filterZero;
685: }
686:
687: /**
688: * This is a test to ensure that the example in the javadoc actually works.
689: * if the javadoc example changes, then those changes should be pasted here.
690: */
691: public void testJavaDocExample() {
692:
693: Filter[] filters = new Filter[] {
694: new PatternFilter("^S", 0, 1), // regex, matchflags, column
695: new ShuttleSorter(1, false), // column 1, descending
696: new ShuttleSorter(0, true), // column 0, ascending
697: };
698: FilterPipeline pipeline = new FilterPipeline(filters);
699: JXTable table = new JXTable();
700: table.setFilters(pipeline);
701: }
702:
703: protected void setUp() throws Exception {
704: super .setUp();
705: tableModel = new AncientSwingTeam();
706: directModelAdapter = new DirectModelAdapter(tableModel);
707: pipelineReport = new PipelineReport();
708: }
709:
710: /**
711: * ComponentAdapter directly on top of a TableModel.
712: */
713: public static class DirectModelAdapter extends ComponentAdapter {
714:
715: private TableModel tableModel;
716:
717: public DirectModelAdapter(TableModel tableModel) {
718: super (null);
719: this .tableModel = tableModel;
720: }
721:
722: public int getColumnCount() {
723: return tableModel.getColumnCount();
724: }
725:
726: public int getRowCount() {
727: return tableModel.getRowCount();
728: }
729:
730: public String getColumnName(int columnIndex) {
731: return tableModel.getColumnName(columnIndex);
732: }
733:
734: public String getColumnIdentifier(int columnIndex) {
735: return getColumnName(columnIndex);
736: }
737:
738: public Object getValueAt(int row, int column) {
739: return tableModel.getValueAt(row, column);
740: }
741:
742: public Object getFilteredValueAt(int row, int column) {
743: return null;
744: }
745:
746: public void setValueAt(Object aValue, int row, int column) {
747: tableModel.setValueAt(aValue, row, column);
748:
749: }
750:
751: public boolean isCellEditable(int row, int column) {
752: return tableModel.isCellEditable(row, column);
753: }
754:
755: public boolean hasFocus() {
756: return false;
757: }
758:
759: public boolean isSelected() {
760: return false;
761: }
762:
763: public void refresh() {
764: // do nothing
765: }
766: }
767:
768: /**
769: * just to see the filtering effects...
770: *
771: */
772: public void interactiveTestColumnControlAndFilters() {
773: final JXTable table = new JXTable(tableModel);
774: table.setColumnControlVisible(true);
775: Action toggleFilter = new AbstractAction("Toggle Filters") {
776: boolean hasFilters;
777:
778: public void actionPerformed(ActionEvent e) {
779: if (hasFilters) {
780: table.setFilters(null);
781: } else {
782: table.setFilters(createPipeline());
783: }
784: hasFilters = !hasFilters;
785:
786: }
787:
788: };
789: toggleFilter.putValue(Action.SHORT_DESCRIPTION,
790: "filtering first column - problem if invisible ");
791: JXFrame frame = wrapWithScrollingInFrame(table,
792: "JXTable ColumnControl and Filters");
793: addAction(frame, toggleFilter);
794: frame.setVisible(true);
795: }
796:
797: /**
798: * just to see the filtering effects...
799: *
800: */
801: public void interactiveTestAndFilter() {
802: final JXTable table = new JXTable(tableModel);
803: table.setColumnControlVisible(true);
804: Action toggleFilter = new AbstractAction("Toggle Filters") {
805: boolean hasFilters;
806:
807: public void actionPerformed(ActionEvent e) {
808: if (hasFilters) {
809: table.setFilters(null);
810: } else {
811: PatternFilter first = new PatternFilter("a", 0, 0);
812: PatternFilter second = new PatternFilter("b", 0, 1);
813: FilterPipeline pipeline = new FilterPipeline(
814: new Filter[] { first, second });
815: table.setFilters(pipeline);
816: }
817: hasFilters = !hasFilters;
818:
819: }
820:
821: };
822: toggleFilter
823: .putValue(Action.SHORT_DESCRIPTION,
824: "Filtered rows: col(0) contains 'a' AND col(1) contains 'b'");
825: JXFrame frame = wrapWithScrollingInFrame(table,
826: "JXTable ColumnControl and Filters");
827: addAction(frame, toggleFilter);
828: frame.setVisible(true);
829: }
830:
831: public static void main(String args[]) {
832: setSystemLF(true);
833: FilterTest test = new FilterTest();
834: try {
835: test.runInteractiveTests();
836: } catch (Exception e) {
837: System.err
838: .println("exception when executing interactive tests:");
839: e.printStackTrace();
840: }
841: }
842:
843: }
|