Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 16f36eb73bc663c50f2133715d2dd57bdef4a3bc (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
/*
 * Copyright (c) 2011-2013, 2015 Eike Stepper (Loehne, 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 399285 support IDawnEditor adapters
 */
package org.eclipse.emf.cdo.dawn.gmf.editors.impl;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.dawn.appearance.DawnElementStylizer;
import org.eclipse.emf.cdo.dawn.editors.IDawnEditor;
import org.eclipse.emf.cdo.dawn.editors.impl.DawnAbstractEditorSupport;
import org.eclipse.emf.cdo.dawn.gmf.appearance.DawnAppearancer;
import org.eclipse.emf.cdo.dawn.gmf.notifications.impl.DawnGMFHandler;
import org.eclipse.emf.cdo.dawn.gmf.notifications.impl.DawnGMFLockingHandler;
import org.eclipse.emf.cdo.dawn.gmf.synchronize.DawnChangeHelper;
import org.eclipse.emf.cdo.dawn.gmf.util.DawnDiagramUpdater;
import org.eclipse.emf.cdo.dawn.notifications.BasicDawnListener;
import org.eclipse.emf.cdo.dawn.spi.DawnState;
import org.eclipse.emf.cdo.dawn.ui.stylizer.DawnElementStylizerRegistry;
import org.eclipse.emf.cdo.transaction.CDOTransactionHandlerBase;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.view.CDOView;

import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor;
import org.eclipse.gmf.runtime.notation.View;

import java.util.Map;

/**
 * @author Martin Fluegge
 */
public class DawnGMFEditorSupport extends DawnAbstractEditorSupport
{
  private DawnGMFHandler dawnGMFHandler;

  public DawnGMFEditorSupport(IDawnEditor editor)
  {
    super(editor);
    dawnGMFHandler = createDawnGMFHandler(editor);
  }

  public void close()
  {
    CDOView view = getView();
    if (view != null && !view.isClosed())
    {
      view.close();
    }
  }

  @Override
  protected BasicDawnListener getBasicHandler()
  {
    return dawnGMFHandler;
  }

  /**
   * Creates a GMF-specific handler for the specified {@code editor}.
   * <p>
   * <strong>Note</strong> that this method is called in the constructor, so subclasses must
   * be aware that {@code this} has not been fully initialized.
   *
   * @since 2.1
   */
  protected DawnGMFHandler createDawnGMFHandler(IDawnEditor editor)
  {
    return new DawnGMFHandler(editor);
  }

  @Override
  protected BasicDawnListener getLockingHandler()
  {
    return new DawnGMFLockingHandler(getEditor());
  }

  @Override
  protected CDOTransactionHandlerBase getTransactionHandler()
  {
    return dawnGMFHandler;
  }

  /**
   * @since 1.0
   */
  @Override
  public void rollback()
  {
    super.rollback();
    final DiagramDocumentEditor diagramDocumentEditor = getDiagramEditor(getEditor());
    TransactionalEditingDomain editingDomain = diagramDocumentEditor.getEditingDomain();
    CommandStack commandStack = editingDomain.getCommandStack();
    commandStack.execute(new RecordingCommand(editingDomain)
    {
      @Override
      public void doExecute()
      {
        DawnAppearancer.setEditPartDefaultAllChildren(diagramDocumentEditor.getDiagramEditPart());
        DawnDiagramUpdater.refreshEditPart(diagramDocumentEditor.getDiagramEditPart());
      }
    });
  }

  /**
   * Obtains the GMF diagram editor from the given Dawn {@code editor}.  If the
   * {@code editor} is a {@link DiagramDocumentEditor}, then that is returned as
   * is.  Otherwise, try to get an {@code DiagramDocumentEditor}
   * {@linkplain IAdaptable#getAdapter(Class) adapter} from the {@code editor}.
   *
   * @param editor the Dawn editor from which to get the GMF editor
   * @since 2.1
   */
  public static DiagramDocumentEditor getDiagramEditor(IDawnEditor editor)
  {
    if (editor instanceof DiagramDocumentEditor)
    {
      return (DiagramDocumentEditor)editor;
    }

    return editor.getAdapter(DiagramDocumentEditor.class);
  }

  public void refresh()
  {
    final DiagramDocumentEditor diagramDocumentEditor = getDiagramEditor(getEditor());
    TransactionalEditingDomain editingDomain = diagramDocumentEditor.getEditingDomain();
    CommandStack commandStack = editingDomain.getCommandStack();
    commandStack.execute(new RecordingCommand(editingDomain)
    {
      @Override
      public void doExecute()
      {
        DawnDiagramUpdater.refreshEditPart(diagramDocumentEditor.getDiagramEditPart());
      }
    });
  }

  public void lockObject(Object objectToBeLocked)
  {
    if (objectToBeLocked instanceof EditPart)
    {
      EditPart editPart = (EditPart)objectToBeLocked;
      Object model = editPart.getModel();

      if (model instanceof EObject)
      {
        CDOUtil.getCDOObject((EObject)model).cdoWriteLock().lock();
        if (model instanceof View)
        {
          EObject element = ((View)model).getElement();
          CDOUtil.getCDOObject(element).cdoWriteLock().lock();
        }
      }
      DawnElementStylizer stylizer = DawnElementStylizerRegistry.instance.getStylizer(editPart);
      if (stylizer != null)
      {
        stylizer.setLocked(editPart, DawnAppearancer.TYPE_LOCKED_LOCALLY);
      }
    }
    refresh();
  }

  public void unlockObject(Object objectToBeUnlocked)
  {
    if (objectToBeUnlocked instanceof EditPart)
    {
      EditPart editPart = (EditPart)objectToBeUnlocked;

      Object model = editPart.getModel();

      if (model instanceof EObject)
      {
        CDOUtil.getCDOObject((EObject)model).cdoWriteLock().unlock();
        if (model instanceof View)
        {
          EObject element = ((View)model).getElement();
          CDOUtil.getCDOObject(element).cdoWriteLock().unlock();
        }
      }
      DawnElementStylizer stylizer = DawnElementStylizerRegistry.instance.getStylizer(editPart);
      if (stylizer != null)
      {
        stylizer.setDefault(editPart);
      }
    }
    refresh();
  }

  public void handleRemoteLockChanges(Map<Object, DawnState> changedObjects)
  {
    for (Object o : changedObjects.keySet())
    {
      handleLock((CDOObject)o, getView());
    }
    refresh();
  }

  private void handleLock(CDOObject object, CDOView cdoView)
  {
    EObject element = CDOUtil.getEObject(object); // either semantic object or notational
    View view = DawnDiagramUpdater.findView(element);
    if (view != null)
    {
      // if there is no view, the semantic object is not displayed.
      EditPart editPart = DawnDiagramUpdater.createOrFindEditPartIfViewExists(view, getDiagramEditor(getEditor()));

      if (object.cdoWriteLock().isLocked())
      {
        DawnAppearancer.setEditPartLocked(editPart, DawnAppearancer.TYPE_LOCKED_LOCALLY);
      }
      else if (object.cdoWriteLock().isLockedByOthers())
      {
        DawnAppearancer.setEditPartLocked(editPart, DawnAppearancer.TYPE_LOCKED_GLOBALLY);
        DawnChangeHelper.deactivateEditPart(editPart);
      }
      else
      {
        DawnAppearancer.setEditPartDefault(editPart);
        DawnChangeHelper.activateEditPart(editPart);
      }
    }
  }
}

Back to the top