Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1a0341ebb4e5eb5280c1da5f926ecf7b05fad949 (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
/*
 * 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.notifications;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.lock.CDOLockState;
import org.eclipse.emf.cdo.common.revision.CDOIDAndBranch;
import org.eclipse.emf.cdo.dawn.editors.IDawnEditor;
import org.eclipse.emf.cdo.dawn.editors.IDawnEditorSupport;
import org.eclipse.emf.cdo.dawn.spi.DawnState;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.cdo.view.CDOViewLocksChangedEvent;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Martin Fluegge
 * @since 2.0
 */
public class BasicDawnLockingHandler extends BasicDawnListener
{
  public BasicDawnLockingHandler(IDawnEditor editor)
  {
    super(editor);
  }

  @Override
  public void handleLocksChangedEvent(CDOViewLocksChangedEvent event)
  {
    CDOViewLocksChangedEvent lockEvent = event;

    CDOLockState[] lockStates = lockEvent.getLockStates();

    Map<Object, DawnState> changedObjects = new HashMap<Object, DawnState>();

    for (CDOLockState state : lockStates)
    {
      Object lockedObject = state.getLockedObject();

      CDOView view = editor.getDawnEditorSupport().getView();
      CDOID id;
      if (lockedObject instanceof CDOID)
      {
        id = (CDOID)lockedObject;
      }
      else if (lockedObject instanceof CDOIDAndBranch)
      {
        id = ((CDOIDAndBranch)lockedObject).getID();
      }
      else
      {
        throw new RuntimeException("Unexpected object type: " + lockedObject);
      }

      if (id != null)
      {
        CDOObject object = view.getObject(id);
        if (object.cdoWriteLock().isLockedByOthers())
        {
          changedObjects.put(object, DawnState.LOCKED_REMOTELY);
        }
        else
        {
          changedObjects.put(object, DawnState.CLEAN);
        }
      }
    }

    IDawnEditorSupport dawnEditorSupport = editor.getDawnEditorSupport();
    dawnEditorSupport.handleRemoteLockChanges(changedObjects);
  }
}

Back to the top