Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 840428a31c29f331bd12c6bc2957d7f8621d8319 (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
/*****************************************************************************
 * Copyright (c) 2013, 2014 CEA LIST 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:
 *   CEA LIST - Initial API and implementation
 *   Christian W. Damus (CEA) - bug 432813
 *   
 *****************************************************************************/
package org.eclipse.papyrus.cdo.core.resource;

import java.util.Collections;
import java.util.Set;

import org.eclipse.core.commands.operations.IOperationHistory;
import org.eclipse.core.commands.operations.IUndoContext;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.cdo.internal.core.l10n.Messages;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;


/**
 * A CDO-specific undo context that records at the level of objects the scope of what is affected by an operation.
 */
public final class CDOUndoContext implements IUndoContext {

	private static CDOUndoContext ALL_CDO = new CDOUndoContext();

	private final Set<EObject> affectedObjects;

	private String cachedLabel;

	/**
	 * Initializes me with a non-empty aggregate of affected objects.
	 * 
	 * @param affectedObjects
	 *        the affected objects to record
	 * 
	 * @throws IllegalArgumentException
	 *         if the {@code affectedObjects} is empty
	 */
	public CDOUndoContext(Iterable<? extends EObject> affectedObjects) {
		super();

		if(Iterables.isEmpty(affectedObjects)) {
			throw new IllegalArgumentException("affectedObjects is empty"); //$NON-NLS-1$
		}

		this.affectedObjects = ImmutableSet.copyOf(affectedObjects);
	}

	private CDOUndoContext() {
		super();

		this.affectedObjects = Collections.<EObject> emptySet();
	}

	/**
	 * Flushes all {@code CDOUndoContext}s from the specified operation {@code history}.
	 * 
	 * @param history
	 *        a history to flush
	 */
	public static void flushAll(IOperationHistory history) {
		history.dispose(ALL_CDO, true, true, true);
	}

	/**
	 * Obtains an immutable set of the objects affected by the operation.
	 * 
	 * @return my affected objects. This set is immutable
	 */
	public Set<EObject> getAffectedObjects() {
		return affectedObjects;
	}

	@Override
	public boolean matches(IUndoContext context) {
		return (context == ALL_CDO) || (context instanceof CDOUndoContext) && !Sets.intersection(getAffectedObjects(), ((CDOUndoContext)context).getAffectedObjects()).isEmpty();
	}

	@Override
	public String getLabel() {
		if(cachedLabel == null) {
			StringBuilder buf = new StringBuilder();
			Joiner.on(", ").appendTo(buf, Iterables.limit(affectedObjects, 3)); //$NON-NLS-1$
			if(affectedObjects.size() > 3) {
				buf.append(", ..."); //$NON-NLS-1$
			}

			cachedLabel = NLS.bind(Messages.CDOUndoContext_0, buf);
		}

		return cachedLabel;
	}

	@Override
	public String toString() {
		return getLabel();
	}
}

Back to the top