Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b3a1405aaadd4324f3a1b21f2b82be7e194adf36 (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
/*****************************************************************************
 * Copyright (c) 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
 *
 *****************************************************************************/

package org.eclipse.papyrus.infra.nattable.tree;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.RollbackException;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.papyrus.infra.emf.gmf.util.GMFUnsafe;
import org.eclipse.papyrus.infra.nattable.Activator;
import org.eclipse.papyrus.infra.nattable.manager.axis.ITreeItemAxisManagerForEventList;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.EObjectTreeItemAxis;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.ITreeItemAxis;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.NattableaxisFactory;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxisconfiguration.AxisManagerRepresentation;

/**
 * @author VL222926
 *
 */
// TODO : refactore me to use IAxisFactory ?
public class ITreeItemAxisHelper {

	private ITreeItemAxisHelper() {
		// to prevent instanciation
	}

	/**
	 *
	 * @param editingDomain
	 *            the editing domain to use to create the ITreeItemAxis
	 * @param parentAxis
	 *            the parent axis to use for the created ITreeItemAxis
	 * @param child
	 *            the object to wrap in the created ITreeItemAxis
	 * @param axisRepresentation
	 *            the axis representation of the axis manager providing its ITreeItemAxis
	 * @return
	 *         the created {@link ITreeItemAxis}
	 */
	public static ITreeItemAxis createITreeItemAxis(final TransactionalEditingDomain editingDomain, final ITreeItemAxis parentAxis, final Object child, final AxisManagerRepresentation axisRepresentation) {
		final ITreeItemAxis axis;
		if (child instanceof EObject) {
			axis = NattableaxisFactory.eINSTANCE.createEObjectTreeItemAxis();
			((EObjectTreeItemAxis) axis).setElement((EObject) child);
		} else {
			axis = null;
			throw new UnsupportedOperationException("The only managed case is EObject, others cases are not yet managed"); //$NON-NLS-1$
		}

		try {
			if(null != editingDomain){
				GMFUnsafe.write(editingDomain, new Runnable() {
	
					@Override
					public void run() {
						if (parentAxis != null) {
							axis.setParent(parentAxis);
						}
						axis.setManager(axisRepresentation);
					}
				});
			}else{
				if (parentAxis != null) {
					axis.setParent(parentAxis);
				}
				axis.setManager(axisRepresentation);
			}
		} catch (InterruptedException e) {
			Activator.log.error(e);
		} catch (RollbackException e) {
			Activator.log.error(e);
		}
		return axis;
	}

	/**
	 * This method to destroy an {@link ITreeItemAxis} from the model
	 *
	 * @param domain
	 *            the editing domain to use to drestoy the {@link ITreeItemAxis}
	 * @param axisToDestroy
	 *            the {@link ITreeItemAxis} to destroy
	 */
	public static void destroyITreeItemAxis(final TransactionalEditingDomain domain, final ITreeItemAxis axisToDestroy) {
		try {
			GMFUnsafe.write(domain, new Runnable() {

				@Override
				public void run() {
					axisToDestroy.setParent(null);
				}
			});
		} catch (InterruptedException e) {
			Activator.log.error(e);
		} catch (RollbackException e) {
			Activator.log.error(e);
		}
	}

	/**
	 * Does the linkage between semantic element and IAxis inside a map used by an {@link ITreeItemAxisManagerForEventList}
	 *
	 * @param managedElements
	 *            a map owning elements managed by an {@link ITreeItemAxisManagerForEventListt} : keys are semantic element and values are {@link ITreeItemAxis} representation for the semantic element
	 * @param axis
	 *            the axis
	 */
	public static final void linkITreeItemAxisToSemanticElement(final Map<Object, Set<ITreeItemAxis>> managedElements, final ITreeItemAxis axis) {
		final Object object = axis.getElement();
		Set<ITreeItemAxis> value = managedElements.get(object);
		if (value == null) {
			value = new HashSet<ITreeItemAxis>();
			managedElements.put(object, value);
		}
		value.add(axis);
	}

	/**
	 * Undoes the linkage between semantic element and IAxis inside a map used by an {@link ITreeItemAxisManagerForEventList}
	 *
	 * @param managedElements
	 *            a map owning elements managed by an {@link ITreeItemAxisManagerForEventListt} : keys are semantic element and values are {@link ITreeItemAxis} representation for the semantic element
	 * @param axis
	 *            the axis
	 */
	public static final void unlinkITreeItemAxisToSemanticElement(final Map<Object, Set<ITreeItemAxis>> managedElements, final ITreeItemAxis axis) {
		final Object element = axis.getElement();
		Set<ITreeItemAxis> values = managedElements.get(element);
		if (values != null) {
			values.remove(axis);
		}
		if (values == null || values.size() == 0) {
			managedElements.remove(element);
		}
	}
}

Back to the top