view src/MPipeline2/src/mpipeline/ltpdapreferences/ModelsPrefGroup.java @ 9:fbbfcd56e449 database-connection-manager

Remove dead code
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents f0afece42f48
children
line wrap: on
line source

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mpipeline.ltpdapreferences;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.EventListener;
import java.util.EventObject;
import java.util.Iterator;
import javax.swing.event.EventListenerList;
import mpipeline.utils.MXMLUtils;
import mpipeline.utils.Utils;
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 hewitson
 */
public class ModelsPrefGroup extends LTPDAPrefGroup {

  public static final int MODELS_SEARCH_PATHS_CHANGED = 1;
  private ArrayList<String> searchPaths = new ArrayList<String>();
  private EventListenerList listenerList = new EventListenerList();

  public ModelsPrefGroup() {
  }

  public ModelsPrefGroup(Node node, float version) {

    NodeList nl = node.getChildNodes();
    for (int ii = 0; ii < nl.getLength(); ii++) {
      Node n = nl.item(ii);
      Utils.dmsg(" reading child node: " + n.getNodeName());
      if (n.getNodeName().equals("SearchPath")) {
        NamedNodeMap wbnm = n.getAttributes();
        String path = MXMLUtils.getStringFromNode("Path", wbnm, version, version);
        this.addSearchPath(path);
      }
    }
  }

  public void attachToDom(Document doc, Element tnode) {

    Element pnode = doc.createElement("Models");

    if (searchPaths == null)
      return;
    
    Iterator it = searchPaths.iterator();
    while (it.hasNext()) {
      String path = (String) it.next();
      Element snode = doc.createElement("SearchPath");
      snode.setAttribute("Path", path);
      pnode.appendChild(snode);
    }

    tnode.appendChild(pnode);

  }

  public void addSearchPath(String path) {
    fireAddModelPath(new AddModelPathEvent(this, path));
    searchPaths.add(path);
    this.setChanged();
    this.notifyObservers(MODELS_SEARCH_PATHS_CHANGED);
  }

  public ArrayList<String> getSearchPaths() {
    return searchPaths;
  }

  public void setSearchPaths(ArrayList<String> searchPaths) {
    this.searchPaths = searchPaths;
    setChanged();
    notifyObservers(MODELS_SEARCH_PATHS_CHANGED);
  }

  public void display() {
    System.out.println("Search Paths: " + searchPaths);
  }

  public void removePathAtIndex(int idx) {
    fireRemoveModelPath(new RemoveModelPathEvent(this, searchPaths.get(idx)));
    searchPaths.get(idx);
    this.searchPaths.remove(idx);
    this.setChanged();
    this.notifyObservers(MODELS_SEARCH_PATHS_CHANGED);
  }

  public void removePaths(Object paths[]) {
    searchPaths.removeAll(Arrays.asList(paths));
    this.setChanged();
    this.notifyObservers(MODELS_SEARCH_PATHS_CHANGED);
  }

  public synchronized void addAddModelPathListener(AddModelPathListener l) {
    listenerList.add(AddModelPathListener.class, l);
  }

  public synchronized void removeAddModelPathListener(AddModelPathListener l) {
    listenerList.remove(AddModelPathListener.class, l);
  }

  public void addRemoveModelPathListener(RemoveModelPathListener l) {
    listenerList.add(RemoveModelPathListener.class, l);
  }

  public void removeRemoveModelPathListener(RemoveModelPathListener l) {
    listenerList.remove(RemoveModelPathListener.class, l);
  }

  protected void fireAddModelPath(AddModelPathEvent propertyChangedEvent) {
    EventListener listenerListArray[] = listenerList.getListeners(AddModelPathListener.class);
    for (int i = 0, n = listenerListArray.length; i < n; i++) {
      ((AddModelPathListener) listenerListArray[i]).addPath(propertyChangedEvent);
    }
  }

  protected void fireRemoveModelPath(RemoveModelPathEvent propertyChangedEvent) {
    EventListener listenerListArray[] = listenerList.getListeners(RemoveModelPathListener.class);
    for (int i = 0, n = listenerListArray.length; i < n; i++) {
      ((RemoveModelPathListener) listenerListArray[i]).removePath(propertyChangedEvent);
    }
  }

  public class RemoveModelPathEvent extends EventObject {

    public String path;

    RemoveModelPathEvent(Object source, String path) {
      super(source);
      this.path = path;
    }
    public String getPath() {
      return path;
    }
  }

  public class AddModelPathEvent extends EventObject {

    public Object path;

    AddModelPathEvent(Object source, String path) {
      super(source);
      this.path = path;
    }

    public Object getPath() {
      return path;
    }
  }

  public interface AddModelPathListener extends EventListener {
    void addPath(AddModelPathEvent e);
  }

  public interface RemoveModelPathListener extends EventListener {
    void removePath(RemoveModelPathEvent e);
  }

}