Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5fa463fbb233da660f8f370bb04abef29ca53ece (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
/*
 * Copyright (c) 2004 - 2011 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:
 *    Victor Roldan Betancort - initial API and implementation
 */
package org.eclipse.emf.cdo.util;

import org.eclipse.emf.cdo.CDONotification;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.eresource.EresourcePackage;
import org.eclipse.emf.cdo.transaction.CDOCommitContext;
import org.eclipse.emf.cdo.transaction.CDODefaultTransactionHandler;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.transaction.CDOTransactionHandler;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.spi.cdo.InternalCDOView;

/**
 * Maintains the {@link Resource#isModified() modified state} of a CDO {@link CDOResource resource} with the help of a
 * {@link CDOTransactionHandler transaction handler}.
 * 
 * @author Victor Roldan Betancort
 * @since 4.0
 */
public class CDOModificationTrackingAdapter extends CDOLazyContentAdapter
{
  private CDOResource container;

  public CDOModificationTrackingAdapter(CDOResource resource)
  {
    container = resource;
    CDOTransaction transaction = ((InternalCDOView)resource.cdoView()).toTransaction();
    transaction.addTransactionHandler(new CDODefaultTransactionHandler()
    {
      @Override
      public void committedTransaction(CDOTransaction transaction, CDOCommitContext commitContext)
      {
        container.setModified(false);
      }

      @Override
      public void rolledBackTransaction(CDOTransaction transaction)
      {
        if (!transaction.getLastSavepoint().wasDirty())
        {
          container.setModified(false);
        }
      }
    });
  }

  @Override
  public void notifyChanged(Notification notification)
  {
    if (notification.isTouch())
    {
      return;
    }

    if (notification instanceof CDONotification)
    {
      return;
    }

    // Listen to changes on Resources, only if its the Resource when this adapter is installed on
    Object notifier = notification.getNotifier();
    if (notifier == container)
    {
      // The only attribute that triggers modified = true is "contents". The rest are ignored.
      if (notification.getFeature() == EresourcePackage.Literals.CDO_RESOURCE__CONTENTS)
      {
        container.setModified(true);
      }
    }
    else if (!(notifier instanceof Resource))
    {
      container.setModified(true);
    }
  }
}

Back to the top