view src/MPipeline2/src/mpipeline/plisttable/JPlist.java @ 5:5a49956df427 database-connection-manager

LTPDAPreferences panel for new LTPDADatabaseConnectionManager
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents f0afece42f48
children
line wrap: on
line source

/*
 * Class JPlist: Creates a parameter list 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 java.util.Iterator;
import java.util.UUID;
import mpipeline.canvas.MBlock;
import mpipeline.ltpdapreferences.LTPDAPreferences;
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;

/**
 * Model class that implements the behaviour of the ltpda/plist class from the
 * matlab side.
 * 
 * @author Ingo Diepholz <ingo.diepholz@aei.mpg.de>
 */
public class JPlist implements Serializable {

  MBlock owner = null;
  protected ArrayList<JParam> params = new ArrayList<JParam>();
  protected long created;
  protected Object creator;
  protected String version = "no cvs string";
  protected String name = "none";
  protected String description = "";
  protected UUID uuid = UUID.randomUUID();

  /**
   * Constructs en empty parameter list object.
   *
   */
  public JPlist() {
  }

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

    NamedNodeMap nm = pn.getAttributes();
    // get name attribute
    name = MXMLUtils.getNameFromNode(nm, curVer, fileVer);
    // get created attribute
    created = MXMLUtils.getIntegerFromNode("created", nm, curVer, fileVer);
    // get description attribute
    description = MXMLUtils.getStringFromNode("description", nm, curVer, fileVer);
    // get uuid from node
    uuid = MXMLUtils.getUUIDfromNode(nm, curVer, fileVer);
    if (uuid == null) {
      uuid = UUID.randomUUID();
    }

