1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package com.oracle.javafx.scenebuilder.kit.metadata.util;
  33 
  34 
  35 /**
  36  *
  37  */
  38 public class PropertyName implements Comparable<PropertyName> {
  39 
  40     private final String name;
  41     private final Class<?> residenceClass;
  42 
  43     public PropertyName(String propertyName, Class<?> foreignClass) {
  44         assert propertyName != null;
  45 
  46         this.residenceClass = foreignClass;
  47         this.name = propertyName;
  48     }
  49 
  50     public PropertyName(String propertyName) {
  51         this(propertyName, null);
  52     }
  53 
  54     public Class<?> getResidenceClass() {
  55         return residenceClass;
  56     }
  57 
  58     public String getName() {
  59         return name;
  60     }
  61 
  62     public Object getValue(Object sceneGraphObject) {
  63         final Object result;
  64 
  65         if (residenceClass == null) {
  66             final BeanPropertyIntrospector bpi
  67                     = new BeanPropertyIntrospector(sceneGraphObject);
  68             result = bpi.getValue(name);
  69         } else {
  70             final StaticPropertyIntrospector spi
  71                     = new StaticPropertyIntrospector(sceneGraphObject, residenceClass);
  72             result = spi.getValue(name);
  73         }
  74 
  75         return result;
  76     }
  77 
  78     public void setValue(Object sceneGraphObject, Object value) {
  79         if (residenceClass == null) {
  80             final BeanPropertyIntrospector bpi
  81                     = new BeanPropertyIntrospector(sceneGraphObject);
  82             bpi.setValue(name, value);
  83         } else {
  84             final StaticPropertyIntrospector spi
  85                     = new StaticPropertyIntrospector(sceneGraphObject, residenceClass);
  86             spi.setValue(name, value);
  87         }
  88     }
  89 
  90 
  91     public static String makeClassFullName(Class<?> aClass) {
  92         assert aClass != null;
  93 
  94         final StringBuilder result = new StringBuilder();
  95         result.append(aClass.getSimpleName());
  96         Class<?> declaringClass = aClass.getDeclaringClass();
  97         while (declaringClass != null) {
  98             result.insert(0, '.');
  99             result.insert(0, declaringClass.getSimpleName());
 100             declaringClass = declaringClass.getDeclaringClass();
 101         }
 102 
 103         return result.toString();
 104     }
 105 
 106     /*
 107      * Object
 108      */
 109 
 110     @Override
 111     public boolean equals(Object o) {
 112         boolean result;
 113 
 114         if (this == o) {
 115             result = true;
 116         } else if ((o == null) || (o.getClass() != this.getClass())) {
 117             result = false;
 118         } else {
 119             final PropertyName other = (PropertyName) o;
 120 
 121             result = true;
 122             if (residenceClass == null) {
 123                 result = result && (other.residenceClass == null);
 124             } else {
 125                 result = result && residenceClass.equals(other.residenceClass);
 126             }
 127             result = result && name.equals(other.name);
 128         }
 129 
 130         return result;
 131     }
 132 
 133 
 134     @Override
 135     public int hashCode() {
 136         int result = 7;
 137         if (residenceClass != null) {
 138             result = 31 * result + residenceClass.hashCode();
 139         }
 140         result = 31 * result + name.hashCode();
 141         return result;
 142     }
 143 
 144 
 145     @Override
 146     public String toString() {
 147         final String result;
 148 
 149         if (residenceClass == null) {
 150             result = name;
 151         } else {
 152             result = makeClassFullName(residenceClass) + "." + name; //NOI18N
 153         }
 154 
 155         return result;
 156     }
 157 
 158     /*
 159      * Comparable
 160      */
 161     @Override
 162     public int compareTo(PropertyName t) {
 163         int result;
 164 
 165         if (this == t) {
 166             result = 0;
 167         } else if (t == null) {
 168             result = -1;
 169         } else {
 170             if ((this.residenceClass == null) && (t.residenceClass == null)) {
 171                 result = 0;
 172             } else if (t.residenceClass == null) {
 173                 result = +1;
 174             } else if (this.residenceClass == null) {
 175                 result = -1;
 176             }  else {
 177                 result = residenceClass.getCanonicalName().compareToIgnoreCase(t.residenceClass.getCanonicalName());
 178             }
 179             if (result == 0) {
 180                 result = name.compareToIgnoreCase(t.name);
 181             }
 182         }
 183 
 184         return result;
 185     }
 186 }