|
1 | 1 | package org.gemoc.execution.sequential.javaxdsml.ide.ui.commands; |
2 | 2 |
|
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.Files; |
| 5 | +import java.nio.file.Paths; |
| 6 | +import java.util.List; |
| 7 | + |
3 | 8 | import org.eclipse.core.commands.ExecutionEvent; |
4 | 9 | import org.eclipse.core.commands.ExecutionException; |
5 | 10 | import org.eclipse.core.commands.IHandler; |
6 | 11 | import org.eclipse.core.resources.IProject; |
7 | | -import org.gemoc.xdsmlframework.ide.ui.commands.AbstractGemocLanguageProjectHandler; |
| 12 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 13 | +import org.eclipse.core.runtime.OperationCanceledException; |
| 14 | +import org.eclipse.core.runtime.jobs.Job; |
| 15 | +import org.eclipse.emf.common.util.URI; |
| 16 | +import org.eclipse.emf.ecore.EStructuralFeature; |
| 17 | +import org.eclipse.xtext.nodemodel.INode; |
| 18 | +import org.eclipse.xtext.nodemodel.util.NodeModelUtils; |
| 19 | +import org.eclipse.xtext.resource.XtextResource; |
| 20 | +import org.eclipse.xtext.ui.editor.XtextEditor; |
| 21 | +import org.eclipse.xtext.ui.editor.model.IXtextDocument; |
| 22 | +import org.eclipse.xtext.ui.editor.utils.EditorUtils; |
| 23 | +import org.gemoc.xdsmlframework.ide.ui.commands.AbstractMelangeSelectHandler; |
8 | 24 | import org.gemoc.xdsmlframework.ide.ui.xdsml.wizards.CreateDomainModelWizardContextAction; |
9 | 25 | import org.gemoc.xdsmlframework.ide.ui.xdsml.wizards.CreateDomainModelWizardContextAction.CreateDomainModelAction; |
10 | 26 | //import org.eclipse.jface.dialogs.MessageDialog; |
11 | 27 |
|
12 | | -public class CreateDomainModelProjectHandler extends AbstractGemocLanguageProjectHandler implements |
| 28 | +import fr.inria.diverse.melange.metamodel.melange.Language; |
| 29 | + |
| 30 | +public class CreateDomainModelProjectHandler extends AbstractMelangeSelectHandler implements |
13 | 31 | IHandler { |
14 | 32 |
|
15 | 33 | @Override |
16 | | - public Object execute(ExecutionEvent event) throws ExecutionException { |
17 | | - |
18 | | - // get the optional selection and eventually project data to preset the wizard |
19 | | - IProject updatedGemocLanguageProject = getUpdatedGemocLanguageProjectFromSelection(event); |
20 | | - |
21 | | - // launch the wizard that will select the action and do the job |
22 | | - //WizardDialog wizardDialog = new WizardDialog(HandlerUtil.getActiveWorkbenchWindow(event).getShell(), |
23 | | - // new CreateDomainModelWizard(updatedGemocLanguageProject)); |
24 | | - //wizardDialog.open(); |
25 | | - |
26 | | - // FIXME if the selection is a melange file we should precise which language must be updated |
| 34 | + public Object executeForSelectedLanguage(ExecutionEvent event, |
| 35 | + IProject updatedGemocLanguageProject, Language language) |
| 36 | + throws ExecutionException { |
27 | 37 | CreateDomainModelWizardContextAction action = new CreateDomainModelWizardContextAction( |
28 | 38 | updatedGemocLanguageProject, null); |
29 | 39 | action.actionToExecute = CreateDomainModelAction.CREATE_NEW_EMF_PROJECT; |
30 | 40 | action.execute(); |
31 | 41 |
|
| 42 | + if(action.getCreatedEcoreUri() != null){ |
| 43 | + waitForAutoBuild(); |
| 44 | + updateMelange(event,language,action.getCreatedEcoreUri()); |
| 45 | + } |
| 46 | + |
32 | 47 | return null; |
33 | 48 | } |
| 49 | + |
| 50 | + protected void updateMelange(ExecutionEvent event, Language language, String ecoreURI){ |
| 51 | + // Compute offset & new string |
| 52 | + int startOffset = -1; |
| 53 | + int length = -1; |
| 54 | + String newRegion = null; |
| 55 | + |
| 56 | + EStructuralFeature operators = language.eClass().getEStructuralFeature("operators"); |
| 57 | + List<INode> nodesOp = NodeModelUtils.findNodesForFeature(language, operators); |
| 58 | + int lastOffset = Integer.MAX_VALUE; |
| 59 | + for(INode node : nodesOp){ |
| 60 | + if(node.getOffset() < lastOffset) lastOffset = node.getOffset(); |
| 61 | + } |
| 62 | + if(lastOffset != Integer.MAX_VALUE){ |
| 63 | + startOffset = lastOffset; |
| 64 | + length = 0; |
| 65 | + newRegion = "syntax \""+ecoreURI+"\"\n\t"; |
| 66 | + } |
| 67 | + |
| 68 | + // Replace in document or Melange file |
| 69 | + if(startOffset != -1 && length != -1 && newRegion != null){ |
| 70 | + int _startOffset = startOffset; |
| 71 | + int _length = length; |
| 72 | + String _newRegion = newRegion; |
| 73 | + XtextEditor editor = EditorUtils.getActiveXtextEditor(); |
| 74 | + if (editor != null) { //Update the editor content |
| 75 | + IXtextDocument document = editor.getDocument(); |
| 76 | + document.modify((XtextResource it) -> { |
| 77 | + document.replace(_startOffset,_length, _newRegion); |
| 78 | + return null; // no computed value |
| 79 | + }); |
| 80 | + } |
| 81 | + else{ //Update the Melange file content |
| 82 | + |
| 83 | + try { |
| 84 | + //Load Melange file |
| 85 | + String melangeWSLocation = language.eResource().getURI().toPlatformString(true); |
| 86 | + URI uri = language.eResource().getURI(); |
| 87 | + String melangeLocation =ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString()+melangeWSLocation; |
| 88 | + List<String> lines = Files.readAllLines(Paths.get(melangeLocation)); |
| 89 | + |
| 90 | + StringBuffer newContent = new StringBuffer(); |
| 91 | + lines.forEach( |
| 92 | + line -> newContent.append(line+"\n") |
| 93 | + ); |
| 94 | + |
| 95 | + newContent.replace(startOffset,startOffset+length, newRegion); |
| 96 | + |
| 97 | + //Write new content |
| 98 | + Files.write(Paths.get(melangeLocation), newContent.toString().getBytes()); |
| 99 | + } catch (IOException e) { |
| 100 | + e.printStackTrace(); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + protected void waitForAutoBuild() { |
| 107 | + boolean wasInterrupted = false; |
| 108 | + do { |
| 109 | + try { |
| 110 | + Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, null); |
| 111 | + wasInterrupted = false; |
| 112 | + } catch (OperationCanceledException e) { |
| 113 | + e.printStackTrace(); |
| 114 | + } catch (InterruptedException e) { |
| 115 | + wasInterrupted = true; |
| 116 | + } |
| 117 | + } while (wasInterrupted); |
| 118 | + } |
34 | 119 |
|
| 120 | + @Override |
| 121 | + public String getSelectionMessage() { |
| 122 | + return "Select Melange language that will be used to initialize the new Domain Model project"; |
| 123 | + } |
35 | 124 | } |
0 commit comments