KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > preferences > PropertyMapUnion


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.preferences;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.ui.internal.util.Util;
18
19 /**
20  * @since 3.1
21  */

22 public class PropertyMapUnion implements IPropertyMap {
23
24     private Map JavaDoc values;
25     
26     private final static class PropertyInfo {
27         Object JavaDoc value;
28         boolean commonAttribute;
29         
30         PropertyInfo(Object JavaDoc value, boolean commonAttribute) {
31             this.value = value;
32             this.commonAttribute = commonAttribute;
33         }
34     }
35     
36     /* (non-Javadoc)
37      * @see org.eclipse.ui.internal.preferences.IPropertyMap#keySet()
38      */

39     public Set JavaDoc keySet() {
40         return values.keySet();
41     }
42
43     /* (non-Javadoc)
44      * @see org.eclipse.ui.internal.preferences.IPropertyMap#getValue(java.lang.String, java.lang.Class)
45      */

46     public Object JavaDoc getValue(String JavaDoc propertyId, Class JavaDoc propertyType) {
47         PropertyInfo info = (PropertyInfo)values.get(propertyId);
48         
49         if (info == null) {
50             return null;
51         }
52         
53         Object JavaDoc value = info.value;
54         
55         if (propertyType.isInstance(value)) {
56             return value;
57         }
58         
59         return null;
60     }
61
62     /* (non-Javadoc)
63      * @see org.eclipse.ui.internal.preferences.IPropertyMap#isCommonProperty(java.lang.String)
64      */

65     public boolean isCommonProperty(String JavaDoc propertyId) {
66         PropertyInfo info = (PropertyInfo)values.get(propertyId);
67         
68         if (info == null) {
69             return false;
70         }
71         
72         return info.commonAttribute;
73     }
74
75     /* (non-Javadoc)
76      * @see org.eclipse.ui.internal.preferences.IPropertyMap#propertyExists(java.lang.String)
77      */

78     public boolean propertyExists(String JavaDoc propertyId) {
79         return values.get(propertyId) != null;
80     }
81
82     /* (non-Javadoc)
83      * @see org.eclipse.ui.internal.preferences.IPropertyMap#setValue(java.lang.String, java.lang.Object)
84      */

85     public void setValue(String JavaDoc propertyId, Object JavaDoc newValue) {
86         PropertyInfo info = new PropertyInfo(newValue, true);
87         
88         values.put(propertyId, info);
89     }
90
91     public void addMap(IPropertyMap toAdd) {
92         Set JavaDoc keySet = toAdd.keySet();
93
94         // Update any existing attributes
95
for (Iterator JavaDoc iter = keySet().iterator(); iter.hasNext();) {
96             String JavaDoc key = (String JavaDoc) iter.next();
97             
98             PropertyInfo localInfo = (PropertyInfo)values.get(key);
99             // Shouldn't be null, but just in case...
100
if (localInfo != null) {
101                 // If the attribute exists in the new map
102
if (toAdd.propertyExists(key)) {
103                     // Determine if the value is common
104
Object JavaDoc value = toAdd.getValue(key, Object JavaDoc.class);
105                     
106                     if (!Util.equals(value, toAdd.getValue(key, Object JavaDoc.class))) {
107                         // Set the value to null if not common
108
localInfo.value = null;
109                     }
110                     
111                     // The attribute must be common in both the receiver and the new map to be common
112
// everywhere
113
localInfo.commonAttribute = localInfo.commonAttribute && toAdd.isCommonProperty(key);
114                 } else {
115                     // If the attribute doesn't exist in the new map, it cannot be common
116
localInfo.commonAttribute = false;
117                 }
118             }
119         }
120         
121         // Add any new attributes that exist in the target
122
for (Iterator JavaDoc iter = keySet.iterator(); iter.hasNext();) {
123             String JavaDoc element = (String JavaDoc) iter.next();
124             
125             PropertyInfo localInfo = (PropertyInfo)values.get(element);
126             if (localInfo == null) {
127                 Object JavaDoc value = toAdd.getValue(element, Object JavaDoc.class);
128                 
129                 boolean isCommon = toAdd.isCommonProperty(element);
130                 
131                 localInfo = new PropertyInfo(value, isCommon);
132                 values.put(element, localInfo);
133             }
134         }
135     }
136     
137     public void removeValue(String JavaDoc propertyId) {
138         values.remove(propertyId);
139     }
140 }
141
Popular Tags