package rcpdemo;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.part.ViewPart;
public class View extends ViewPart {
private static String fileSplit = System.getProperty("file.separator");
private static String projectRootPath = "f:";
private static String projectPath = projectRootPath + fileSplit + "workspace";
private static String procFlag = ".proc";
private static String xmlFlag = ".xml";
private static String rrFlag = ".rr";
private static Map<TreeItem,String> fileMap = new Hashtable<TreeItem, String>();
private static File file = new File(projectPath);
Tree upTree = null;
/**初始化工作区间的所有文件
* @param file
* @param projectSet
*/
private void initProjectFile(File file,Set<File> projectSet){
File allFile [] = file.listFiles();
for (File allfile : allFile) {
if(allfile.isDirectory()){
String list [] = allfile.list(/*new FilenameFilter() {
boolean flag = false;
public boolean accept(File dir, String name) {
if(name.endsWith(suepFlagString)){
flag = true;
}
return flag;
}
}*/);
if(list !=null/* && list.length>0*/){
projectSet.add(allfile);
}
}
}
}
/**显示所有的项目
* @param fileSet
*/
private void showAllProjectFile(Set<File> fileSet){
for (File allfile : fileSet) {
String fileName = allfile.getName();
TreeItem treeItem = new TreeItem(upTree, SWT.NONE);
treeItem.setText(fileName);
fileMap.put(treeItem, allfile.toString());
if(allfile.isDirectory()){
forShowProjectFile(treeItem,allfile);
}
}
}
/**显示一个根节点下面的所有子节点
* @param treeItem
* @param file
*/
int i = 0;
private void forShowProjectFile(TreeItem treeItem, File file) {
File[] listFiles = file.listFiles();
Map<TreeItem, File> childFileMap = new HashMap<TreeItem, File>();
for (File allfile : listFiles) {
i++;
String fileName = allfile.getName();
if (!fileName.endsWith(procFlag)) {
String treeName = fileName.indexOf(".") != -1 ? fileName.substring(0,fileName.indexOf(".")): fileName;
TreeItem flagTreeItem = new TreeItem(treeItem, SWT.NULL);
flagTreeItem.setText(treeName);
fileMap.put(flagTreeItem, allfile.toString());
if (fileName.endsWith(xmlFlag)) {
String allfilePath = allfile.toString();
String procPath = allfilePath.substring(0,(allfilePath.lastIndexOf(fileSplit)+1))+fileName.substring(0,fileName.lastIndexOf("."))+procFlag;
File procFile = new File(procPath);
if(procFile.exists() && procFile.isDirectory()){
File [] procChildFiles = procFile.listFiles();
for (File procChildFile : procChildFiles) {
String procFiles = procChildFile.getName();
String chidfile = procFiles.indexOf(".") != -1 ? procFiles.substring(0,procFiles.indexOf(".")): procFiles;
TreeItem chidTreeItem = new TreeItem(flagTreeItem, SWT.NULL);
chidTreeItem.setText(chidfile);
fileMap.put(chidTreeItem, procChildFile.toString());
}
}
}
if (allfile.isDirectory() && allfile.list().length > 0) {
childFileMap.put(flagTreeItem, allfile);
}
}
if (i == listFiles.length) {
i = 0;
if (childFileMap != null) {
Set<TreeItem> entrySet = childFileMap.keySet();
for (TreeItem treeItem2 : entrySet) {
forShowProjectFile(treeItem2,childFileMap.get(treeItem2));
}
}
}
}
}
/**
* This is a callback that will allow us to create the viewer and initialize
* it.
*/
public void createPartControl(Composite parent) {
Set<File> projectSet = new HashSet<File>();
SashForm sashForm = new SashForm(parent, SWT.VERTICAL | SWT.SMOOTH);
Group upGroup = new Group(sashForm, SWT.NONE);
upGroup.setLayout(new FillLayout());
upTree = new Tree(upGroup, SWT.SINGLE);
initProjectFile(file,projectSet);
showAllProjectFile(projectSet);
// 定义拖放源对象
DragSource dragSource = new DragSource(upTree, DND.DROP_MOVE | DND.DROP_COPY);
// 设置传输的数据为文本型String类型
dragSource.setTransfer(new Transfer[] { TextTransfer.getInstance() });
// 注册拖放源时的事件处理
dragSource.addDragListener(new DragSourceListener() {
public void dragStart(DragSourceEvent event) {
if (upTree.getSelectionCount() == 0)
event.doit = false;
}
public void dragSetData(DragSourceEvent event) {
if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = upTree.getSelection()[0].getText(0);
/*fileItem = upTree.getSelection(); */
//event.
}
}
public void dragFinished(DragSourceEvent event) {
}
});
DropTarget dropTarget = new DropTarget(upTree, DND.DROP_MOVE
| DND.DROP_DEFAULT | DND.DROP_COPY);
// 设置目标对象可传输的数据类型
dropTarget.setTransfer(new Transfer[] { TextTransfer.getInstance() });
// 注册目标对象的事件处理
dropTarget.addDropListener(new DropTargetListener() {
public void dragEnter(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT)
event.detail = DND.DROP_COPY;
}
public void dragOperationChanged(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT)
event.detail = DND.DROP_COPY;
}
public void dragOver(DropTargetEvent event) {
event.feedback = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SELECT;
}
// 当松开鼠标时触发的事件
public void drop(DropTargetEvent event) {
if (event.item == null)
return;
if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) {
// 首先获得目标对象节点
TreeItem eventItem = (TreeItem) event.item;
// 得到当前选中的对象节点
TreeItem selectTreeItem = upTree.getSelection()[0];
TreeItem[] childItem = selectTreeItem.getItems();
if (childItem.length == 0 || childItem == null) {
return;
}
if(fileMap.get(eventItem).equals(fileMap.get(selectTreeItem)))
return ;
String selectTreeItemText = event.data.toString();
TreeItem treeItem = new TreeItem(eventItem, SWT.NONE);
treeItem.setText(selectTreeItemText);
createChildTreeItem(selectTreeItem,treeItem);
//更改文件路径
//选中文件的路径
String selectFilePath = fileMap.get(selectTreeItem);
//选中文件的最后一个路径
String endSelectFilePath = selectFilePath.substring(selectFilePath.indexOf(selectTreeItemText), selectFilePath.length());
File selectFile = new File(selectFilePath);
//目标路径
String eventFilePath = fileMap.get(eventItem) + fileSplit + endSelectFilePath;
File eventFile = new File(eventFilePath);
selectFile.renameTo(eventFile);
//删除选中节点
selectTreeItem.dispose();
}
}
public void dragLeave(DropTargetEvent event) {
}
public void dropAccept(DropTargetEvent event) {
}
});
/*upTree.addMenuDetectListener(new MenuDetectListener() {
@Override
public void menuDetected(MenuDetectEvent e) {
TreeItem[] items = upTree.getSelection();
if ((null == items) || (items.length == 0)) {
return;
}
final TreeItem one = items[0];
MenuManager mgr = new MenuManager();
addProjectAction(one, mgr);
Menu mu = mgr.createContextMenu(upTree);
mu.setVisible(true);
}
});*/
}
/**创建子节点
* @param treeItem
*/
private void createChildTreeItem(TreeItem treeItem,TreeItem linkTreeItem){
if (treeItem.getItems().length != 0) {
TreeItem[] childTreeItems = treeItem.getItems();
for (TreeItem childTreeItem : childTreeItems) {
TreeItem childItem = new TreeItem(linkTreeItem,SWT.NONE);
childItem.setText(childTreeItem.getText());
createChildTreeItem(childTreeItem,childItem);
}
}
}
/**得到当前选择的节点所对应的子节点的File集合
* @param file
* @param projectList
* @return
*/
private List<File> getAllFile(File file, List<File> projectList) {
projectList.add(file);
File allFile[] = file.listFiles();
for (File allfile : allFile) {
if (allfile.isDirectory()) {
getAllFile(allfile, projectList);
} else {
projectList.add(allfile);
}
}
return projectList;
}
private Stack<String> getItemMappingFilePath(TreeItem treeItem,
Stack<String> stack) {
if (treeItem != null) {
stack.push(treeItem.getText());
TreeItem item = treeItem.getParentItem();
if (item != null)
getItemMappingFilePath(item, stack);
}
return stack;
}
/**得到TreeItrem 对应的文件类型
* @param treeItem
* @return
*/
private String getItemMappingFileType(TreeItem treeItem) {
String fileType = "default";
if (treeItem != null) {
String filePath = fileMap.get(treeItem);
fileType = filePath.substring(filePath.lastIndexOf(fileSplit),filePath.length());
}
return fileType;
}
/**得到一级根节点
* @param item
* @return
*/
private TreeItem getFirstParentTreeItem(TreeItem item){
if(item.getParentItem() != null){
item = getFirstParentTreeItem(item.getParentItem());
}
return item;
}
/**设置根节点和根节点下面的所以子节点展开
* @param treeItem
*/
private void setTreeItemExpanded(TreeItem treeItem){
treeItem.setExpanded(true);
TreeItem [] treeItems = treeItem.getItems();
for (TreeItem tree : treeItems) {
if(tree.getItems() != null){
setTreeItemExpanded(tree);
}
}
}
private void addProjectAction(final TreeItem item, MenuManager mgr) {
Action newProjectAction = new Action("New Project") {
public void run() {
InputDialog input = MyDlg.getInputDiaLog(item,"default");
input.open();
String projectName = input.getValue();
TreeItem projectItem = new TreeItem(upTree, SWT.NONE);
if(projectName != null && !"".equals(projectName)){
projectItem.setText(projectName);
File createFile = new File(projectPath+fileSplit+projectName);
if(!createFile.exists()){
createFile.mkdir();
fileMap.put(projectItem, createFile.toString());
}
}
}
};
Action openAllTree = new Action("open Tree"){
public void run() {
TreeItem[] items = upTree.getSelection();
if ((null == items) || (items.length == 0)) {
return;
}
TreeItem firstParentTreeItem = getFirstParentTreeItem(items[0]);
setTreeItemExpanded(firstParentTreeItem);
}
};
Action addPackageAction = new Action("Add Package") {
@Override
public void run() {
String defaultPackage = fileMap.get(item);
InputDialog log = MyDlg.getInputDiaLog(null,defaultPackage.replace(fileSplit, "."));
log.open();
TreeItem packageItem = new TreeItem(item, SWT.NULL);
String packageValue = log.getValue();
if(packageValue != null && !"".equals(packageValue)){
packageItem.setText(packageValue);
File createFile = new File(fileMap.get(item) + fileSplit + log.getValue());
if (!createFile.exists()) {
createFile.mkdir();
fileMap.put(packageItem, createFile.toString());
}
}
}
};
Action newTestCaseAction = new Action("New TestCase") {
@Override
public void run() {
String filePath = fileMap.get(item);
String testCaseName = "testCase";
TreeItem treeItem = new TreeItem(item, SWT.NULL);
treeItem.setText(testCaseName);
File createXmlFile = new File(filePath + fileSplit+ testCaseName + xmlFlag);
try {
PrintWriter xmlPrint = new PrintWriter(createXmlFile);
xmlPrint.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xmlPrint.flush();
xmlPrint.close();
fileMap.put(treeItem, createXmlFile.toString());
File createProcFile = new File(filePath + fileSplit + testCaseName + procFlag);
if (!createProcFile.exists()) {
createProcFile.mkdir();
File createTestCaseFile = new File(filePath + fileSplit + testCaseName + procFlag + fileSplit+ testCaseName + rrFlag);
TreeItem testCaseTreeItem = new TreeItem(treeItem, SWT.NULL);
testCaseTreeItem.setText(testCaseName);
PrintWriter testCasePrint = new PrintWriter(createTestCaseFile);
testCasePrint.flush();
testCasePrint.close();
fileMap.put(testCaseTreeItem, createTestCaseFile.toString());
}
}catch (IOException e) {
e.printStackTrace();
}
}
};
Action newProcessAction = new Action("New Process") {
@Override
public void run() {
String filePath = fileMap.get(item);
String processName = "bbb";
TreeItem testCaseTreeItem = new TreeItem(item, SWT.NULL);
testCaseTreeItem.setText(processName);
try {
File createTestCaseFile = new File(filePath.substring(0,filePath.lastIndexOf(".")) + procFlag + fileSplit+ processName+rrFlag);
PrintWriter testCasePrint = new PrintWriter(createTestCaseFile);
testCasePrint.flush();
testCasePrint.close();
fileMap.put(testCaseTreeItem, createTestCaseFile.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
};
mgr.add(new Separator());
Action deleteAction = new Action("Delete") {
@Override
public void run() {
String filePath = fileMap.get(item);
File file = new File(filePath);
if (file.exists() && file.isDirectory()) {
item.dispose();
List<File> fileList = new ArrayList<File>();
fileList = getAllFile(file, fileList);
for (int i = fileList.size() - 1; i >= 0; i--) {
fileList.get(i).delete();
}
} else {
String itemName = getItemMappingFileType(item);
if (itemName.endsWith(xmlFlag)) {
String xmlBeginFilePath = filePath.substring(0,filePath.lastIndexOf(fileSplit));
String procBeginFileName = itemName.substring(0,itemName.indexOf("."));
String procFileName =procBeginFileName + procFlag;
TreeItem[] items = item.getParentItem().getItems();
for (TreeItem treeItem : items) {
if (treeItem.getText().equalsIgnoreCase(procBeginFileName)) {
treeItem.dispose();
break;
}
}
String procFilePath = xmlBeginFilePath + fileSplit
+ procFileName;
File procFile = new File(procFilePath);
if (procFile.exists() && procFile.isDirectory()) {
List<File> fileList = new ArrayList<File>();
fileList = getAllFile(procFile, fileList);
for (int i = fileList.size() - 1; i >= 0; i--) {
fileList.get(i).delete();
}
}
}
item.dispose();
file.delete();
}
}
};
mgr.add(newProjectAction);
mgr.add(addPackageAction);
mgr.add(newTestCaseAction);
mgr.add(newProcessAction);
mgr.add(deleteAction);
mgr.add(openAllTree);
String fileType = getItemMappingFileType(item);
if(fileType.indexOf(xmlFlag) != -1){
addPackageAction.setEnabled(false);
newTestCaseAction.setEnabled(false);
}else if(fileType.indexOf(rrFlag) != -1){
addPackageAction.setEnabled(false);
newTestCaseAction.setEnabled(false);
newProcessAction.setEnabled(false);
}else{
newProcessAction.setEnabled(false);
}
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
// viewer.getControl().setFocus();
}
public static void main(String[] args) {
String moveFilePath = "f:/test/aatest/aa";
String initFilePath = "f:/test/aa";
File initFile = new File(initFilePath);
File moveFile = new File(moveFilePath);
initFile.renameTo(moveFile);
}
}