1 7 8 package org.jdesktop.jdnc; 9 10 import java.awt.Color ; 11 import java.awt.Font ; 12 import java.awt.Point ; 13 import java.awt.event.ActionEvent ; 14 import java.io.IOException ; 15 import java.net.URL ; 16 import java.text.SimpleDateFormat ; 17 import java.util.Date ; 18 import java.util.Locale ; 19 20 import javax.swing.AbstractAction ; 21 import javax.swing.Action ; 22 import javax.swing.ImageIcon ; 23 import javax.swing.JFrame ; 24 import javax.swing.JLabel ; 25 import javax.swing.table.DefaultTableModel ; 26 import javax.swing.table.TableModel ; 27 28 import org.jdesktop.jdnc.actions.FindAction; 29 import org.jdesktop.jdnc.actions.PrintAction; 30 import org.jdesktop.swing.InteractiveTestCase; 31 import org.jdesktop.swing.JXSearchPanel; 32 import org.jdesktop.swing.actions.TargetManager; 33 import org.jdesktop.swing.data.DefaultTableModelExt; 34 import org.jdesktop.swing.data.EnumeratedMetaData; 35 import org.jdesktop.swing.data.MetaData; 36 import org.jdesktop.swing.data.NumberMetaData; 37 import org.jdesktop.swing.decorator.AlternateRowHighlighter; 38 import org.jdesktop.swing.decorator.Filter; 39 import org.jdesktop.swing.decorator.FilterPipeline; 40 import org.jdesktop.swing.decorator.Highlighter; 41 import org.jdesktop.swing.decorator.HighlighterPipeline; 42 import org.jdesktop.swing.decorator.PatternFilter; 43 import org.jdesktop.swing.decorator.PatternHighlighter; 44 45 46 public class JNTableUnitTest extends InteractiveTestCase { 47 48 final static String DATA_URL = "resources/bugdata.txt"; 49 50 final static String bugstates[] = { 51 "dispatched", "accepted", "evaluated", "committed", 52 "fixed", "integrated", "verified", "closed"}; 53 54 public static DefaultTableModelExt createRawData(URL dataURL) { 55 DefaultTableModelExt rawData = new DefaultTableModelExt(dataURL); 56 try { 57 rawData.startLoading(); 58 } 59 catch (IOException e) { 60 e.printStackTrace(); 61 } 62 return rawData; 63 } 64 65 public static DefaultTableModelExt createRichData(URL dataURL) { 66 DefaultTableModelExt richdata = new DefaultTableModelExt(dataURL); 67 richdata.setColumnCount(8); 68 69 MetaData metaData = richdata.getColumnMetaData(0); 70 metaData.setName("bugid"); 71 metaData.setLabel("Bug ID"); 72 metaData.setElementClass(java.lang.Integer .class); 73 74 NumberMetaData numberMetaData = new NumberMetaData("priority"); 75 numberMetaData.setLabel("Pri"); 76 numberMetaData.setMinimum(new Integer (1)); 77 numberMetaData.setMaximum(new Integer (5)); 78 richdata.setColumnMetaData(1, numberMetaData); 79 80 numberMetaData = new NumberMetaData("severity"); 81 numberMetaData.setLabel("Sev"); 82 numberMetaData.setReadOnly(true); 83 richdata.setColumnMetaData(2, numberMetaData); 84 85 metaData = richdata.getColumnMetaData(3); 86 metaData.setName("engineer"); 87 metaData.setLabel("Engineer"); 88 89 metaData = richdata.getColumnMetaData(4); 90 metaData.setName("dispatchdate"); 91 metaData.setLabel("Dispatched"); 92 metaData.setElementClass(java.util.Date .class); 93 metaData.setDecodeFormat(new SimpleDateFormat ( 94 "EEE MMM dd HH:mm:ss zzz yyyy")); 95 96 numberMetaData = new NumberMetaData("jdcvotes"); 97 numberMetaData.setLabel("JDC Votes"); 98 numberMetaData.setReadOnly(true); 99 richdata.setColumnMetaData(5, numberMetaData); 100 101 metaData = richdata.getColumnMetaData(6); 102 metaData.setName("synopsis"); 103 metaData.setLabel("Synopsis"); 104 105 EnumeratedMetaData enumMetaData = new EnumeratedMetaData("state"); 106 enumMetaData.setLabel("State"); 107 enumMetaData.setEnumeration(bugstates); 108 richdata.setColumnMetaData(7, enumMetaData); 109 110 try { 111 richdata.startLoading(); 112 } 113 catch (IOException e) { 114 e.printStackTrace(); 115 } 116 return richdata; 117 } 118 119 private URL dataURL; 120 private DefaultTableModelExt rawdata; 121 private DefaultTableModelExt richdata; 122 123 private Point frameLocation = new Point (0,0); 124 125 public JNTableUnitTest() { 126 super("JNTable unit test"); 127 } 128 129 protected void setUp() { 130 Locale.setDefault(Locale.US); 133 dataURL = JNTableUnitTest.class.getResource(DATA_URL); 134 135 rawdata = createRawData(dataURL); 139 richdata = createRichData(dataURL); 140 } 141 142 public void testInstantiation() { 144 JNTable table = new JNTable(); 145 } 146 147 152 public void testHighlighers() { 153 Highlighter[] highlighters = new Highlighter[] { 154 new AlternateRowHighlighter(Color.white, new Color (0xF0, 0xF0, 0xE0), null), 155 new PatternHighlighter(null, Color.red, "s.*", 0, 0) 156 }; 157 158 HighlighterPipeline highlighterPipeline = new HighlighterPipeline(highlighters); 159 JNTable table = new JNTable(); 160 table.setHighlighters(highlighterPipeline); 161 table.setColumnForeground("bugid", Color.green); 162 } 163 164 public void interactiveTestJNTableActions() { 165 JNTable table = new JNTable(rawdata); 166 table.setRowHeaderLocked(true); 167 168 PatternFilter patternFilter = new PatternFilter(null, 0, 3); 169 JXSearchPanel searchPanel = new JXSearchPanel(); 170 searchPanel.setPatternFilter(patternFilter); 171 table.setFilters(new FilterPipeline(new Filter[] { 172 patternFilter, 173 })); 174 175 PrintAction print = new PrintAction(); 176 table.addAction(print); 177 178 FindAction search = new FindAction(); 179 table.addAction(search); 180 TargetManager.getInstance().setTarget(table); 183 table.addToolBarComponent(searchPanel); 184 JFrame frame = wrapInFrame(table, "JNTable Actions"); 185 frame.setVisible(true); 186 } 187 188 public void interactiveTestJNTableAlternatingRowColors() { 189 JNTable table = new JNTable(rawdata); 190 table.setOddRowBackground(new Color (0xCC, 0xCC, 0xFF)); 191 table.setEvenRowBackground(Color.white); 192 table.setShowHorizontalLines(false); 193 JFrame frame = wrapInFrame(table, "JNTable Alternating Row Colors"); 194 frame.setVisible(true); 195 } 196 197 205 public void interactiveTestToggleModel() { 206 final JNTable table = new JNTable(); 207 Filter filter = new PatternFilter(".*4.*", 0, 0); 208 table.setFilters(new FilterPipeline(new Filter[] {filter})); 209 Action toggleAction = new AbstractAction ("Toggle Model") { 210 int startRow = 0; 211 public void actionPerformed(ActionEvent e) { 212 int count = new Date ().getSeconds(); 213 table.setModel(createModel(startRow, count)); 214 startRow +=count; 215 216 } 217 218 private TableModel createModel(int startRow, int count) { 219 DefaultTableModel model = new DefaultTableModel (count, 5); 220 for (int i = 0; i < model.getRowCount(); i++) { 221 model.setValueAt(new Integer (startRow++), i, 0); 222 } 223 return model; 224 } 225 226 }; 227 table.addAction(toggleAction); 228 table.setModel(new DefaultTableModel (10, 5)); 229 table.setHasColumnControl(true); 230 JFrame frame = wrapInFrame(table, "JNTable Toggle Model"); 231 frame.setVisible(true); 232 } 233 234 239 public void interactiveTestAddRow() { 240 final JNTable table = new JNTable(); 241 Filter filter = new PatternFilter(".*4.*", 0, 0); 242 table.setFilters(new FilterPipeline(new Filter[] {filter})); 243 Action toggleAction = new AbstractAction ("Add Row") { 244 int count = -100; 245 public void actionPerformed(ActionEvent e) { 246 ((DefaultTableModel ) table.getModel()).addRow(new Object [] { new Integer (count--) }); 247 248 } 249 250 251 }; 252 table.addAction(toggleAction); 253 table.setModel(createModel(40, 10)); 254 table.setHasColumnControl(true); 255 JFrame frame = wrapInFrame(table, "JNTable Add Row"); 256 frame.setVisible(true); 257 } 258 259 265 public void interactiveTestRemoveRow() { 266 final JNTable table = new JNTable(); 267 Filter filter = new PatternFilter(".*4.*", 0, 0); 268 table.setFilters(new FilterPipeline(new Filter[] {filter})); 269 Action toggleAction = new AbstractAction ("Remove Row") { 270 public void actionPerformed(ActionEvent e) { 271 if (table.getModel().getRowCount() > 0) 272 ((DefaultTableModel ) table.getModel()).removeRow(0); 273 274 } 275 276 277 }; 278 table.addAction(toggleAction); 279 table.setModel(createModel(40, 10)); 280 table.setHasColumnControl(true); 281 JFrame frame = wrapInFrame(table, "JNTable Remove Row"); 282 frame.setVisible(true); 283 } 284 285 private DefaultTableModel createModel(int startRow, int count) { 286 DefaultTableModel model = new DefaultTableModel (count, 5); 287 for (int i = 0; i < model.getRowCount(); i++) { 288 model.setValueAt(new Integer (startRow++), i, 0); 289 } 290 return model; 291 } 292 293 297 public void interactiveTestJNTableFewRows() { 298 final JNTable table = new JNTable(); 299 Action toggleAction = new AbstractAction ("Toggle Control") { 300 301 public void actionPerformed(ActionEvent e) { 302 table.setHasColumnControl(!table.getHasColumnControl()); 303 304 } 305 306 }; 307 table.addAction(toggleAction); 308 table.setModel(new DefaultTableModel (10, 5)); 309 table.setHasColumnControl(true); 310 JFrame frame = wrapInFrame(table, "JNTable ColumnControl with few rows"); 311 frame.setVisible(true); 312 } 313 319 public void testColumnControlAndFilters() { 320 final JNTable table = new JNTable(richdata); 321 table.setHasColumnControl(true); 322 Filter filter = new PatternFilter("41.*", 0, 0); 323 table.setFilters(new FilterPipeline(new Filter[] {filter})); 324 table.getColumn(0).setVisible(false); 327 table.setFilters(null); 328 329 } 330 334 public void interactiveTestJNTableColumnControlAndFilters() { 335 final JNTable table = new JNTable(richdata); 336 Action toggleFilter = new AbstractAction ("Toggle Filter") { 337 338 public void actionPerformed(ActionEvent e) { 339 if (table.getFilters() != null) { 340 table.setFilters(null); 341 } else { 342 Filter filter = new PatternFilter("41.*", 0, 0); 343 table.setFilters(new FilterPipeline(new Filter[] {filter})); 344 345 } 346 347 } 348 349 }; 350 table.addAction(toggleFilter); 351 table.setHasColumnControl(true); 352 JFrame frame = wrapInFrame(table, "JNTable ColumnControl and Filters"); 353 frame.setVisible(true); 354 } 355 364 public void interactiveTestJNTableAlternatingRowColorsWithHighlighters() { 365 JNTable table = new JNTable(rawdata); 366 Highlighter[] highlighters = new Highlighter[] { 367 new PatternHighlighter(null, Color.red, "41.*", 0, 0) 369 }; 370 371 HighlighterPipeline highlighterPipeline = new HighlighterPipeline(highlighters); 372 373 table.setOddRowBackground(new Color (0xCC, 0xCC, 0xFF)); 375 table.setEvenRowBackground(Color.white); 376 table.setShowHorizontalLines(false); 377 table.setHighlighters(highlighterPipeline); 379 JFrame frame = wrapInFrame(table, "JNTable Alternating Row Colors with Highlighters"); 380 frame.setVisible(true); 381 } 382 383 389 public void interactiveTestJNTableRowHeaderLocked() { 390 JNTable table = new JNTable(rawdata); 391 table.setRowHeaderLocked(true); 392 JFrame frame = wrapInFrame(table, "JNTable RowHeader Locked"); 393 frame.setVisible(true); 394 } 395 396 public void interactiveTestJNTableColumnControl() { 397 JNTable table = new JNTable(rawdata); 398 table.setHasColumnControl(true); 399 JFrame frame = wrapInFrame(table, "JNTable ColumnControl"); 400 frame.setVisible(true); 401 } 402 403 409 public void interactiveTestJNTableColumnControlAndRowHeader() { 410 JNTable table = new JNTable(rawdata); 411 table.setHasColumnControl(true); 412 table.setRowHeaderLocked(true); 413 JFrame frame = wrapInFrame(table, "JNTable ColumnControl and RowHeader"); 414 frame.setVisible(true); 415 } 416 417 public void interactiveTestJNTableColumnProperties() { 418 JNTable table = new JNTable(richdata); 419 table.setOddRowBackground(Color.cyan); 420 421 table.setHeaderBackground(Color.blue); 422 table.setHeaderForeground(Color.white); 423 table.setHeaderFont(new Font ("sanserif", Font.BOLD, 18)); 424 table.setHeaderSortDownIcon(new ImageIcon ( 425 JNTableUnitTest.class.getResource("resources/down.png"))); 426 table.setHeaderSortUpIcon(new ImageIcon ( 427 JNTableUnitTest.class.getResource("resources/up.png"))); 428 429 table.setColumnHorizontalAlignment("bugid", JLabel.CENTER); 430 table.setColumnPrototypeValue("bugid", "888888"); 431 table.setColumnBackground("dispatchdate", Color.green); 432 table.setColumnBackground("priority", Color.red); 433 table.setColumnForeground("priority", Color.blue); 434 table.setColumnPrototypeValue("priority", new Integer (5)); 435 table.setColumnFont("priority", new Font ("sanserif", Font.ITALIC, 20)); 436 table.setColumnBackground("severity", Color.black); 437 table.setColumnForeground("severity", Color.white); 438 table.setColumnPrototypeValue("synopsis", "JTable Horizontal Scrollbar doesn't work"); 439 440 JFrame frame = wrapInFrame(table, "JNTable Column Properties"); 441 frame.setVisible(true); 442 } 443 444 public void interactiveTestJNTableFiltering() { 445 final JNTable table = new JNTable(richdata); 446 table.setHasColumnControl(true); 447 448 Filter[] filters = new Filter[] { 449 new PatternFilter(".*", 0, 0)}; 450 451 FilterPipeline fp = new FilterPipeline(filters) { 452 public void flush() { 453 super.flush(); 454 } 462 }; 463 464 table.setFilters(fp); 465 table.setOddRowBackground(new Color (255, 0, 0)); 466 467 JFrame frame = wrapInFrame(table, "JNTable Filtering"); 468 frame.setVisible(true); 469 470 } 471 472 public static void main(String args[]) { 473 JNTableUnitTest test = new JNTableUnitTest(); 475 try { 476 test.runInteractiveTests("interactive.*Toggle.*"); 478 } catch (Exception e) { 481 System.err.println("exception when executing interactive tests:"); 482 e.printStackTrace(); 483 } 484 } 485 } 486 487 | Popular Tags |