    // read child nodes
    NodeList cns = pn.getChildNodes();
    for (int jj = 0; jj < cns.getLength(); jj++) {
      Node cn = cns.item(jj);
      if (cn.getNodeName().equals("param")) {
        JParam p = new JParam(cn, curVer, fileVer);
        params.add(p);
      }
    }

  }

  /**
   * Attach this JPlist 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("plist");
    docnode.setAttribute("name", name);
    docnode.setAttribute("created", "" + created);
    docnode.setAttribute("description", description);
    docnode.setAttribute("UUID", uuid.toString());


    // attach each parameter
    Iterator it = params.iterator();
    while (it.hasNext()) {
      JParam p = (JParam) it.next();
      p.attachToDom(doc, docnode);
    }

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

  public void clear() {
    params.clear();
  }

  /**
   * Copy constructor
   * @param jPlist
   */
  public JPlist(JPlist jPlist) {

    Iterator it = jPlist.params.iterator();
    while (it.hasNext()) {
      JParam p = (JParam) it.next();
      JParam np = new JParam(p);
      params.add(np);
    }

    created = jPlist.created;
    creator = jPlist.creator;
    version = jPlist.version;
    name = jPlist.name;
    description = jPlist.description;
    uuid = jPlist.uuid;


  }

  /**
   * Construct a JPlist with the given name.
   * @param aName
   */
  public JPlist(String aName) {
    name = aName;
  }

  /**
   * Remove the given JParam from the params array.
   * 
   * @param p
   */
  public void removeParam(JParam p) {
    params.remove(p);
  }

  /**
   * Adds a key/value pair to the parameter list
   *
   * @param key Key of the key/value pair
   * @param val Value of the key/value pair
   * @param valType MATLAB object type of the value
   */
  public void add(String key, Object val, String valType) {

    // check name
    String newKey = checkParamKey(key);


    this.params.add(new JParam(newKey, val, valType));
  }

  /**
   * Adds a key/value pair and the related description to the parameter list.
   *
   * @param key Key of the key/value pair
   * @param val Value of the key/value pair
   * @param valType MATLAB object type of the value
   * @param desc Description of the parameter
   */
  public void add(String key, Object val, String valType, String desc) {

    String newKey = checkParamKey(key);
    this.params.add(new JParam(newKey, val, valType, desc));
  }

  /**
   * Check a parameter key and return a valid key.
   * @param s
   * @return
   */
  private String checkParamKey(String s) {

    s = s.toUpperCase();

    Iterator it = params.iterator();
    while (it.hasNext()) {
      JParam pt = (JParam) it.next();
      if (pt.getKey().equals(s)) {
        s += "_1";
      }
    }

    return s;
  }

  /**
   * Adds a parameter object (JParam) to the parameter list.
   *
   * @param p
   */
  public void add(JParam p) {
    String newKey = checkParamKey(p.getKey());
    p.setKey(newKey);
    this.params.add(p);
  }

  /**
   * Retruns the numer of parameters in the parameter list.
   *
   * @return int
   */
  public int getLength() {
    return params.size();
  }

  /**
   * Returns the JParam object at the given index in to the params array.
   * 
   * @param i
   * @return
   */
  public JParam getParam(int i) {
    return params.get(i);
  }

  /**
   * Returns a string representation of the parameter list.
   *
   * @return a string representation of the parameter list.
   */
  @Override
  public String toString() {
    String str = "pl = ";
    if (this.params.isEmpty()) {
      str = "empty plist";
    }
    else {
      for (int i = 0; i < this.params.size(); i++) {
        str += "(" + this.params.get(i).key + ", " + this.params.get(i).val.toString() + ")";
      }
    }
    return str;
  }

  public String toHTMLtable() {

    String str = "<html>";
    if (this.params.isEmpty()) {
      str += "<i>empty</i></html";
    }
    else {
      str += "<table cellspacing=\"0\" border=\"1\">";
      Iterator it = params.iterator();
      while (it.hasNext()) {
        JParam p = (JParam) it.next();
        if (p.isActive()) {
          str += p.toHTML() + "<br>";
        }
      }
      str += "</table>";
    }

    return str;
  }

  /**
   * Display this JPlist to standard output.
   */
  public void display() {
    System.out.println("----------- plist -----------");
    System.out.println("n params: " + params.size());
    Iterator it = params.iterator();
    while (it.hasNext()) {
      JParam pp = (JParam) it.next();
      pp.display();
    }
    System.out.println("description: " + description);
    System.out.println("UUID:        " + uuid);
    System.out.println("-----------------------------");
  }

  /**
   * Get the JParam object with the given key, or null if none is found.
   * 
   * @param key
   * @return
   */
  public JParam getParamForKey(String key) {
    Iterator it = params.iterator();
    while (it.hasNext()) {
      JParam p = (JParam) it.next();
      if (p.getKey().equalsIgnoreCase(key)) {
        return p;
      }
    }
    return null;
  }

  /**
   * Returns the params array.
   * 
   * @return
   */
  public ArrayList<JParam> getParams() {
    return params;
  }

  /**
   * Returns the UUID for this plist.
   * 
   * @return
   */
  public UUID getUUID() {
    return uuid;
  }

  /**
   * Set the UUID for this JPlist.
   * 
   * @param uuid
   */
  public void setUUID(UUID uuid) {
    this.uuid = uuid;
  }

  /**
   * Get the created time of this plist.
   * 
   * @return
   */
  public long getCreated() {
    return created;
  }

  /**
   * Set the created time of this plist.
   * 
   * @param created
   */
  public void setCreated(long created) {
    this.created = created;
  }

  /**
   * Get the creator object.
   * @return
   */
  public Object getCreator() {
    return creator;
  }

  /**
   * Set the creator object.
   * @param creator
   */
  public void setCreator(Object creator) {
    this.creator = creator;
  }

  /**
   * Get the description for this plist.
   * 
   * @return
   */
  public String getDescription() {
    return description;
  }

  /**
   * Set the description of this plist.
   * 
   * @param description
   */
  public void setDescription(String description) {
    this.description = description;
  }

  /**
   * Get the name of this plist.
   * 
   * @return
   */
  public String getName() {
    return name;
  }

  /**
   * Set the name for this plist.
   * 
   * @param name
   */
  public void setName(String name) {
    this.name = name;
  }

  /**
   * Get the block that owns this plist.
   * @return
   */
  public MBlock getOwner() {
    return owner;
  }

  /**
   * Set the block that owns this plist.
   * @param owner
   */
  public void setOwner(MBlock owner) {
    this.owner = owner;
  }

  /**
   * Get the CVS version string from this plist.
   *
   * @return
   */
  public String getVersion() {
    return version;
  }

  /**
   * Set the CVS version string of this plist.
   * 
   * @param version
   */
  public void setVersion(String version) {
    this.version = version;
  }

  /**
   * Remove the parameter with the given key.
   * @param pm
   */
  public void removeParam(String pm) {
    Iterator it = params.iterator();
    JParam rp = null;
    while (it.hasNext()) {
      JParam p = (JParam) it.next();
      if (p.getKey().equals(pm)) {
        rp = p;
        break;
      }
    }
    if (rp != null) {
      params.remove(rp);
    }
  }

  /**
   * Set the default value of the param with the given key.
   * @param key
   * @param value
   */
  public void setValueForKey(String key, Object value) {
    Iterator it = params.iterator();
    while (it.hasNext()) {
      JParam p = (JParam) it.next();
      if (p.getKey().toLowerCase().equals(key.toLowerCase())) {
        p.setDefaultVal(value);
        break;
      }
    }
  }

  /**
   * Set the key of the parameter with the given key.
   * @param key
   * @param newKey
   */
  public void setKeyForKey(String key, String newKey) {
    Iterator it = params.iterator();
    while (it.hasNext()) {
      JParam p = (JParam) it.next();
      if (p.getKey().equals(key)) {
        p.setKey(newKey);
        break;
      }
    }
  }

  /**
   * Returns the number of parameters in this plist.
   * @return
   */
  public int getNparams() {
    return params.size();
  }

  /**
   *
   * @param rowIndex
   * @return
   */
  public Object getKey(int rowIndex) {
    return params.get(rowIndex).getKey();
  }

  /**
   * Returns the value of the parameter at the given index in the params array.
   * @param rowIndex
   * @return
   */
  public Object getVal(int rowIndex) {
    return params.get(rowIndex).getDefaultVal();
  }

  public void appendPlist(JPlist pl) {
    Iterator it = pl.params.iterator();
    while (it.hasNext()) {
      JParam p = (JParam) it.next();
      this.add(p);
    }
  }

  public void activateAllParams() {
    Iterator it = params.iterator();
    while (it.hasNext()) {
      JParam p = (JParam) it.next();
      p.setActive(true);
    }
  }

  public void deactivateAllParams() {
    Iterator it = params.iterator();
    while (it.hasNext()) {
      JParam p = (JParam) it.next();
      p.setActive(false);
    }
  }

  /**
   * This method replaces different key with the default values in the preferences
   *
   * @param ltpdaPreferences2
   */
  public void replaceKeysWithDefaultPreferences(LTPDAPreferences ltpdaPreferences2) {
    // Loop over all parameters and set if necessary the default values from
    // the preferences to the parameter. At the moment is this necessary for:
    // HOSTNAME
    // WIN
    Iterator it = params.iterator();
    while (it.hasNext()) {
      JParam p = (JParam) it.next();
      if (p.getKey().equalsIgnoreCase("WIN")) {
        String defaultWin = ltpdaPreferences2.getMiscPrefs().getDefaultWindow();
        p.setDefaultVal(defaultWin);
      }
    }
  }
}