/* (non-Javadoc)
  * @see com.google.gwt.cell.client.AbstractCell#render(com.google.gwt.cell.client.Cell.Context, java.lang.Object, com.google.gwt.safehtml.shared.SafeHtmlBuilder)
  */
 @Override
 public void render(Context context, CellTreeNode cellTreeNode, SafeHtmlBuilder sb) {
   if (cellTreeNode == null) {
     return;
   }
   // TODO :  We can add classes based on the NodeType with the specified image.
   // The classes will be picked up from Mat.css
   if ((cellTreeNode.getNodeType() == CellTreeNode.MASTER_ROOT_NODE)
       || (cellTreeNode.getNodeType() == CellTreeNode.ROOT_NODE)) {
     sb.append(
         template.outerDiv(
             getStyleClass(cellTreeNode),
             cellTreeNode.getTitle(),
             cellTreeNode.getLabel() != null
                 ? cellTreeNode.getLabel()
                 : cellTreeNode.getName()));
   } else {
     sb.append(
         template.outerDivItem(
             getStyleClass(cellTreeNode),
             cellTreeNode.getTitle(),
             cellTreeNode.getLabel() != null
                 ? cellTreeNode.getLabel()
                 : cellTreeNode.getName()));
   }
 }
 /* (non-Javadoc)
  * @see mat.client.clause.clauseworkspace.presenter.XmlTreeDisplay#editNode(java.lang.String, java.lang.String)
  */
 @Override
 public void editNode(String name, String label) {
   if (selectedNode != null) {
     selectedNode.setName(name);
     selectedNode.setLabel(label);
     closeParentOpenNodes(cellTree.getRootTreeNode());
   }
 }
 /**
  * Iterating through all the child nodes and setting the isOpen boolean to false.
  *
  * @param node the new open to false
  */
 private void setOpenToFalse(CellTreeNode node) {
   if (node.hasChildren()) {
     for (CellTreeNode child : node.getChilds()) {
       child.setOpen(false);
       setOpenToFalse(child);
     }
   }
 }
 /* (non-Javadoc)
  * @see mat.client.clause.clauseworkspace.presenter.XmlTreeDisplay#removeNode()
  */
 @Override
 public void removeNode() {
   if (selectedNode != null) {
     CellTreeNode parent = selectedNode.getParent();
     parent.removeChild(selectedNode);
     closeParentOpenNodes(cellTree.getRootTreeNode());
     selectionModel.setSelected(parent, true);
   }
 }
 /* (non-Javadoc)
  * @see mat.client.clause.clauseworkspace.presenter.XmlTreeDisplay#paste()
  */
 @Override
 public void paste() {
   if (selectedNode != null) {
     CellTreeNode pasteNode = copiedNode.cloneNode();
     selectedNode.appendChild(pasteNode);
     closeSelectedOpenNodes(cellTree.getRootTreeNode());
     selectionModel.setSelected(selectedNode, true);
     copiedNode = pasteNode;
   }
 }
 /* (non-Javadoc)
  * @see com.google.gwt.view.client.TreeViewModel#isLeaf(java.lang.Object)
  */
 @Override
 public boolean isLeaf(Object value) {
   if (value instanceof CellTreeNode) {
     CellTreeNode t = (CellTreeNode) value;
     if (!t.hasChildren()) {
       return true;
     }
   }
   return false;
 }
 /* (non-Javadoc)
  * @see mat.client.clause.clauseworkspace.presenter.XmlTreeDisplay#addNode(java.lang.String, java.lang.String, java.lang.String, short)
  */
 @Override
 public CellTreeNode addNode(String name, String label, String uuid, short nodeType) {
   CellTreeNode childNode = null;
   if ((selectedNode != null)
       && (name != null)
       && (name.trim().length() > 0)) { // if nodeTex textbox is not empty
     childNode = selectedNode.createChild(name, label, nodeType);
     childNode.setUUID(uuid);
     closeSelectedOpenNodes(cellTree.getRootTreeNode());
     selectionModel.setSelected(selectedNode, true);
   }
   return childNode;
 }
 /* (non-Javadoc)
  * @see com.google.gwt.view.client.TreeViewModel#getNodeInfo(java.lang.Object)
  */
 @Override
 public <T> NodeInfo<?> getNodeInfo(T value) {
   if (value == null) {
     NodeCell nodeCell = new NodeCell();
     return new DefaultNodeInfo<CellTreeNode>(nodeDataProvider, nodeCell, selectionModel, null);
   } else {
     CellTreeNode myValue = (CellTreeNode) value;
     ListDataProvider<CellTreeNode> dataProvider =
         new ListDataProvider<CellTreeNode>(myValue.getChilds());
     NodeCell nodeCell = new NodeCell();
     return new DefaultNodeInfo<CellTreeNode>(dataProvider, nodeCell, selectionModel, null);
   }
 }
  /**
   * Gets the style class.
   *
   * @param cellTreeNode the cell tree node
   * @return the style class
   */
  private String getStyleClass(CellTreeNode cellTreeNode) {

    if (cellTreeNode.getValidNode() != false) {
      switch (cellTreeNode.getNodeType()) {
        case CellTreeNode.ROOT_NODE:
          return "cellTreeRootNode";
        default:
          break;
      }
    } else {
      return "clauseWorkSpaceInvalidNode";
    }
    return "";
  }
  /* (non-Javadoc)
   * @see mat.client.clause.clauseworkspace.presenter.XmlTreeDisplay#validateCellTreeNodes(com.google.gwt.user.cellview.client.TreeNode)
   */
  @Override
  public boolean validateCellTreeNodes(TreeNode treeNode) {

    if (treeNode != null) {
      openAllNodes(treeNode);
      for (int i = 0; i < treeNode.getChildCount(); i++) {
        TreeNode subTree = null;
        CellTreeNode node = (CellTreeNode) treeNode.getChildValue(i);
        if ((node.getNodeType() == CellTreeNode.TIMING_NODE)
            || (node.getNodeType() == CellTreeNode.RELATIONSHIP_NODE)) {
          // this check is performed since IE was giving JavaScriptError after removing a node and
          // closing all nodes.
          subTree = treeNode.setChildOpen(i, true, true);
          if ((subTree != null) && (subTree.getChildCount() == 2)) {
            if (!node.getValidNode()) {
              editNode(true, node, subTree);
            }
          } else {
            editNode(false, node, subTree);
            if (isValid) {
              isValid = false;
            }
          }
        }
        subTree =
            treeNode.setChildOpen(
                i,
                ((CellTreeNode) treeNode.getChildValue(i)).isOpen(),
                ((CellTreeNode) treeNode.getChildValue(i)).isOpen());
        if ((subTree != null) && (subTree.getChildCount() > 0)) {
          validateCellTreeNodes(subTree);
        }
      }
    }
    return isValid;
  }
 /**
  * This method is called after removing or editing of the node. When a node is removed, parent
  * node is closed first and then opened. Remaining all nodes will be opened or closed based on the
  * isOpen boolean in CellTreeNode
  *
  * @param treeNode the tree node
  */
 private void closeParentOpenNodes(TreeNode treeNode) {
   if (treeNode != null) {
     for (int i = 0; i < treeNode.getChildCount(); i++) {
       TreeNode subTree = null;
       if (treeNode.getChildValue(i).equals(selectedNode.getParent())) {
         // this check is performed since IE was giving JavaScriptError
         // after removing a node and closing all nodes.
         // to avoid that we are closing the parent of the removed node.
         subTree = treeNode.setChildOpen(i, false, false);
       }
       subTree = treeNode.setChildOpen(i, ((CellTreeNode) treeNode.getChildValue(i)).isOpen());
       if ((subTree != null) && (subTree.getChildCount() > 0)) {
         closeParentOpenNodes(subTree);
       }
     }
   }
 }
 /* (non-Javadoc)
  * @see mat.client.clause.clauseworkspace.presenter.XmlTreeDisplay#editNode(boolean, mat.client.clause.clauseworkspace.model.CellTreeNode, com.google.gwt.user.cellview.client.TreeNode)
  */
 @Override
 public void editNode(boolean isValideNodeValue, CellTreeNode node, TreeNode subTree) {
   node.setValidNode(isValideNodeValue);
   selectedNode = node;
   closeParentOpenNodes(cellTree.getRootTreeNode());
 }
  /* (non-Javadoc)
   * @see com.google.gwt.event.dom.client.KeyDownHandler#onKeyDown(com.google.gwt.event.dom.client.KeyDownEvent)
   */
  @Override
  public void onKeyDown(KeyDownEvent event) {
    //		System.out.println(event.getNativeKeyCode());
    int keyCode = event.getNativeKeyCode();
    if (selectedNode != null) {
      short nodeType = selectedNode.getNodeType();
      if (event.isControlKeyDown()) {
        if (keyCode == ClauseConstants.COPY_C) { // COPY
          if ((nodeType != CellTreeNode.MASTER_ROOT_NODE) && (nodeType != CellTreeNode.ROOT_NODE)) {
            popupPanel.hide();
            copy();
          }

        } else if (keyCode == ClauseConstants.PASTE_V) { // PASTE
          boolean canPaste = false;
          popupPanel.hide();
          if (copiedNode != null) {
            switch (selectedNode.getNodeType()) {
              case CellTreeNode.ROOT_NODE:
                if (selectedNode.equals(copiedNode.getParent())) {
                  clauseWorkspaceContextMenu.pasteRootNodeTypeItem();
                  isDirty = true;
                }
                break;
              case CellTreeNode.LOGICAL_OP_NODE:
              case CellTreeNode.FUNCTIONS_NODE:
                if (copiedNode.getNodeType() != CellTreeNode.CLAUSE_NODE) {
                  canPaste = true;
                }
                break;
              case CellTreeNode.TIMING_NODE:
                if ((copiedNode.getNodeType() != CellTreeNode.CLAUSE_NODE)
                    && ((selectedNode.getChilds() == null)
                        || (selectedNode.getChilds().size() < 2))) {
                  canPaste = true;
                }
                break;
              default:
                break;
            }
            if (canPaste) {
              paste();
              isDirty = true;
            }
          }

        } else if (keyCode == ClauseConstants.CUT_X) { // CUT
          popupPanel.hide();
          if ((selectedNode.getNodeType() != CellTreeNode.MASTER_ROOT_NODE)
              && (selectedNode.getNodeType() != CellTreeNode.CLAUSE_NODE)
              && (selectedNode.getNodeType() != CellTreeNode.ROOT_NODE)
              && (selectedNode.getParent().getNodeType() != CellTreeNode.CLAUSE_NODE)) {
            copy();
            removeNode();
            isDirty = true;
          }
        }
      } else if (keyCode == ClauseConstants.DELETE_DELETE) { // DELETE
        popupPanel.hide();
        if (((selectedNode.getNodeType() != CellTreeNode.MASTER_ROOT_NODE)
                && (selectedNode.getNodeType() != CellTreeNode.ROOT_NODE)
                && (selectedNode.getParent().getNodeType() != CellTreeNode.CLAUSE_NODE)
                && (selectedNode.getNodeType() != CellTreeNode.CLAUSE_NODE))
            || ((selectedNode.getNodeType() == CellTreeNode.CLAUSE_NODE)
                && (selectedNode.getParent().getChilds().size() > 1))) {
          removeNode();
          isDirty = true;
        }
      }
    }
    if ((event.isShiftKeyDown()
        && ((keyCode == ClauseConstants.PLUS_FF) || (keyCode == ClauseConstants.PLUS_IE)))) {
      // EXPAND/COLLAPSE (+(Shift +) Expand| - Collapse)
      popupPanel.hide();
      openAllNodes(cellTree.getRootTreeNode());
    } else if ((event.isShiftKeyDown()
        && ((keyCode == ClauseConstants.MINUS_FF) || (keyCode == ClauseConstants.MINUS_IE)))) {
      popupPanel.hide();
      closeNodes(cellTree.getRootTreeNode());
    }
    /*if(event.isControlKeyDown() && event.isAltKeyDown() && keyCode == 83){
    	saveBtn.getElement().focus();
    	saveBtn.click();
    }*/
  }
 /**
  * Creates the Root Node in the CellTree. Sets the Root node to the ListData Provider.
  *
  * @param cellTreeNode the cell tree node
  */
 private void createRootNode(CellTreeNode cellTreeNode) {
   nodeDataProvider = new ListDataProvider<CellTreeNode>(cellTreeNode.getChilds());
 }