KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > decorator > HighlighterTest


1 /*
2  * $Id: HighlighterTest.java,v 1.2 2004/10/14 01:28:21 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.decorator;
9
10 import java.awt.Color JavaDoc;
11
12 import junit.framework.TestCase;
13
14 import org.jdesktop.swing.JXTable;
15
16 public class HighlighterTest extends TestCase {
17
18     private Highlighter[] highlighters;
19
20     protected void setUp() {
21         highlighters = new Highlighter[] {
22             new AlternateRowHighlighter(Color.white, new Color JavaDoc(0xF0, 0xF0, 0xE0), null),
23             new PatternHighlighter(null, Color.red, "s.*", 0, 0)
24         };
25     }
26
27     protected void tearDown() {
28         for (int i = 0; i < highlighters.length; i++) {
29             highlighters[i] = null;
30         }
31         highlighters = null;
32     }
33
34     /**
35      * This is a test to ensure that the example in the javadoc actually works.
36      * if the javadoc example changes, then those changes should be pasted here.
37      */

38     public void testJavaDocExample() {
39         Highlighter[] highlighters = new Highlighter[] {
40             new AlternateRowHighlighter(Color.white, new Color JavaDoc(0xF0, 0xF0, 0xE0), null),
41             new PatternHighlighter(null, Color.red, "s.*", 0, 0)
42         };
43
44         HighlighterPipeline highlighterPipeline = new HighlighterPipeline(highlighters);
45         JXTable table = new JXTable();
46         table.setHighlighters(highlighterPipeline);
47     }
48
49     /*
50      */

51     public void testAddRemoveHighlighter() {
52         HighlighterPipeline pipeline = new HighlighterPipeline(highlighters);
53
54         Highlighter hl = new PatternHighlighter(Color.blue, Color.red, "mark", 0, 0);
55         Highlighter hl2 = new PatternHighlighter(Color.white, Color.black, "amy", 0, 0);
56
57         // added highlighter should be appended
58
pipeline.addHighlighter(hl);
59
60         Highlighter[] hls = pipeline.getHighlighters();
61
62         assertTrue(hls.length == 3);
63         assertTrue(hls[2] == hl);
64
65         // added highlighter should be prepended
66
pipeline.addHighlighter(hl2, true);
67
68         hls = pipeline.getHighlighters();
69
70         assertTrue(hls.length == 4);
71         assertTrue(hls[0] == hl2);
72
73         // remove highlighter
74
pipeline.removeHighlighter(hl);
75
76         hls = pipeline.getHighlighters();
77         assertTrue(hls.length == 3);
78         for (int i = 0; i < hls.length; i++) {
79             assertTrue(hls[i] != hl);
80         }
81
82         // remove another highligher
83
pipeline.removeHighlighter(hl2);
84
85         hls = pipeline.getHighlighters();
86         assertTrue(hls.length == 2);
87         for (int i = 0; i < hls.length; i++) {
88             assertTrue(hls[i] != hl2);
89         }
90     }
91 }
92
Popular Tags