SaNewFileWizard.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package be.uantwerpen.ansymo.semanticadaptation.ui.wizard;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import org.apache.commons.io.IOUtils;
  6. import org.eclipse.core.resources.IFile;
  7. import org.eclipse.core.resources.IResource;
  8. import org.eclipse.core.runtime.CoreException;
  9. import org.eclipse.jface.viewers.IStructuredSelection;
  10. import org.eclipse.jface.wizard.Wizard;
  11. import org.eclipse.ui.IWorkbench;
  12. import org.eclipse.ui.IWorkbenchWizard;
  13. import org.eclipse.ui.PlatformUI;
  14. import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
  15. import org.eclipse.ui.ide.IDE;
  16. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SemanticAdaptation;
  17. public class SaNewFileWizard extends Wizard implements IWorkbenchWizard {
  18. private static final String WIZARD_NAME = "VDM New File Wizard";
  19. private WizardNewFileCreationPage _pageOne;
  20. private String fPageName;
  21. private String fPageTitle;
  22. private String fPageDescription;
  23. private IStructuredSelection fStructuredSelection;
  24. public SaNewFileWizard() {
  25. setWindowTitle(WIZARD_NAME);
  26. this.fPageName = getPageName();
  27. this.fPageTitle = getPageTitle();
  28. this.fPageDescription = getPageDescription();
  29. }
  30. @Override
  31. public void addPages() {
  32. super.addPages();
  33. _pageOne = new WizardNewFileCreationPage(this.fPageName, this.fStructuredSelection);
  34. _pageOne.setFileExtension(getFileExtension());
  35. _pageOne.setTitle(this.fPageTitle);
  36. _pageOne.setDescription(this.fPageDescription);
  37. addPage(_pageOne);
  38. }
  39. /*
  40. * Gets the main page name
  41. */
  42. protected String getPageName() {
  43. return "New Semantic Adaptation";
  44. }
  45. /*
  46. * Gets the main page title to be displayed
  47. */
  48. protected String getPageTitle() {
  49. return getPageName();
  50. }
  51. /*
  52. * Gets the main page description
  53. */
  54. protected String getPageDescription() {
  55. return "Create a new basic semantic adaptation";
  56. }
  57. /*
  58. * Gets the file extension of the file to create
  59. */
  60. protected String getFileExtension() {
  61. return "sa";
  62. }
  63. /*
  64. * Gets the file template or null if none is provided
  65. */
  66. protected String getFileTemplate(String fileName) {
  67. InputStream stream = null;
  68. try {
  69. stream = SemanticAdaptation.class.getResourceAsStream("/templates/new_file_template.sa");
  70. return IOUtils.toString(stream);
  71. } catch (IOException e) {
  72. // TODO Auto-generated catch block
  73. e.printStackTrace();
  74. return null;
  75. } finally {
  76. IOUtils.closeQuietly(stream);
  77. }
  78. }
  79. @Override
  80. public boolean canFinish() {
  81. return super.canFinish() && _pageOne.getErrorMessage() == null;
  82. }
  83. @Override
  84. public boolean performFinish() {
  85. IFile file = _pageOne.createNewFile();
  86. if (file.exists()) {
  87. String fileName = file.getName();
  88. if (fileName.contains(".")) {
  89. fileName = fileName.substring(0, fileName.indexOf("."));
  90. }
  91. boolean isClean = false;
  92. InputStream in = null;
  93. try {
  94. in = file.getContents();
  95. if (file.getContents().read() == -1) {
  96. isClean = true;
  97. }
  98. } catch (IOException e) {
  99. } catch (CoreException e) {
  100. } finally {
  101. if (in != null) {
  102. try {
  103. in.close();
  104. } catch (IOException e) {
  105. }
  106. }
  107. }
  108. if (isClean) {
  109. String fileTemplate = getFileTemplate(fileName);
  110. if (fileTemplate != null) {
  111. applyTemplate(file, fileTemplate);
  112. }
  113. }
  114. }
  115. try {
  116. IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), file, true);
  117. file.touch(null);
  118. file.refreshLocal(IResource.DEPTH_ONE, null);
  119. } catch (CoreException e) {
  120. e.printStackTrace();
  121. }
  122. return true;
  123. }
  124. private void applyTemplate(IFile file, String fileTemplate) {
  125. InputStream stream;
  126. try {
  127. stream = new ByteArrayInputStream(fileTemplate.getBytes());
  128. file.setContents(stream, IFile.FORCE, null);
  129. } catch (CoreException e) {
  130. e.printStackTrace();
  131. }
  132. }
  133. public void init(IWorkbench workbench, IStructuredSelection selection) {
  134. this.fStructuredSelection = selection;
  135. }
  136. }