View Javadoc

    1   /*
    2    * joey-gen and its relative products are published under the terms
    3    * of the Apache Software License.
    4    *
    5    * Created on 2004/08/10 17:00:26
    6    */
    7   package org.asyrinx.joey.gen.model;
    8
    9   import java.util.Collections;
    10  import java.util.HashMap;
    11  import java.util.Map;
    12
    13  import org.apache.commons.lang.builder.EqualsBuilder;
    14  import org.asyrinx.brownie.core.lang.ClassUtils;
    15
    16  /***
    17   * @author akima
    18   */
    19  public abstract class Element implements Cloneable {
    20
    21      /***
    22       *
    23       */
    24      public Element() {
    25          this(null, null);
    26      }
    27
    28      /***
    29       * @param parent
    30       */
    31      public Element(Element parent) {
    32          this(parent, null);
    33      }
    34
    35      /***
    36       * @param name
    37       */
    38      public Element(String name) {
    39          this(null, name);
    40      }
    41
    42      /***
    43       * @param parent
    44       * @param name
    45       */
    46      public Element(Element parent, String name) {
    47          this(parent, name, null);
    48      }
    49
    50      /***
    51       * @param parent
    52       * @param name
    53       */
    54      public Element(Element parent, String name, String label) {
    55          super();
    56          this.name = name;
    57          this.label = label;
    58          this.setParentElement(parent);
    59      }
    60
    61      public String getElementType() {
    62          return ClassUtils.toShortClassName(this.getClass());
    63      }
    64
    65      /*
    66       * (non-Javadoc)
    67       *
    68       * @see java.lang.Object#clone()
    69       */
    70      public Object clone() throws CloneNotSupportedException {
    71          return super.clone();
    72      }
    73
    74      public String getFullName() {
    75          return (getParentElement() == null) ? getName() : getParentElement().getFullName() + "."
    76                  + getName();
    77      }
    78
    79      private Element parent = null;
    80
    81      /***
    82       * @return Returns the parent.
    83       */
    84      public Element getParentElement() {
    85          return parent;
    86      }
    87
    88      /***
    89       * @param parent
    90       *            The parent to set.
    91       */
    92      protected void setParentElement(Element parent) {
    93          if (this.parent == parent)
    94              return;
    95          this.parent = (parent == null) ? null : (parent.isEntity()) ? parent : parent
    96                  .getParentElement();
    97          if (this.parent != null)
    98              this.parent.add(this);
    99      }
    100
    101     public boolean isEntity() {
    102         return true;
    103     }
    104
    105     public void add(Element element) {
    106         if (element instanceof ElementSet) {
    107             //final ElementSet elementSet = (ElementSet)element;
    108         } else {
    109             throw new UnsupportedOperationException("element must not be ."
    110                     + element.getClass().getName() + " @" + this.getClass().getName());
    111         }
    112     }
    113
    114     private String name = null;
    115
    116     private String label = null;
    117
    118     private String description = null;
    119
    120     private Map options = null;
    121
    122     /***
    123      * @return Returns the label.
    124      */
    125     public String getLabel() {
    126         return label;
    127     }
    128
    129     /***
    130      * @param label
    131      *            The label to set.
    132      */
    133     public void setLabel(String label) {
    134         this.label = label;
    135     }
    136
    137     /***
    138      * @return Returns the name.
    139      */
    140     public String getName() {
    141         return name;
    142     }
    143
    144     /***
    145      * @param name
    146      *            The name to set.
    147      */
    148     public void setName(String name) {
    149         this.name = name;
    150     }
    151
    152     /***
    153      * @return Returns the description.
    154      */
    155     public String getDescription() {
    156         return description;
    157     }
    158
    159     /***
    160      * @param description
    161      *            The description to set.
    162      */
    163     public void setDescription(String description) {
    164         this.description = description;
    165     }
    166
    167     /***
    168      * @return Returns the options.
    169      */
    170     public Map getOptions() {
    171         if (this.options == null)
    172             this.options = new HashMap(1);
    173         return options;
    174     }
    175
    176     /***
    177      * @param options
    178      *            The options to set.
    179      */
    180     public void setOptions(Map options) {
    181         this.options = (options != null) ? options : Collections.EMPTY_MAP;
    182     }
    183
    184     public Object getOption(String key) {
    185         final Object option = getOptions().get(key);
    186         return (option != null) ? option : (getParentElement() == null) ? null : getParentElement()
    187                 .getOption(key);
    188     }
    189
    190     private Element original = null;
    191
    192     /***
    193      * @return Returns the original.
    194      */
    195     public Element getOriginal() {
    196         return original;
    197     }
    198
    199     /***
    200      * @param original
    201      *            The original to set.
    202      */
    203     public void setOriginal(Element original) {
    204         this.original = original;
    205     }
    206
    207     /*
    208      * (non-Javadoc)
    209      *
    210      * @see java.lang.Object#equals(java.lang.Object)
    211      */
    212     public boolean equals(Object obj) {
    213         if (!(obj instanceof Element))
    214             return false;
    215         final Element other = (Element) obj;
    216         return new EqualsBuilder() //
    217                 .append(this.getName(), other.getName()) //
    218                 .append(this.getLabel(), other.getLabel()) //
    219                 .append(this.getOptions(), other.getOptions()) //
    220                 .isEquals();
    221     }
    222
    223     public int getAncestorDepth() {
    224         if (getParentElement() != null)
    225             return getParentElement().getAncestorDepth() + 1;
    226         else
    227             return 0;
    228     }
    229
    230     /*
    231      * (non-Javadoc)
    232      *
    233      * @see java.lang.Object#toString()
    234      */
    235     public String toString() {
    236         return getFullName();
    237     }
    238
    239 }