1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ide.eclipse.adt.internal.editors.layout.properties; 18 19 import com.google.common.base.Objects; 20 21 import org.eclipse.wb.internal.core.model.property.Property; 22 23 import java.util.Arrays; 24 25 /** 26 * Property holding multiple instances of the same {@link XmlProperty} (but 27 * bound to difference objects. This is used when multiple objects are selected 28 * in the layout editor and the common properties are shown; editing a value 29 * will (via {@link #setValue(Object)}) set it on all selected objects. 30 * <p> 31 * Similar to 32 * org.eclipse.wb.internal.core.model.property.GenericPropertyComposite 33 */ 34 class XmlPropertyComposite extends XmlProperty { 35 private static final Object NO_VALUE = new Object(); 36 37 private final XmlProperty[] mProperties; 38 39 public XmlPropertyComposite(XmlProperty primary, XmlProperty[] properties) { 40 super( 41 primary.getEditor(), 42 primary.getFactory(), 43 primary.getNode(), 44 primary.getDescriptor()); 45 mProperties = properties; 46 } 47 48 @Override 49 public String getTitle() { 50 return mProperties[0].getTitle(); 51 } 52 53 @Override 54 public int hashCode() { 55 return mProperties.length; 56 } 57 58 @Override 59 public boolean equals(Object obj) { 60 if (obj == this) { 61 return true; 62 } 63 64 if (obj instanceof XmlPropertyComposite) { 65 XmlPropertyComposite property = (XmlPropertyComposite) obj; 66 return Arrays.equals(mProperties, property.mProperties); 67 } 68 69 return false; 70 } 71 72 @Override 73 public boolean isModified() throws Exception { 74 for (Property property : mProperties) { 75 if (property.isModified()) { 76 return true; 77 } 78 } 79 80 return false; 81 } 82 83 @Override 84 public Object getValue() throws Exception { 85 Object value = NO_VALUE; 86 for (Property property : mProperties) { 87 Object propertyValue = property.getValue(); 88 if (value == NO_VALUE) { 89 value = propertyValue; 90 } else if (!Objects.equal(value, propertyValue)) { 91 return UNKNOWN_VALUE; 92 } 93 } 94 95 return value; 96 } 97 98 @Override 99 public void setValue(final Object value) throws Exception { 100 // TBD: Wrap in ExecutionUtils.run? 101 for (Property property : mProperties) { 102 property.setValue(value); 103 } 104 } 105 106 public static XmlPropertyComposite create(Property... properties) { 107 // Cast from Property into XmlProperty 108 XmlProperty[] xmlProperties = new XmlProperty[properties.length]; 109 for (int i = 0; i < properties.length; i++) { 110 Property property = properties[i]; 111 xmlProperties[i] = (XmlProperty) property; 112 } 113 114 XmlPropertyComposite composite = new XmlPropertyComposite(xmlProperties[0], xmlProperties); 115 composite.setCategory(xmlProperties[0].getCategory()); 116 return composite; 117 } 118 } 119