Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8183578200972656122f34065e7c1c8c48350df3 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*****************************************************************************
 * Copyright (c) 2008 CEA LIST.
 *
 *
 * 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:
 *  Chokri Mraidha (CEA LIST) Chokri.Mraidha@cea.fr - Initial API and implementation
 *  Patrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - modification
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.profile.ui.dialogs;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;


/**
 * The Class ElementList.
 *
 * @author Remi Schnekenburger
 */
public class ElementList {

	/**
	 * The elements.
	 */
	private ArrayList elements = new ArrayList();

	/**
	 * The change listeners.
	 */
	private Set changeListeners = new HashSet();

	/**
	 * Constructor.
	 */
	public ElementList() {
		this.initData();
	}

	/**
	 * initialize the list of elements.
	 */
	protected void initData() {

	}

	/**
	 * Return the collection of tasks.
	 *
	 * @return the elements
	 */
	public ArrayList getElements() {
		return elements;
	}

	/**
	 * Add a new element to the collection of selected elements.
	 *
	 * @param element
	 *            the element
	 */
	public void addElement(Object element) {
		elements.add(elements.size(), element);
		Iterator iterator = changeListeners.iterator();
		while (iterator.hasNext()) {
			((IListViewer) iterator.next()).addElement(element);
		}
	}


	/**
	 * Removes the element.
	 *
	 * @param element
	 *            the element
	 */
	public void removeElement(Object element) {
		elements.remove(element);
		Iterator iterator = changeListeners.iterator();
		while (iterator.hasNext()) {
			((IListViewer) iterator.next()).removeElement(element);
		}
	}

	/**
	 * Move element up.
	 *
	 * @param element
	 *            the element
	 */
	public void moveElementUp(Object element) {
		// remove element, then add it to Min(its last index -1, 0)
		int index = elements.indexOf(element);
		if (index > 0) {
			elements.remove(element);
			elements.add(index - 1, element);
		}

		Iterator iterator = changeListeners.iterator();
		while (iterator.hasNext()) {
			((IListViewer) iterator.next()).updateElement(element);
		}

	}

	/**
	 * Move element down.
	 *
	 * @param element
	 *            the element
	 */
	public void moveElementDown(Object element) {
		// remove element, then add it to Max(its last index +1, elements.size())
		int index = elements.indexOf(element);
		if (index >= 0 && index < elements.size() - 1) {
			elements.remove(element);
			elements.add(index + 1, element);
		}

		Iterator iterator = changeListeners.iterator();
		while (iterator.hasNext()) {
			((IListViewer) iterator.next()).updateElement(element);
		}
	}


	/**
	 * Element changed.
	 *
	 * @param element
	 *            the element
	 */
	public void elementChanged(Object element) {
		Iterator iterator = changeListeners.iterator();
		while (iterator.hasNext()) {
			((IListViewer) iterator.next()).updateElement(element);
		}
	}

	/**
	 * Contains.
	 *
	 * @param element
	 *            the element
	 *
	 * @return true, if contains
	 */
	public boolean contains(Object element) {
		return elements.contains(element);
	}

	/**
	 * Removes the change listener.
	 *
	 * @param viewer
	 *            the viewer
	 */
	public void removeChangeListener(IListViewer viewer) {
		changeListeners.remove(viewer);
	}

	/**
	 * Adds the change listener.
	 *
	 * @param viewer
	 *            the viewer
	 */
	public void addChangeListener(IListViewer viewer) {
		changeListeners.add(viewer);
	}

}

Back to the top