KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > deferred > SetModel


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  *******************************************************************************/

11 package org.eclipse.jface.viewers.deferred;
12
13 import java.util.Collection JavaDoc;
14 import java.util.HashSet JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17
18
19 /**
20  * Trivial implementation of an <code>IConcurrentModel</code>. Implements
21  * an unordered set of elements that fires off change notifications whenever
22  * elements are added or removed from the set. All notifications are sent
23  * synchronously.
24  *
25  * @since 3.1
26  */

27 public class SetModel extends AbstractConcurrentModel {
28     
29     private HashSet JavaDoc data = new HashSet JavaDoc();
30     
31     /**
32      * Return the contents of the model.
33      * @return the array of elements
34      *
35      */

36     public Object JavaDoc[] getElements() {
37         return data.toArray();
38     }
39
40     /**
41      * Sets the contents to the given array of elements
42      *
43      * @param newContents new contents of this set
44      */

45     public void set(Object JavaDoc[] newContents) {
46         Assert.isNotNull(newContents);
47         data.clear();
48         for (int i = 0; i < newContents.length; i++) {
49             Object JavaDoc object = newContents[i];
50             
51             data.add(object);
52         }
53         
54         IConcurrentModelListener[] listeners = getListeners();
55         for (int i = 0; i < listeners.length; i++) {
56             IConcurrentModelListener listener = listeners[i];
57             
58             listener.setContents(newContents);
59         }
60     }
61
62     /**
63      * Empties the set
64      */

65     public void clear() {
66         Object JavaDoc[] removed = data.toArray();
67         data.clear();
68         fireRemove(removed);
69     }
70     
71     /**
72      * Adds the given elements to the set
73      *
74      * @param toAdd elements to add
75      */

76     public void addAll(Object JavaDoc[] toAdd) {
77         Assert.isNotNull(toAdd);
78         for (int i = 0; i < toAdd.length; i++) {
79             Object JavaDoc object = toAdd[i];
80             
81             data.add(object);
82         }
83         
84         fireAdd(toAdd);
85     }
86     
87     /**
88      * Adds the given elements to the set. Duplicate elements are ignored.
89      *
90      * @param toAdd elements to add
91      */

92     public void addAll(Collection JavaDoc toAdd) {
93         Assert.isNotNull(toAdd);
94         addAll(toAdd.toArray());
95     }
96     
97     /**
98      * Fires a change notification for all elements in the given array
99      *
100      * @param changed array of elements that have changed
101      */

102     public void changeAll(Object JavaDoc[] changed) {
103         Assert.isNotNull(changed);
104         fireUpdate(changed);
105     }
106     
107     /**
108      * Removes all of the given elements from the set.
109      *
110      * @param toRemove elements to remove
111      */

112     public void removeAll(Object JavaDoc[] toRemove) {
113         Assert.isNotNull(toRemove);
114         for (int i = 0; i < toRemove.length; i++) {
115             Object JavaDoc object = toRemove[i];
116             
117             data.remove(object);
118         }
119         
120         fireRemove(toRemove);
121     }
122
123     /* (non-Javadoc)
124      * @see org.eclipse.jface.viewers.deferred.IConcurrentModel#requestUpdate(org.eclipse.jface.viewers.deferred.IConcurrentModelListener)
125      */

126     public void requestUpdate(IConcurrentModelListener listener) {
127         Assert.isNotNull(listener);
128         listener.setContents(getElements());
129     }
130 }
131
Popular Tags