private static void RemoveFromProjectOptionalSaveAndRegenerate(bool saveAndRegenerate, bool askAreYouSure, bool askToDelete)
        {
            // delete object, remove object, DeleteObject, RemoveObject, remove from project, 
            // remove from screen, remove from entity, remove file
            ///////////////////////////////EARLY OUT///////////////////////////////////////
            // This can now be called by pushing Delete, so we should check if deleting is valid
            if (EditorLogic.CurrentTreeNode == null || EditorLogic.CurrentTreeNode.Parent == null ||
                EditorLogic.CurrentTreeNode.Text.EndsWith(".cs") || EditorLogic.CurrentTreeNode.Tag == null)
            {
                return;
            }
            //////////////////////////////END EARLY OUT/////////////////////////////////////

            lock (FileWatchManager.LockObject)
            {
                // Search terms: removefromproject, remove from project, remove file, remove referencedfilesave
                List<string> filesToRemove = new List<string>();

                if (ProjectManager.StatusCheck() == ProjectManager.CheckResult.Passed)
                {
                    #region Find out if the user really wants to remove this - don't ask if askAreYouSure is false
                    DialogResult reallyRemoveResult = DialogResult.Yes;

                    if (askAreYouSure)
                    {
                        string message = "Are you sure you want to remove this:\n\n" + EditorLogic.CurrentTreeNode.Tag.ToString();

                        reallyRemoveResult =
                            MessageBox.Show(message, "Remove?", MessageBoxButtons.YesNo);
                    }
                    #endregion

                    if (reallyRemoveResult == DialogResult.Yes)
                    {
                        #region If is NamedObjectSave
                        // test deep first
                        if (EditorLogic.CurrentNamedObject != null)
                        {
                            ProjectManager.RemoveNamedObject(EditorLogic.CurrentNamedObject, true, true, filesToRemove);
                            //ProjectManager.RemoveNamedObject(EditorLogic.CurrentNamedObject);
                        }
                        #endregion

                        #region Else if is StateSave
                        else if (EditorLogic.CurrentStateSave != null)
                        {
                            var name = EditorLogic.CurrentStateSave.Name;

                            EditorLogic.CurrentElement.RemoveState(EditorLogic.CurrentStateSave);

                            AskToRemoveCustomVariablesWithoutState(EditorLogic.CurrentElement);

                            EditorLogic.CurrentElementTreeNode.UpdateReferencedTreeNodes();

                            PluginManager.ReactToStateRemoved(EditorLogic.CurrentElement, name);

                            


                            GluxCommands.Self.SaveGlux();
                        }

                        #endregion

                        #region Else if is StateSaveCategory

                        else if (EditorLogic.CurrentStateSaveCategory != null)
                        {
                            EditorLogic.CurrentElement.StateCategoryList.Remove(EditorLogic.CurrentStateSaveCategory);

                            EditorLogic.CurrentElementTreeNode.UpdateReferencedTreeNodes();

                            GluxCommands.Self.SaveGlux();
                        }

                        #endregion

                        #region Else if is ReferencedFileSave

                        else if (EditorLogic.CurrentReferencedFile != null)
                        {
                            var toRemove = EditorLogic.CurrentReferencedFile;
                            // this could happen at the same time as file flushing, which can cause locks.  Therefore we need to add this as a task:
                            TaskManager.Self.AddSync(() =>
                            {
                                GluxCommands.Self.RemoveReferencedFile(toRemove, filesToRemove, saveAndRegenerate);
                            },
                            "Remove file " + toRemove.ToString());
                            //ProjectManager.RemoveReferencedFile(EditorLogic.CurrentReferencedFile);
                        }
                        #endregion

                        #region Else if is CustomVariable

                        else if (EditorLogic.CurrentCustomVariable != null)
                        {
                            ProjectManager.RemoveCustomVariable(EditorLogic.CurrentCustomVariable, filesToRemove);
                            //ProjectManager.RemoveCustomVariable(EditorLogic.CurrentCustomVariable);
                        }

                        #endregion

                        #region Else if is EventSave
                        else if (EditorLogic.CurrentEventResponseSave != null)
                        {
                            EditorLogic.CurrentElement.Events.Remove(EditorLogic.CurrentEventResponseSave);
                            GlueCommands.Self.RefreshCommands.RefreshUiForSelectedElement();
                        }
                        #endregion

                        #region Else if is ScreenSave

                        // Then test higher if deep didn't get removed
                        else if (EditorLogic.CurrentScreenSave != null)
                        {
                            var screenToRemove = EditorLogic.CurrentScreenSave;
                            TaskManager.Self.AddSync(() =>
                            {
                                RemoveScreen(screenToRemove, filesToRemove);
                            },
                            "Remove screen");
                        }

                        #endregion

                        #region Else if is EntitySave

                        else if (EditorLogic.CurrentEntitySave != null)
                        {
                            RemoveEntity(EditorLogic.CurrentEntitySave, filesToRemove);
                            //ProjectManager.RemoveEntity(EditorLogic.CurrentEntitySave);
                        }

                        #endregion


                        #region Files were deleted and the user wants to be asked to delete

                        if (filesToRemove.Count != 0 && askToDelete)
                        {

                            for (int i = 0; i < filesToRemove.Count; i++)
                            {
                                if (FileManager.IsRelative(filesToRemove[i]))
                                {
                                    filesToRemove[i] = ProjectManager.MakeAbsolute(filesToRemove[i]);
                                }
                                filesToRemove[i] = filesToRemove[i].Replace("\\", "/");
                            }

                            StringFunctions.RemoveDuplicates(filesToRemove, true);

                            ListBoxWindow lbw = new ListBoxWindow();
                            
                            string messageString = "What would you like to do with the following files:\n";
                            lbw.Message = messageString;

                            foreach (string s in filesToRemove)
                            {
                                
                                lbw.AddItem(s);
                            }
                            lbw.ClearButtons();
                            lbw.AddButton("Nothing - leave them as part of the game project", DialogResult.No);
                            lbw.AddButton("Remove them from the project but keep the files", DialogResult.OK);
                            lbw.AddButton("Remove and delete the files", DialogResult.Yes);


                            DialogResult result = lbw.ShowDialog();

                            if (result == DialogResult.OK || result == DialogResult.Yes)
                            {
                                foreach (string file in filesToRemove)
                                {
                                    string fileName = ProjectManager.MakeAbsolute(file);
                                    // This file may have been removed
                                    // in windows explorer, and now removed
                                    // from Glue.  Check to prevent a crash.

                                    ProjectManager.RemoveItemFromAllProjects(fileName, false);
                                }
                            }

                            if (result == DialogResult.Yes)
                            {
                                foreach (string file in filesToRemove)
                                {
                                    string fileName = ProjectManager.MakeAbsolute(file);
                                    // This file may have been removed
                                    // in windows explorer, and now removed
                                    // from Glue.  Check to prevent a crash.
                                    if (File.Exists(fileName))
                                    {
                                        FileHelper.DeleteFile(fileName);
                                    }
                                }
                            }
                        }

                        #endregion

                        if (saveAndRegenerate)
                        {

                            Action regenerateAction = null;

                            if (EditorLogic.CurrentScreenTreeNode != null)
                            {
                                var screen = EditorLogic.CurrentScreenSave;
                                regenerateAction = () =>
                                    FlatRedBall.Glue.CodeGeneration.CodeGeneratorIElement.GenerateElementAndDerivedCode(screen);
                            }
                            else if (EditorLogic.CurrentEntityTreeNode != null)
                            {
                                var entity = EditorLogic.CurrentEntitySave;
                                regenerateAction = () =>
                                    FlatRedBall.Glue.CodeGeneration.CodeGeneratorIElement.GenerateElementAndDerivedCode(entity);
                            }
                            else if (EditorLogic.CurrentReferencedFile != null)
                            {
                                regenerateAction = ContentLoadWriter.UpdateLoadGlobalContentCode;

                                // Vic asks - do we have to do anything else here?  I don't think so...
                            }



                            TaskManager.Self.AddSync(() =>
                                {
                                    if (regenerateAction != null)
                                    {
                                        regenerateAction();
                                    }
                                    ProjectManager.SaveProjects();
                                    GluxCommands.Self.SaveGlux();
                                },
                                "Save and regenerate after removal");
                            
                        }
                    }
                }
            }
        }
        void HandleAfteruseGlobalContentChanged(object sender, MemberChangeArgs args)
        {
            IElement element = Instance as IElement;
                
            List<IElement> elementsToMakeGlobal = new List<IElement>();

            if (element.UseGlobalContent)
            {
                GetAllElementsReferencedThroughObjectsRecursively(element, elementsToMakeGlobal);

                if (elementsToMakeGlobal.Count != 0)
                {
                    string message = "Setting " + FileManager.RemovePath(element.Name) +
                        " to use a global content manager will result in the following Entities being " +
                        " loaded with a global content manager.  What would you like to do?";

                    ListBoxWindow lbw = new ListBoxWindow();
                    lbw.Message = message;

                    foreach (var item in elementsToMakeGlobal)
                    {
                        lbw.AddItem(item);
                    }

                    lbw.ClearButtons();
                    lbw.AddButton("Set all to use global content", System.Windows.Forms.DialogResult.Yes);
                    lbw.AddButton("Nothing - this may result in runtime errors", System.Windows.Forms.DialogResult.No);
                    var result = lbw.ShowDialog();

                    if (result == System.Windows.Forms.DialogResult.Yes)
                    {
                        foreach (IElement toMakeGlobal in elementsToMakeGlobal)
                        {
                            toMakeGlobal.UseGlobalContent = true;
                            GlueCommands.Self.GenerateCodeCommands.GenerateElementCode(toMakeGlobal);
                        }
                    }
                }
            }
        }