1 7 8 package org.jdesktop.swing; 9 10 import java.awt.BorderLayout ; 11 import java.awt.Component ; 12 import java.awt.GraphicsEnvironment ; 13 14 import java.net.URL ; 15 16 import java.util.regex.Pattern ; 17 18 import javax.swing.*; 19 20 import javax.swing.table.AbstractTableModel ; 21 import javax.swing.table.TableModel ; 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 } 43 44 public void testEditor() { 45 URL url = FindTest.class.getResource("resources/test.txt"); 46 try { 47 JXEditorPane editor = new JXEditorPane(url); 48 49 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 ex) { 62 throw new RuntimeException ("Error finding resource for JXEditorPane", ex); 63 } 64 } 65 66 public void testTable() { 67 JXTable table = new JXTable(new TestTableModel()); 68 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 value = (String )table.getValueAt(table.getSelectedRow(), 80 table.getSelectedColumn()); 81 assertTrue(value.startsWith("One")); 82 83 useIndex = lastIndex; 84 } 85 } 86 87 90 public void testFlags() { 91 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 [] 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 ex) { 132 throw new RuntimeException ("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 ex) { 146 throw new RuntimeException ("Error finding resource for JXEditorPane", ex); 147 } 148 } 149 150 private static void showComponent(Component 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 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 ()); 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 { 175 176 private static String [] 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 getValueAt(int row, int column) { 183 StringBuffer buffer = new StringBuffer (data[column]); 184 buffer.append(row); 185 return buffer.toString(); 186 } 187 } 188 189 192 public static class TestSearchable extends JLabel implements Searchable { 193 194 private boolean succeed; 195 196 public TestSearchable() { 197 this(false); 198 } 199 200 203 public TestSearchable(boolean succeed) { 204 this.succeed = succeed; 205 } 206 207 public int search(String searchString) { 208 return search(searchString, -1); 209 } 210 public int search(String searchString, int startIndex) { 211 return succeed ? 100 : -1; 212 } 213 214 public int search(Pattern pattern) { 215 return search(pattern, -1); 216 } 217 218 public int search(Pattern pattern, int startIndex) { 219 return succeed ? 100 : -1; 220 } 221 222 public int search(Pattern pattern, int startIndex, boolean backwards) { 223 return succeed ? 100 : -1; 224 } 225 226 } 227 } 228 | Popular Tags |