comparison dwtx/jface/viewers/deferred/SetModel.d @ 10:b6c35faf97c8

Viewers
author Frank Benoit <benoit@tionex.de>
date Mon, 31 Mar 2008 00:47:19 +0200
parents
children 04b47443bb01
comparison
equal deleted inserted replaced
9:6c14e54dfc11 10:b6c35faf97c8
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 dwtx.jface.viewers.deferred.SetModel;
14
15 import dwtx.jface.viewers.deferred.AbstractConcurrentModel;
16 import dwtx.jface.viewers.deferred.IConcurrentModelListener;
17
18 import tango.util.collection.HashSet;
19 import tango.util.collection.model.View;
20
21 import dwtx.core.runtime.Assert;
22
23 import dwt.dwthelper.utils;
24
25 /**
26 * Trivial implementation of an <code>IConcurrentModel</code>. Implements
27 * an unordered set of elements that fires off change notifications whenever
28 * elements are added or removed from the set. All notifications are sent
29 * synchronously.
30 *
31 * @since 3.1
32 */
33 public class SetModel : AbstractConcurrentModel {
34
35 private HashSet!(Object) data;
36
37 public this(){
38 data = new HashSet!(Object);
39 }
40
41 /**
42 * Return the contents of the model.
43 * @return the array of elements
44 *
45 */
46 public Object[] getElements() {
47 return data.toArray();
48 }
49
50 /**
51 * Sets the contents to the given array of elements
52 *
53 * @param newContents new contents of this set
54 */
55 public void set(Object[] newContents) {
56 // Assert.isNotNull(newContents);
57 data.clear();
58 for (int i = 0; i < newContents.length; i++) {
59 Object object = newContents[i];
60
61 data.add(object);
62 }
63
64 IConcurrentModelListener[] listeners = getListeners();
65 foreach( listener; listeners ){
66 listener.setContents(newContents);
67 }
68 }
69
70 /**
71 * Empties the set
72 */
73 public void clear() {
74 Object[] removed = data.toArray();
75 data.clear();
76 fireRemove(removed);
77 }
78
79 /**
80 * Adds the given elements to the set
81 *
82 * @param toAdd elements to add
83 */
84 public void addAll(Object[] toAdd) {
85 // Assert.isNotNull(toAdd);
86 for (int i = 0; i < toAdd.length; i++) {
87 Object object = toAdd[i];
88
89 data.add(object);
90 }
91
92 fireAdd(toAdd);
93 }
94
95 /**
96 * Adds the given elements to the set. Duplicate elements are ignored.
97 *
98 * @param toAdd elements to add
99 */
100 public void addAll(View!(Object) toAdd) {
101 Assert.isNotNull(cast(Object)toAdd);
102 addAll(toAdd.toArray());
103 }
104
105 /**
106 * Fires a change notification for all elements in the given array
107 *
108 * @param changed array of elements that have changed
109 */
110 public void changeAll(Object[] changed) {
111 // Assert.isNotNull(changed);
112 fireUpdate(changed);
113 }
114
115 /**
116 * Removes all of the given elements from the set.
117 *
118 * @param toRemove elements to remove
119 */
120 public void removeAll(Object[] toRemove) {
121 // Assert.isNotNull(toRemove);
122 for (int i = 0; i < toRemove.length; i++) {
123 Object object = toRemove[i];
124
125 data.remove(object);
126 }
127
128 fireRemove(toRemove);
129 }
130
131 /* (non-Javadoc)
132 * @see dwtx.jface.viewers.deferred.IConcurrentModel#requestUpdate(dwtx.jface.viewers.deferred.IConcurrentModelListener)
133 */
134 public void requestUpdate(IConcurrentModelListener listener) {
135 Assert.isNotNull(cast(Object)listener);
136 listener.setContents(getElements());
137 }
138 }