/**
   * Returns the default value for the attribute we want to create. This function may throw a
   * RuntimeException, if the value filled in for the default value can not be parsed!
   *
   * @return default value
   */
  protected Object getDefaultValue() {
    if (FeatureCollectionTools.isAttributeTypeNumeric(
        (AttributeType) this.typeDropDown.getSelectedItem())) {
      AttributeType at = (AttributeType) this.typeDropDown.getSelectedItem();

      if (at.equals(AttributeType.INTEGER)) {
        int i = Integer.parseInt(this.defValueTextField.getText());
        return new Integer(i);
      }
      double d = Double.parseDouble(this.defValueTextField.getText());
      return new Double(d);
    }
    return this.defValueTextField.getText();
  }
  /**
   * checks and fixes the integrity of the values given, when the attribute type is changed.
   *
   * @param event the action event
   */
  public void actionPerformed(ActionEvent event) {
    if (JComboBox.class.isInstance(event.getSource())) {
      if (this.needDefaultValue
          && FeatureCollectionTools.isAttributeTypeNumeric(
              (AttributeType) this.typeDropDown.getSelectedItem())) {
        AttributeType at = (AttributeType) this.typeDropDown.getSelectedItem();

        if (at.equals(AttributeType.INTEGER)) {
          try {
            Integer.parseInt(this.defValueTextField.getText());
          } catch (Exception e) {
            this.defValueTextField.setText("0");
          }
        } else {
          try {
            Double.parseDouble(this.defValueTextField.getText());
          } catch (Exception e) {
            this.defValueTextField.setText("0.0");
          }
        }
      }
    }
  }
  public void addAttribute(Attribute attribute) {
    attribute.setSchema(this);
    // comprobación de clones de tablas
    GeopistaSchema schema = this;

    // busca si existe otra tabla con este nombre
    // java2xml desvincula las tablas y crea múltiples instancias

    for (int i = 0; i < schema.getAttributeCount(); i++) {
      if (schema
          .getColumnByAttribute(i)
          .getTable()
          .getName()
          .equals(attribute.getColumn().getTable().getName())) {
        // Es un clon de la ya existente -> uso la existente en lugar del parámetro.
        attribute.getColumn().setTable(schema.getColumnByAttribute(i).getTable());
      }
    }
    /**
     * Busca si existe un dominio con el mismo nombre de tipo TreeDomain java2xml desvincula los
     * dominios y crea instancias separadas
     */
    Domain newdomain = attribute.getColumn().getDomain();
    if (newdomain instanceof TreeDomain) {
      TreeDomain newTreeDomain = (TreeDomain) newdomain;
      // busca por nombre de dominio
      for (int i = 0; i < schema.getAttributeCount(); i++) {
        if (schema.getAttributeDomain(i) != null
            && schema.getAttributeDomain(i).getName().equals(newTreeDomain.getName())) {
          // se ha encontrado el dominio ya definido: Usamos el actual
          Domain dom = schema.getAttributeDomain(i);
          newTreeDomain = (TreeDomain) dom; // overrides suplied domain
        }
      }
      attribute.getColumn().setDomain(newTreeDomain);
      //		 enlaza la columna actual al dominio existente
      int level = newTreeDomain.getLevelNames().indexOf(attribute.getColumn().getName());
      if (level > -1) newTreeDomain.attachColumnToLevel(attribute.getColumn(), level);
    }

    addAttribute(
        attribute.getName(),
        AttributeType.toAttributeType(attribute.getType()),
        attribute.getColumn(),
        attribute.getAccessType());
  }