KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > FindTest


1 /*
2  * $Id: FindTest.java,v 1.3 2004/09/08 01:37:46 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;
9
10 import java.awt.BorderLayout JavaDoc;
11 import java.awt.Component JavaDoc;
12 import java.awt.GraphicsEnvironment JavaDoc;
13
14 import java.net.URL JavaDoc;
15
16 import java.util.regex.Pattern JavaDoc;
17
18 import javax.swing.*;
19
20 import javax.swing.table.AbstractTableModel JavaDoc;
21 import javax.swing.table.TableModel JavaDoc;
22
23 import org.jdesktop.swing.JXFindDialog;
24 import org.jdesktop.swing.JXTable;
25 import org.jdesktop.swing.JXEditorPane;
26 import org.jdesktop.swing.Searchable;
27
28 import org.jdesktop.swing.actions.TargetableAction;
29
30 import junit.framework.Test;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
33
34 public class FindTest extends TestCase {
35
36     public FindTest() {
37         super("Find Action Test");
38     }
39
40     public void testCreate() {
41         // JXFindDialog dialog = new JXFindDialog(null);
42
}
43
44     public void testEditor() {
45         URL JavaDoc url = FindTest.class.getResource("resources/test.txt");
46         try {
47             JXEditorPane editor = new JXEditorPane(url);
48
49             // There are 9 instances of "four" in the test document
50
int useIndex = -1;
51             int lastIndex = -1;
52             for (int i = 0; i < 9; i++) {
53                 lastIndex = editor.search("four", useIndex);
54                 assertTrue(lastIndex != -1);
55                 assertTrue(lastIndex != useIndex);
56
57                 assertEquals("Error text selection is incorrect", "four", editor.getSelectedText());
58
59                 useIndex = lastIndex;
60             }
61         } catch (Exception JavaDoc ex) {
62             throw new RuntimeException JavaDoc("Error finding resource for JXEditorPane", ex);
63         }
64     }
65
66     public void testTable() {
67         JXTable table = new JXTable(new TestTableModel());
68         // There are 100 instances of "One" in the test document
69
int useIndex = -1;
70         int lastIndex = -1;
71         for (int i = 0; i < 100; i++) {
72             lastIndex = table.search("One", useIndex);
73             assertTrue(lastIndex != -1);
74             assertTrue(lastIndex != useIndex);
75
76             assertEquals("Row not selected", lastIndex, table.getSelectedRow());
77             assertEquals("Column not selected", 0, table.getSelectedColumn());
78
79             String JavaDoc value = (String JavaDoc)table.getValueAt(table.getSelectedRow(),
80                                                     table.getSelectedColumn());
81             assertTrue(value.startsWith("One"));
82
83             useIndex = lastIndex;
84         }
85     }
86
87     /**
88      * Simple test to ensure that flags are set correctly.
89      */

90     public void testFlags() {
91         // This test will not work in a headless configuration.
92
if (GraphicsEnvironment.isHeadless()) {
93             return;
94         }
95
96         JXFindDialog dialog = new JXFindDialog(new TestSearchable());
97
98         boolean[] states = { true, false, false, true, true };
99         for (int i = 0; i < states.length; i++) {
100             dialog.setMatchFlag(states[i]);
101             assertEquals(states[i], dialog.getMatchFlag());
102
103             dialog.setWrapFlag(states[i]);
104             assertEquals(states[i], dialog.getWrapFlag());
105
106             dialog.setBackwardsFlag(states[i]);
107             assertEquals(states[i], dialog.getBackwardsFlag());
108         }
109     }
110
111
112     public void testMatchCase() {
113     }
114
115     public static void main(String JavaDoc[] args) {
116         showDialog();
117         showTable();
118         showEditor();
119         showSplitPane();
120     }
121
122     private static void showDialog() {
123         JXFindDialog dialog = new JXFindDialog(new TestSearchable());
124         dialog.setDebug(true);
125         dialog.setVisible(true);
126     }
127
128     private static void showEditor() {
129         try {
130             showComponent(new JXEditorPane(FindTest.class.getResource("resources/test.txt")));
131         } catch (Exception JavaDoc ex) {
132             throw new RuntimeException JavaDoc("Error finding resource for JXEditorPane", ex);
133         }
134     }
135
136     private static void showTable() {
137         showComponent(new JXTable(new TestTableModel()));
138     }
139
140     private static void showSplitPane() {
141         try {
142             showComponent(new JSplitPane(JSplitPane.VERTICAL_SPLIT,
143                                          new JXEditorPane(FindTest.class.getResource("resources/test.txt")),
144                                          new JXTable(new TestTableModel())));
145         } catch (Exception JavaDoc ex) {
146             throw new RuntimeException JavaDoc("Error finding resource for JXEditorPane", ex);
147         }
148     }
149
150     private static void showComponent(Component JavaDoc component) {
151         Action action = new TargetableAction("Find", "find");
152         JToolBar toolbar = new JToolBar();
153         JButton button = new JButton(action);
154         button.setFocusable(false);
155         toolbar.add(button);
156
157         // Must add a menu bar so that the Ctrl-F will work.
158
JMenuBar menubar = new JMenuBar();
159         JMenu menu = menubar.add(new JMenu("File"));
160         menu.add(new JMenuItem(action));
161
162         JPanel panel = new JPanel(new BorderLayout JavaDoc());
163         panel.add(toolbar, BorderLayout.NORTH);
164         panel.add(new JScrollPane(component), BorderLayout.CENTER);
165
166         JFrame frame = new JFrame();
167         frame.setJMenuBar(menubar);
168         frame.getContentPane().add(panel);
169
170         frame.pack();
171         frame.setVisible(true);
172     }
173
174     public static class TestTableModel extends AbstractTableModel JavaDoc {
175
176         private static String JavaDoc[] data = { "One", "Two", "Three",
177                                          "Four", "Five" };
178
179         public int getRowCount() { return 100; }
180         public int getColumnCount() { return data.length; }
181
182         public Object JavaDoc getValueAt(int row, int column) {
183             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(data[column]);
184             buffer.append(row);
185             return buffer.toString();
186         }
187     }
188
189     /**
190      * A small class that implements the Searchable interface.
191      */

192     public static class TestSearchable extends JLabel implements Searchable {
193
194         private boolean succeed;
195
196         public TestSearchable() {
197             this(false);
198         }
199
200         /**
201          * @param succeed flag to indicate that all searches succeed.
202          */

203         public TestSearchable(boolean succeed) {
204             this.succeed = succeed;
205         }
206
207         public int search(String JavaDoc searchString) {
208             return search(searchString, -1);
209         }
210         public int search(String JavaDoc searchString, int startIndex) {
211             return succeed ? 100 : -1;
212         }
213
214         public int search(Pattern JavaDoc pattern) {
215             return search(pattern, -1);
216         }
217
218         public int search(Pattern JavaDoc pattern, int startIndex) {
219             return succeed ? 100 : -1;
220         }
221
222         public int search(Pattern JavaDoc pattern, int startIndex, boolean backwards) {
223             return succeed ? 100 : -1;
224         }
225
226     }
227 }
228
Popular Tags