treeViewer节点上下移动时,焦点始终为之前选择的焦点,怎么做????
------解决方案--------------------
/**
*
* 上移结点的Action类
*
*/
public class UpAction extends Action {
public UpAction() {
ImageDescriptor imageDesc = SampleImages.getImageDescriptor(SampleImages.UP);
setHoverImageDescriptor(imageDesc);
setText("上移");
}
@SuppressWarnings("unchecked")
public void run() {
VisitTree visitTree = getSelTree();
if(visitTree == null) {
MessageDialog.openInformation(shell, null, "请选择节点!");
} else {
tree = treeViewer.getTree();
TreeItem treeItem = tree.getSelection()[0];
VisitTree selTree = (VisitTree) treeItem.getData();
VisitTree upTree = (VisitTree)getTreeItem(treeItem , UP).getData();
upAndDown(upTree , selTree);
treeViewer.setSelection(new StructuredSelection(new Object[] { upTree }));
treeViewer.refresh(selTree , true);
treeViewer.refresh(upTree , true);
VisitFactory.getVisitTreeSave(selTree);
VisitFactory.getVisitTreeSave(upTree);
}
}
}
/**
* 根据传过来的str值判断上移还是下移
* @param item
* @param str
* @return
*/
public TreeItem getTreeItem(TreeItem item , String str) {
TreeItem parentItem = item.getParentItem();
int parentCount = parentItem.getItemCount();
int current = 0;
for(int i = 0 ; i < parentCount ; i++) {
TreeItem childItem = parentItem.getItem(i);
if(childItem.equals(item)) {
current = i;
}
}
if(current >= 1 && str.equals(UP)) {
return parentItem.getItem(current - 1);
}
if(current <= parentCount && str.equals(DOWN)) {
return parentItem.getItem(current + 1);
}
return null;
}
/**
* 除Rank外,upTree和downTree其他值全部调换
* @param upTree
* @param downTree
*/
private void upAndDown(VisitTree upTree , VisitTree downTree) {
VisitTree middleTree = new VisitTree();
Long upRank = upTree.getRank();
Long downRank = downTree.getRank();
middleTree.setIdx(upTree.getIdx());
middleTree.setNodeName(upTree.getNodeName());
middleTree.setRank(upTree.getRank());
middleTree.setUplink(upTree.getUplink());
if(upTree.getBelongTo() != null) {
middleTree.setBelongTo(upTree.getBelongTo());
}
if(upTree.getUnderDept() != null) {
middleTree.setUnderDept(upTree.getUnderDept());
}
upTree.setRank(upRank);
upTree.setIdx(downTree.getIdx());
upTree.setNodeName(downTree.getNodeName());
upTree.setUplink(downTree.getUplink());
if(downTree.getBelongTo() != null) {
upTree.setBelongTo(downTree.getBelongTo());
} else {
upTree.setBelongTo(null);
}
if(downTree.getUnderDept() != null) {
upTree.setUnderDept(downTree.getUnderDept());
} else {
upTree.setUnderDept(null);
}
downTree.setRank(downRank);
downTree.setIdx(middleTree.getIdx());
downTree.setNodeName(middleTree.getNodeName());
downTree.setUplink(middleTree.getUplink());
if(middleTree.getBelongTo() != null) {
downTree.setBelongTo(middleTree.getBelongTo());
} else {
downTree.setBelongTo(null);
}
if(middleTree.getUnderDept() != null) {
downTree.setBelongTo(middleTree.getUnderDept());