Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0705ed717bc1a4e29a20ff6a9f25a1896f7ece5a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*******************************************************************************
 * Copyright (c) 2016-2017, Thales Global Services S.A.S.
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Abel Hegedus, Tamas Borbas, Peter Lunk (IncQuery Labs Ltd.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.diffmerge.patch.ui.handlers

import java.io.File
import org.eclipse.core.commands.AbstractHandler
import org.eclipse.core.commands.ExecutionEvent
import org.eclipse.core.commands.ExecutionException
import org.eclipse.emf.diffmerge.api.Role
import org.eclipse.emf.diffmerge.api.diff.IDifference
import org.eclipse.emf.diffmerge.patch.api.ModelPatchException
import org.eclipse.emf.diffmerge.patch.runtime.ModelPatchRecorder
import org.eclipse.emf.diffmerge.patch.ui.preferences.ModelPatchPreferenceProvider
import org.eclipse.emf.diffmerge.patch.ui.utils.DialogFactory
import org.eclipse.emf.diffmerge.patch.ui.utils.SerializerProvider
import org.eclipse.emf.diffmerge.ui.diffuidata.ComparisonSelection
import org.eclipse.emf.diffmerge.ui.setup.EMFDiffMergeEditorInput
import org.eclipse.emf.diffmerge.ui.viewers.EMFDiffNode
import org.eclipse.jface.viewers.IStructuredSelection
import org.eclipse.swt.widgets.Shell
import org.eclipse.ui.IWorkbench
import org.eclipse.ui.handlers.HandlerUtil

class ModelpatchGenerationHandler extends AbstractHandler {
  public static val String MODELPATCH_GENERATION_ERROR_TITLE = "Model Patch Generation Error"
  extension ModelPatchPreferenceProvider = ModelPatchPreferenceProvider.INSTANCE
  extension ComparisonSelectionUtil util = new ComparisonSelectionUtil

  override Object execute(ExecutionEvent event) throws ExecutionException {
    val shell = HandlerUtil.getActiveShell(event)
    val workbench = HandlerUtil.getActiveWorkbenchWindow(event).workbench
    var IStructuredSelection selection = null

    try {
        val activeMenuSelection = HandlerUtil.getActiveMenuSelection(event)
        if (activeMenuSelection instanceof IStructuredSelection) {
            selection = activeMenuSelection
        }
        if (selection === null) {
            val currentSelection = HandlerUtil.getCurrentSelection(event)
            if (currentSelection instanceof IStructuredSelection) {
                selection = currentSelection
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace
    }

    val part = HandlerUtil.getActiveEditorInput(event)
    var EMFDiffNode diffNode = null
    if (part instanceof EMFDiffMergeEditorInput) {
      diffNode = part.compareResult
      val editorSelection = part.viewer.selection
      if(editorSelection instanceof IStructuredSelection){
        selection = editorSelection          
      }
    }

    if (selection instanceof ComparisonSelection) {
      if(diffNode === null) {
        diffNode = selection.diffNode;
      }
      val diffs = selection.getDiffsToMerge(false, diffNode)
      if (!diffs.empty) {
        diffs.generatePatch(shell, workbench, selection)
      }
    } else if (selection !== null && diffNode !== null){
      val comparison = diffNode.actualComparison
      val differences = comparison.getDifferences(Role.REFERENCE) + comparison.getDifferences(Role.TARGET)
      differences.generatePatch(shell, workbench, selection)
    }

    return null
  }

  public def void generatePatchFromDiffNode(EMFDiffNode diffNode, ComparisonSelection selection, Shell shell, IWorkbench workbench) {
    val diffs = selection.getDiffsToMerge(false, diffNode)
    if (!diffs.empty) {
      diffs.generatePatch(shell, workbench, selection)
    }
  }

  public def void generatePatch(Iterable<IDifference> diff, Shell shell, IWorkbench workbench, IStructuredSelection selection) {
    extension val DialogFactory factory = new DialogFactory(shell)
    val SerializerProvider serializerProvider = new SerializerProvider
    try {
      val serializer = serializerProvider.getSelectedSerializer(serializationType)
      val path = openSaveFileDialog(workbench, selection, serializer.preferredFileExtension)
      if (path !== null) {
        val modelPatchRecorder = new ModelPatchRecorder
        val generatedPatch = modelPatchRecorder.generateModelPatch(diff)
        val patchFile = path.location.toFile
        serializer.serialize(generatedPatch, patchFile)

        if (isFileGenerated(patchFile) == true) {
          openModelPatchEditor(workbench.activeWorkbenchWindow, path);
        } else {
          openErrorDialog(MODELPATCH_GENERATION_ERROR_TITLE, '''File cannot be created at the following path: «path»''')
        }
      } else {
        openInformationDialog("Model Patch Generation Information", "No file has been selected!")
      }
    } catch (ModelPatchException ex) {
      openErrorDialog(MODELPATCH_GENERATION_ERROR_TITLE, "Modelpatch generation finished with errors!", ex,
        "org.eclipse.emf.diffmerge.patch")
    } catch (ClassCastException ex) {
      openErrorDialog(MODELPATCH_GENERATION_ERROR_TITLE, "Patch generation only works from EMF Diff/Merge!")
    } catch (Exception ex) {
      ex.printStackTrace
      openErrorDialog(MODELPATCH_GENERATION_ERROR_TITLE, "Unknown error!", ex, "org.eclipse.emf.diffmerge.patch")
    }
    return
  }

  def boolean isFileGenerated(File file) {
    return file.exists
  }

}

Back to the top