/*------------------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved.
------------------------------------------------------------------------------------*/

package com.togethersoft.modules.xmleditor.xmltree;

import java.util.*;
import org.w3c.dom.*;
import com.togethersoft.openapi.rwi.*;
import com.togethersoft.modules.xmleditor.dtd.*;
import com.togethersoft.modules.xmleditor.api.*;


public class AttributeInst extends ValueHolder implements XmlAttributeNode
{
  private DTDAttributeDef m_attrDef;

  /**
   * @supplierCardinality 1 
   */
  private ElementInst  m_containingElementInst = null;

  //--------------------------------------------------------------------
  public AttributeInst (XMLTreeManager xmlTreeManager, DTDAttributeDef attrDef, ElementInst containingElementInst)
  {
    super (xmlTreeManager, attrDef);

    m_attrDef = attrDef;
    m_containingElementInst = containingElementInst;
  }

  //--------------------------------------------------------------------
  public DTDAttributeDef getAttributeDef()
  {
    return m_attrDef;
  }

  //--------------------------------------------------------------------
  public void loadFromDOMElement (Element domElement)
  {
    String strValue = domElement.getAttribute (m_attrDef.getName());
    setStringValue (strValue);
  }

  //--------------------------------------------------------------------
  public void storeToDOMElement (Element domElement)
  {
    String strValue = getStringValue().trim();
    if (strValue.length() > 0)
    {
      try
      {
        domElement.setAttribute (m_attrDef.getName(), strValue);
      }
      catch (DOMException e) { }
    }
  }

  //--------------------------------------------------------------------
  public void updateTogetherModel()
  {
    // write value of the element
    RwiElement rwiElement = m_containingElementInst.getRwiElement();
    String     rwiPropertyName = getRwiPropertyToUpdate();

    if (rwiElement != null && rwiPropertyName != null)
      rwiElement.setProperty (rwiPropertyName, getValue());
  }

  //--------------------------------------------------------------------
  /**
    defines property by full XML name of the containing element like

    ejb-jar.enterprise-beans.session.attr.description.rwiProperty = $doc
  */
	protected String getRwiPropertyToUpdate()
	{
    if (m_rwiPropertyName == IS_UNDEFINED)
    {
      String keySuffix = ".attr." + m_attrDef.getName() + ".rwiProperty";
		  m_rwiPropertyName = m_containingElementInst.findFullXMLNameRelatedProperty (keySuffix);
    }

	 	return m_rwiPropertyName;
	}

  private String m_rwiPropertyName = IS_UNDEFINED;
  private static String IS_UNDEFINED = "undefined";

  //--------------------------------------------------------------------
  public boolean hasReadonlyValue()
  {
    boolean readOnly = super.hasReadonlyValue();

    if (! readOnly)
    {
  		String s = m_containingElementInst.findFullXMLNameRelatedProperty (".attr." + m_attrDef.getName() + ".readOnly");
      readOnly = (s != null && (s.equalsIgnoreCase ("true") || s.trim().length() == 0));
    }

    return readOnly;
  }

  //--------------------------------------------------------------------
  //  Implementation of XmlAttributeNode interface
  //--------------------------------------------------------------------

  public String getName()
  {
    return m_attrDef.getName();
  }

  //--------------------------------------------------------------------
  public String getValue()
  {
    return getStringValue();
  }

  //--------------------------------------------------------------------
  public XmlElementNode getContainingElement()
  {
    return m_containingElementInst;
  }
}
