Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d63b3d95c2174ad9f8b6fca17867d14ee7bf5769 (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
/*
 * Copyright (c) 2011, 2012, 2015 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.creators.impl;

import org.eclipse.emf.cdo.dawn.codegen.actions.GenerateDawnGenModelAction;
import org.eclipse.emf.cdo.dawn.codegen.creators.Creator;
import org.eclipse.emf.cdo.dawn.codegen.dawngenmodel.DawnFragmentGenerator;
import org.eclipse.emf.cdo.dawn.codegen.util.ProjectCreationHelper;
import org.eclipse.emf.cdo.dawn.codegen.util.Utils;

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.emf.mwe.core.WorkflowEngine;
import org.eclipse.emf.mwe.core.monitor.NullProgressMonitor;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.JavaCore;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Martin Fluegge
 * @since 1.0
 */
public abstract class AbstractFragmentCreator implements Creator
{
  protected final IResource selectedElement;

  protected final String PLUGIN_NATURE = "org.eclipse.pde.PluginNature";

  protected final String JAVA_NATURE = JavaCore.NATURE_ID;

  public AbstractFragmentCreator(IResource selectedElement)
  {
    this.selectedElement = selectedElement;
  }

  public void create(IProgressMonitor monitor)
  {
    IFile dawnGenModelFile = (IFile)selectedElement;

    ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(GenerateDawnGenModelAction.dawngenmodelFileExtension, new XMIResourceFactoryImpl());

    Resource dawnGenModelResource = resourceSet.getResource(URI.createURI(dawnGenModelFile.getRawLocationURI().toString()), true);

    DawnFragmentGenerator dawnFragmentGenerator = (DawnFragmentGenerator)dawnGenModelResource.getContents().get(0);
    // DawnGenerator dawnGenerator = dawnFragmentGenerator.getDawnGenerator();

    if (dawnFragmentGenerator != null)
    {
      monitor.subTask("Create client fragment's basic structure");

      // String[] natures = new String[] { JavaCore.NATURE_ID, PDE.PLUGIN_NATURE };
      String[] natures = new String[] { JAVA_NATURE, PLUGIN_NATURE };

      ProjectCreationHelper projectCreationHelper = new ProjectCreationHelper();
      projectCreationHelper.setName(dawnFragmentGenerator.getFragmentName());
      projectCreationHelper.setNatures(natures);
      IProject project;
      try
      {
        project = projectCreationHelper.createProject();
        projectCreationHelper.createJavaProject(project);

        monitor.worked(100);

        monitor.subTask("Creating folder structure...");
        projectCreationHelper.createFolder("lib", project);
        projectCreationHelper.createFolder("META-INF", project);
        projectCreationHelper.createFolder("icons", project);

        monitor.worked(100);

        monitor.subTask("Creating generic content...");
        createGenericContent(monitor, project.getLocation());
        ProjectCreationHelper.refreshProject(project, monitor);
      }
      catch (CoreException ex)
      {
        throw new RuntimeException(ex);
      }
      catch (InvocationTargetException ex)
      {
        throw new RuntimeException(ex);
      }
      catch (InterruptedException ex)
      {
        throw new RuntimeException(ex);
      }
    }
    else
    {
      throw new RuntimeException("Could not find DawnFragmentGenerator for " + selectedElement);
    }

    monitor.worked(100);
  }

  protected abstract URL getWorkflowURL();

  protected void createGenericContent(IProgressMonitor monitor, IPath location)
  {
    IFile file = (IFile)selectedElement;
    Map<String, String> properties = new HashMap<String, String>();

    Map<String, ?> slotMap = new HashMap<String, Object>();
    try
    {
      String dawnGenFile = file.getRawLocationURI().toString();// ;file.getLocation().toFile().getAbsoluteFile().toURI().toURL().toString();

      IFile dawnGen = (IFile)selectedElement;
      IProject hostProject = dawnGen.getProject();

      Utils.setPackage(hostProject.getName());

      String ouputFolder = location.toFile().getAbsoluteFile().toString();// + "/" + hostProject.getName()+".diagram";

      properties.put("model", dawnGenFile);
      properties.put("src-gen", ouputFolder);

      URL workFlowURL = getWorkflowURL();
      // WorkflowRunner workflowRunner = new WorkflowRunner();
      String workflow = FileLocator.toFileURL(workFlowURL).getFile();

      // workflowRunner.run(workflow, , properties, slotMap);

      new WorkflowEngine().run(workflow, new NullProgressMonitor(), properties, slotMap);
    }
    catch (IOException ex)
    {
      throw new RuntimeException(ex);
    }
  }
}

Back to the top