Home | History | Annotate | Download | only in properties
      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.android.annotations.NonNull;
     20 import com.google.common.base.Objects;
     21 
     22 import org.eclipse.wb.internal.core.model.property.Property;
     23 
     24 import java.util.Arrays;
     25 
     26 /**
     27  * Property holding multiple instances of the same {@link XmlProperty} (but
     28  * bound to difference objects. This is used when multiple objects are selected
     29  * in the layout editor and the common properties are shown; editing a value
     30  * will (via {@link #setValue(Object)}) set it on all selected objects.
     31  * <p>
     32  * Similar to
     33  * org.eclipse.wb.internal.core.model.property.GenericPropertyComposite
     34  */
     35 class XmlPropertyComposite extends XmlProperty {
     36     private static final Object NO_VALUE = new Object();
     37 
     38     private final XmlProperty[] mProperties;
     39 
     40     public XmlPropertyComposite(XmlProperty primary, XmlProperty[] properties) {
     41         super(
     42             primary.getEditor(),
     43             primary.getFactory(),
     44             primary.getNode(),
     45             primary.getDescriptor());
     46         mProperties = properties;
     47     }
     48 
     49     @Override
     50     @NonNull
     51     public String getTitle() {
     52         return mProperties[0].getTitle();
     53     }
     54 
     55     @Override
     56     public int hashCode() {
     57         return mProperties.length;
     58     }
     59 
     60     @Override
     61     public boolean equals(Object obj) {
     62         if (obj == this) {
     63             return true;
     64         }
     65 
     66         if (obj instanceof XmlPropertyComposite) {
     67             XmlPropertyComposite property = (XmlPropertyComposite) obj;
     68             return Arrays.equals(mProperties, property.mProperties);
     69         }
     70 
     71         return false;
     72     }
     73 
     74     @Override
     75     public boolean isModified() throws Exception {
     76         for (Property property : mProperties) {
     77             if (property.isModified()) {
     78                 return true;
     79             }
     80         }
     81 
     82         return false;
     83     }
     84 
     85     @Override
     86     public Object getValue() throws Exception {
     87         Object value = NO_VALUE;
     88         for (Property property : mProperties) {
     89             Object propertyValue = property.getValue();
     90             if (value == NO_VALUE) {
     91                 value = propertyValue;
     92             } else if (!Objects.equal(value, propertyValue)) {
     93                 return UNKNOWN_VALUE;
     94             }
     95         }
     96 
     97         return value;
     98     }
     99 
    100     @Override
    101     public void setValue(final Object value) throws Exception {
    102         // TBD: Wrap in ExecutionUtils.run?
    103         for (Property property : mProperties) {
    104             property.setValue(value);
    105         }
    106     }
    107 
    108     @NonNull
    109     public static XmlPropertyComposite create(Property... properties) {
    110         // Cast from Property into XmlProperty
    111         XmlProperty[] xmlProperties = new XmlProperty[properties.length];
    112         for (int i = 0; i < properties.length; i++) {
    113             Property property = properties[i];
    114             xmlProperties[i] = (XmlProperty) property;
    115         }
    116 
    117         XmlPropertyComposite composite = new XmlPropertyComposite(xmlProperties[0], xmlProperties);
    118         composite.setCategory(xmlProperties[0].getCategory());
    119         return composite;
    120     }
    121 }
    122