下面列出了javax.swing.tree.DefaultMutableTreeNode#insert ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void sort(DefaultMutableTreeNode parent) {
int n = parent.getChildCount();
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (tnc.compare((DefaultMutableTreeNode) parent.getChildAt(min), (DefaultMutableTreeNode) parent.getChildAt(j)) > 0) {
min = j;
}
}
if (i != min) {
MutableTreeNode a = (MutableTreeNode) parent.getChildAt(i);
MutableTreeNode b = (MutableTreeNode) parent.getChildAt(min);
parent.insert(b, i);
parent.insert(a, min);
updateTree = true;
}
}
}
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 sort2(DefaultMutableTreeNode parent) {
int n = parent.getChildCount();
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (tnc.compare((DefaultMutableTreeNode) parent.getChildAt(min),
(DefaultMutableTreeNode) parent.getChildAt(j)) > 0) {
min = j;
}
}
if (i != min) {
SWAP_COUNTER.getAndIncrement();
MutableTreeNode a = (MutableTreeNode) parent.getChildAt(i);
MutableTreeNode b = (MutableTreeNode) parent.getChildAt(min);
parent.insert(b, i);
parent.insert(a, min);
// MutableTreeNode node = (MutableTreeNode) parent.getChildAt(min);
// parent.insert(node, i);
// COMPARE_COUNTER++;
}
}
}
@SuppressWarnings("unchecked")
private DefaultMutableTreeNode getBandRowTreeNode(String bandName, int row) {
DefaultMutableTreeNode bandNode = getBandTreeNode(bandName);
Enumeration rows = bandNode.children();
while (rows.hasMoreElements()) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) rows.nextElement();
Object userObject = node.getUserObject();
if ((Integer) userObject == row) {
return node;
}
}
StructureTreeNode rowNode = new StructureTreeNode(row);
bandNode.insert(rowNode, row);
return rowNode;
}
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);
}
}
}
}
}
}
public void addNode(TaskPane taskPane, String parentTaskPaneToString,
int index) {
if (nodeMap.containsKey(parentTaskPaneToString)) {
DefaultMutableTreeNode parentNode = nodeMap
.get(parentTaskPaneToString);
DefaultMutableTreeNode node = new DefaultMutableTreeNode(taskPane);
if (index == -1)
parentNode.add(node);
else
parentNode.insert(node, index);
nodeMap.put(taskPane.toString(), node);
} else
throw new IllegalArgumentException("Illegal arguements in addNode");
}
public DefaultMutableTreeNode getRemainingExpression(PsiExpression expressionToBeRemoved) {
DefaultMutableTreeNode newRoot = new DefaultMutableTreeNode();
processExpression(newRoot, completeExpression);
DefaultMutableTreeNode leaf = newRoot.getFirstLeaf();
while (leaf != null) {
PsiExpression expression = (PsiExpression) leaf.getUserObject();
if (expression.equals(expressionToBeRemoved)) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) leaf.getParent();
if (parent != null) {
DefaultMutableTreeNode grandParent = (DefaultMutableTreeNode) parent.getParent();
DefaultMutableTreeNode sibling = null;
if (leaf.getNextSibling() != null) {
sibling = leaf.getNextSibling();
} else if (leaf.getPreviousSibling() != null) {
sibling = leaf.getPreviousSibling();
}
if (grandParent != null) {
int parentIndex = grandParent.getIndex(parent);
grandParent.remove(parent);
grandParent.insert(sibling, parentIndex);
} else {
newRoot = sibling;
}
} else {
newRoot = null;
}
break;
}
leaf = leaf.getNextLeaf();
}
return newRoot;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
selectedNode = (DefaultMutableTreeNode)treeMem.getLastSelectedPathComponent();
if(selectedNode != null && !txtTree.getText().isEmpty()){
selectedNode.insert(new DefaultMutableTreeNode(txtTree.getText()), selectedNode.getIndex(selectedNode.getLastChild()));
tree.reload(selectedNode);
}
txtTree.setText("");
}
/**
* Creates a new base type tree node below the given parent node and recursively creates all
* member nodes.
*/
private ChildNodeDescriptor insertType(final DefaultMutableTreeNode parentNode,
final BaseType baseType, final int index) {
final BaseTypeTreeNode newNode = new BaseTypeTreeNode(baseType);
parentNode.insert(newNode, index);
createTypeNodes(newNode, baseType);
return new ChildNodeDescriptor(newNode, index);
}
/**
* Inserts member as a child of parentNode at the given index without raising any events
* regarding the updates model!
*/
private void insertMemberAt(final TypeMember member, final DefaultMutableTreeNode parentNode,
final int index) {
final TypeMemberTreeNode memberNode = new TypeMemberTreeNode(member);
parentNode.insert(memberNode, index);
createTypeNodes(memberNode, member.getBaseType());
nestedStructNodes.put(member.getBaseType(), memberNode);
memberNodes.put(member, memberNode);
}
private void processExpression(DefaultMutableTreeNode parent, Expression expression) {
if(expression instanceof InfixExpression) {
InfixExpression infixExpression = (InfixExpression)expression;
InfixExpression.Operator operator = infixExpression.getOperator();
if(operator.equals(InfixExpression.Operator.CONDITIONAL_AND) || operator.equals(InfixExpression.Operator.CONDITIONAL_OR)) {
parent.setUserObject(operator);
DefaultMutableTreeNode leftOperandNode = new DefaultMutableTreeNode();
DefaultMutableTreeNode rightOperandNode = new DefaultMutableTreeNode();
parent.add(leftOperandNode);
parent.add(rightOperandNode);
processExpression(leftOperandNode, infixExpression.getLeftOperand());
processExpression(rightOperandNode, infixExpression.getRightOperand());
if(infixExpression.hasExtendedOperands()) {
DefaultMutableTreeNode grandParent = (DefaultMutableTreeNode)parent.getParent();
int parentIndex = -1;
if(grandParent != null)
parentIndex = grandParent.getIndex(parent);
DefaultMutableTreeNode newParent = processExtendedOperands(parent, infixExpression.extendedOperands());
if(grandParent != null)
grandParent.insert(newParent, parentIndex);
else
root = newParent;
}
}
else {
parent.setUserObject(infixExpression);
}
}
else {
parent.setUserObject(expression);
}
}
public DefaultMutableTreeNode getRemainingExpression(Expression expressionToBeRemoved) {
DefaultMutableTreeNode newRoot = new DefaultMutableTreeNode();
processExpression(newRoot, completeExpression);
DefaultMutableTreeNode leaf = newRoot.getFirstLeaf();
while(leaf != null) {
Expression expression = (Expression)leaf.getUserObject();
if(expression.equals(expressionToBeRemoved)) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode)leaf.getParent();
if(parent != null) {
DefaultMutableTreeNode grandParent = (DefaultMutableTreeNode)parent.getParent();
DefaultMutableTreeNode sibling = null;
if(leaf.getNextSibling() != null) {
sibling = leaf.getNextSibling();
}
else if(leaf.getPreviousSibling() != null) {
sibling = leaf.getPreviousSibling();
}
if(grandParent != null) {
int parentIndex = grandParent.getIndex(parent);
grandParent.remove(parent);
grandParent.insert(sibling, parentIndex);
}
else {
newRoot = sibling;
}
break;
}
else {
newRoot = null;
break;
}
}
leaf = leaf.getNextLeaf();
}
return newRoot;
}
private void updateNode(Object arg) {
UpdateElementEvent event = (UpdateElementEvent) arg;
if(event.getElement() instanceof GScreen){
//This function update the position in the tree
//but Screens never needs such of feature.
return;
}
int i = this.getIndex(event.getElement());
DefaultMutableTreeNode node = searchNode(event.getElement());
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
parent.insert(node, i);
jTree2.updateUI();
}
private StructureTreeNode getBandElementTreeNode(String bandName, int row, int column) {
DefaultMutableTreeNode bandNode = getBandRowTreeNode(bandName, row);
StructureTreeNode child;
if (bandNode.getChildCount() <= column) {
child = new StructureTreeNode(new ReportGridCell(null, row, column));
child.setVisible(false);
bandNode.insert(child, column);
} else {
child = (StructureTreeNode) bandNode.getChildAt(column);
}
return child;
}
@SuppressWarnings("unchecked")
private DefaultMutableTreeNode getBandRowInsertedTreeNode(String bandName, int bandRow, int row) {
DefaultMutableTreeNode bandNode = getBandTreeNode(bandName);
// tree must be traversed in preorder (band by band)
Enumeration en = rootNode.preorderEnumeration();
String currentBandName = ReportLayout.HEADER_BAND_NAME;
while (en.hasMoreElements()) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) en.nextElement();
Object userObject = node.getUserObject();
if (userObject instanceof Band) {
currentBandName = ((Band) userObject).getName();
} else if (userObject instanceof ReportGridCell) {
// increment rows for all report grid cells which are in the rows
// following the inserted row
ReportGridCell reportGridCell = (ReportGridCell) userObject;
if (row <= reportGridCell.getRow()) {
reportGridCell.setRow(reportGridCell.getRow() + 1);
}
} else if (userObject instanceof Integer) {
// modify 'row' nodes in the tree (for the current band)
if (bandName.equals(currentBandName)) {
if ((Integer) userObject == bandRow) {
DefaultMutableTreeNode sibling = node.getNextSibling();
node.setUserObject(((Integer) node.getUserObject()).intValue() + 1);
while (sibling != null) {
sibling.setUserObject(((Integer) sibling.getUserObject()).intValue() + 1);
sibling = sibling.getNextSibling();
}
}
}
}
}
DefaultMutableTreeNode rowNode = new StructureTreeNode(bandRow);
bandNode.insert(rowNode, bandRow);
return rowNode;
}
private static void addPathToActionsTree(JTree tree, ActionUrl url) {
final TreePath treePath = CustomizationUtil.getTreePath(tree, url);
if (treePath == null) return;
DefaultMutableTreeNode node = (DefaultMutableTreeNode)treePath.getLastPathComponent();
final int absolutePosition = url.getAbsolutePosition();
if (node.getChildCount() >= absolutePosition && absolutePosition >= 0) {
if (url.getComponent() instanceof Group){
node.insert(ActionsTreeUtil.createNode((Group)url.getComponent()), absolutePosition);
} else {
node.insert(new DefaultMutableTreeNode(url.getComponent()), absolutePosition);
}
}
}
private void renormalizeBlockingThreadTree(MonitorMap mmap, Map directChildMap) {
Map allBlockingThreadsMap = new HashMap(directChildMap); // All threads that are blocking at least one other thread
// First, renormalize based on monitors to get our unique tree
// Tree will be unique as long as there are no deadlocks aka monitor loops
for (Iterator iter = mmap.iterOfKeys(); iter.hasNext();) {
String monitor1 = (String) iter.next();
Map[] threads1 = mmap.getFromMonitorMap(monitor1);
DefaultMutableTreeNode thread1Node = (DefaultMutableTreeNode) allBlockingThreadsMap.get(monitor1);
if (thread1Node == null) {
continue;
}
// Get information on the one thread holding this lock
Iterator it = threads1[MonitorMap.LOCK_THREAD_POS].keySet().iterator();
if (!it.hasNext()) {
continue;
}
String threadLine1 = (String) it.next();
for (Iterator iter2 = mmap.iterOfKeys(); iter2.hasNext();) {
String monitor2 = (String) iter2.next();
if (monitor1 == monitor2) {
continue;
}
Map[] threads2 = mmap.getFromMonitorMap(monitor2);
if (threads2[MonitorMap.WAIT_THREAD_POS].containsKey(threadLine1)) {
// Get the node of the thread that is holding this lock
DefaultMutableTreeNode thread2Node = (DefaultMutableTreeNode) allBlockingThreadsMap.get(monitor2);
// Get the node of the monitor itself
DefaultMutableTreeNode monitor2Node = (DefaultMutableTreeNode) thread2Node.getFirstChild();
// If a redundant node for thread2 exists with no children, remove it
// To compare, we have to remove "Thread - " from the front of display strings
for (int i = 0; i < monitor2Node.getChildCount(); i++) {
DefaultMutableTreeNode child2 = (DefaultMutableTreeNode) monitor2Node.getChildAt(i);
if (child2.toString().substring(9).equals(threadLine1) && child2.getChildCount() == 0) {
monitor2Node.remove(i);
break;
}
}
// Thread1 is blocked by monitor2 held by thread2, so move thread1 under thread2
monitor2Node.insert(thread1Node, 0);
directChildMap.remove(monitor1);
break;
}
}
}
allBlockingThreadsMap.clear();
// Second, renormalize top level based on threads for cases where one thread holds multiple monitors
boolean changed = false;
do {
changed = false;
for (Iterator iter = directChildMap.entrySet().iterator(); iter.hasNext();) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) ((Map.Entry) iter.next()).getValue();
if (checkForDuplicateThreadItem(directChildMap, node)) {
changed = true;
break;
}
}
} while (changed);
// Third, renormalize lower levels of the tree based on threads for cases where one thread holds multiple monitors
for (Iterator iter = directChildMap.entrySet().iterator(); iter.hasNext();) {
renormalizeThreadDepth((DefaultMutableTreeNode) ((Map.Entry) iter.next()).getValue());
}
}