Skip to main content
summaryrefslogtreecommitdiffstats
blob: a78dd7e1932722503d3188b5729fac0b9d01129a (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * Copyright (c) 2004 - 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
 *     Christian W. Damus (CEA) - bug 399733: tolerate UML CacheAdapter
 */
package org.eclipse.emf.cdo.dawn.gmf.util;

import org.eclipse.emf.cdo.dawn.internal.util.bundle.OM;

import org.eclipse.net4j.util.om.trace.ContextTracer;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.ECrossReferenceAdapter;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.CanonicalEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor;
import org.eclipse.gmf.runtime.diagram.ui.services.editpart.EditPartService;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.PlatformUI;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * @author Martin Fluegge
 */
public class DawnDiagramUpdater
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, DawnDiagramUpdater.class);

  public static void refresh(final IGraphicalEditPart editPart)
  {
    TransactionalEditingDomain editingDomain = editPart.getEditingDomain();
    editingDomain.getCommandStack().execute(new RecordingCommand(editingDomain)
    {
      @Override
      public void doExecute()
      {
        editPart.refresh();

        if (editPart instanceof ConnectionEditPart)
        {
          DawnDiagramUpdater.refresh((IGraphicalEditPart)((ConnectionEditPart)editPart).getTarget());
          DawnDiagramUpdater.refresh((IGraphicalEditPart)((ConnectionEditPart)editPart).getSource());
        }
      }
    });
  }

  public static void refreshEditPart(EditPart editPart)
  {
    refeshEditpartInternal(editPart);
  }

  public static void refreshEditPart(final EditPart editPart, DiagramDocumentEditor editor)
  {
    try
    {
      editor.getEditingDomain().runExclusive(new Runnable()
      {
        public void run()
        {
          DawnDiagramUpdater.refreshEditPart(editPart);
        }
      });
    }
    catch (InterruptedException ex)
    {
      throw new RuntimeException(ex);
    }
  }

  public static void refreshEditCurrentSelected(TransactionalEditingDomain editingDomain)
  {
    editingDomain.getCommandStack().execute(new RecordingCommand(editingDomain)
    {

      @Override
      protected void doExecute()
      {
        // ((ExamplediagramDocumentProvider)getDocumentProvider()).changed(getEditorInput());
        ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService()
            .getSelection();
        if (selection instanceof IStructuredSelection)
        {
          IStructuredSelection structuredSelection = (IStructuredSelection)selection;
          if (structuredSelection.size() != 1)
          {
            return;
          }
          if (structuredSelection.getFirstElement() instanceof EditPart
              && ((EditPart)structuredSelection.getFirstElement()).getModel() instanceof View)
          {
            EObject modelElement = ((View)((EditPart)structuredSelection.getFirstElement()).getModel()).getElement();
            List<?> editPolicies = CanonicalEditPolicy.getRegisteredEditPolicies(modelElement);
            for (Iterator<?> it = editPolicies.iterator(); it.hasNext();)
            {
              CanonicalEditPolicy nextEditPolicy = (CanonicalEditPolicy)it.next();
              nextEditPolicy.refresh();
            }
          }
        }
      }
    });
  }

  private static void refeshEditpartInternal(EditPart editPart)
  {
    if (editPart != null)
    {
      try
      {

        // EObject modelElement = ((View)(editPart).getModel()).getElement();
        // List editPolicies = CanonicalEditPolicy.getRegisteredEditPolicies(modelElement);
        // for (Iterator it = editPolicies.iterator(); it.hasNext();)
        // {
        // CanonicalEditPolicy nextEditPolicy = (CanonicalEditPolicy)it.next();
        // nextEditPolicy.refresh();
        // }

        editPart.refresh();
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }

      if (editPart instanceof DiagramEditPart)
      {
        for (Object connectionEditPart : ((DiagramEditPart)editPart).getConnections())
        {
          if (connectionEditPart instanceof EditPart)
          {
            if (((Edge)((EditPart)connectionEditPart).getModel()).getBendpoints() != null)
            {
              refeshEditpartInternal((EditPart)connectionEditPart);
            }
          }
        }
      }

      for (Object childEditPart : editPart.getChildren())
      {
        if (childEditPart instanceof EditPart)
        {
          refeshEditpartInternal((EditPart)childEditPart);
        }
      }
    }
  }

  public static View findViewByContainer(EObject element)
  {
    if (element instanceof View)
    {
      return (View)element;
    }

    if (element.eContainer() == null)
    {
      return null;
    }

    if (element.eContainer() instanceof View)
    {
      return (View)element.eContainer();
    }

    return findViewByContainer(element.eContainer());
  }

  public static View findViewForModel(EObject object, DiagramDocumentEditor editor)
  {
    if (object == null)
    {
      return null;
    }
    for (EObject e : editor.getDiagram().eContents())
    {
      if (e instanceof View)
      {
        View view = (View)e;
        if (object.equals(view.getElement()))
        {
          if (TRACER.isEnabled())
          {
            TRACER.format("FOUND View: {0} for view obj: {1} ", e, object); //$NON-NLS-1$
          }
        }
        return (View)e;
      }
    }
    return null;
  }

  public static View getViewFromObject(EObject element, DiagramDocumentEditor editor)
  {
    View view;

    if (element instanceof Diagram)
    {
      view = ViewUtil.getViewContainer(element);
      return view;
    }

    if (element instanceof View)
    {
      view = (View)element;
    }
    else
    {
      view = DawnDiagramUpdater.findViewByContainer(element); // something which is not view (Edge or Node)
      if (view == null)
      {
        view = DawnDiagramUpdater.findViewForModel(element, editor);
      }
    }
    return view;
  }

  public static EditPart createOrFindEditPartIfViewExists(View view, DiagramDocumentEditor editor)
  {
    EditPart editPart = findEditPart(view, editor.getDiagramEditPart());

    if (view != null)
    {
      if (editPart == null) // does not exist?
      {
        if (TRACER.isEnabled())
        {
          TRACER.format("EditPart does not exist for view  {0} ", view); //$NON-NLS-1$
        }
        editPart = EditPartService.getInstance().createGraphicEditPart(view);
        setParentEditPart(editor, view, editPart);
      }
    }
    if (TRACER.isEnabled())
    {
      TRACER.format("Found EditPart:  {0} ", editPart); //$NON-NLS-1$
    }
    return editPart;
  }

  public static void setParentEditPart(final DiagramDocumentEditor editor, View view, EditPart editPart)
  {
    View viewParent = (View)view.eContainer();
    EditPart parentEditPart = findEditPart(viewParent, editor);
    if (parentEditPart == null)
    {
      parentEditPart = editor.getDiagramEditPart();
    }
    editPart.setParent(parentEditPart);
  }

  public static EditPart findEditPart(View view, DiagramDocumentEditor dawnDiagramEditor)
  {
    DiagramEditPart diagramEditPart = dawnDiagramEditor.getDiagramEditPart();

    for (Object e : diagramEditPart.getChildren())
    {
      EditPart ep = (EditPart)e;
      if (ep.getModel().equals(view))
      {
        return ep;
      }
    }

    for (Object e : diagramEditPart.getConnections())
    {
      EditPart ep = (EditPart)e;
      if (ep.getModel().equals(view))
      {
        return ep;
      }
    }
    return null;
  }

  public static EditPart findEditPart(View view, EditPart editPart)
  {
    EditPart ret;

    if (editPart.getModel().equals(view))
    {
      return editPart;
    }

    for (Object o : editPart.getChildren())
    {
      EditPart child = (EditPart)o;
      ret = findEditPart(view, child);
      if (ret != null)
      {
        return ret;
      }
    }

    if (editPart instanceof DiagramEditPart)
    {
      for (Object o : ((DiagramEditPart)editPart).getConnections())
      {
        EditPart child = (EditPart)o;
        ret = findEditPart(view, child);
        if (ret != null)
        {
          return ret;
        }
      }
    }
    return null;
  }

  public static EditPart findEditPart(View view, EditPartViewer viewer)
  {
    return (EditPart)viewer.getEditPartRegistry().get(view);
  }

  /**
   * In a normal GMF environment the diagram is loaded from a xml resource. In this scenario the XMLHelper sets "null"
   * to the element of edge. Thus the value is 'set' with a null value. We do not have this in our case because the
   * element is carefully loaded from the CDO repository. But if the value is not set a getElement() call fills the
   * element with the eContainer. See <b>org.eclipse.gmf.runtime.notation.impl.ViewImpl.isSetElement()</b>. This happens
   * when the ConnectionEditPart is activated and the model registered. See
   * <b>org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart.registerModel()</b>
   * <p>
   * In our scenario the Edges will be wrongly connected to the diagram itself, which makes the CanonicalEditingPolicy
   * to remove and restore the edge. Long, short story, we must 'touch' the elements here to have the element set.
   * 
   * @param diagram
   */
  public static void initializeElement(Diagram diagram)
  {
    for (Object obj : diagram.getEdges())
    {
      Edge edge = (Edge)obj;
      if (!edge.isSetElement())
      {
        boolean eDeliver = edge.eDeliver();
        edge.eSetDeliver(false);
        edge.setElement(null);
        edge.eSetDeliver(eDeliver);
      }
    }
  }

  public static View findViewFromCrossReferences(EObject element)
  {
    ECrossReferenceAdapter crossreferenceAdapter = ECrossReferenceAdapter.getCrossReferenceAdapter(element);
    if (crossreferenceAdapter != null)
    {
      Collection<EStructuralFeature.Setting> iinverseReferences = crossreferenceAdapter
          .getNonNavigableInverseReferences(element);

      for (EStructuralFeature.Setting f : iinverseReferences)
      {
        if (f.getEStructuralFeature() == NotationPackage.Literals.VIEW__ELEMENT)
        {
          return (View)f.getEObject();
        }
      }
    }
    return null;
  }

  public static View findView(EObject element)
  {
    View view = DawnDiagramUpdater.findViewByContainer(element);

    if (view == null)
    {
      view = DawnDiagramUpdater.findViewFromCrossReferences(element);
    }
    return view;
  }
}

Back to the top