KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > util > TestSupport


1 /*
2  * $Id: TestSupport.java,v 1.4 2005/01/27 14:40:31 kleopatra Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7 package org.jdesktop.swing.util;
8
9 import java.beans.PropertyChangeEvent JavaDoc;
10 import java.beans.PropertyChangeListener JavaDoc;
11
12 import java.io.IOException JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.util.HashMap JavaDoc;
15
16 import org.jdesktop.swing.data.MetaData;
17 import org.jdesktop.swing.data.DefaultTableModelExt;
18 import org.jdesktop.swing.data.TableModelExtTextLoader;
19
20 /**
21  * Contains utilities to assist with automated and interactive API tests.
22  * @author Amy Fowler
23  * @version 1.0
24  */

25 public class TestSupport {
26
27     public static DefaultTableModelExt createTabularDataModel(MetaData[] metaData, int rowCount) {
28         DefaultTableModelExt tabularData = new DefaultTableModelExt(metaData.length);
29         for (int i = 0; i < metaData.length; i++) {
30             tabularData.setColumnMetaData(i, metaData[i]);
31         }
32         for (int i = 0; i < rowCount; i++) {
33             tabularData.addRow(new Object JavaDoc[metaData.length]);
34             
35         }
36         return tabularData;
37       }
38
39     
40     public static DefaultTableModelExt loadTabularCSVData(URL JavaDoc dataURL, boolean firstRowHeader) {
41         DefaultTableModelExt data = new DefaultTableModelExt(dataURL);
42         data.getColumnCount();
43         TableModelExtTextLoader loader = (TableModelExtTextLoader) data.getLoader();
44         loader.setColumnDelimiter(",");
45         loader.setFirstRowHeader(firstRowHeader);
46         try {
47             data.startLoading();
48         } catch (IOException JavaDoc e) {
49             // TODO Auto-generated catch block
50
e.printStackTrace();
51         }
52         int row = 0;
53         while (data.isLoading()) {
54             // System.out.println(row++);
55
}
56         return data;
57     }
58
59     /**
60      * Class which can be used to track property change events.
61      * @author not attributable
62      * @version 1.0
63      */

64     public static class PropertyChangeTracker implements PropertyChangeListener JavaDoc {
65         private HashMap JavaDoc changedProperties = new HashMap JavaDoc();
66
67         public void propertyChange(PropertyChangeEvent JavaDoc e) {
68             changedProperties.put(e.getPropertyName(), e);
69         }
70
71         /**
72          *
73          * @param propertyName String containing the name of the property
74          * @return boolean indicating whether or not a property change event
75          * was fired for the property
76          */

77         public boolean gotPropertyChangeEvent(String JavaDoc propertyName) {
78             return changedProperties.containsKey(propertyName);
79         }
80
81         /**
82          * @throws NullPointerException if property change event did not occur
83          * @param propertyName String containing the name of the property
84          * @return Object containing the new value of the property
85          */

86         public Object JavaDoc getNewPropertyValue(String JavaDoc propertyName) {
87             PropertyChangeEvent JavaDoc e = (PropertyChangeEvent JavaDoc)
88                                           changedProperties.get(propertyName);
89             return e.getNewValue();
90         }
91
92         /**
93          * @throws NullPointerException if property change event did not occur
94          * @param propertyName String containing the name of the property
95          * @return Object containing the old value of the property
96          */

97         public Object JavaDoc getOldPropertyValue(String JavaDoc propertyName) {
98             PropertyChangeEvent JavaDoc e = (PropertyChangeEvent JavaDoc)
99                                           changedProperties.get(propertyName);
100             return e.getOldValue();
101         }
102
103         public boolean hasPropertyChangeEvents() {
104             return !changedProperties.isEmpty();
105         }
106
107         /**
108          * Clears any recorded property change events from tracker.
109          */

110         public void clear() {
111             changedProperties.clear();
112         }
113     }
114 }
Popular Tags