Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 860ff80b5f7f3859a2b47dadad83d40a4584dd08 (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
/*****************************************************************************
 * 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.utils;

import org.eclipse.papyrus.infra.nattable.Activator;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.IAxis;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.ITreeItemAxis;

import ca.odell.glazedlists.EventList;

/**
 * @author Vincent Lorenzo
 *         This class provides methods to manipulate properly EventList calling appropriate lock and unlock methods
 */
public class EventListHelper {

	private EventListHelper() {
		// to prevent instanciation
	}

	/**
	 *
	 * @param list
	 *            an event list to edit
	 * @param object
	 *            an object to add to the event list
	 */
	public static final void addToEventList(final EventList<Object> list, final Object object) {
		list.getReadWriteLock().readLock().lock();
		try {
			if (list.contains(object)) {
				return;
			}
		} catch (Exception e1) {
			Activator.log.error(e1);
		} finally {
			list.getReadWriteLock().readLock().unlock();
		}
		int parentIndex = -1;
		// 1. get the index to add the element
		if (object instanceof ITreeItemAxis) {
			IAxis parent = ((ITreeItemAxis) object).getParent();
			if (parent != null) {
				try {
					list.getReadWriteLock().readLock().lock();
					parentIndex = list.indexOf(parent);
				} catch (Exception e) {
					Activator.log.error(e);
				} finally {
					list.getReadWriteLock().readLock().unlock();
				}
			}
		}


		list.getReadWriteLock().writeLock().lock();
		try {
			if (parentIndex != -1) {
				list.add(parentIndex + 1, object);
			} else {
				list.add(object);
			}
		} catch (Exception e) {
			Activator.log.error(e);
		} finally {
			list.getReadWriteLock().writeLock().unlock();
		}
	}

	/**
	 *
	 * @param list
	 *            an event list to edit
	 * @param object
	 *            an object to remove from the list
	 */
	public static final void removeFromEventList(final EventList<Object> list, final Object object) {
		list.getReadWriteLock().readLock().lock();
		try {
			if (!list.contains(object)) {
				return;
			}
		} catch (Exception e1) {
			Activator.log.error(e1);
		} finally {
			list.getReadWriteLock().readLock().unlock();
		}

		list.getReadWriteLock().writeLock().lock();
		try {

			if (object instanceof ITreeItemAxis) {
				list.remove(object);
			} else {
				list.remove(object);
			}
		} catch (Exception e) {
			Activator.log.error(e);
		} finally {
			list.getReadWriteLock().writeLock().unlock();
		}
	}
}

Back to the top