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.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.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.MenuDetectEvent;
import org.eclipse.swt.events.MenuDetectListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Menu;
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 + "myProject";
private static String procFlag = ".proc";
private static String xmlFlag = ".xml";
private static File file = new File(projectPath);
Tree upTree = null;
private void showAllProjectFile(File file) {
String fileName = file.getName();
TreeItem treeItem = new TreeItem(upTree, SWT.NONE);
treeItem.setText(fileName);
if (file.isDirectory()) {
forShowProjectFile(treeItem, 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;
TreeItem flagTreeItem = new TreeItem(treeItem, SWT.NULL);
flagTreeItem.setText(treeName);
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) {
new TreeItem(flagTreeItem, SWT.NULL).setText(procChildFile.getName());
}
}
}
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) {
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);
showAllProjectFile(file);
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);
}
});
}
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;
}
private void addProjectAction(final TreeItem item, MenuManager mgr) {
mgr.add(new Action("Add Package") {
@Override
public void run() {
Stack<String> stack = new Stack<String>();
String filePath = projectRootPath;
stack = getItemMappingFilePath(item, stack);
for (int i = stack.size() - 1; i >= 0; i--) {
filePath += fileSplit + stack.get(i);
}
new TreeItem(item, SWT.NULL).setText("add package");
File createFile = new File(filePath + fileSplit + "add package");
if (!createFile.exists()) {
createFile.mkdir();
}
}
});
mgr.add(new Action("New TestCase") {
@Override
public void run() {
Stack<String> stack = new Stack<String>();
String filePath = projectRootPath;
stack = getItemMappingFilePath(item, stack);
for (int i = stack.size() - 1; i >= 0; i--) {
filePath += fileSplit + stack.get(i);
}
String testCaseName = "testCase";
TreeItem treeItem = new TreeItem(item, SWT.NULL);
treeItem.setText(testCaseName + ".xml");
File createFile = new File(filePath + fileSplit + testCaseName
+ ".proc");
if (!createFile.exists()) {
createFile.mkdir();
try {
File createXmlFile = new File(filePath + fileSplit+ testCaseName + ".xml");
PrintWriter xmlPrint = new PrintWriter(createXmlFile);
xmlPrint.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xmlPrint.flush();
xmlPrint.close();
File createTestCaseFile = new File(filePath + fileSplit + testCaseName + ".proc" + fileSplit+ testCaseName + ".rr");
new TreeItem(treeItem, SWT.NULL).setText(createTestCaseFile.getName());
PrintWriter testCasePrint = new PrintWriter(createTestCaseFile);
testCasePrint.flush();
testCasePrint.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
mgr.add(new Action("New Process") {
@Override
public void run() {
String processName = String.valueOf(Math.random())+".rr";
new TreeItem(item, SWT.NULL).setText(processName);
Stack<String> stack = new Stack<String>();
String filePath = projectRootPath;
stack = getItemMappingFilePath(item, stack);
for (int i = stack.size() - 1; i >= 0; i--) {
filePath += fileSplit + stack.get(i);
}
try {
File createTestCaseFile = new File(filePath.substring(0,filePath.indexOf(".")) + procFlag + fileSplit+ processName);
PrintWriter testCasePrint = new PrintWriter(createTestCaseFile);
testCasePrint.flush();
testCasePrint.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
mgr.add(new Separator());
mgr.add(new Action("Delete Project") {
@Override
public void run() {
Stack<String> stack = new Stack<String>();
String filePath = projectRootPath;
stack = getItemMappingFilePath(item, stack);
for (int i = stack.size() - 1; i >= 0; i--) {
filePath += fileSplit + stack.get(i);
}
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 = item.getText();
if (itemName.endsWith(".xml")) {
String xmlBeginFilePath = filePath.substring(0,
filePath.lastIndexOf(fileSplit));
String procFileName = itemName.substring(0,
itemName.indexOf("."))
+ ".proc";
TreeItem[] items = item.getParentItem().getItems();
for (TreeItem treeItem : items) {
if (treeItem.getText().equalsIgnoreCase(
procFileName)) {
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();
}
}
});
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
// viewer.getControl().setFocus();
}
}