📄 extdefaulttreemodel.java
字号:
/*
* ExtDefaultTreeModel.java ver 0.1.3
*
* Created on 2006年12月6日, 下午10:13
*
*This Class is extended from the defaultTreeModel,extends its function by add some useful method
*
*history : 2006年12月6日,ver 0.1 create the class
* 2006年12月7日,ver 0.1.1 create the getParent(String[] dentry) method,
* create the getParent(TreeNode node) method,
* 2006年12月8日,ver 0.1.2 create the getParent() method,
* create the getCurrentNode() method,
* create the setCurrentNode(TreeNode current) method
* create the setParentOfCurrent() method
* getParent(String[] dentry) method become trustable,still has a problem that the dentry can not be a "/"
* 2006年12月8日,ver0.1.3 create the getChild(String[] dentry) method
* modofied the removeFromParent() method
* modified the insertNodeInto() method
*
*/
package ossimulation;
import javax.swing.tree.*;
/**
*
* @author Decell.Zhou
*/
public class ExtDefaultTreeModel extends DefaultTreeModel {
protected TreeNode currentNode;//add the new field indicate the current dirctory
protected TreeNode parentOfCurrent;//add the new field indicate the parent node of the current
/** Creates a new instance of ExtDefaultTreeModel */
public ExtDefaultTreeModel(TreeNode root) {
super(root);
currentNode = root;//set the current Node as the root,this is the start point for the system
parentOfCurrent = this.getParent(currentNode);
}
/**add this method to change the current working dirctory*/
public void setCurrentNode(TreeNode current){
currentNode = current;//change the current working dirctory
parentOfCurrent = getParent(currentNode);
}
/**add this method to get the current working dirctory*/
public TreeNode getCurrentNode(){
return currentNode;
}
/**add this method to set the parent dirctory manally */
public void setParentOfCurrent(){
parentOfCurrent = getParent(currentNode);
}
/**add this method to get the parent dirctory of current working dirctory*/
public TreeNode getParent(){
return parentOfCurrent;
}
/**add this getPatent method to find the parent of the node*/
public TreeNode getParent(TreeNode node){
TreeNode parent;
if(node == root){
parent = root;
} else {
parent = node.getParent();
}
return parent;
}
/**add this method to resolve the path ,inoder to get the parent in the path*/
public TreeNode getParent(String[] dentry){
TreeNode startNode = null;//starting point for the path resolve
TreeNode resultNode = null;//the result of the search
//deal with the special path argument such as ".","",".."
if(dentry[0].equals("")){
startNode = root;
resultNode = startNode;//at first the startNode is equal to the resultNode;
//search each of the dentry
for(int i = 1;i < dentry.length - 1;i++){
//search each child
startNode = resultNode;//prepare for the next search ,reset the startNode to the new start
for(int j = 0;j < startNode.getChildCount();j++){
//if the dentry is equal to the name of the inode
if(dentry[i].equals(((DefaultMutableTreeNode)startNode.getChildAt(j)).getUserObject().toString())){
resultNode = startNode.getChildAt(j);//let the resultNode record the tree of the dentry
break;//jump to next dentry
}
}
//if the resultNode = startNode, it means that the search of the dentry at this time has been failed
if(startNode == resultNode){
break;//search failed,no longer need to search next dentry
}
}
//if search failed , set the resultNode null
if(resultNode == startNode){
if(dentry.length <=2 ){
resultNode = root;
} else {
resultNode = null;
}
}
} else if (dentry[0].equals(".")){
startNode = currentNode;//if the dentry[0] is ".",treat it as the current dirctory
resultNode = startNode;//at first the startNode is equal to the resultNode;
//search each of the dentry
for(int i = 1;i < dentry.length - 1;i++){
//search each child
startNode = resultNode;//prepare for the next search ,reset the startNode to the new start
for(int j = 0;j < startNode.getChildCount();j++){
//if the dentry is equal to the name of the inode
if(dentry[i].equals(((DefaultMutableTreeNode)startNode.getChildAt(j)).getUserObject().toString())){
resultNode = startNode.getChildAt(j);//let the resultNode record the tree of the dentry
break;//jump to next dentry
}
}
//if the resultNode = startNode, it means that the search of the dentry at this time has been failed
if(startNode == resultNode){
break;//search failed,no longer need to search next dentry
}
}
//if search failed , set the resultNode null
if(resultNode == startNode){
if(dentry.length == 1){
resultNode = this.getParent(currentNode);
} else if(dentry.length == 2){
resultNode = currentNode;
} else {
resultNode = null;
}
}
} else if(dentry[0].equals("..")){
startNode = parentOfCurrent;//if the dentry[0] is "..",treat it as the parent of the current dirctory
resultNode = startNode;//at first the startNode is equal to the resultNode;
//search each of the dentry
for(int i = 1;i < dentry.length - 1;i++){
//search each child
startNode = resultNode;//prepare for the next search ,reset the startNode to the new start
for(int j = 0;j < startNode.getChildCount();j++){
//if the dentry is equal to the name of the inode
if(dentry[i].equals(((DefaultMutableTreeNode)startNode.getChildAt(j)).getUserObject().toString())){
resultNode = startNode.getChildAt(j);//let the resultNode record the tree of the dentry
break;//jump to next dentry
}
}
//if the resultNode = startNode, it means that the search of the dentry at this time has been failed
if(startNode == resultNode){
break;//search failed,no longer need to search next dentry
}
}
//if search failed , set the resultNode null
if(resultNode == startNode){
if(dentry.length == 1){
resultNode = this.getParent(parentOfCurrent);
} else if(dentry.length == 2){
resultNode = parentOfCurrent;
} else {
resultNode = null;
}
}
} else {
startNode = currentNode;//the default sitiation
resultNode = startNode;//at first the startNode is equal to the resultNode;
//search each of the dentry
for(int i = 0;i < dentry.length - 1;i++){
//search each child
startNode = resultNode;//prepare for the next search ,reset the startNode to the new start
for(int j = 0;j < startNode.getChildCount();j++){
//if the dentry is equal to the name of the inode
if(dentry[i].equals(((DefaultMutableTreeNode)startNode.getChildAt(j)).getUserObject().toString())){
resultNode = startNode.getChildAt(j);//let the resultNode record the tree of the dentry
break;//jump to next dentry
}
}
//if the resultNode = startNode, it means that the search of the dentry at this time has been failed
if(startNode == resultNode){
break;//search failed,no longer need to search next dentry
}
}
//if search failed , set the resultNode null
if((resultNode == startNode) && (dentry.length != 1)){
resultNode = null;
}
}
return resultNode;
}
/**add this method to resolve the path ,inoder to get the child in the path*/
public TreeNode getChild(String[] dentry){
TreeNode startNode = null;//starting point for the path resolve
TreeNode resultNode = null;//the result of the search
//deal with the special path argument such as ".","",".."
if(dentry[0].equals("")){
startNode = root;
resultNode = startNode;//at first the startNode is equal to the resultNode;
//search each of the dentry
for(int i = 1;i < dentry.length;i++){
//search each child
startNode = resultNode;//prepare for the next search ,reset the startNode to the new start
for(int j = 0;j < startNode.getChildCount();j++){
//if the dentry is equal to the name of the inode
if(dentry[i].equals(((DefaultMutableTreeNode)startNode.getChildAt(j)).getUserObject().toString())){
resultNode = startNode.getChildAt(j);//let the resultNode record the tree of the dentry
break;//jump to next dentry
}
}
//if the resultNode = startNode, it means that the search of the dentry at this time has been failed
if(startNode == resultNode){
break;//search failed,no longer need to search next dentry
}
}
//if search failed , set the resultNode null
if(resultNode == startNode){
if(dentry.length <= 1){
resultNode = root;
} else {
resultNode = null;
}
}
} else if (dentry[0].equals(".")){
startNode = currentNode;//if the dentry[0] is ".",treat it as the current dirctory
resultNode = startNode;//at first the startNode is equal to the resultNode;
//search each of the dentry
for(int i = 1;i < dentry.length;i++){
//search each child
startNode = resultNode;//prepare for the next search ,reset the startNode to the new start
for(int j = 0;j < startNode.getChildCount();j++){
//if the dentry is equal to the name of the inode
if(dentry[i].equals(((DefaultMutableTreeNode)startNode.getChildAt(j)).getUserObject().toString())){
resultNode = startNode.getChildAt(j);//let the resultNode record the tree of the dentry
break;//jump to next dentry
}
}
//if the resultNode = startNode, it means that the search of the dentry at this time has been failed
if(startNode == resultNode){
break;//search failed,no longer need to search next dentry
}
}
//if search failed , set the resultNode null
if(resultNode == startNode){
if(dentry.length == 1){
resultNode = currentNode;
} else {
resultNode = null;
}
}
} else if(dentry[0].equals("..")){
startNode = parentOfCurrent;//if the dentry[0] is "..",treat it as the parent of the current dirctory
resultNode = startNode;//at first the startNode is equal to the resultNode;
//search each of the dentry
for(int i = 1;i < dentry.length;i++){
//search each child
startNode = resultNode;//prepare for the next search ,reset the startNode to the new start
for(int j = 0;j < startNode.getChildCount();j++){
//if the dentry is equal to the name of the inode
if(dentry[i].equals(((DefaultMutableTreeNode)startNode.getChildAt(j)).getUserObject().toString())){
resultNode = startNode.getChildAt(j);//let the resultNode record the tree of the dentry
break;//jump to next dentry
}
}
//if the resultNode = startNode, it means that the search of the dentry at this time has been failed
if(startNode == resultNode){
break;//search failed,no longer need to search next dentry
}
}
//if search failed , set the resultNode null
if(resultNode == startNode){
if(dentry.length == 1){
resultNode = parentOfCurrent;
} else {
resultNode = null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -