SGXDialogBase.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. ///////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) 2016 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.dialogs;
  13. import java.io.InputStream;
  14. import java.util.Scanner;
  15. import org.eclipse.core.resources.IContainer;
  16. import org.eclipse.core.resources.IFile;
  17. import org.eclipse.core.resources.IProject;
  18. import org.eclipse.core.resources.IResource;
  19. import org.eclipse.core.resources.ResourcesPlugin;
  20. import org.eclipse.core.runtime.IAdaptable;
  21. import org.eclipse.core.runtime.IPath;
  22. import org.eclipse.core.runtime.IStatus;
  23. import org.eclipse.core.runtime.Status;
  24. import org.eclipse.jface.dialogs.Dialog;
  25. import org.eclipse.jface.viewers.IStructuredSelection;
  26. import org.eclipse.jface.window.IShellProvider;
  27. import org.eclipse.swt.SWT;
  28. import org.eclipse.swt.events.SelectionEvent;
  29. import org.eclipse.swt.events.SelectionListener;
  30. import org.eclipse.swt.layout.GridData;
  31. import org.eclipse.swt.layout.GridLayout;
  32. import org.eclipse.swt.widgets.Button;
  33. import org.eclipse.swt.widgets.Composite;
  34. import org.eclipse.swt.widgets.Group;
  35. import org.eclipse.swt.widgets.Label;
  36. import org.eclipse.swt.widgets.Shell;
  37. import org.eclipse.swt.widgets.Text;
  38. import org.eclipse.ui.IWorkbenchWindow;
  39. import org.eclipse.ui.PlatformUI;
  40. import org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog;
  41. import com.intel.sgx.Activator;
  42. public abstract class SGXDialogBase extends Dialog {
  43. protected Shell shell;
  44. public Text configFileField;
  45. public static FilteredResourcesSelectionDialog dialogForConfig(Shell shell) {
  46. // final IContainer container = ResourcesPlugin.getWorkspace().getRoot();
  47. final IContainer container = SGXDialogBase.getCurrentProject();
  48. FilteredResourcesSelectionDialog d = new FilteredResourcesSelectionDialog(
  49. shell, false, container, IResource.FILE) {
  50. {
  51. setInitialPattern("**");
  52. }
  53. @Override
  54. protected IStatus validateItem(Object item) {
  55. // return Status.OK_STATUS;
  56. IFile f = (IFile) item;
  57. if (f.getParent() instanceof IProject) {
  58. return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
  59. "The selected resource has to be part of the source folder");
  60. }
  61. return super.validateItem(item);
  62. }
  63. protected ItemsFilter createFilter() {
  64. return new ResourceFilter(container, true, IResource.FILE) {
  65. @Override
  66. public boolean matchItem(Object item) {
  67. return isConfigFile(item);
  68. }
  69. private boolean isConfigFile(Object item) {
  70. if (!(item.toString().endsWith(".xml") && super
  71. .matchItem(item))) {
  72. return false;
  73. }
  74. try {
  75. IFile iFile = (IFile) item;
  76. return streamContainsString(iFile.getContents(),
  77. "<EnclaveConfiguration>");
  78. } catch (Throwable e) {
  79. return false;
  80. }
  81. }
  82. };
  83. }
  84. public boolean streamContainsString(InputStream is,
  85. String searchString) {
  86. Scanner streamScanner = new Scanner(is);
  87. if (streamScanner.findWithinHorizon(searchString, 0) != null) {
  88. return true;
  89. } else {
  90. return false;
  91. }
  92. }
  93. };
  94. return d;
  95. }
  96. protected SelectionListener configFileSelectionListener = new SelectionListener() {
  97. @Override
  98. public void widgetSelected(SelectionEvent event) {
  99. FilteredResourcesSelectionDialog d = dialogForConfig(shell);
  100. d.setTitle("Select Config File");
  101. if (d.open() == Dialog.OK) {
  102. IFile target = (IFile) d.getResult()[0];
  103. configFileField.setText(target.getLocation().toOSString());
  104. }
  105. ;
  106. }
  107. @Override
  108. public void widgetDefaultSelected(SelectionEvent arg0) {
  109. // TODO Auto-generated method stub
  110. }
  111. };
  112. public SGXDialogBase(Shell parentShell) {
  113. super(parentShell);
  114. }
  115. public SGXDialogBase(IShellProvider parentShell) {
  116. super(parentShell);
  117. }
  118. protected Text addGroup(Composite composite, String title, String subtitle,
  119. String label, String selectButtonLabel, SelectionListener selectionListener) {
  120. final Group container = new Group(composite, SWT.None);
  121. container.setLayout(new GridLayout(3, false));
  122. GridData innergrid1 = new GridData(GridData.FILL_HORIZONTAL);
  123. innergrid1.horizontalSpan = 3;
  124. container.setLayoutData(innergrid1);
  125. container.setText(title);
  126. final Label messageLabel = new Label(container, SWT.NONE);
  127. messageLabel.setLayoutData(new GridData(GridData.BEGINNING,
  128. GridData.CENTER, false, false, 3, 1));
  129. messageLabel.setText(subtitle);
  130. final Label messageLabel1 = new Label(container, SWT.NONE);
  131. messageLabel1.setText(label);
  132. messageLabel1.setLayoutData(new GridData(GridData.BEGINNING));
  133. Text directoryNameField = new Text(container, SWT.SINGLE | SWT.BORDER);
  134. GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
  135. gridData.horizontalSpan = 1;
  136. gridData.widthHint = 400;
  137. directoryNameField.setLayoutData(gridData);
  138. final Button selectButton = new Button(container, SWT.PUSH);
  139. selectButton.setText(selectButtonLabel);
  140. GridData buttonGridData = new GridData(GridData.END);
  141. buttonGridData.horizontalAlignment = SWT.RIGHT;
  142. buttonGridData.horizontalSpan = 1;
  143. buttonGridData.minimumWidth = 120;
  144. selectButton.setLayoutData(buttonGridData);
  145. selectButton.addSelectionListener(selectionListener);
  146. return directoryNameField;
  147. }
  148. public IPath getCurrentProjectPath() {
  149. IProject project = getCurrentProject();
  150. IPath path = null;
  151. if (project != null) {
  152. path = project.getLocation();
  153. }
  154. return path;
  155. }
  156. static public IProject getCurrentProject() {
  157. IProject project = null;
  158. IWorkbenchWindow window = PlatformUI.getWorkbench()
  159. .getActiveWorkbenchWindow();
  160. if (window != null) {
  161. IStructuredSelection selection = (IStructuredSelection) window
  162. .getSelectionService().getSelection();
  163. Object firstElement = selection.getFirstElement();
  164. if (firstElement instanceof IAdaptable) {
  165. project = (IProject) ((IAdaptable) firstElement)
  166. .getAdapter(IProject.class);
  167. }
  168. }
  169. return project;
  170. }
  171. @Override
  172. protected void configureShell(Shell newShell){
  173. super.configureShell(newShell);
  174. }
  175. protected void addLabel(final Group container, String labelText) {
  176. final Label messageLabel = new Label(container, SWT.NONE);
  177. messageLabel.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, false, false, 3, 1));
  178. messageLabel.setText(labelText);
  179. }
  180. }