📄 tree.java
字号:
return jTree.getModel().getChildCount(jTreePath.getLastPathComponent());
}
/**
* Checks that a given node is selected, and that is is the only selection.
*/
public Assertion selectionEquals(final String path) {
return new Assertion() {
public void check() {
TreePath selectionPath = jTree.getSelectionPath();
Assert.assertNotNull(selectionPath);
TreePath expectedPath = getTreePath(path);
Assert.assertNotNull(expectedPath);
Assert.assertEquals(path, pathToString(selectionPath, separator));
}
};
}
/**
* Checks the selection contents.
*/
public Assertion selectionEquals(final String[] paths) {
return new Assertion() {
public void check() {
String[] expectedPaths = (String[])paths.clone();
TreePath[] selectionPaths = jTree.getSelectionPaths();
if (selectionPaths == null) {
selectionPaths = new TreePath[0];
}
String[] actual = new String[selectionPaths.length];
for (int i = 0; i < selectionPaths.length; i++) {
TreePath selectionPath = selectionPaths[i];
Assert.assertNotNull(selectionPath);
TreePath expectedPath = getTreePath(paths[i]);
actual[i] = pathToString(expectedPath, separator);
}
Arrays.sort(actual);
ArrayUtils.assertEquals(expectedPaths, actual);
}
};
}
/**
* Checks that the selection is empty.
*/
public Assertion selectionIsEmpty() {
return new Assertion() {
public void check() {
Assert.assertEquals(0, jTree.getSelectionCount());
}
};
}
/**
* Checks the font color used on a given node.
*/
public Assertion foregroundEquals(final String path, final String color) {
return new Assertion() {
public void check() {
Object userObject = getTreePath(path).getLastPathComponent();
ColorUtils.assertEquals(color, getShownColor(userObject));
}
};
}
/**
* Checks that the a given node of the jTree is expanded - i.e. that its children are made visible.
*
* @param path a String identifying the path to be expanded or collapsed
*/
public Assertion pathIsExpanded(final String path) {
return new Assertion() {
public void check() {
Assert.assertTrue(jTree.isExpanded(getTreePath(path)));
}
};
}
/**
* Expands or collapses a given node.
*
* @param path a String identifying the path to be expanded or collapsed
* @param expand if true, expand the node, and collapse it otherwise
*/
public void expand(String path, boolean expand) {
TreePath jTreePath = getTreePath(path);
if (expand) {
jTree.expandPath(jTreePath);
}
else {
jTree.collapsePath(jTreePath);
}
}
/**
* Expands all the nodes of the jTree.
*/
public void expandAll() {
expandSubTree(new TreePath(jTree.getModel().getRoot()));
}
public String toString() {
return getContent();
}
private TreePath computeChildTreePath(String parentPath, int childIndex) {
TreePath jTreePath = getTreePath(parentPath);
Object child = jTree.getModel().getChild(jTreePath.getLastPathComponent(), childIndex);
return jTreePath.pathByAddingChild(child);
}
private void clickOnTreePath(TreePath path,
boolean useRightClick,
Key.Modifier keyModifier) {
jTree.expandPath(path.getParentPath());
Rectangle rect = jTree.getRowBounds(jTree.getRowForPath(path));
if (rect != null) {
Mouse.doClickInRectangle(jTree, rect, useRightClick, keyModifier);
}
}
private String getShownText(Object object) {
return cellValueConverter.getValue(getRenderedComponent(object), object);
}
private Color getShownColor(Object object) {
return cellValueConverter.getForeground(getRenderedComponent(object), object);
}
private boolean isBold(Object object) {
return cellValueConverter.isBold(getRenderedComponent(object), object);
}
private Component getRenderedComponent(Object object) {
TreeCellRenderer renderer = jTree.getCellRenderer();
return renderer.getTreeCellRendererComponent(jTree, object,
false, false, false, 0, false);
}
private TreePath getTreePath(String path) {
TreePath jTreePath = findTreePath(path);
if (jTreePath == null) {
Assert.fail(badTreePath(path));
}
return jTreePath;
}
private TreePath findTreePath(String path) {
String[] pathArray = toArray(path, separator);
TreeModel model = jTree.getModel();
Object[] objects = new Object[pathArray.length + 1];
Object node = model.getRoot();
objects[0] = node;
for (int i = 0; i < pathArray.length; i++) {
Object exactMatch = null;
Object substringMatch = null;
boolean substringAmbiguity = false;
for (int j = 0; (j < model.getChildCount(node)); j++) {
Object child = model.getChild(node, j);
String shownText = getShownText(child);
if (pathArray[i].equals(shownText)) {
if (exactMatch != null) {
throw new AssertionFailedError("Naming ambiguity: there are several '" +
pathArray[i] + "' under '" +
getShownText(node) + "'");
}
exactMatch = child;
}
else if (shownText.indexOf(pathArray[i]) >= 0) {
if (substringMatch != null) {
substringAmbiguity = true;
}
substringMatch = child;
}
}
Object result;
if (exactMatch != null) {
result = exactMatch;
}
else if (substringAmbiguity) {
throw new AssertionFailedError("Naming ambiguity: there are several '" +
pathArray[i] + "' under '" +
getShownText(node) + "'");
}
else {
result = substringMatch;
}
if (result == null) {
return null;
}
objects[i + 1] = result;
node = result;
}
return new TreePath(objects);
}
private static String[] toArray(String path, String separator) {
List result = new ArrayList();
for (int index = 0; index < path.length();) {
int nextSeparatorPosition = path.indexOf(separator, index);
if (nextSeparatorPosition == -1) {
nextSeparatorPosition = path.length();
}
result.add(path.substring(index, nextSeparatorPosition));
index = nextSeparatorPosition + separator.length();
}
return (String[])(result.toArray(new String[result.size()]));
}
private void expandSubTree(TreePath path) {
TreeModel jTreeModel = jTree.getModel();
Object node = path.getLastPathComponent();
for (int i = 0; i < jTreeModel.getChildCount(node); i++) {
Object child = jTreeModel.getChild(node, i);
TreePath childPath = path.pathByAddingChild(child);
if (!isLeaf(childPath)) {
expandSubTree(childPath);
}
}
jTree.expandPath(path);
}
private boolean isLeaf(TreePath path) {
return jTree.getModel().isLeaf(path.getLastPathComponent());
}
private String pathToString(TreePath jTreePath, String separator) {
Object[] path = jTreePath.getPath();
StringBuffer buffer = new StringBuffer();
for (int i = 1; i < path.length; i++) {
buffer.append(getShownText(path[i]));
if (i < path.length - 1) {
buffer.append(separator);
}
}
return buffer.toString();
}
private void checkContents(String trimmedExpected) {
compareContents(trimmedExpected, getContent());
}
private String getContent() {
TreeModel model = jTree.getModel();
Object root = model.getRoot();
StringBuffer buffer = new StringBuffer();
fillBuffer(root, model, buffer, "");
return buffer.toString();
}
private void compareContents(String expected, String actual) {
if (expected.equals(actual)) {
return;
}
if (!areLinesEqual(toLines(expected), toLines(actual))) {
Assert.assertEquals(expected, actual);
}
}
private boolean areLinesEqual(List expected, List actual) {
if (expected.size() != actual.size()) {
return false;
}
for (Iterator expectedIter = expected.iterator(), actualIter = actual.iterator();
expectedIter.hasNext() && actualIter.hasNext();) {
if (!areLinesEqual((String)expectedIter.next(),
(String)actualIter.next())) {
return false;
}
}
return true;
}
private boolean areLinesEqual(String expected, String actual) {
if (expected.equals(actual)) {
return true;
}
Matcher expectedMatcher = COLOR_PROPERTY_PATTERN.matcher(expected);
Matcher actualMatcher = COLOR_PROPERTY_PATTERN.matcher(actual);
String expectedWithoutColor = expectedMatcher.replaceFirst("C");
String actualWithoutColor = actualMatcher.replaceFirst("C");
if (!expectedWithoutColor.equals(actualWithoutColor)) {
return false;
}
String expectedColor = expectedMatcher.group(1);
String actualColor = actualMatcher.group(1);
return ColorUtils.equals(expectedColor, ColorUtils.getColor(actualColor));
}
private List toLines(String text) {
StringTokenizer tokenizer = new StringTokenizer(text, "\n");
List result = new ArrayList();
while (tokenizer.hasMoreTokens()) {
result.add(tokenizer.nextToken());
}
return result;
}
private void fillBuffer(Object obj,
TreeModel model,
StringBuffer buffer,
String indent) {
String text = getShownText(obj);
buffer.append(indent).append(text);
boolean bold = isBold(obj);
fillNodeProperties(bold, getShownColor(obj), buffer);
buffer.append('\n');
for (int i = 0, max = model.getChildCount(obj); i < max; i++) {
Object child = model.getChild(obj, i);
fillBuffer(child, model, buffer, indent + " ");
}
}
private void fillNodeProperties(boolean bold, Color shownColor, StringBuffer buffer) {
String shownColorDescription = getShownColorDescription(shownColor);
if (bold || (shownColorDescription != null)) {
buffer.append(" #(");
if (bold) {
buffer.append("bold");
if (shownColorDescription != null) {
buffer.append(',');
}
}
if (shownColorDescription != null) {
buffer.append("color=").append(shownColorDescription);
}
buffer.append(")");
}
}
private String getShownColorDescription(Color shownColor) {
return isDefaultColor(shownColor) ? null : ColorUtils.getColorDescription(shownColor);
}
private boolean isDefaultColor(Color color) {
if (Color.BLACK.equals(color)) {
return true;
}
if (Utils.equals(color, getDefaultForegroundColor())) {
return true;
}
return false;
}
private Color getDefaultForegroundColor() {
AccessibleContext context = jTree.getAccessibleContext();
if (context instanceof AccessibleComponent) {
return ((AccessibleComponent)context).getForeground();
}
return null;
}
static String badTreePath(String path) {
return "Could not find element '" + path + "'";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -