直接在包中创建用例

 

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();
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值