KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > Model


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.ui.internal;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * Represents an object with state that will notifies a set of listeners
19  * whenever its state changes
20  */

21 public class Model {
22
23     /**
24      * Current state of the model
25      */

26     private Object JavaDoc state;
27
28     /**
29      * Set of objects that will be notified about state changes
30      */

31     private List JavaDoc views = new ArrayList JavaDoc(1);
32
33     /**
34      * Creates a new model with the given initial state.
35      *
36      * @param initialState
37      */

38     public Model(Object JavaDoc initialState) {
39         state = initialState;
40     }
41
42     /**
43      * Returns the current state of the model.
44      *
45      * @return the current model state
46      */

47     public Object JavaDoc getState() {
48         return state;
49     }
50
51     /**
52      * Sets the current state of the model.
53      *
54      * @param newState the new state of the model
55      * @param toOmit change listener that should be omitted from change notifications (or null if all
56      * listeners should be notified).
57      */

58     public void setState(Object JavaDoc newState, IChangeListener toOmit) {
59         if (areEqual(newState, state)) {
60             return;
61         }
62
63         state = newState;
64
65         Iterator JavaDoc iter = views.iterator();
66         while (iter.hasNext()) {
67             IChangeListener next = (IChangeListener) iter.next();
68
69             if (next != toOmit) {
70                 next.update(true);
71             }
72         }
73     }
74
75     private boolean areEqual(Object JavaDoc o1, Object JavaDoc o2) {
76         if (o1 == null) {
77             return o2 == null;
78         }
79         if (o2 == null) {
80             return false;
81         }
82
83         return o1.equals(o2);
84     }
85
86     /**
87      * Adds the given listener to the set of listeners that will be
88      * notified when the state changes.
89      *
90      * @param toAdd
91      */

92     public void addChangeListener(IChangeListener changeListener) {
93         changeListener.update(false);
94         views.add(changeListener);
95     }
96
97     /**
98      * Stops this model from sending change events from the given listener.
99      *
100      * @param toRemove
101      */

102     public void removeChangeListener(IChangeListener changeListener) {
103         views.remove(changeListener);
104     }
105
106 }
107
Popular Tags