来源:互联网 发布:冰点营销软件 编辑:程序博客网 时间:2024/04/27 21:40
 

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.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.MessageBox;
import org.eclipse.swt.widgets.Shell;
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;

 
 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);
   fileMap.put(treeItem, allfile.toString());
   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(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);

  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 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;

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