comparison org.eclipse.jface/src/org/eclipse/jface/viewers/deferred/SetModel.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2004, 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.jface.viewers.deferred.SetModel;
14
15 import org.eclipse.jface.viewers.deferred.AbstractConcurrentModel;
16 import org.eclipse.jface.viewers.deferred.IConcurrentModelListener;
17
18
19 import org.eclipse.core.runtime.Assert;
20
21 import java.lang.all;
22 import java.util.Collection;
23 import java.util.Set;
24 import java.util.HashSet;
25
26 /**
27 * Trivial implementation of an <code>IConcurrentModel</code>. Implements
28 * an unordered set of elements that fires off change notifications whenever
29 * elements are added or removed from the set. All notifications are sent
30 * synchronously.
31 *
32 * @since 3.1
33 */
34 public class SetModel : AbstractConcurrentModel {
35
36 private HashSet data;
37
38 public this(){
39 data = new HashSet();
40 }
41
42 /**
43 * Return the contents of the model.
44 * @return the array of elements
45 *
46 */
47 public Object[] getElements() {
48 return data.toArray();
49 }
50
51 /**
52 * Sets the contents to the given array of elements
53 *
54 * @param newContents new contents of this set
55 */
56 public void set(Object[] newContents) {
57 // Assert.isNotNull(newContents);
58 data.clear();
59 for (int i = 0; i < newContents.length; i++) {
60 Object object = newContents[i];
61
62 data.add(object);
63 }
64
65 IConcurrentModelListener[] listeners = getListeners();
66 for (int i = 0; i < listeners.length; i++) {
67 IConcurrentModelListener listener = listeners[i];
68 listener.setContents(newContents);
69 }
70 }
71
72 /**
73 * Empties the set
74 */
75 public void clear() {
76 Object[] removed = data.toArray();
77 data.clear();
78 fireRemove(removed);
79 }
80
81 /**
82 * Adds the given elements to the set
83 *
84 * @param toAdd elements to add
85 */
86 public void addAll(Object[] toAdd) {
87 // Assert.isNotNull(toAdd);
88 for (int i = 0; i < toAdd.length; i++) {
89 Object object = toAdd[i];
90
91 data.add(object);
92 }
93
94 fireAdd(toAdd);
95 }
96
97 /**
98 * Adds the given elements to the set. Duplicate elements are ignored.
99 *
100 * @param toAdd elements to add
101 */
102 public void addAll(Collection toAdd) {
103 Assert.isNotNull(cast(Object)toAdd);
104 addAll(toAdd.toArray());
105 }
106
107 /**
108 * Fires a change notification for all elements in the given array
109 *
110 * @param changed array of elements that have changed
111 */
112 public void changeAll(Object[] changed) {
113 // Assert.isNotNull(changed);
114 fireUpdate(changed);
115 }
116
117 /**
118 * Removes all of the given elements from the set.
119 *
120 * @param toRemove elements to remove
121 */
122 public void removeAll(Object[] toRemove) {
123 // Assert.isNotNull(toRemove);
124 for (int i = 0; i < toRemove.length; i++) {
125 Object object = toRemove[i];
126
127 data.remove(object);
128 }
129
130 fireRemove(toRemove);
131 }
132
133 /* (non-Javadoc)
134 * @see org.eclipse.jface.viewers.deferred.IConcurrentModel#requestUpdate(org.eclipse.jface.viewers.deferred.IConcurrentModelListener)
135 */
136 public void requestUpdate(IConcurrentModelListener listener) {
137 Assert.isNotNull(cast(Object)listener);
138 listener.setContents(getElements());
139 }
140 }