<commit_before>package edu.colorado.csdms.wmt.client.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;

import edu.colorado.csdms.wmt.client.control.DataManager;
import edu.colorado.csdms.wmt.client.data.Component;
import edu.colorado.csdms.wmt.client.data.ModelJSO;
import edu.colorado.csdms.wmt.client.data.ModelMetadataJSO;
import edu.colorado.csdms.wmt.client.data.Port;

/**
 * A ModelTree is used to graphically represent the construction of a
 * simulation through component models, each represented by a
 * {@link ModelCell}.
 * 
 * @author <NAME> (<EMAIL>)
 */
public class ModelTree extends Tree {

  public DataManager data; // experimenting with a public member variable
  private ComponentCell driverComponentCell;

  /**
   * Creates a ModelTree with an open "driver" port.
   * 
   * @param data A DataManager object.
   */
  public ModelTree(DataManager data) {

    this.data = data;
    initializeTree();
  }

  /**
   * A worker that sets up the root TreeItem (the "driver") of the ModelTree.
   * It also initializes the {@link ModelJSO} and {@link ModelMetadataJSO}
   * objects used to save the model created with this ModelTree.
   */
  public void initializeTree() {

    this.clear();
    
    driverComponentCell = new ComponentCell(data);
    HTML driverCell = new HTML("<i class='fa fa-play-circle fa-2x'></i>");
    driverCell.setStyleName("mwmb-driverCell");
    
    Grid driverGrid = new Grid(1, 2);
    driverGrid.setWidget(0, 0, driverComponentCell);
    driverGrid.setWidget(0, 1, driverCell);
    
    TreeItem driverItem = new TreeItem(driverGrid);
    driverComponentCell.setEnclosingTreeItem(driverItem);
    this.addItem(driverItem);
    
    ModelJSO model = (ModelJSO) ModelJSO.createObject();
    data.setModel(model);
    ModelMetadataJSO metadata =
        (ModelMetadataJSO) ModelMetadataJSO.createObject();
    data.setMetadata(metadata);
  }

  public ComponentCell getDriverComponentCell() {
    return driverComponentCell;
  }

  public void setDriverComponentCell(ComponentCell driverComponentCell) {
    this.driverComponentCell = driverComponentCell;
  }

  /**
   * Adds a new TreeItem with a ModelCell to the ModelTree at the targeted
   * leaf location, or at the root if the target is missing.
   * 
   * @param port the Port used to create a ModelCell for the TreeItem
   * @param target the targeted leaf TreeItem
   * @return the reference to the created TreeItem
   */
  @Deprecated
  public TreeItem addTreeItem(Port port, TreeItem target) {

    ModelCell cell = new ModelCell(port, Component.makeInfoComponent());

    TreeItem item = null;
    if (target == null) {
      item = new TreeItem(cell);
      this.addItem(item);
    } else {
      item = target.addItem(cell);
      item.setStyleName("wmt-TreeItem");
    }
    cell.setParentTreeItem(item); // Clumsy

    return item;
  }

  /**
   * Adds a new TreeItem with a {@link ComponentCell} to the ModelTree at the
   * targeted leaf location.
   * 
   * @param portId the id of the exposed uses port at the leaf location
   * @param target the targeted leaf TreeItem
   * @return the reference to the created TreeItem
   */
  public TreeItem addTreeItem(String portId, TreeItem target) {
    ComponentCell cell = new ComponentCell(data, portId);
    Grid container = new Grid(1, 1);
    container.setWidget(0, 0, cell);
    TreeItem item = target.addItem(container);
    item.setStyleName("wmt-TreeItem");
    cell.setEnclosingTreeItem(item);
    return item;
  }
  
