Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 77cc09d43021a24b9e0f19bfa7c2fc2816747eeb (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
/**
 * Copyright (c) 2011-2012 Eclipse contributors 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
 */
package org.eclipse.emf.test.ecore.xcore;


import org.eclipse.emf.codegen.ecore.genmodel.GenClass;
import org.eclipse.emf.codegen.ecore.genmodel.GenDataType;
import org.eclipse.emf.codegen.ecore.genmodel.GenFeature;
import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
import org.eclipse.emf.codegen.ecore.genmodel.GenModelPackage;
import org.eclipse.emf.codegen.ecore.genmodel.GenPackage;
import org.eclipse.emf.codegen.ecore.genmodel.util.GenModelSwitch;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.util.EcoreSwitch;
import org.eclipse.xtext.EcoreUtil2;


public class GenModelFormatter extends EObjectFormatter
{

  private static class EcoreTitleSwitch extends EcoreSwitch<String>
  {
    @Override
    public String caseENamedElement(ENamedElement object)
    {
      EPackage pkg = EcoreUtil2.getContainerOfType(object, EPackage.class);
      StringBuilder result = new StringBuilder();
      if (pkg != object)
      {
        result.append(pkg.getName());
        result.append("::");
      }
      result.append(object.getName());
      return result.toString();
    }

    @Override
    public String caseEStructuralFeature(EStructuralFeature object)
    {
      StringBuilder result = new StringBuilder();
      result.append(doSwitch(object.getEContainingClass()));
      result.append("::");
      result.append(object.getName());
      return result.toString();
    }
  }

  private static class GenModelTitleSwitch extends GenModelSwitch<String>
  {
    @Override
    public String caseGenClass(GenClass object)
    {
      return object.getQualifiedInterfaceName();
    }

    @Override
    public String caseGenDataType(GenDataType object)
    {
      return object.getQualifiedInstanceClassName();
    }

    @Override
    public String caseGenFeature(GenFeature object)
    {
      StringBuilder result = new StringBuilder();
      result.append(doSwitch(object.getGenClass()));
      result.append(".");
      result.append(object.getName());
      return result.toString();
    }

    @Override
    public String caseGenModel(GenModel object)
    {
      return object.getModelName();
    }

    @Override
    public String caseGenPackage(GenPackage object)
    {
      return object.getQualifiedPackageInterfaceName();
    }

  }

  @Override
  protected String formatCrossRefValue(EObject object, EReference feature, EObject value)
  {
    if (value != null && !value.eIsProxy())
    {
      String title = null;
      if (value.eClass().getEPackage() == EcorePackage.eINSTANCE)
        title = new EcoreTitleSwitch().doSwitch(value);
      else if (value.eClass().getEPackage() == GenModelPackage.eINSTANCE)
        title = new GenModelTitleSwitch().doSwitch(value);
      if (title != null)
        return value.eClass().getName() + "  " + title;
    }
    return super.formatCrossRefValue(object, feature, value);
  }
}

Back to the top