package rcpdemo;
import java.io.File;
import java.io.FilenameFilter;
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.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
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.graphics.Image;
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.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
public class View extends ViewPart {
private static String fileSplitString = System.getProperty("file.separator");
private static String projectPathString = "f:";
private static String suepFlagString = ".suep";
private static File file = new File(projectPathString);
Tree upTree = null;
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);
}
}
}
}
private void showAllProjectFile(Set<File> fileSet){
for (File allfile : fileSet) {
String fileName = allfile.getName();
TreeItem treeItem = new TreeItem(upTree, SWT.NONE);
treeItem.setText(fileName);
if(allfile.isDirectory()){
forShowProjectFile(treeItem,allfile);
}
}
}
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(suepFlagString)){
String treeName = /*fileName.indexOf(".") != -1 ? fileName.substring(0,fileName.indexOf(".")): */fileName;
TreeItem flagTreeItem = new TreeItem(treeItem, SWT.NULL);
flagTreeItem.setText(treeName);
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);
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 item
* @param mgr
*/
private void createProject(String projectName,String endPreFix){
TreeItem treeItem = new TreeItem(upTree, SWT.NONE);
treeItem.setText(projectName);
File createFile = new File(projectPathString+fileSplitString+projectName);
if(!createFile.exists()){
createFile.mkdir();
try {
File createFlagFile = new File(projectPathString+fileSplitString+projectName+fileSplitString+projectName+endPreFix);
PrintWriter print = new PrintWriter(createFlagFile);
print.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
print.flush();
print.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
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 void addProjectAction(final TreeItem item, MenuManager mgr) {
mgr.add(new Action("New Project"){
@Override
public void run() {
String projectName = "testName";//+String.valueOf(Math.random()*100).substring(0,String.valueOf(Math.random()*100).indexOf("."));
TreeItem [] treeItem = item.getParent().getItems();
for (TreeItem treeItem2 : treeItem) {
if(treeItem2.getText().equals(projectName)) {
System.out.println("已存在的项目!");
return;
}
}
createProject(projectName,suepFlagString);
}
@Override
public ImageDescriptor getImageDescriptor() {
return null;
// return ImageRoom.getImageDescriptor(IImageName.FILE_NEW);
}
});
mgr.add(new Action("Delete Project"){
@Override
public void run() {
Stack<String> stack = new Stack<String>();
String filePath = projectPathString;
stack = getItemMappingFilePath(item, stack);
for(int i = stack.size()-1;i >= 0;i--){
filePath += fileSplitString+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{
item.dispose();
file.delete();
}
}
@Override
public ImageDescriptor getImageDescriptor() {
return null;
// return ImageRoom.getImageDescriptor(IImageName.FILE_NEW);
}
});
addPackageCreateAction(item,mgr);
}
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 addPackageCreateAction(final TreeItem item, MenuManager mgr){
mgr.add(new Action("New TestCase"){
@Override
public void run() {
Stack<String> stack = new Stack<String>();
String filePath = projectPathString;
stack = getItemMappingFilePath(item, stack);
for(int i = stack.size()-1;i >= 0;i--){
filePath += fileSplitString+stack.get(i);
}
String testCaseName = "testCase";
new TreeItem(item, SWT.NULL).setText(testCaseName+".xml");
File createFile = new File(filePath+fileSplitString+testCaseName+".proc");
if(!createFile.exists()){
createFile.mkdir();
TreeItem treeItem = new TreeItem(item, SWT.NULL);
treeItem.setText(createFile.getName());
try {
File createXmlFile = new File(filePath+fileSplitString+testCaseName+".xml");
File createFlagFile = new File(filePath+fileSplitString+testCaseName+".proc"+fileSplitString+testCaseName+".rr");
new TreeItem(treeItem, SWT.NULL).setText(createFlagFile.getName());
PrintWriter print = new PrintWriter(createXmlFile);
PrintWriter prints = new PrintWriter(createFlagFile);
print.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
print.flush();
prints.flush();
prints.close();
print.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public ImageDescriptor getImageDescriptor() {
return null;
// return ImageRoom.getImageDescriptor(IImageName.FILE_NEW);
}
});
mgr.add(new Separator());
// 打开
mgr.add(new Action("Add Package"){
@Override
public void run() {
Stack<String> stack = new Stack<String>();
String filePath = projectPathString;
stack = getItemMappingFilePath(item, stack);
for(int i = stack.size()-1;i >= 0;i--){
filePath += fileSplitString+stack.get(i);
}
new TreeItem(item, SWT.NULL).setText("add package");
File createFile = new File(filePath+fileSplitString+"add package");
if(!createFile.exists()){
createFile.mkdir();
}
}
@Override
public ImageDescriptor getImageDescriptor() {
return null;
// return ImageRoom.getImageDescriptor(IImageName.FOLDER_ADD);
}
});
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
//viewer.getControl().setFocus();
}
}