KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > extensions > ExtensionStateModel


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.navigator.extensions;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.commands.common.EventManager;
17 import org.eclipse.jface.util.IPropertyChangeListener;
18 import org.eclipse.jface.util.PropertyChangeEvent;
19 import org.eclipse.ui.navigator.IExtensionStateModel;
20
21 /**
22  *
23  *
24  * @since 3.2
25  * @see IExtensionStateModel
26  */

27 public class ExtensionStateModel extends EventManager implements
28         IExtensionStateModel {
29
30     private final String JavaDoc id;
31
32     private final String JavaDoc viewerId;
33
34     private final Map JavaDoc values = new HashMap JavaDoc();
35
36     /**
37      * Create an extension state model for the given extension (anId) associated
38      * with the given viewer (aViewerId).
39      *
40      * @param anId
41      * The id of the extension this state model is used for.
42      * @param aViewerId
43      * The id of the viewer this state model is associated with.
44      */

45     public ExtensionStateModel(String JavaDoc anId, String JavaDoc aViewerId) {
46         id = anId;
47         viewerId = aViewerId;
48     }
49
50     public String JavaDoc getId() {
51         return id;
52     }
53
54     public String JavaDoc getViewerId() {
55         return viewerId;
56     }
57
58     public String JavaDoc getStringProperty(String JavaDoc aPropertyName) {
59         return (String JavaDoc) values.get(aPropertyName);
60     }
61
62     public boolean getBooleanProperty(String JavaDoc aPropertyName) {
63
64         Boolean JavaDoc b = (Boolean JavaDoc) values.get(aPropertyName);
65         return b != null ? b.booleanValue() : false;
66     }
67
68     public int getIntProperty(String JavaDoc aPropertyName) {
69         Integer JavaDoc i = (Integer JavaDoc) values.get(aPropertyName);
70         return i != null ? i.intValue() : -1;
71     }
72
73     public void setStringProperty(String JavaDoc aPropertyName, String JavaDoc aPropertyValue) {
74         String JavaDoc oldValue = (String JavaDoc) values.get(aPropertyName);
75         String JavaDoc newValue = aPropertyValue;
76         if (hasPropertyChanged(oldValue, newValue)) {
77             values.put(aPropertyName, newValue);
78             firePropertyChangeEvent(new PropertyChangeEvent(this,
79                     aPropertyName, oldValue, newValue));
80         }
81     }
82
83     public void setBooleanProperty(String JavaDoc aPropertyName, boolean aPropertyValue) {
84         Boolean JavaDoc oldValue = (Boolean JavaDoc) values.get(aPropertyName);
85         Boolean JavaDoc newValue = aPropertyValue ? Boolean.TRUE : Boolean.FALSE;
86         if (hasPropertyChanged(oldValue, newValue)) {
87
88             values.put(aPropertyName, aPropertyValue ? Boolean.TRUE
89                     : Boolean.FALSE);
90             firePropertyChangeEvent(new PropertyChangeEvent(this,
91                     aPropertyName, oldValue, newValue));
92         }
93     }
94
95     public void setIntProperty(String JavaDoc aPropertyName, int aPropertyValue) {
96         Integer JavaDoc oldValue = (Integer JavaDoc) values.get(aPropertyName);
97         Integer JavaDoc newValue = new Integer JavaDoc(aPropertyValue);
98         if (hasPropertyChanged(oldValue, newValue)) {
99             values.put(aPropertyName, newValue);
100             firePropertyChangeEvent(new PropertyChangeEvent(this,
101                     aPropertyName, oldValue, newValue));
102         }
103     }
104
105     public void addPropertyChangeListener(IPropertyChangeListener aListener) {
106         addListenerObject(aListener);
107     }
108
109     public void removePropertyChangeListener(IPropertyChangeListener aListener) {
110         removeListenerObject(aListener);
111     }
112
113     public Object JavaDoc getProperty(String JavaDoc aPropertyName) {
114         return values.get(aPropertyName);
115     }
116
117     public void setProperty(String JavaDoc aPropertyName, Object JavaDoc aPropertyValue) {
118
119         Object JavaDoc oldValue = values.get(aPropertyName);
120         Object JavaDoc newValue = aPropertyValue;
121         if (hasPropertyChanged(oldValue, newValue)) {
122             values.put(aPropertyName, newValue);
123             firePropertyChangeEvent(new PropertyChangeEvent(this,
124                     aPropertyName, oldValue, newValue));
125         }
126     }
127  
128     private boolean hasPropertyChanged(Object JavaDoc oldValue, Object JavaDoc newValue) {
129         return oldValue == null || !oldValue.equals(newValue);
130     }
131
132     protected void firePropertyChangeEvent(PropertyChangeEvent anEvent) {
133         Object JavaDoc[] listeners = getListeners();
134         for (int i = 0; i < listeners.length; ++i) {
135             ((IPropertyChangeListener) listeners[i]).propertyChange(anEvent);
136         }
137     }
138
139 }
140
Popular Tags