SGXPreferencePage.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.preferences;
  13. import org.eclipse.jface.preference.DirectoryFieldEditor;
  14. import org.eclipse.jface.preference.FieldEditorPreferencePage;
  15. import org.eclipse.swt.widgets.Composite;
  16. import org.eclipse.swt.widgets.Text;
  17. import org.eclipse.ui.IWorkbench;
  18. import org.eclipse.ui.IWorkbenchPreferencePage;
  19. import com.intel.sgx.Activator;
  20. import com.intel.sgx.SdkPathVariableProvider;
  21. /**
  22. * This class represents a preference page that
  23. * is contributed to the Preferences dialog. By
  24. * subclassing <samp>FieldEditorPreferencePage</samp>, we
  25. * can use the field support built into JFace that allows
  26. * us to create a page that is small and knows how to
  27. * save, restore and apply itself.
  28. * <p>
  29. * This page is used to modify preferences only. They
  30. * are stored in the preference store that belongs to
  31. * the main plug-in class. That way, preferences can
  32. * be accessed directly via the preference store.
  33. */
  34. public class SGXPreferencePage
  35. extends FieldEditorPreferencePage
  36. implements IWorkbenchPreferencePage {
  37. private SGXSdkDirectoryFieldEditor sgxSdkDirectoryEditor;
  38. public SGXPreferencePage() {
  39. super(GRID);
  40. setPreferenceStore(Activator.getDefault().getPreferenceStore());
  41. setDescription("Intel(R) SGX Preferences");
  42. }
  43. /**
  44. * Creates the field editors. Field editors are abstractions of
  45. * the common GUI blocks needed to manipulate various types
  46. * of preferences. Each field editor knows how to save and
  47. * restore itself.
  48. */
  49. @Override
  50. protected void createFieldEditors() {
  51. sgxSdkDirectoryEditor = new SGXSdkDirectoryFieldEditor(PreferenceConstants.SDK_PATH,
  52. "&Intel(R) SGX SDK Directory:", getFieldEditorParent());
  53. addField(sgxSdkDirectoryEditor);
  54. }
  55. /*
  56. * Validates whether the path entered in the Intel(R) SGX SDK Preferences points to the Intel(R) SGX SDK or not.
  57. */
  58. private static class SGXSdkDirectoryFieldEditor extends DirectoryFieldEditor {
  59. public SGXSdkDirectoryFieldEditor(String name, String labelText, Composite parent) {
  60. super(name, labelText, parent);
  61. setEmptyStringAllowed(true);
  62. }
  63. @Override
  64. protected boolean doCheckState() {
  65. if (!super.doCheckState()) {
  66. setErrorMessage("Intel(R) SGX Preferences: Not a Valid directory");
  67. return false;
  68. }
  69. String dirname = getTextControl().getText().trim();
  70. if (!dirname.isEmpty() && !SdkPathVariableProvider.isValidSGXSdkLocation(dirname)) {
  71. setErrorMessage("Intel(R) SGX SDK: Not a Valid SGX SDK directory");
  72. return false;
  73. }
  74. return true;
  75. }
  76. @Override
  77. public Text getTextControl(Composite parent) {
  78. setValidateStrategy(VALIDATE_ON_KEY_STROKE);
  79. return super.getTextControl(parent);
  80. }
  81. }
  82. @Override
  83. public void init(IWorkbench workbench) {
  84. }
  85. @Override
  86. public void dispose() {
  87. super.dispose();
  88. if (sgxSdkDirectoryEditor != null) {
  89. sgxSdkDirectoryEditor.dispose();
  90. sgxSdkDirectoryEditor = null;
  91. }
  92. }
  93. }