Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9bf0cdceebb44afadfc3a161791792d3cb69d794 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright (c) 2010-2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Martin Fluegge - initial API and implementation
 */
package org.eclipse.emf.cdo.dawn.codegen.actions;

import org.eclipse.emf.cdo.dawn.codegen.dawngenmodel.DawnFragmentGenerator;
import org.eclipse.emf.cdo.dawn.codegen.dawngenmodel.DawnGenerator;
import org.eclipse.emf.cdo.dawn.codegen.dawngenmodel.DawngenmodelFactory;
import org.eclipse.emf.cdo.dawn.codegen.util.ProjectCreationHelper;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;

/**
 * @author Martin Fluegge
 */
public abstract class GenerateDawnGenModelAction implements IObjectActionDelegate
{
  /**
   * @since 1.0
   */
  protected IResource selectedElement;

  public final static String dawngenmodelFileExtension = "dawngenmodel";

  /**
   * @since 1.0
   */
  protected final String generalPrefix = "Dawn";

  public void setActivePart(IAction action, IWorkbenchPart targetPart)
  {
  }

  public void run(IAction action)
  {
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    try
    {
      window.run(true, true, new IRunnableWithProgress()
      {
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
        {
          monitor.beginTask("Generate Dawn Code", 1000);
          IFile genFile = (IFile)selectedElement;

          String genModelFile = genFile.getRawLocationURI().toString();
          String path = genModelFile.replace(genFile.getName(), "");

          ResourceSet resourceSet = createResourceSet();
          String modelname = "default";
          modelname = getModelName(genFile);

          Resource dawnGenModelResource = getResource(path, modelname, resourceSet, dawngenmodelFileExtension);
          Resource dawnFragmentModelResource = getDawnFragmentModelResource(path, modelname, resourceSet);

          DawnGenerator dawnGenerator = getDawngenerator(dawnGenModelResource);

          DawnFragmentGenerator fragmentGenerator = getDawnFragmentGenerator(genFile, resourceSet);
          fragmentGenerator.setDawnGenerator(dawnGenerator);

          dawnGenModelResource.getContents().add(dawnGenerator);
          dawnFragmentModelResource.getContents().add(fragmentGenerator);

          try
          {
            dawnGenModelResource.save(Collections.EMPTY_MAP);
            dawnFragmentModelResource.save(Collections.EMPTY_MAP);
          }
          catch (IOException ex)
          {
            throw new RuntimeException(ex);
          }

          ProjectCreationHelper.refreshProject(null, monitor);
        }

        private DawnGenerator getDawngenerator(Resource dawnGenModelResource)
        {
          DawnGenerator dawnGenerator;

          if (dawnGenModelResource.getContents().size() != 0)
          {
            dawnGenerator = (DawnGenerator)dawnGenModelResource.getContents().get(0);
          }
          else
          {
            dawnGenerator = DawngenmodelFactory.eINSTANCE.createDawnGenerator();
          }
          return dawnGenerator;
        }

      });
    }
    catch (InvocationTargetException ex)
    {
      throw new RuntimeException(ex);
    }
    catch (InterruptedException ex)
    {
      throw new RuntimeException(ex);
    }
  }

  /**
   * @since 1.0
   */
  protected abstract Resource getDawnFragmentModelResource(String path, String modelname, ResourceSet resourceSet);

  /**
   * @since 1.0
   */
  protected abstract DawnFragmentGenerator getDawnFragmentGenerator(IFile genFile, ResourceSet resourceSet);

  private String getModelName(IFile genFile)
  {
    int lastIndexOf = genFile.getName().lastIndexOf(".");
    return genFile.getName().substring(0, lastIndexOf);
  }

  /**
   * @since 1.0
   */
  protected Resource getResource(String path, String modelname, ResourceSet resourceSet, String extension)
  {
    String resourcePath = path + "" + modelname + "." + extension;
    URI uri = URI.createURI(resourcePath);
    Resource resource = null;
    try
    {
      resource = resourceSet.getResource(uri, true);
    }
    catch (Exception ignore)
    {
      // ignore
    }

    if (resource == null)
    {
      resource = resourceSet.createResource(uri);
    }
    return resource;
  }

  /**
   * @since 1.0
   */
  protected ResourceSet createResourceSet()
  {
    ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(dawngenmodelFileExtension,
        new XMIResourceFactoryImpl());
    return resourceSet;
  }

  public void selectionChanged(IAction action, ISelection selection)
  {
    if (selection instanceof IStructuredSelection)
    {
      Object sel = ((IStructuredSelection)selection).getFirstElement();
      if (sel instanceof IResource)
      {
        selectedElement = (IResource)sel;
      }
    }
  }
}

Back to the top