001: /*******************************************************************************
002: * Copyright (c) 2004, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.preferences;
011:
012: import java.util.Iterator;
013: import java.util.Map;
014: import java.util.Set;
015:
016: import org.eclipse.ui.internal.util.Util;
017:
018: /**
019: * @since 3.1
020: */
021: public class PropertyMapUnion implements IPropertyMap {
022:
023: private Map values;
024:
025: private final static class PropertyInfo {
026: Object value;
027: boolean commonAttribute;
028:
029: PropertyInfo(Object value, boolean commonAttribute) {
030: this .value = value;
031: this .commonAttribute = commonAttribute;
032: }
033: }
034:
035: /* (non-Javadoc)
036: * @see org.eclipse.ui.internal.preferences.IPropertyMap#keySet()
037: */
038: public Set keySet() {
039: return values.keySet();
040: }
041:
042: /* (non-Javadoc)
043: * @see org.eclipse.ui.internal.preferences.IPropertyMap#getValue(java.lang.String, java.lang.Class)
044: */
045: public Object getValue(String propertyId, Class propertyType) {
046: PropertyInfo info = (PropertyInfo) values.get(propertyId);
047:
048: if (info == null) {
049: return null;
050: }
051:
052: Object value = info.value;
053:
054: if (propertyType.isInstance(value)) {
055: return value;
056: }
057:
058: return null;
059: }
060:
061: /* (non-Javadoc)
062: * @see org.eclipse.ui.internal.preferences.IPropertyMap#isCommonProperty(java.lang.String)
063: */
064: public boolean isCommonProperty(String propertyId) {
065: PropertyInfo info = (PropertyInfo) values.get(propertyId);
066:
067: if (info == null) {
068: return false;
069: }
070:
071: return info.commonAttribute;
072: }
073:
074: /* (non-Javadoc)
075: * @see org.eclipse.ui.internal.preferences.IPropertyMap#propertyExists(java.lang.String)
076: */
077: public boolean propertyExists(String propertyId) {
078: return values.get(propertyId) != null;
079: }
080:
081: /* (non-Javadoc)
082: * @see org.eclipse.ui.internal.preferences.IPropertyMap#setValue(java.lang.String, java.lang.Object)
083: */
084: public void setValue(String propertyId, Object newValue) {
085: PropertyInfo info = new PropertyInfo(newValue, true);
086:
087: values.put(propertyId, info);
088: }
089:
090: public void addMap(IPropertyMap toAdd) {
091: Set keySet = toAdd.keySet();
092:
093: // Update any existing attributes
094: for (Iterator iter = keySet().iterator(); iter.hasNext();) {
095: String key = (String) iter.next();
096:
097: PropertyInfo localInfo = (PropertyInfo) values.get(key);
098: // Shouldn't be null, but just in case...
099: if (localInfo != null) {
100: // If the attribute exists in the new map
101: if (toAdd.propertyExists(key)) {
102: // Determine if the value is common
103: Object value = toAdd.getValue(key, Object.class);
104:
105: if (!Util.equals(value, toAdd.getValue(key,
106: Object.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
114: && toAdd.isCommonProperty(key);
115: } else {
116: // If the attribute doesn't exist in the new map, it cannot be common
117: localInfo.commonAttribute = false;
118: }
119: }
120: }
121:
122: // Add any new attributes that exist in the target
123: for (Iterator iter = keySet.iterator(); iter.hasNext();) {
124: String element = (String) iter.next();
125:
126: PropertyInfo localInfo = (PropertyInfo) values.get(element);
127: if (localInfo == null) {
128: Object value = toAdd.getValue(element, Object.class);
129:
130: boolean isCommon = toAdd.isCommonProperty(element);
131:
132: localInfo = new PropertyInfo(value, isCommon);
133: values.put(element, localInfo);
134: }
135: }
136: }
137:
138: public void removeValue(String propertyId) {
139: values.remove(propertyId);
140: }
141: }
|