AddSGXNature.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ///////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) 2018 Intel Corporation. //
  3. // //
  4. // All rights reserved. This program and the accompanying materials //
  5. // are made available under the terms of the Eclipse Public License v1.0 //
  6. // which accompanies this distribution, and is available at //
  7. // http://www.eclipse.org/legal/epl-v10.html //
  8. // //
  9. // Contributors: //
  10. // Intel Corporation - initial implementation and documentation //
  11. ///////////////////////////////////////////////////////////////////////////
  12. package com.intel.sgx.handlers;
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.lang.reflect.InvocationTargetException;
  18. import java.util.Map;
  19. import org.eclipse.cdt.core.templateengine.TemplateCore;
  20. import org.eclipse.cdt.core.templateengine.TemplateEngine;
  21. import org.eclipse.core.commands.ExecutionEvent;
  22. import org.eclipse.core.commands.ExecutionException;
  23. import org.eclipse.core.commands.IHandler;
  24. import org.eclipse.core.commands.IHandlerListener;
  25. import org.eclipse.core.resources.IProject;
  26. import org.eclipse.core.resources.IProjectDescription;
  27. import org.eclipse.core.resources.IResource;
  28. import org.eclipse.core.resources.IWorkspace;
  29. //import org.eclipse.core.resources.IWorkspaceRunnable;
  30. //import org.eclipse.core.resources.IWorkspaceRunnable;
  31. import org.eclipse.core.resources.ResourcesPlugin;
  32. import org.eclipse.core.runtime.CoreException;
  33. import org.eclipse.core.runtime.IProgressMonitor;
  34. import org.eclipse.core.runtime.IStatus;
  35. import org.eclipse.core.runtime.NullProgressMonitor;
  36. //import org.eclipse.jface.operation.IRunnableWithProgress;
  37. import org.eclipse.jface.viewers.ISelection;
  38. import org.eclipse.jface.viewers.IStructuredSelection;
  39. import org.eclipse.ui.handlers.HandlerUtil;
  40. import com.intel.sgx.Activator;
  41. import com.intel.sgx.natures.SGXNature;
  42. public class AddSGXNature implements IHandler {
  43. private IProject project;
  44. public AddSGXNature() {
  45. project = null;
  46. }
  47. @Override
  48. public void addHandlerListener(IHandlerListener arg0) {
  49. }
  50. @Override
  51. public void dispose() {
  52. }
  53. @Override
  54. public Object execute(ExecutionEvent event) throws ExecutionException {
  55. ISelection selection = HandlerUtil.getCurrentSelection(event);
  56. Object element = null;
  57. if (selection instanceof IStructuredSelection) {
  58. element = ((IStructuredSelection) selection).getFirstElement();
  59. if (element instanceof IResource) {
  60. project = ((IResource) element).getProject();
  61. }
  62. }
  63. if (!project.exists()) {
  64. System.err.println("Error: Project not found");
  65. return null;
  66. } else {
  67. try {
  68. TemplateCore template = TemplateEngine.getDefault().getTemplateById("AddSGXNature");
  69. Map<String,String> valueStore = template.getValueStore();
  70. valueStore.put("projectName",project.getName());
  71. valueStore.put("baseName",project.getName());
  72. IProgressMonitor monitor = new NullProgressMonitor();
  73. template.executeTemplateProcesses(monitor, false);
  74. IWorkspace workspace = ResourcesPlugin.getWorkspace();
  75. try{
  76. IProjectDescription description = project.getDescription();
  77. String[] natures = description.getNatureIds();
  78. String[] newNatures = new String[natures.length + 1];
  79. System.arraycopy(natures, 0, newNatures, 0, natures.length);
  80. newNatures[natures.length] = SGXNature.NATURE_ID;
  81. IStatus status = workspace.validateNatureSet(newNatures);
  82. if (status.getCode() == IStatus.OK) {
  83. description.setNatureIds(newNatures);
  84. project.setDescription(description, null);
  85. }
  86. project.refreshLocal(IResource.DEPTH_ONE,null);
  87. } catch(CoreException e){
  88. Activator.log(e);
  89. throw new InvocationTargetException(e);
  90. }
  91. } catch(InvocationTargetException e){
  92. Activator.log(e);
  93. e.printStackTrace();
  94. }
  95. try {
  96. project.refreshLocal(IResource.DEPTH_INFINITE,null);
  97. } catch (CoreException e) {
  98. e.printStackTrace();
  99. }
  100. }
  101. return null;
  102. }
  103. @Override
  104. public boolean isEnabled() {
  105. return true;
  106. }
  107. @Override
  108. public boolean isHandled() {
  109. return true;
  110. }
  111. @Override
  112. public void removeHandlerListener(IHandlerListener arg0) {
  113. }
  114. public static void copyFile(File source, File dest) throws IOException {
  115. byte[] bytes = new byte[4092];
  116. if (source != null && dest != null) {
  117. if (source.isFile()) {
  118. FileInputStream in = null;
  119. FileOutputStream out = null;
  120. try {
  121. in = new FileInputStream(source);
  122. out = new FileOutputStream(dest);
  123. int len;
  124. while ((len = in.read(bytes)) != -1) {
  125. out.write(bytes, 0, len);
  126. }
  127. } catch (Exception e) {
  128. System.err.println("Error: " + e.toString());
  129. } finally {
  130. try {
  131. if (in != null)
  132. in.close();
  133. } finally {
  134. if (out != null)
  135. out.close();
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }