Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: db5cce12a1b69067ee30f05431b25d330774a4ea (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
/*
 * 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.graphiti.util;

import org.eclipse.emf.cdo.dawn.preferences.PreferenceConstants;
import org.eclipse.emf.cdo.dawn.transaction.DawnTransactionalEditingDomainImpl;
import org.eclipse.emf.cdo.dawn.util.connection.CDOConnectionUtil;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.transaction.TransactionalCommandStack;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl;
import org.eclipse.emf.workspace.WorkspaceEditingDomainFactory;

import org.eclipse.core.commands.operations.DefaultOperationHistory;
import org.eclipse.gef.EditPart;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.services.Graphiti;

import java.util.Collections;
import java.util.List;

/**
 * @author Martin Fluegge
 */
public class DawnGraphitiUtil
{
  public static TransactionalEditingDomain createResourceSetAndEditingDomain()
  {
    // TODO check if this is still needed here
    CDOConnectionUtil.instance.init(PreferenceConstants.getRepositoryName(), PreferenceConstants.getProtocol(),
        PreferenceConstants.getServerName());
    CDOConnectionUtil.instance.getCurrentSession();

    final ResourceSet resourceSet = new ResourceSetImpl();

    @SuppressWarnings("restriction")
    final TransactionalCommandStack workspaceCommandStack = new org.eclipse.graphiti.ui.internal.editor.GFWorkspaceCommandStackImpl(
        new DefaultOperationHistory());

    final TransactionalEditingDomainImpl editingDomain = new DawnTransactionalEditingDomainImpl(
        new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE), workspaceCommandStack,
        resourceSet);
    WorkspaceEditingDomainFactory.INSTANCE.mapResourceSet(editingDomain);
    return editingDomain;
  }

  /**
   * This method tries to find an editpart for a given pictogram element. It recursivly searches all children for the
   * given editpart if the model matches to pictogramElement.
   */
  public static EditPart getEditpart(PictogramElement pictogramElement, EditPart part)
  {
    for (Object object : part.getChildren())
    {
      EditPart child = (EditPart)object;
      if (child.getModel().equals(pictogramElement))
      {
        return child;
      }

      EditPart childEditPart = getEditpart(pictogramElement, child);
      if (childEditPart != null)
      {
        return childEditPart;
      }
    }
    return null;
  }

  public static List<PictogramElement> getPictgramElements(Diagram diagram, EObject element)
  {
    PictogramElement pictgramElement = getPictgramElement(element);

    if (element instanceof PictogramElement)
    {
      return Collections.singletonList((PictogramElement)element);
    }

    if (pictgramElement != null)
    {
      return Collections.singletonList(pictgramElement);
    }

    return Graphiti.getLinkService().getPictogramElements(diagram, element);
  }

  /**
   * Tries to retriev the pictogram element from a given element. If the element itself is a PictogramElement, the
   * element will be returned. Otherwise all eContainers will be checked until a PictogramElement is found.
   */
  public static PictogramElement getPictgramElement(EObject element)
  {
    if (element == null)
    {
      return null;
    }

    if (element instanceof PictogramElement)
    {
      return (PictogramElement)element;
    }

    EObject eContainer = element.eContainer();

    if (eContainer instanceof PictogramElement)
    {
      return (PictogramElement)eContainer;
    }
    return getPictgramElement(eContainer);
  }
}

Back to the top