///////////////////////////////////////////////////////////
        public static void PopulateRightClickItems(TreeNode node, MenuShowingAction menuShowingAction = MenuShowingAction.RegularRightClick)
        {

            MainGlueWindow.Self.ElementTreeView.SelectedNode = node;
            MainGlueWindow form = MainGlueWindow.Self;

            ContextMenuStrip menu = MainGlueWindow.Self.mElementContextMenu;

            menu.Items.Clear();


            #region IsScreenNode

            if (node.IsScreenNode())
            {
                if (menuShowingAction == MenuShowingAction.RightButtonDrag)
                {
                    menu.Items.Add(mAddEntityInstance);
                    menu.Items.Add(mAddEntityList);
                }
                else
                {
                    menu.Items.Add(form.setAsStartUpScreenToolStripMenuItem);
                    menu.Items.Add(mMakeRequiredAtStartup);
                    mExportElement.Text = "Export Screen";
                    menu.Items.Add(mExportElement);

                    AddRemoveFromProjectItems(form, menu);

                    if (EditorLogic.CurrentScreenSave.IsRequiredAtStartup)
                    {
                        mMakeRequiredAtStartup.Text = "Remove StartUp Requirement";
                    }
                    else
                    {
                        mMakeRequiredAtStartup.Text = "Make Required at StartUp";
                    }


                    menu.Items.Add("-");
                    menu.Items.Add(mFindAllReferences);
                    menu.Items.Add(mRefreshTreeNodesMenuItem);
                }
            }

            #endregion

            #region IsEntityNode

            else if (node.IsEntityNode())
            {
                if (menuShowingAction == MenuShowingAction.RightButtonDrag)
                {
                    menu.Items.Add(mAddEntityInstance);
                    menu.Items.Add(mAddEntityList);
                }
                else
                {
                    AddRemoveFromProjectItems(form, menu);

                    menu.Items.Add("-");
                    mExportElement.Text = "Export Entity";
                    menu.Items.Add(mExportElement);
                    menu.Items.Add(mFindAllReferences);

                    EntitySave entitySave = ((EntityTreeNode)node).EntitySave;

                    if (entitySave.PooledByFactory)
                    {
                        menu.Items.Add(mAddResetVariablesForPooling);
                    }
                    menu.Items.Add(mRefreshTreeNodesMenuItem);
                }
            }

            #endregion

            #region IsFileContainerNode OR IsFolderInFilesContainerNode

            else if (node.IsFilesContainerNode() || node.IsFolderInFilesContainerNode())
            {
                menu.Items.Add(form.addFileToolStripMenuItem);
                menu.Items.Add(form.addFolderToolStripMenuItem);
                menu.Items.Add("-");
                menu.Items.Add(form.viewInExplorerToolStripMenuItem);
                if (node.IsFolderInFilesContainerNode())
                {
                    menu.Items.Add(mDeleteFolder);
                }
            }

            #endregion

            #region IsScreenObjectContainerNode

            else if (node.IsRootObjectNode())
            {
                if (menuShowingAction == MenuShowingAction.RightButtonDrag)
                {
                    menu.Items.Add(mAddEntityInstance);
                    menu.Items.Add(mAddEntityList);
                }
                else
                {
                    menu.Items.Add(form.addObjectToolStripMenuItem);
                }
            }

            #endregion

            #region IsGlobalContentContainerNode
            else if (node.IsGlobalContentContainerNode())
            {
                menu.Items.Add(form.addFileToolStripMenuItem);
                menu.Items.Add(form.addFolderToolStripMenuItem);
                menu.Items.Add(form.reGenerateCodeToolStripMenuItem);

                menu.Items.Add(form.viewInExplorerToolStripMenuItem);

                menu.Items.Add(mViewFileLoadOrder);
            }
            #endregion

            #region IsRootEntityNode
            else if (node.IsRootEntityNode())
            {
                menu.Items.Add(form.addEntityToolStripMenuItem);

                mImportElement.Text = "Import Entity";
                menu.Items.Add(mImportElement);

                menu.Items.Add(form.addFolderToolStripMenuItem);
            }
            #endregion

            #region IsRootScreenNode
            else if (node.IsRootScreenNode())
            {
                menu.Items.Add(form.addScreenToolStripMenuItem);

                mImportElement.Text = "Import Screen";
                menu.Items.Add(mImportElement);

            }
            #endregion

            #region IsRootBehaviorsNode
            else if (node.IsRootBehaviorsNode())
            {
                menu.Items.Add(form.addBehaviorToolStripMenuItem);
                menu.Items.Add(form.openBehaviorFolderToolStripMenuItem);
                menu.Items.Add(form.importBehaviorToolStripMenuItem);
            }
            #endregion

            #region IsRootCustomVariables

            else if (node.IsRootCustomVariablesNode())
            {
                menu.Items.Add(form.addVariableToolStripMenuItem);
            }

            #endregion

            #region IsRootEventNode
            else if (node.IsRootEventsNode())
            {
                menu.Items.Add(mAddEventMenuItem);
            }
            #endregion

            #region IsNamedObjectNode

            else if (node.IsNamedObjectNode())
            {
                AddRemoveFromProjectItems(form, menu);

                menu.Items.Add(form.editResetVariablesToolStripMenuItem);
                menu.Items.Add(mFindAllReferences);

                menu.Items.Add("-");

                menu.Items.Add(mDuplicate);

                menu.Items.Add("-");

                menu.Items.Add(mMoveToTop);
                menu.Items.Add(mMoveUp);
                menu.Items.Add(mMoveDown);
                menu.Items.Add(mMoveToBottom);

                menu.Items.Add("-");

                NamedObjectSave currentNamedObject = EditorLogic.CurrentNamedObject;

                if (currentNamedObject.SourceType == SourceType.FlatRedBallType &&
                    currentNamedObject.SourceClassType == "PositionedObjectList<T>" &&
                    !string.IsNullOrEmpty(currentNamedObject.SourceClassGenericType) &&
                    !currentNamedObject.SetByDerived)
                {
                    menu.Items.Add(form.addObjectToolStripMenuItem);
                }

            }

            #endregion

            #region IsReferencedFileNode
            else if (node.IsReferencedFile())
            {
                menu.Items.Add(form.viewInExplorerToolStripMenuItem);
                menu.Items.Add(mFindAllReferences);
                menu.Items.Add("Copy path to clipboard", null, HandleCopyToClipboardClick);
                menu.Items.Add("-");

                menu.Items.Add(mCreateZipPackage);
                menu.Items.Add(mRecreateCompanionFiles);

                menu.Items.Add("-");

                AddRemoveFromProjectItems(form, menu);

                menu.Items.Add(mUseContentPipeline);
                //menu.Items.Add(form.openWithDEFAULTToolStripMenuItem);

                ReferencedFileSave rfs = (ReferencedFileSave)node.Tag;

                if (FileManager.GetExtension(rfs.Name) == "csv" || rfs.TreatAsCsv)
                {
                    menu.Items.Add("-");
                    menu.Items.Add(form.setCreatedClassToolStripMenuItem);
                    menu.Items.Add(form.reGenerateCodeToolStripMenuItem);
                }


                if (!string.IsNullOrEmpty(rfs.SourceFile) || rfs.SourceFileCache.Count != 0)
                {
                    menu.Items.Add("-");
                    menu.Items.Add(mViewSourceInExplorer);
                    menu.Items.Add(mRebuildFile);
                }

                menu.Items.Add(mCopyToBuildFolder);

                if (!File.Exists(ProjectManager.MakeAbsolute(rfs.Name, true)))
                {
                    menu.Items.Add(mCreateNewFileForMissingFile);
                }
            }

            #endregion

            #region IsCustomVariable
            else if (node.IsCustomVariable())
            {
                AddRemoveFromProjectItems(form, menu);

                menu.Items.Add("-");


                menu.Items.Add(mFindAllReferences);

                menu.Items.Add("-");
                    
                menu.Items.Add(mMoveToTop);
                menu.Items.Add(mMoveUp);
                menu.Items.Add(mMoveDown);
                menu.Items.Add(mMoveToBottom);
            }

            #endregion

            #region IsBehaviorNode
            else if (node.IsBehaviorNode())
            {

                AddRemoveFromProjectItems(form, menu);

            }

            #endregion

            #region IsCodeNode
            else if (node.IsCodeNode())
            {

                menu.Items.Add(form.viewInExplorerToolStripMenuItem);
                menu.Items.Add(form.reGenerateCodeToolStripMenuItem);
            }

            #endregion

            #region IsRootCodeNode

            else if (node.IsRootCodeNode())
            {
                menu.Items.Add(form.reGenerateCodeToolStripMenuItem);
            }


            #endregion

            #region IsDirectoryNode
            else if (node.IsDirectoryNode())
            {
                //menu.Items.Add(form.viewInExplorerToolStripMenuItem);
                menu.Items.Add(mViewContentFilesInExplorer);
                menu.Items.Add(mViewCodeFolderInExplorer);
                menu.Items.Add("-");


                menu.Items.Add(form.addFolderToolStripMenuItem);

                if (node.Root().IsRootEntityNode())
                {
                    menu.Items.Add(form.addEntityToolStripMenuItem);


                    mImportElement.Text = "Import Entity";
                    menu.Items.Add(mImportElement);
                }
                else
                {
                    // If not in the Entities tree structure, assume global content
                    menu.Items.Add(form.addFileToolStripMenuItem);
                }

                menu.Items.Add("-");

                menu.Items.Add(mDeleteFolder);

            }

            #endregion

            #region IsStateListNode

            else if (node.IsStateListNode())
            {
                menu.Items.Add(mAddState);
                menu.Items.Add(mAddStateCategory);
            }

            #endregion

            #region IsStateCategoryNode
            else if (node.IsStateCategoryNode())
            {
                menu.Items.Add(mAddState);
                AddRemoveFromProjectItems(form, menu);

            }
            #endregion

            #region IsStateNode

            else if (node.IsStateNode())
            {
                AddRemoveFromProjectItems(form, menu);

                menu.Items.Add("-");
                menu.Items.Add(mDuplicate);
                menu.Items.Add("-");
                menu.Items.Add(mFillValuesFromDefault);
            }

            #endregion

            #region IsEventTreeNode

            else if (node.IsEventResponseTreeNode())
            {
                AddRemoveFromProjectItems(form, menu);

            }

            #endregion
            PluginManager.ReactToTreeViewRightClick(node, menu);
        }
        private static void MoveCustomVariable(TreeNode nodeMoving, TreeNode targetNode)
        {
            CustomVariable customVariable = nodeMoving.Tag as CustomVariable;

            if (targetNode.IsRootEventsNode())
            {
                // The user dragged a variable onto the events node, so they want to make
                // an event for this.  We'll assume an "after" event since I think no one makes
                // before events
             

                if (customVariable != null)
                {
                    customVariable.CreatesEvent = true;

                    FlatRedBall.Glue.Events.EventResponseSave eventResponseSave = new Events.EventResponseSave();
                    eventResponseSave.EventName = "After" + customVariable.Name + "Set";

                    eventResponseSave.SourceObject = null;
                    eventResponseSave.SourceObjectEvent = null;

                    eventResponseSave.SourceVariable = customVariable.Name;
                    eventResponseSave.BeforeOrAfter = BeforeOrAfter.After;

                    eventResponseSave.DelegateType = null;

                    RightClickHelper.AddEventToElementAndSave(EditorLogic.CurrentElement, eventResponseSave);

                }
            }
            else if (targetNode.IsRootCustomVariablesNode())
            {
                // let's see if the user is moving a variable from one element to another
                IElement sourceElement = nodeMoving.GetContainingElementTreeNode().Tag as IElement;
                IElement targetElement = targetNode.GetContainingElementTreeNode().Tag as IElement;

                if (sourceElement != targetElement)
                {
                    // copying a variable from one element to another
                    // eventually we need to add some error checking here.
                    CustomVariable newVariable = customVariable.Clone();

                    targetElement.CustomVariables.Add(newVariable);


                    GlueCommands.Self.GenerateCodeCommands.GenerateElementCode(targetElement);
                    GlueCommands.Self.RefreshCommands.RefreshUi(targetElement);
                }
            }
        }
        private static void MoveNamedObject(TreeNode treeNodeMoving, TreeNode targetNode)
        {
            if (targetNode != null)
            {
                NamedObjectSave targetNos = targetNode.Tag as NamedObjectSave;
                NamedObjectSave movingNos = treeNodeMoving.Tag as NamedObjectSave;

                bool succeeded = false;
                if (targetNode == null)
                {
                    // Didn't move on to anything
                }
                else if (targetNode.IsRootNamedObjectNode())
                {
                    succeeded = MoveObjectOnObjectsRoot(treeNodeMoving, targetNode, movingNos, succeeded);
                }
                else if (targetNode.IsRootCustomVariablesNode())
                {
                    MoveObjectOnRootCustomVariablesNode(treeNodeMoving, targetNode);
                }
                else if (targetNode.Tag is IElement)
                {
                    succeeded = DragDropNosIntoElement(movingNos, targetNode.Tag as IElement);
                }
                else if (targetNode.IsRootEventsNode())
                {
                    succeeded = DragDropNosOnRootEventsNode(treeNodeMoving, targetNode);
                }
                else if (targetNos != null && targetNos.SourceType == SourceType.FlatRedBallType)
                {
                    string targetClassType = targetNos.SourceClassType;

                    #region Failure cases

                    if (string.IsNullOrEmpty(targetClassType))
                    {
                        MessageBox.Show("The target Object does not have a defined type.  This operation is not valid");
                    }

                    #endregion

                    #region Moving on to a Layer

                    else if (targetClassType == "Layer")
                    {
                        // Only allow this if the NOS's are on the same object
                        if (ObjectFinder.Self.GetElementContaining(movingNos) ==
                            ObjectFinder.Self.GetElementContaining(targetNos))
                        {
                            succeeded = true;
                            movingNos.LayerOn = targetNos.InstanceName;
                            MainGlueWindow.Self.PropertyGrid.Refresh();
                        }
                    }

                    #endregion

                    #region Moving on to a PositionedObjectList

                    else if (targetClassType == "PositionedObjectList<T>")
                    {
                        succeeded = HandleDropOnList(treeNodeMoving, targetNode, targetNos, movingNos);
                    }
                    #endregion
                }
                else
                {
                    MessageBox.Show("Invalid movement");
                }


                if (succeeded)
                {
                    if (EditorLogic.CurrentElement != null)
                    {
                        ElementViewWindow.GenerateSelectedElementCode();

                    }
                    else
                    {
                        ContentLoadWriter.UpdateLoadGlobalContentCode();
                    }
                    ProjectManager.SaveProjects();
                    GluxCommands.Self.SaveGlux();
                }
            }
        }
        private static void MoveStateCategory(TreeNode nodeMoving, TreeNode targetNode)
        {
            if (targetNode.IsRootCustomVariablesNode() || targetNode.IsCustomVariable())
            {
                // The user drag+dropped a state category into the variables
                // Let's make sure that it's all in the same Element though:
                if (targetNode.GetContainingElementTreeNode() == nodeMoving.GetContainingElementTreeNode())
                {
                    StateSaveCategory category = nodeMoving.Tag as StateSaveCategory;

                    // expose a variable that exposes the category
                    CustomVariable customVariable = new CustomVariable();

                    if (category.SharesVariablesWithOtherCategories)
                    {
                        customVariable.Type = "VariableState";
                        customVariable.Name = "CurrentState";
                    }
                    else
                    {
                        customVariable.Type = category.Name;
                        customVariable.Name = "Current" + category.Name + "State";
                    }

                    IElement element = targetNode.GetContainingElementTreeNode().Tag as IElement;

                    element.CustomVariables.Add(customVariable);
                    GlueCommands.Self.GenerateCodeCommands.GenerateCurrentElementCode();

                    EditorLogic.CurrentElementTreeNode.UpdateReferencedTreeNodes();
                }
            }
        }