下面列出了javax.swing.tree.DefaultMutableTreeNode#getChildAt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static DefaultMutableTreeNode wrapNodes(DefaultMutableTreeNode source, String separator) {
if (isEmpty(separator)) {
return source;
}
DefaultMutableTreeNode targetRootNode = (DefaultMutableTreeNode) source.clone();
for (int i = 0; i < source.getChildCount(); i++) {
DefaultMutableTreeNode originalChildNode = (DefaultMutableTreeNode) source.getChildAt(i);
NoSqlTreeNode clonedChildNode = (NoSqlTreeNode) originalChildNode.clone();
RedisKeyValueDescriptor descriptor = (RedisKeyValueDescriptor) clonedChildNode.getDescriptor();
String[] explodedKey = StringUtils.explode(descriptor.getKey(), separator);
if (explodedKey.length == 1) {
addChildren(clonedChildNode, originalChildNode);
targetRootNode.add(clonedChildNode);
} else {
updateTree(targetRootNode, originalChildNode, explodedKey, descriptor);
}
}
return targetRootNode;
}
public static void sortTree1(DefaultMutableTreeNode root) {
int n = root.getChildCount();
for (int i = 0; i < n - 1; i++) {
for (int j = n - 1; j > i; j--) {
DefaultMutableTreeNode curNode = (DefaultMutableTreeNode) root.getChildAt(j);
DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root.getChildAt(j - 1);
if (!prevNode.isLeaf()) {
sortTree1(prevNode);
}
if (tnc.compare(prevNode, curNode) > 0) {
SWAP_COUNTER.getAndIncrement();
root.insert(curNode, j - 1);
root.insert(prevNode, j);
}
}
}
}
private static void movePathInActionsTree(JTree tree, ActionUrl url){
final TreePath treePath = CustomizationUtil.getTreePath(tree, url);
if (treePath != null){
if (treePath.getLastPathComponent() != null){
final DefaultMutableTreeNode parent = ((DefaultMutableTreeNode)treePath.getLastPathComponent());
final int absolutePosition = url.getAbsolutePosition();
final int initialPosition = url.getInitialPosition();
if (parent.getChildCount() > absolutePosition && absolutePosition >= 0) {
if (parent.getChildCount() > initialPosition && initialPosition >= 0) {
final DefaultMutableTreeNode child = (DefaultMutableTreeNode)parent.getChildAt(initialPosition);
if (child.getUserObject().equals(url.getComponent())){
parent.remove(child);
parent.insert(child, absolutePosition);
}
}
}
}
}
}
private void renormalizeMonitorDepth(DefaultMutableTreeNode monitorNode, int index) {
// First, remove all duplicates of the item at index "index"
DefaultMutableTreeNode threadNode1 = (DefaultMutableTreeNode) monitorNode.getChildAt(index);
ThreadInfo mi1 = (ThreadInfo) threadNode1.getUserObject();
int i = index + 1;
while (i < monitorNode.getChildCount()) {
DefaultMutableTreeNode threadNode2 = (DefaultMutableTreeNode) monitorNode.getChildAt(i);
ThreadInfo mi2 = (ThreadInfo) threadNode2.getUserObject();
if (mi1.getName().equals(mi2.getName())) {
if (threadNode2.getChildCount() > 0) {
threadNode1.add((DefaultMutableTreeNode) threadNode2.getFirstChild());
monitorNode.remove(i);
continue;
}
}
i++;
}
// Second, recurse into item "index"
renormalizeThreadDepth(threadNode1);
}
private static boolean hasBaseTypeChildNode(final DefaultMutableTreeNode node,
final BaseType baseType) {
int count = 0;
for (int i = 0; i < node.getChildCount(); ++i) {
final TreeNode child = node.getChildAt(i);
if (child instanceof BaseTypeTreeNode) {
if (((BaseTypeTreeNode) child).getBaseType() == baseType) {
++count;
}
}
}
return count == 1;
}
private static void removeChildSettings(DefaultMutableTreeNode node) {
int count = node.getChildCount();
for (int i = 0; i < count; i++) {
DefaultMutableTreeNode child = (DefaultMutableTreeNode)node.getChildAt(i);
Object object = child.getUserObject();
if (object instanceof SettingsWrapper) {
((SettingsWrapper)object).remove();
}
else {
removeChildSettings(child);
}
}
}
private void removeDisplayedItems(DefaultMutableTreeNode treeNode, ArrayStr names) {
int count = treeNode.getChildCount();
for( int i=0; i < count; i++ ) {
DefaultMutableTreeNode child = (DefaultMutableTreeNode) treeNode.getChildAt(i);
if ( child.isLeaf())
names.remove(names.findIndex(child.toString()));
else
removeDisplayedItems( child, names );
}
}
public static void position(JTree tree, JBScrollPane scrollPane, Question question) {
DefaultTreeModel treeMode = (DefaultTreeModel) tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeMode.getRoot();
if (root.isLeaf()) {
return;
}
DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(0);
if (node.isLeaf()) {
return;
}
for (int i = 0, j = node.getChildCount(); i < j; i++) {
DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) node.getChildAt(i);
Question nodeData = (Question) childNode.getUserObject();
if (nodeData.getQuestionId().equals(question.getQuestionId())) {
TreePath toShowPath = new TreePath(childNode.getPath());
tree.setSelectionPath(toShowPath);
Rectangle bounds = tree.getPathBounds(toShowPath);
Point point = new Point(0, (int) bounds.getY());
JViewport viewport = scrollPane.getViewport();
viewport.setViewPosition(point);
return;
}
}
}
@Nullable
private static TreePath getTreePath(final int positionInPath, final List<String> path, final Object root, JTree tree) {
if (!(root instanceof DefaultMutableTreeNode)) return null;
final DefaultMutableTreeNode treeNode = ((DefaultMutableTreeNode)root);
final Object userObject = treeNode.getUserObject();
final String pathElement;
if (path.size() > positionInPath) {
pathElement = path.get(positionInPath);
}
else {
return null;
}
if (pathElement == null) return null;
if (!(userObject instanceof Group)) return null;
if (!pathElement.equals(((Group)userObject).getName())) return null;
TreePath currentPath = new TreePath(treeNode.getPath());
if (positionInPath == path.size() - 1) {
return currentPath;
}
for (int j = 0; j < treeNode.getChildCount(); j++) {
final TreeNode child = treeNode.getChildAt(j);
currentPath = getTreePath(positionInPath + 1, path, child, tree);
if (currentPath != null) {
break;
}
}
return currentPath;
}
/** Test method for {@link com.sldeditor.ui.tree.SLDTreeTools#addNewMarker()}. */
@Test
public void testAddNewMarker() {
SLDTreeTools treeTools = new SLDTreeTools();
TestSLDTree sldTree = new TestSLDTree(null, treeTools);
StyleFactoryImpl styleFactory = (StyleFactoryImpl) CommonFactoryFinder.getStyleFactory();
// Start off with just a top level SLD and no structure below it
StyledLayerDescriptor sld = styleFactory.createStyledLayerDescriptor();
SelectedSymbol.getInstance().setSld(sld);
sldTree.populateSLD();
sldTree.selectFirstSymbol();
treeTools.addNewThing(NamedLayer.class);
treeTools.addNewThing(null);
treeTools.addNewThing(null);
treeTools.addNewThing(null);
DefaultMutableTreeNode rootNode = sldTree.getRootNode();
// Make sure we have a rule selected
DefaultMutableTreeNode rule =
(DefaultMutableTreeNode)
rootNode.getChildAt(0).getChildAt(0).getChildAt(0).getChildAt(0);
assertEquals(RuleImpl.class, rule.getUserObject().getClass());
assertEquals(0, rule.getChildCount());
treeTools.addNewMarker();
DefaultMutableTreeNode marker = (DefaultMutableTreeNode) rule.getChildAt(0);
assertEquals(PointSymbolizerImpl.class, marker.getUserObject().getClass());
}
@Override
protected void onClick(final AjaxRequestTarget target, final TableTreeData data) {
final DefaultMutableTreeNode root = (DefaultMutableTreeNode) data.getModel().getRoot();
final SelectionHandler handler = data.getSelectionHandler();
handler.clearSelection();
for (int i=0; i<root.getChildCount(); i++) {
final DefaultMutableTreeNode dmtnDriver = (DefaultMutableTreeNode) root.getChildAt(i);
for (int j=0; j<dmtnDriver.getChildCount(); j++) {
final DefaultMutableTreeNode dmtnNode = (DefaultMutableTreeNode) dmtnDriver.getChildAt(j);
final AbstractTopologyComponent node = (AbstractTopologyComponent) dmtnNode.getUserObject();
if (node.isNode()) handler.select(node.getUuid());
}
}
}
@Override
protected void onClick(final AjaxRequestTarget target, final TableTreeData data) {
final DefaultMutableTreeNode root = (DefaultMutableTreeNode) data.getModel().getRoot();
final SelectionHandler handler = data.getSelectionHandler();
handler.clearSelection();
for (int i=0; i<root.getChildCount(); i++) {
final DefaultMutableTreeNode dmtnDriver = (DefaultMutableTreeNode) root.getChildAt(i);
handler.select(((AbstractComponent<?>) dmtnDriver.getUserObject()).getUuid());
for (int j=0; j<dmtnDriver.getChildCount(); j++) {
final DefaultMutableTreeNode dmtnNode = (DefaultMutableTreeNode) dmtnDriver.getChildAt(j);
final AbstractTopologyComponent node = (AbstractTopologyComponent) dmtnNode.getUserObject();
if (node.isNode()) handler.select(node.getUuid());
}
}
}
@Override
protected void onClick(final AjaxRequestTarget target, final TableTreeData data) {
final DefaultMutableTreeNode root = (DefaultMutableTreeNode) data.getModel().getRoot();
final SelectionHandler handler = data.getSelectionHandler();
handler.clearSelection();
for (int i=0; i<root.getChildCount(); i++) {
final DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) root.getChildAt(i);
handler.select(((AbstractComponent<?>) dmtn.getUserObject()).getUuid());
}
}
/** Test method for {@link com.sldeditor.ui.tree.SLDTreeTools#addNewLine()}. */
@Test
public void testAddNewLine() {
SLDTreeTools treeTools = new SLDTreeTools();
TestSLDTree sldTree = new TestSLDTree(null, treeTools);
StyleFactoryImpl styleFactory = (StyleFactoryImpl) CommonFactoryFinder.getStyleFactory();
// Start off with just a top level SLD and no structure below it
StyledLayerDescriptor sld = styleFactory.createStyledLayerDescriptor();
SelectedSymbol.getInstance().setSld(sld);
sldTree.populateSLD();
sldTree.selectFirstSymbol();
treeTools.addNewThing(NamedLayer.class);
treeTools.addNewThing(null);
treeTools.addNewThing(null);
treeTools.addNewThing(null);
DefaultMutableTreeNode rootNode = sldTree.getRootNode();
// Make sure we have a rule selected
DefaultMutableTreeNode rule =
(DefaultMutableTreeNode)
rootNode.getChildAt(0).getChildAt(0).getChildAt(0).getChildAt(0);
assertEquals(RuleImpl.class, rule.getUserObject().getClass());
assertEquals(0, rule.getChildCount());
treeTools.addNewLine();
DefaultMutableTreeNode lineNode = (DefaultMutableTreeNode) rule.getChildAt(0);
assertEquals(LineSymbolizerImpl.class, lineNode.getUserObject().getClass());
}
/**
* Determines if the child exists in the tree structure. This is a helper function for addChildNodeToTree to ensure
* the duplicate child nodes aren't added
*
* @param childNodeId
* @param parentNode
* @return
*/
private boolean doesChildExist(String childNodeId, DefaultMutableTreeNode parentNode){
boolean exists = false;
for(int i = 0; i < parentNode.getChildCount(); i++){
DefaultMutableTreeNode child = (DefaultMutableTreeNode) parentNode.getChildAt(i);
if(childNodeId.equals(((NodeModel) child.getUserObject()).getNodeId())){
exists = true;
break;
}
}
return exists;
}
void clear() {
DefaultMutableTreeNode root = ((DefaultMutableTreeNode)treeModel.getRoot());
if (root==null) return;
int numColumns= root.getChildCount();
for (int i=numColumns-1; i>= 0; i--){
DefaultMutableTreeNode columnNode = (DefaultMutableTreeNode)root.getChildAt(i);
// Remove node
treeModel.removeNodeFromParent(columnNode);
// Remove Panel
getExcitationGridPanel().removeColumn((ExcitationColumnJPanel)columnNode.getUserObject());
}
}
/**
* Select style in tree.
*
* @param styleWrapper the style wrapper
*/
protected void selectStyleInTree(StyleWrapper styleWrapper) {
DefaultMutableTreeNode node = null;
if (styleWrapper != null) {
for (int index = 0; (index < rootNode.getChildCount()) && (node == null); index++) {
DefaultMutableTreeNode workspaceNode =
(DefaultMutableTreeNode) rootNode.getChildAt(index);
if (workspaceNode.toString().compareTo(styleWrapper.getWorkspace()) == 0) {
// Found workspace node, now find style node
for (int styleIndex = 0;
styleIndex < workspaceNode.getChildCount() && (node == null);
styleIndex++) {
LayerStyleNode styleNode =
(LayerStyleNode) workspaceNode.getChildAt(styleIndex);
if (styleNode.getStyleWrapper().compareTo(styleWrapper) == 0) {
node = styleNode;
}
}
}
}
}
if (node != null) {
TreeNode[] nodes = treeModel.getPathToRoot(node);
TreePath path = new TreePath(nodes);
tree.setSelectionPath(path);
tree.scrollPathToVisible(path);
}
}
public void testAddAnnotationToTree() throws Exception {
try {
// create an annotation
createExampleFS(this.cas);
FSIterator iter = this.cas.getAnnotationIndex(exampleType).iterator();
AnnotationFS annot = (AnnotationFS) iter.get();
// init viewer
viewer.setCAS(this.cas);
// add to tree
viewer.addAnnotationToTree(annot);
// inspect results
TreeModel model = viewer.getSelectedAnnotationTree().getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
assertEquals("Annotations", root.getUserObject().toString());
DefaultMutableTreeNode typeNode = (DefaultMutableTreeNode) root.getChildAt(0);
assertEquals("Example", typeNode.getUserObject().toString());
DefaultMutableTreeNode fsNode = (DefaultMutableTreeNode) typeNode.getChildAt(0);
Enumeration children = fsNode.children();
assertEquals("begin = 1", ((DefaultMutableTreeNode) children.nextElement()).getUserObject()
.toString());
assertEquals("end = 5", ((DefaultMutableTreeNode) children.nextElement()).getUserObject()
.toString());
assertEquals("floatFeature = " + (float) 99.99, ((DefaultMutableTreeNode) children
.nextElement()).getUserObject().toString());
assertEquals("stringFeature = aaaaaaa", ((DefaultMutableTreeNode) children.nextElement())
.getUserObject().toString());
assertEquals("boolFeature = true", ((DefaultMutableTreeNode) children.nextElement())
.getUserObject().toString());
assertEquals("byteFeature = 122", ((DefaultMutableTreeNode) children.nextElement())
.getUserObject().toString());
assertEquals("shortFeature = " + Short.MIN_VALUE, ((DefaultMutableTreeNode) children
.nextElement()).getUserObject().toString());
assertEquals("longFeature = " + Long.MIN_VALUE, ((DefaultMutableTreeNode) children
.nextElement()).getUserObject().toString());
assertEquals("doubleFeature = " + Double.MAX_VALUE, ((DefaultMutableTreeNode) children
.nextElement()).getUserObject().toString());
assertEquals("intArrayFeature = [" + Integer.MAX_VALUE + "," + (Integer.MAX_VALUE - 1)
+ ",42," + (Integer.MIN_VALUE + 1) + "," + Integer.MIN_VALUE + "]",
((DefaultMutableTreeNode) children.nextElement()).getUserObject().toString());
assertEquals("floatArrayFeature = [" + Float.MAX_VALUE + ","
+ (float) (Float.MAX_VALUE / 1000.0) + "," + 42.0 + ","
+ (float) (Float.MIN_VALUE * 1000) + "," + Float.MIN_VALUE + "]",
((DefaultMutableTreeNode) children.nextElement()).getUserObject().toString());
assertEquals("stringArrayFeature = [zzzzzz,yyyyyy,xxxxxx,wwwwww,vvvvvv]",
((DefaultMutableTreeNode) children.nextElement()).getUserObject().toString());
assertEquals("boolArrayFeature = [true,false,true,false,true,false,true,false]",
((DefaultMutableTreeNode) children.nextElement()).getUserObject().toString());
assertEquals("byteArrayFeature = [8,16,64,-128,-1]", ((DefaultMutableTreeNode) children
.nextElement()).getUserObject().toString());
assertEquals("shortArrayFeature = [" + Short.MAX_VALUE + "," + (Short.MAX_VALUE - 1) + ","
+ (Short.MAX_VALUE - 2) + "," + (Short.MAX_VALUE - 3) + "," + (Short.MAX_VALUE - 4)
+ "]", ((DefaultMutableTreeNode) children.nextElement()).getUserObject().toString());
assertEquals("longArrayFeature = [" + Long.MAX_VALUE + "," + (Long.MAX_VALUE - 1) + ","
+ (Long.MAX_VALUE - 2) + "," + (Long.MAX_VALUE - 3) + "," + (Long.MAX_VALUE - 4)
+ "]", ((DefaultMutableTreeNode) children.nextElement()).getUserObject().toString());
assertEquals("doubleArrayFeature = [" + Double.MAX_VALUE + "," + Double.MIN_VALUE + ","
+ Double.parseDouble("1.5555") + "," + Double.parseDouble("99.000000005") + ","
+ Double.parseDouble("4.44444444444444444") + "]", ((DefaultMutableTreeNode) children
.nextElement()).getUserObject().toString());
} catch (Exception e) {
JUnitExtension.handleException(e);
}
}
/** Used by the Loader.importStack and the "many new layers" command. */
public void addLayer(LayerSet layer_set, Layer layer) {
if (null == layer_set || null == layer) return;
try {
// find the node that contains the LayerSet
DefaultMutableTreeNode root_node = (DefaultMutableTreeNode)this.getModel().getRoot();
LayerThing root_lt = (LayerThing)root_node.getUserObject();
Thing thing = null;
if (root_lt.getObject().equals(layer_set)) thing = root_lt;
else thing = root_lt.findChild(layer_set);
DefaultMutableTreeNode parent_node = DNDTree.findNode(thing, this);
if (null == parent_node) { Utils.log("LayerTree: LayerSet not found."); return; }
LayerThing parent_thing = (LayerThing)parent_node.getUserObject();
double z = layer.getZ();
// find the node whose 'z' is larger than z, and add the Layer before that.
int n = parent_node.getChildCount();
int i = 0;
for (; i < n; i++) {
DefaultMutableTreeNode child_node = (DefaultMutableTreeNode)parent_node.getChildAt(i);
LayerThing child_thing = (LayerThing)child_node.getUserObject();
if (!child_thing.getType().equals("layer")) {
continue;
}
double iz = ((Layer)child_thing.getObject()).getZ();
if (iz < z) {
continue;
}
// else, add the layer here, after the 'i' layer which has a larger z
break;
}
TemplateThing tt = parent_thing.getChildTemplate("layer");
if (null == tt) {
Utils.log("LayerTree: Null template Thing!");
return;
}
LayerThing new_thing = new LayerThing(tt, layer.getProject(), layer);
// Add the new_thing to the tree
if (null != new_thing) {
parent_thing.addChild(new_thing);
DefaultMutableTreeNode new_node = new DefaultMutableTreeNode(new_thing);
//TODO when changing the Z of a layer, the insertion is proper but an empty space is left //Utils.log("LayerTree: inserting at: " + i);
((DefaultTreeModel)this.getModel()).insertNodeInto(new_node, parent_node, i);
TreePath treePath = new TreePath(new_node.getPath());
this.scrollPathToVisible(treePath);
this.setSelectionPath(treePath);
}
} catch (Exception e) { IJError.print(e); }
}
@Override
protected void paintVerticalPartOfLeg(final Graphics g, final Rectangle clipBounds, final Insets insets, final TreePath path) {
final DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
if (node.getChildCount() < 2) {
// We could draw lines for nodes with a single child but we omit them
// to more of an emphasis of lines for nodes with multiple children.
return;
}
final DiagnosticsNode diagnostic = maybeGetDiagnostic(node);
if (diagnostic != null && !diagnostic.hasChildren()) {
// This avoids drawing lines for nodes with only property children.
return;
}
final int depth = path.getPathCount() - 1;
if (depth == 0 && !getShowsRootHandles() && !isRootVisible()) {
return;
}
int lineX = getRowX(-1, depth);
if (leftToRight) {
lineX = lineX - getRightChildIndent() + insets.left;
}
else {
lineX = tree.getWidth() - lineX - insets.right +
getRightChildIndent() - 1;
}
final int clipLeft = clipBounds.x;
final int clipRight = clipBounds.x + (clipBounds.width - 1);
if (lineX >= clipLeft && lineX <= clipRight) {
final int clipTop = clipBounds.y;
final int clipBottom = clipBounds.y + clipBounds.height;
Rectangle parentBounds = getPathBounds(tree, path);
boolean previousDashed = false;
int top;
if (parentBounds == null) {
top = Math.max(insets.top + getVerticalLegBuffer(),
clipTop);
}
else {
top = Math.max(parentBounds.y + parentBounds.height +
getVerticalLegBuffer(), clipTop);
}
if (depth == 0 && !isRootVisible()) {
final TreeModel model = getModel();
if (model != null) {
final Object root = model.getRoot();
if (model.getChildCount(root) > 0) {
parentBounds = getPathBounds(tree, path.
pathByAddingChild(model.getChild(root, 0)));
if (parentBounds != null) {
top = Math.max(insets.top + getVerticalLegBuffer(),
parentBounds.y +
parentBounds.height / 2);
}
}
}
}
for (int i = 0; i < node.getChildCount(); ++i) {
final DefaultMutableTreeNode child = (DefaultMutableTreeNode)node.getChildAt(i);
final DiagnosticsNode childDiagnostic = maybeGetDiagnostic(child);
boolean dashed = false;
if (childDiagnostic != null) {
dashed = childDiagnostic.getStyle() == DiagnosticsTreeStyle.offstage;
}
final Rectangle childBounds = getPathBounds(tree, path.pathByAddingChild(child));
if (childBounds == null)
// This shouldn't happen, but if the model is modified
// in another thread it is possible for this to happen.
// Swing isn't multithreaded, but I'll add this check in
// anyway.
{
continue;
}
final int bottom = Math.min(childBounds.y +
(childBounds.height / 2), clipBottom);
if (top <= bottom && bottom >= clipTop && top <= clipBottom) {
g.setColor(JBColor.GRAY);
paintVerticalLine(g, tree, lineX, top, bottom, dashed);
}
top = bottom;
previousDashed = dashed;
}
}
}