  /**
   * Adds a component to the {@link ComponentCell} used by the targeted
   * TreeItem. Uses {@link #setComponent(String, TreeItem)}.
   * 
   * @param componentId the id of the component to add
   * @param target the TreeItem to which the component is to be added
   */
  public void addComponent(String componentId, TreeItem target) {
    
    String componentName = data.getComponent(componentId).getName();
    GWT.log("Adding component: " + componentName);
    this.setComponent(componentId, target);
    target.setState(true);<commit_msg>Add method ModelTree#insertTreeItem<commit_after>package edu.colorado.csdms.wmt.client.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;

import edu.colorado.csdms.wmt.client.control.DataManager;
import edu.colorado.csdms.wmt.client.data.Component;
import edu.colorado.csdms.wmt.client.data.ModelJSO;
import edu.colorado.csdms.wmt.client.data.ModelMetadataJSO;
import edu.colorado.csdms.wmt.client.data.Port;

/**
 * A ModelTree is used to graphically represent the construction of a
 * simulation through component models, each represented by a
 * {@link ModelCell}.
 * 
 * @author <NAME> (<EMAIL>)
 */
public class ModelTree extends Tree {

  public DataManager data; // experimenting with a public member variable
  private ComponentCell driverComponentCell;

  /**
   * Creates a ModelTree with an open "driver" port.
   * 
   * @param data A DataManager object.
   */
  public ModelTree(DataManager data) {

    this.data = data;
    initializeTree();
  }

  /**
   * A worker that sets up the root TreeItem (the "driver") of the ModelTree.
   * It also initializes the {@link ModelJSO} and {@link ModelMetadataJSO}
   * objects used to save the model created with this ModelTree.
   */
  public void initializeTree() {

    this.clear();
    
    driverComponentCell = new ComponentCell(data);
    HTML driverCell = new HTML("<i class='fa fa-play-circle fa-2x'></i>");
    driverCell.setStyleName("mwmb-driverCell");
    
    Grid driverGrid = new Grid(1, 2);
    driverGrid.setWidget(0, 0, driverComponentCell);
    driverGrid.setWidget(0, 1, driverCell);
    
    TreeItem driverItem = new TreeItem(driverGrid);
    driverComponentCell.setEnclosingTreeItem(driverItem);
    this.addItem(driverItem);
    
    ModelJSO model = (ModelJSO) ModelJSO.createObject();
    data.setModel(model);
    ModelMetadataJSO metadata =
        (ModelMetadataJSO) ModelMetadataJSO.createObject();
    data.setMetadata(metadata);
  }

  public ComponentCell getDriverComponentCell() {
    return driverComponentCell;
  }

  public void setDriverComponentCell(ComponentCell driverComponentCell) {
    this.driverComponentCell = driverComponentCell;
  }

  /**
   * Adds a new TreeItem with a ModelCell to the ModelTree at the targeted
   * leaf location, or at the root if the target is missing.
   * 
   * @param port the Port used to create a ModelCell for the TreeItem
   * @param target the targeted leaf TreeItem
   * @return the reference to the created TreeItem
   */
  @Deprecated
  public TreeItem addTreeItem(Port port, TreeItem target) {

    ModelCell cell = new ModelCell(port, Component.makeInfoComponent());

    TreeItem item = null;
    if (target == null) {
      item = new TreeItem(cell);
      this.addItem(item);
    } else {
      item = target.addItem(cell);
      item.setStyleName("wmt-TreeItem");
    }
    cell.setParentTreeItem(item); // Clumsy

    return item;
  }

  /**
   * Adds a new TreeItem with a {@link ComponentCell} to the ModelTree at the
   * targeted leaf location. Uses
   * {@link #insertTreeItem(String, TreeItem, Integer)}.
   * 
   * @param portId the id of the exposed uses port at the leaf location
   * @param target the targeted leaf TreeItem
   * @return the reference to the created TreeItem
   */
  public TreeItem addTreeItem(String portId, TreeItem target) {
    return insertTreeItem(portId, target, target.getChildCount());
  }

  /**
   * Inserts a new TreeItem with a {@link ComponentCell} to the ModelTree at the
   * targeted leaf location.
   * 
   * @param portId the id of the exposed uses port at the leaf location
   * @param target the targeted leaf TreeItem
   * @param index the index into the children of the targeted TreeItem
   * @return the reference to the created TreeItem
   */
  public TreeItem insertTreeItem(String portId, TreeItem target, Integer index) {
    ComponentCell cell = new ComponentCell(data, portId);
    Grid container = new Grid(1, 1);
    container.setWidget(0, 0, cell);
    TreeItem item = target.insertItem(index, container);
    item.setStyleName("wmt-TreeItem");
    cell.setEnclosingTreeItem(item);
    return item;
  }

  /**
   * Adds a component to the {@link ComponentCell} used by the targeted
   * TreeItem. Uses {@link #setComponent(String, TreeItem)}.
   * 
   * @param componentId the id of the component to add
   * @param target the TreeItem to which the component is to be added
   */
  public void addComponent(String componentId, TreeItem target) {
    
    String componentName = data.getComponent(componentId).getName();
    GWT.log("Adding component: " + componentName);
    this.setComponent(componentId, target);
    target.setState(true);
{'fraction_non_alphanumeric': 0.09904222899434044, 'fraction_numerical': 0.00239442751414889, 'mean_word_length': 3.599099099099099, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '823676', 'n_tokens_mistral': 2792, 'n_tokens_neox': 2732, 'n_words': 816}