Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0d6bf019c3f4fcf59c8badc12673beb970df69c7 (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
/**
 * Copyright (c) 2004 - 2010 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.DawnEMFGenerator;
import org.eclipse.emf.cdo.dawn.codegen.dawngenmodel.DawnGenerator;
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.WorkflowRunner;
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 class EMFFragmentCreator implements Creator
{
  private final IResource selectedElement;

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

  private final String JAVA_NATURE = JavaCore.NATURE_ID;

  public EMFFragmentCreator(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);

    DawnGenerator dawnGenerator = (DawnGenerator)dawnGenModelResource.getContents().get(0);
    DawnEMFGenerator dawnEMFGenerator = dawnGenerator.getEmfFragmentgenerator();

    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(dawnEMFGenerator.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 e1)
    {
      e1.printStackTrace();
    }
    catch (InvocationTargetException e)
    {
      e.printStackTrace();
    }
    catch (InterruptedException e)
    {
      e.printStackTrace();
    }

    monitor.done();
  }

  private 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 = this.getClass().getClassLoader().getResource("/workflow/emfFragmentGenerator.oaw");
      WorkflowRunner workflowRunner = new WorkflowRunner();
      String workflow = FileLocator.toFileURL(workFlowURL).getFile();

      workflowRunner.run(workflow, new NullProgressMonitor(), properties, slotMap);
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }
}

Back to the top