Ejemplo n.º 1
0
 public string GetFullFileName(ElementSave element)
 {
     return element.GetFullPathXmlFile();
 }
 public static string GetFullPathXmlFile(this ElementSave instance)
 {
     return(instance.GetFullPathXmlFile(instance.Name));
 }
Ejemplo n.º 3
0
        public void HandleRename(ElementSave elementSave, InstanceSave instance, string oldName)
        {
            try
            {
                IsRenamingXmlFile = instance == null;

                bool shouldContinue = true;

                if (instance != null)
                {
                    string whyNot;
                    if (NameVerifier.Self.IsInstanceNameValid(instance.Name, instance, elementSave, out whyNot) == false)
                    {
                        MessageBox.Show(whyNot);
                        shouldContinue = false;
                    }
                }

                if (shouldContinue && IsRenamingXmlFile)
                {
                    string message = "Are you sure you want to rename " + oldName + "?\n\n" +
                        "This will change the file name for " + oldName + " which may break " +
                        "external references to this object.";
                    var result = MessageBox.Show(message, "Rename Object and File?", MessageBoxButtons.YesNo);

                    shouldContinue = result == DialogResult.Yes;
                }

                if (shouldContinue)
                {
                    // Tell the GumProjectSave to react to the rename.
                    // This changes the names of the ElementSave references.
                    ProjectManager.Self.GumProjectSave.ReactToRenamed(elementSave, instance, oldName);

                    if (instance != null)
                    {
                        string newName = SelectedState.Self.SelectedInstance.Name;

                        foreach (StateSave stateSave in SelectedState.Self.SelectedElement.AllStates)
                        {
                            stateSave.ReactToInstanceNameChange(oldName, newName);
                        }

                        foreach (var eventSave in SelectedState.Self.SelectedElement.Events)
                        {
                            if (eventSave.GetSourceObject() == oldName)
                            {
                                eventSave.Name = instance.Name + "." + eventSave.GetRootName();
                            }
                        }
                    }

                    // Even though this gets called from the PropertyGrid methods which eventually
                    // save this object, we want to force a save here to make sure it worked.  If it
                    // does, then we're safe to delete the old files.
                    if (ProjectManager.Self.GeneralSettingsFile.AutoSave)
                    {
                        ProjectManager.Self.SaveElement(elementSave);
                    }

                    if (IsRenamingXmlFile)
                    {
                        // If we got here that means all went okay, so we should delete the old files
                        string oldXml = elementSave.GetFullPathXmlFile(oldName);
                        // Delete the XML.
                        // If the file doesn't
                        // exist, no biggie - we
                        // were going to delete it
                        // anyway.
                        if (System.IO.File.Exists(oldXml))
                        {
                            System.IO.File.Delete(oldXml);
                        }

                        PluginManager.Self.ElementRename(elementSave, oldName);

                        GumCommands.Self.FileCommands.TryAutoSaveProject();

                        GumCommands.Self.GuiCommands.RefreshElementTreeView(elementSave);
                    }
                }

                if (!shouldContinue && IsRenamingXmlFile)
                {
                    elementSave.Name = oldName;
                }
                else if (!shouldContinue && instance != null)
                {
                    instance.Name = oldName;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error renaming element " + elementSave.ToString() + "\n\n" + e.ToString());
            }
            finally
            {
                IsRenamingXmlFile = false;
            }
        }
Ejemplo n.º 4
0
 public static FilePath GetFullPathXmlFile(this ElementSave elementSave)
 {
     return(elementSave.GetFullPathXmlFile(elementSave.Name));
 }
Ejemplo n.º 5
0
        internal void SaveElement(ElementSave elementSave)
        {
            if (elementSave.IsSourceFileMissing)
            {
                MessageBox.Show("Cannot save " + elementSave + " because its source file is missing");
            }
            else
            {
                bool succeeded = true;

                UndoManager.Self.RecordUndo();

                bool doesProjectNeedToSave = false;
                bool shouldSave = AskUserForProjectNameIfNecessary(out doesProjectNeedToSave);

                if (doesProjectNeedToSave)
                {
                    SaveProject();
                }

                if (shouldSave)
                {
                    PluginManager.Self.BeforeElementSave(elementSave);

                    string fileName = elementSave.GetFullPathXmlFile();

                    // if it's readonly, let's warn the user
                    bool isReadOnly = IsFileReadOnly(fileName);

                    if (isReadOnly)
                    {
                        ShowReadOnlyDialog(fileName);
                    }
                    else
                    {
                        const int maxNumberOfTries = 5;
                        const int msBetweenSaves = 100;
                        int numberOfTimesTried = 0;

                        succeeded = false;
                        Exception exception = null;

                        while (numberOfTimesTried < maxNumberOfTries)
                        {
                            try
                            {
                                elementSave.Save(fileName);
                                succeeded = true;
                                break;
                            }
                            catch (Exception e)
                            {
                                exception = e;
                                System.Threading.Thread.Sleep(msBetweenSaves);
                                numberOfTimesTried++;
                            }
                        }

                        if (succeeded == false)
                        {
                            MessageBox.Show("Unknown error trying to save the file\n\n" + fileName + "\n\n" + exception.ToString());
                            succeeded = false;
                        }
                    }
                    if (succeeded)
                    {
                        OutputManager.Self.AddOutput("Saved " + elementSave + " to " + fileName);
                        PluginManager.Self.AfterElementSave(elementSave);
                    }
                }

                PluginManager.Self.Export(elementSave);
            }
        }