|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| PathData.java | 81.2% | 91.3% | 87.5% | 87.8% |
|
||||||||||||||
| 1 |
package junit.extensions.jfcunit.eventdata;
|
|
| 2 |
|
|
| 3 |
import java.util.Arrays;
|
|
| 4 |
import java.util.Vector;
|
|
| 5 |
|
|
| 6 |
import java.awt.Component;
|
|
| 7 |
import java.awt.Container;
|
|
| 8 |
import javax.swing.JComponent;
|
|
| 9 |
import javax.swing.JMenu;
|
|
| 10 |
import javax.swing.JMenuBar;
|
|
| 11 |
import javax.swing.JMenuItem;
|
|
| 12 |
import javax.swing.JPopupMenu;
|
|
| 13 |
import javax.swing.JTree;
|
|
| 14 |
import javax.swing.tree.TreeModel;
|
|
| 15 |
import javax.swing.tree.TreePath;
|
|
| 16 |
|
|
| 17 |
import junit.extensions.jfcunit.finder.JMenuItemFinder;
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
/**
|
|
| 21 |
* <p>Title: PathData</p>
|
|
| 22 |
* <p>Description: This class encapsulates the data
|
|
| 23 |
* which is to be transformed into a path for the given
|
|
| 24 |
* GUI object type.</p>
|
|
| 25 |
* <p>Copyright: Copyright (c) 2003</p>
|
|
| 26 |
* <p>Company: JFCUnit Project</p>
|
|
| 27 |
* @author Kevin Wilson
|
|
| 28 |
* @version 1.0
|
|
| 29 |
*/
|
|
| 30 |
public class PathData { |
|
| 31 |
/**
|
|
| 32 |
* Indexes of the strings representing the path.
|
|
| 33 |
* Used only when there is a duplicate path element.
|
|
| 34 |
*/
|
|
| 35 |
private int[] m_indexes; |
|
| 36 |
|
|
| 37 |
/**
|
|
| 38 |
* Strings representing the path.
|
|
| 39 |
*/
|
|
| 40 |
private String[] m_path;
|
|
| 41 |
|
|
| 42 |
/**
|
|
| 43 |
* PathData constructor.
|
|
| 44 |
* @param size number of path elements
|
|
| 45 |
*/
|
|
| 46 | 12 |
public PathData(final int size) { |
| 47 | 12 |
m_path = new String[size];
|
| 48 | 12 |
m_indexes = new int[size]; |
| 49 |
} |
|
| 50 |
|
|
| 51 |
/**
|
|
| 52 |
* PathData constructor.
|
|
| 53 |
* @param path Path to be represented.
|
|
| 54 |
*/
|
|
| 55 | 34 |
public PathData(final String[] path) {
|
| 56 | 34 |
this(path, null); |
| 57 |
} |
|
| 58 |
|
|
| 59 |
/**
|
|
| 60 |
* PathData constructor.
|
|
| 61 |
*
|
|
| 62 |
* @param path String names of the items to be traversed.
|
|
| 63 |
* @param indexes index array for paths containing more
|
|
| 64 |
* than one identical string.
|
|
| 65 |
*/
|
|
| 66 | 35 |
public PathData(final String[] path, final int[] indexes) { |
| 67 | 35 |
this.m_path = path;
|
| 68 |
|
|
| 69 | 35 |
if (indexes == null) { |
| 70 | 34 |
this.m_indexes = new int[path.length]; |
| 71 | 34 |
Arrays.fill(this.m_indexes, 0);
|
| 72 |
} else {
|
|
| 73 | 1 |
this.m_indexes = indexes;
|
| 74 |
} |
|
| 75 |
|
|
| 76 | 35 |
if (this.m_indexes.length != path.length) { |
| 77 | 0 |
throw new java.lang.IllegalArgumentException( |
| 78 |
"Index length does not match string length");
|
|
| 79 |
} |
|
| 80 |
} |
|
| 81 |
|
|
| 82 |
/**
|
|
| 83 |
* PathData constructor.
|
|
| 84 |
* @param mi MenuItem to buid the path for.
|
|
| 85 |
*/
|
|
| 86 | 20 |
public PathData(final JMenuItem mi) {
|
| 87 | 20 |
Vector strings = new Vector();
|
| 88 |
|
|
| 89 |
// This loop builds up the name of the field from each of the
|
|
| 90 |
// higher level menu's text labels.
|
|
| 91 | 20 |
strings.insertElementAt( |
| 92 |
mi.getText(), |
|
| 93 |
0); |
|
| 94 |
|
|
| 95 | 20 |
Component parent = ((JPopupMenu) mi.getParent()).getInvoker(); |
| 96 |
|
|
| 97 | 20 |
while (parent instanceof JMenuItem) { |
| 98 | 40 |
String text = ((JMenuItem) parent).getText(); |
| 99 | 40 |
strings.insertElementAt(text, 0); |
| 100 |
|
|
| 101 | 40 |
if (parent instanceof JPopupMenu) { |
| 102 | 0 |
parent = ((JPopupMenu) parent.getParent()).getInvoker(); |
| 103 | 40 |
} else if (parent instanceof JMenu) { |
| 104 | 40 |
parent = ((JMenu) parent).getParent(); |
| 105 |
|
|
| 106 | 40 |
if (parent instanceof JPopupMenu) { |
| 107 | 20 |
parent = ((JPopupMenu) parent).getInvoker(); |
| 108 |
} |
|
| 109 |
} |
|
| 110 |
} |
|
| 111 |
|
|
| 112 | 20 |
this.m_path = (String[]) strings.toArray(new String[0]); |
| 113 | 20 |
m_indexes = new int[m_path.length]; |
| 114 | 20 |
Arrays.fill(m_indexes, 0); |
| 115 |
} |
|
| 116 |
|
|
| 117 |
/**
|
|
| 118 |
* Get the root object.
|
|
| 119 |
* @param mi Menu item to retrieve the root menu for.
|
|
| 120 |
* @return Root object.
|
|
| 121 |
*/
|
|
| 122 | 23 |
public Object getRoot(final JMenuItem mi) {
|
| 123 | 23 |
Component lastParent = null;
|
| 124 | 23 |
Component parent = mi; |
| 125 |
|
|
| 126 | 23 |
while (parent instanceof JMenuItem) { |
| 127 | 62 |
lastParent = parent; |
| 128 | 62 |
parent = parent.getParent(); |
| 129 |
|
|
| 130 | 62 |
if (parent instanceof JPopupMenu) { |
| 131 | 40 |
lastParent = parent; |
| 132 | 40 |
parent = ((JPopupMenu) parent).getInvoker(); |
| 133 |
} |
|
| 134 |
} |
|
| 135 |
|
|
| 136 | 23 |
if (parent instanceof JMenuBar) { |
| 137 | 22 |
return parent;
|
| 138 |
} |
|
| 139 |
|
|
| 140 | 1 |
return lastParent;
|
| 141 |
} |
|
| 142 |
|
|
| 143 |
/**
|
|
| 144 |
* set the path element at a given level.
|
|
| 145 |
* @param i Level of the path.
|
|
| 146 |
* @param data String of path element.
|
|
| 147 |
* @param index Index of path element.
|
|
| 148 |
*/
|
|
| 149 | 0 |
public final void set(final int i, final String data, final int index) { |
| 150 | 0 |
m_path[i] = data; |
| 151 | 0 |
m_indexes[i] = index; |
| 152 |
} |
|
| 153 |
|
|
| 154 |
/**
|
|
| 155 |
* Get the indexes for the JComponent.
|
|
| 156 |
* @param menu menu to be traversed.
|
|
| 157 |
* @return int[] indexes to be traversed.
|
|
| 158 |
*/
|
|
| 159 | 52 |
public int[] getIndexes(final JComponent menu) { |
| 160 | 52 |
int[] result = new int[m_path.length]; |
| 161 | 52 |
int i = 0;
|
| 162 | 52 |
Container parent = menu; |
| 163 | 52 |
JMenuItemFinder mFinder = new JMenuItemFinder(m_path[i]);
|
| 164 | 52 |
mFinder.setWait(0); |
| 165 |
|
|
| 166 | 52 |
Component c = mFinder.find(menu, m_indexes[i]); |
| 167 |
|
|
| 168 | 52 |
if (menu instanceof JMenuBar) { |
| 169 | 52 |
result[i++] = ((JMenuBar) menu).getComponentIndex(c); |
| 170 | 0 |
} else if (menu instanceof JPopupMenu) { |
| 171 | 0 |
result[i++] = ((JPopupMenu) menu).getComponentIndex(c); |
| 172 |
} |
|
| 173 |
|
|
| 174 | 52 |
for (; i < m_path.length; i++) {
|
| 175 | 104 |
if (c instanceof JMenu) { |
| 176 | 104 |
JMenu m = (JMenu) c; |
| 177 | 104 |
mFinder = new JMenuItemFinder(m_path[i]);
|
| 178 |
|
|
| 179 | 104 |
JPopupMenu pm = m.getPopupMenu(); |
| 180 | 104 |
c = mFinder.find(pm, m_indexes[i]); |
| 181 |
|
|
| 182 |
// Locate the element index
|
|
| 183 | 104 |
Component[] comps = pm.getComponents(); |
| 184 | 104 |
result[i] = -1; |
| 185 | 104 |
for (int j = 0; j < comps.length && result[i] == -1; j++) { |
| 186 | 200 |
if (comps[j] == c) {
|
| 187 | 104 |
result[i] = j; |
| 188 |
} |
|
| 189 |
} |
|
| 190 |
} |
|
| 191 |
} |
|
| 192 |
|
|
| 193 | 52 |
return result;
|
| 194 |
} |
|
| 195 |
|
|
| 196 |
/**
|
|
| 197 |
* Generate a tree path for this path and the given tree.
|
|
| 198 |
* @param tree JTree Tree to construct the path for.
|
|
| 199 |
* @return TreePath TreePath to be used in firing events.
|
|
| 200 |
*/
|
|
| 201 | 4 |
public TreePath getTreePath(final JTree tree) {
|
| 202 | 4 |
if (tree == null) { |
| 203 | 0 |
throw new IllegalArgumentException("Tree cannot be null"); |
| 204 |
} |
|
| 205 | 4 |
if (m_path.length == 0) {
|
| 206 | 1 |
return null; |
| 207 |
} |
|
| 208 |
|
|
| 209 | 3 |
TreeModel model = tree.getModel(); |
| 210 | 3 |
Object node = model.getRoot(); |
| 211 | 3 |
Vector path = new Vector();
|
| 212 |
|
|
| 213 | 3 |
for (int i = 0; i < this.m_path.length; i++) { |
| 214 | 11 |
String text = m_path[i]; |
| 215 | 11 |
int idx = m_indexes[i];
|
| 216 | 11 |
boolean found = false; |
| 217 | 11 |
boolean root = false; |
| 218 | 11 |
int children;
|
| 219 |
|
|
| 220 | 11 |
if (i == 0) {
|
| 221 | 3 |
root = true;
|
| 222 | 3 |
children = 1; |
| 223 |
} else {
|
|
| 224 | 8 |
children = model.getChildCount(node); |
| 225 |
} |
|
| 226 |
|
|
| 227 | 11 |
for (int j = 0; (j < children) && !found; j++) { |
| 228 | 14 |
Object newnode; |
| 229 |
|
|
| 230 | 14 |
if (root) {
|
| 231 | 3 |
newnode = node; |
| 232 |
} else {
|
|
| 233 | 11 |
newnode = model.getChild(node, j); |
| 234 |
} |
|
| 235 |
|
|
| 236 | 14 |
if (text.equals(newnode.toString())) {
|
| 237 | 12 |
if (idx == 0) {
|
| 238 | 11 |
node = newnode; |
| 239 | 11 |
path.add(node); |
| 240 | 11 |
found = true;
|
| 241 |
} else {
|
|
| 242 | 1 |
idx--; |
| 243 |
} |
|
| 244 |
} |
|
| 245 |
} |
|
| 246 |
|
|
| 247 | 11 |
if (!found) {
|
| 248 | 0 |
return null; |
| 249 |
} |
|
| 250 |
} |
|
| 251 |
|
|
| 252 | 3 |
TreePath tp = new TreePath(path.toArray());
|
| 253 |
|
|
| 254 | 3 |
return tp;
|
| 255 |
} |
|
| 256 |
|
|
| 257 |
// public int[] getIndexes(JPopupMenu menu) {
|
|
| 258 |
// int[] result = new int[path.length];
|
|
| 259 |
// int i = 0;
|
|
| 260 |
// Container parent = menu;
|
|
| 261 |
// JMenuItemFinder mFinder = new JMenuItemFinder(path[i]);
|
|
| 262 |
// Component c = TestHelper.findComponent(mFinder, menu, indexes[i]);
|
|
| 263 |
// result[i++] = menu.getComponentIndex(c);
|
|
| 264 |
//
|
|
| 265 |
// for (; i < path.length; i++) {
|
|
| 266 |
// if (c instanceof JMenu) {
|
|
| 267 |
// JMenu m = (JMenu) c;
|
|
| 268 |
// mFinder = new JMenuItemFinder(path[i]);
|
|
| 269 |
// JPopupMenu pm = m.getPopupMenu();
|
|
| 270 |
// c = TestHelper.findComponent(mFinder, pm, indexes[i]);
|
|
| 271 |
// result[i] = pm.getComponentIndex(c);
|
|
| 272 |
// }
|
|
| 273 |
// }
|
|
| 274 |
// return result;
|
|
| 275 |
// }
|
|
| 276 |
} |
|
| 277 |
|
|
||||||||||