Exemplo n.º 1
0
        /// <summary>
        /// Creates a new solution file in the file system.
        /// </summary>
        /// <param name="newSolutionName"></param>
        /// <param name="newSolutionLocation"></param>
        /// <param name="newSourceFilePathName"></param>
        /// <param name="TargetFilesCollection"></param>
        private void CreateSolutionCommandExecuted(string newSolutionName,
                                                   string newSolutionLocation,
                                                   string newSourceFilePathName,
                                                   string newProjectComment,
                                                   IList <FileReferenceViewModel> TargetFilesCollection)
        {
            var msg = ServiceContainer.Instance.GetService <IMessageBoxService>();

            try
            {
                RequestApplicationEvent(new ApplicationRequestEvent(ApplicationRequest.InitProcessing));

                string msgString        = null;
                string solutionPathName = null;

                if (System.IO.Directory.Exists(newSolutionLocation) == false)
                {
                    msgString = string.Format(LocultApp.Local.Strings.STR_ERROR_ACCESSING_DIRECTORY, newSolutionLocation);
                }
                else
                {
                    if (System.IO.File.Exists(newSourceFilePathName) == false)
                    {
                        msgString = string.Format(LocultApp.Local.Strings.STR_ERROR_ACCESSING_FILE, newSourceFilePathName);
                    }
                    else
                    {
                        solutionPathName = System.IO.Path.Combine(newSolutionLocation, newSolutionName + TranslatorSolution._DefaultSolutionExtension);

                        if (System.IO.File.Exists(solutionPathName) == true)
                        {
                            msgString = string.Format(LocultApp.Local.Strings.STR_ERROR_ACCESSING_FILE_CREATE_WOULD_Overwrite, solutionPathName);
                        }
                        else
                        {
                            var item = TargetFilesCollection.SingleOrDefault(p => string.Compare(p.Path, newSourceFilePathName, true) == 0);

                            if (item != null)
                            {
                                msgString = string.Format(LocultApp.Local.Strings.STR_ERROR_DUPLICATE_REFERENCE, newSourceFilePathName);
                            }
                        }
                    }
                }

                // Show error message if a prerequisite failed.
                if (msgString != null)
                {
                    throw new Exception(msgString);
                }

                // Build solution model and save and reload it via requested action
                var solution = AppViewModel.ConvertToSolutionModel(solutionPathName, newSolutionName,
                                                                   newSourceFilePathName, newProjectComment,
                                                                   TargetFilesCollection);

                solution.SetComment(string.Format("{0}\n{1}: {2}",
                                                  TranslatorSolution._DefaultComment,
                                                  LocultApp.Local.Strings.STR_CREATED_WITH_DATE_TIME_APPENDED,
                                                  DateTime.Now));

                // switch view and reload saved solution
                if (RequestedAction != null)
                {
                    RequestedAction(this, new ApplicationRequestEvent(ApplicationRequest.NewSolution, solutionPathName, solution));
                }
            }
            catch (System.Exception exp)
            {
                if (RequestApplicationEvent(new ApplicationRequestEvent(ApplicationRequest.DisplayException, exp)) == false)
                {
                    msg.Show(exp);
                }
            }
        }