view src/MPipeline2/src/mpipeline/plisttable/JParam.java @ 16:91f21a0aab35 database-connection-manager

Update utils.jquery * * * Update utils.jmysql.getsinfo
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100 (2011-12-05)
parents f0afece42f48
children
line wrap: on
line source
/*
 * Class JParam: Creates a parameter object.
 *
 * Copyright (c) 2009 Max-Planck-Gesellschaft, Ingo Diepholz <ingo.diepholz@aei.mpg.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package mpipeline.plisttable;

import java.io.Serializable;
import java.util.ArrayList;
import mpipeline.utils.MXMLUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Ingo Diepholz <ingo.diepholz@aei.mpg.de>
 */
public class JParam implements Serializable {

  protected String key;
  protected JParamValue val = new JParamValue();
  protected String desc = "";
  protected String version = "";
  protected boolean active = false;

  /**
   * Construct a JParam from the given XML DOM node.
   * 
   * @param pn
   * @param curVer
   * @param fileVer
   */
  public JParam(Node pn, float curVer, float fileVer) {

//    Utils.msg("Reading param from XML node...");

    // Local variables to allow us to read old version plist files
    String strVal = null;
    String type = null;

    // read attributes
    NamedNodeMap nm = pn.getAttributes();
    key = MXMLUtils.getStringFromNode("key", nm, curVer, fileVer);
    desc = MXMLUtils.getStringFromNode("desc", nm, curVer, fileVer);
    active = MXMLUtils.getBooleanFromNode("active", nm, curVer, fileVer, true);

    // read child nodes
    NodeList cns = pn.getChildNodes();
    for (int jj = 0; jj < cns.getLength(); jj++) {
      Node cn = cns.item(jj);
      if (cn.getNodeName().equals("key")) {
        key = cn.getTextContent();
      }
      else if (cn.getNodeName().equals("val")) {
        strVal = cn.getTextContent();
      }
      else if (cn.getNodeName().equals("ParamValue")) {
        val = new JParamValue(cn, curVer, fileVer);
      }
      else if (cn.getNodeName().equals("type")) {
        type = cn.getTextContent();
      }
    }

    // If the 'val' field has empty options and we have a strVal and a type then we
    // create a new type JParamValue from the old param type.
    if (val.getSizeOfOptions() == 0 && strVal != null && type != null) {
      Object newVal = strVal;
      val = new JParamValue(newVal, "char");
    }
  }

  /**
   * Attach this JParam to the given XML DOM node.
   * 
   * @param doc
   * @param node
   */
  public void attachToDom(Document doc, Element node) {
    // start new document node
    Element docnode = doc.createElement("param");
    docnode.setAttribute("key", key);
    docnode.setAttribute("desc", desc);
    docnode.setAttribute("active", "" + active);

    // attach val
    val.attachToDom(doc, docnode);

    // attach to the DOM
    node.appendChild(docnode);

  }

  /**
   * Constructs an empty <code>JParam</code> (parameter object)
   */
  public JParam() {
  }

  /**
   * Copy constructor
   * @param p
   */
  public JParam(JParam p) {
    key = p.key;
    val = new JParamValue(p.val);
    desc = p.desc;
    active = p.active;
    version = p.version;
  }

  /**
   * Construct a JParam from the given key and JParamValue.
   * @param key
   * @param pval
   */
  public JParam(String key, JParamValue pval) {
    this.key = key;
    this.val = pval;
  }

  /**
   * Construct a JParam from the given key, JParamValue, and description.
   * @param key
   * @param pval
   * @param desc
   */
  public JParam(String key, JParamValue pval, String desc) {
    this.key = key;
    this.val = pval;
    this.desc = desc;
  }

  /**
   * Construct a JParam from the given key, value, and type.
   * @param key
   * @param aVal
   * @param valType
   */
  public JParam(String key, Object aVal, String valType) {
    this.key = key;
    this.val = new JParamValue(aVal, valType);
  }

  /**
   * Construct a JParam from the given key, value, type, and description.
   * @param key
   * @param val
   * @param valType
   * @param desc
   */
  public JParam(String key, Object val, String valType, String desc) {
    this.key = key;
    this.val = new JParamValue(val, valType);
    this.desc = desc;
  }

  /**
   * Add an optional value to this param.
   * @param val
   * @param type
   */
  public void addOption(Object val, String type) {
    this.val.addOption(val, type);
  }

  /**
   * Set the key of this parameter.
   * @param key
   */
  public void setKey(String key) {
    this.key = key;
  }

  /**
   * Set the description of this parameter.
   * @param desc
   */
  public void setDesc(String desc) {
    this.desc = desc.replaceAll("\\\\n", System.getProperty("line.separator"));
  }

  /**
   * Set the default value of the JParamValue of this parameter.
   * @param val
   */
  public void setDefaultVal(Object val) {
    this.val.setOption(val);
  }

  /**
   * Get the default value of the JParamValue of this parameter.
   * @return
   */
  public Object getDefaultVal() {

    return this.val.getOption(this.val.valIndex);
  }

  /**
   * Get the type of this parameter value.
   * @return
   */
  public String getValueType() {
    return this.val.getValType();
  }

  /**
   * Set the value of this parameter to the given JParamValue.
   * @param val
   */
  public void setVal(JParamValue val) {
    this.val = val;
  }

  /**
   * Get the JParamValue for this param.
   * @return
   */
  public JParamValue getVal() {
    return this.val;
  }

  /**
   * Get the key of this param.
   * @return
   */
  public String getKey() {
    return this.key;
  }

  /**
   * Display this parameter to standard out.
   */
  public void display() {
    String arr = JParamValue.displayArray(getDefaultVal());
    Object obj = getDefaultVal();
    if (obj == null) {
      obj = "";
    }

    String cl = obj.getClass().toString();
    if (arr.equals("")) {
      arr = "<empty>";
    }
    System.out.println("---- param ----");
    System.out.println("key:  " + key);
    System.out.println("val:  " + arr + "[" + cl + "]");
    System.out.println("desc: " + desc.replaceAll("\n", (System.getProperty("line.separator") + "      ")));
    System.out.println("----------------------");
  }

  /**
   * Get the description of this parameter.
   * @return
   */
  public String getDesc() {
    return desc;
  }

  public String toHTML() {
    String str = "";

    int max_size = 50;
    String vs = val.toString();
    if (vs.length() > max_size) {
      vs = vs.substring(0, max_size) + "...";
    }
    str += "<tr>";
    str += "<td><b>" + key + "</b></td>";
    str += "<td>" + vs + "</td>";
    str += "</tr>";

    return str;
  }

  public boolean isActive() {
    return active;
  }

  public void setActive(boolean state) {
    active = state;
  }

  public ArrayList<Object> getOptions() {
    return val.getOptions();
  }

  public void setDefaultValFromString(String strValue) {

    Object newVal = null;
    String valType = getValueType();

    if (valType.equals("double")) {

      try {
        newVal = Double.parseDouble(strValue);
      }
      catch (NumberFormatException e) {
        strValue = strValue.trim();

        if (!strValue.startsWith("[")) {
          strValue = "[" + strValue;
        }
        if (!strValue.endsWith("]")) {
          strValue = strValue + "]";
        }
        getVal().setDefaultOptionType("char");
        newVal = strValue;
      }
    }
    else if (valType.equals("boolean") || valType.equals("logical")) {

      if (strValue.equalsIgnoreCase("true") || strValue.equalsIgnoreCase("false")) {
        newVal = Boolean.parseBoolean(strValue);
      }
      else {
        strValue = strValue.trim();

        if (!strValue.startsWith("[")) {
          strValue = "[" + strValue;
        }
        if (!strValue.endsWith("]")) {
          strValue = strValue + "]";
        }
        getVal().setDefaultOptionType("char");
        newVal = strValue;
      }
    }
    else {
      newVal = strValue;
    }

    setDefaultVal(newVal);
  }
}