Beispiel #1
0
        /// <summary>
        /// Write text content to disk and (re-)set associated properties
        /// </summary>
        /// <param name="filePath"></param>
        public override bool SaveFile(string filePath)
        {
            File.WriteAllText(filePath, Document.Text);

            // Set new file name in viewmodel and model
            FilePath  = filePath;
            ContentId = filePath;
            MDocumentModel.SetFileNamePath(filePath, true);

            IsDirty = false;

            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Attempt to open a file and load it into the viewmodel if it exists.
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns>True if file exists and was succesfully loaded. Otherwise false.</returns>
        protected bool OpenFile(string filePath)
        {
            try
            {
                var isReal = File.Exists(filePath);

                if (isReal == true)
                {
                    if (MDocumentModel == null)
                    {
                        MDocumentModel = new DocumentModel();
                    }

                    MDocumentModel.SetFileNamePath(filePath, isReal);

                    FilePath  = filePath;
                    ContentId = _mFilePath;

                    // Mark document loaded from persistence as unedited copy (display without dirty mark '*' in name)
                    IsDirty = false;

                    try
                    {
                        // XXX TODO Extend log4net FileOpen method to support base.FireFileProcessingResultEvent(...);
                        _mDocumentMiniUml.LoadFile(_mFilePath);
                    }
                    catch (Exception ex)
                    {
                        var msgBox = ServiceLocator.Current.GetInstance <IMessageBoxService>();
                        msgBox.Show(ex, ex.Message, "An error has occurred", MsgBoxButtons.OK);

                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception exp)
            {
                var msgBox = ServiceLocator.Current.GetInstance <IMessageBoxService>();
                msgBox.Show(exp, exp.Message, "An error has occurred", MsgBoxButtons.OK);

                return(false);
            }

            return(true);
        }
Beispiel #3
0
        /// <summary>
        /// Write text content to disk and (re-)set associated properties
        /// </summary>
        /// <param name="filePath"></param>
        public override bool SaveFile(string filePath)
        {
            _mDocumentMiniUml.ExecuteSave(filePath);

            if (MDocumentModel == null)
            {
                MDocumentModel = new DocumentModel();
            }

            MDocumentModel.SetFileNamePath(filePath, true);
            FilePath  = filePath;
            ContentId = filePath;
            IsDirty   = false;

            return(true);
        }
Beispiel #4
0
 /// <summary>
 /// Class constructor from <seealso cref="IDocumentModel"/> parameter.
 /// </summary>
 /// <param name="documentModel"></param>
 public EdiViewModel(IDocumentModel documentModel)
     : this()
 {
     MDocumentModel.SetFileNamePath(documentModel.FileNamePath, documentModel.IsReal);
 }
Beispiel #5
0
        /// <summary>
        /// Attempt to open a file and load it into the viewmodel if it exists.
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns>True if file exists and was succesfully loaded. Otherwise false.</returns>
        protected bool OpenFile(string filePath)
        {
            try
            {
                var isReal = File.Exists(filePath);
                MDocumentModel.SetFileNamePath(filePath, isReal);

                if (IsFilePathReal)
                {
                    MDocumentModel.SetIsReal(IsFilePathReal);
                    FilePath  = filePath;
                    ContentId = _FilePath;
                    IsDirty   = false; // Mark document loaded from persistence as unedited copy (display without dirty mark '*' in name)

                    // Check file attributes and set to read-only if file attributes indicate that
                    if ((File.GetAttributes(filePath) & FileAttributes.ReadOnly) != 0)
                    {
                        IsReadOnly       = true;
                        IsReadOnlyReason = Util.Local.Strings.STR_FILE_READONLY_REASON_NO_WRITE_PERMISSION;
                    }

                    try
                    {
                        using (FileStream fs = new FileStream(_FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            using (StreamReader reader = FileReader.OpenStream(fs, Encoding.Default))
                            {
                                TextDocument doc = new TextDocument(reader.ReadToEnd());
                                doc.SetOwnerThread(Application.Current.Dispatcher.Thread);
                                Application.Current.Dispatcher.BeginInvoke(
                                    new Action(
                                        delegate
                                {
                                    Document = doc;
                                }), DispatcherPriority.Normal);

                                FileEncoding = reader.CurrentEncoding; // assign encoding after ReadToEnd() so that the StreamReader can autodetect the encoding
                            }
                        }

                        // Set the correct actualy state of the model into the viewmodel
                        // to either allow editing or continue to block editing depending on what the model says
                        IsReadOnly = MDocumentModel.IsReadonly;

                        State = DocumentState.IsEditing;
                    }
                    catch                // File may be blocked by another process
                    {                    // Try read-only shared method and set file access to read-only
                        try
                        {
                            IsReadOnly       = true; // Open file in readonly mode
                            IsReadOnlyReason = Util.Local.Strings.STR_FILE_READONLY_REASON_USED_BY_OTHER_PROCESS;

                            using (FileStream fs = new FileStream(_FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                            {
                                using (StreamReader reader = FileReader.OpenStream(fs, Encoding.Default))
                                {
                                    TextDocument doc = new TextDocument(reader.ReadToEnd());
                                    doc.SetOwnerThread(Application.Current.Dispatcher.Thread);
                                    Application.Current.Dispatcher.BeginInvoke(
                                        new Action(
                                            delegate
                                    {
                                        Document = doc;
                                    }), DispatcherPriority.Normal);

                                    FileEncoding = reader.CurrentEncoding; // assign encoding after ReadToEnd() so that the StreamReader can autodetect the encoding
                                }
                            }

                            State = DocumentState.IsEditing;
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(Util.Local.Strings.STR_FILE_OPEN_ERROR_MSG_CAPTION, ex);
                        }
                    }
                }
                else
                {
                    throw new FileNotFoundException(filePath);   // File does not exist
                }
            }
            catch (Exception exp)
            {
                throw new Exception(Util.Local.Strings.STR_FILE_OPEN_ERROR_MSG_CAPTION, exp);
            }

            return(true);
        }
Beispiel #6
0
        // Constructors are not intended for external usage outside of this class
        // Use the static methods provided to initialize this viewmodel

        /// <summary>
        /// Class constructor from <seealso cref="IDocumentModel"/> parameter.
        /// </summary>
        /// <param name="documentModel"></param>
        protected EdiViewModel(IDocumentModel documentModel, IMessageBoxService IMsgBox)
            : this(IMsgBox)
        {
            MDocumentModel.SetFileNamePath(documentModel.FileNamePath, documentModel.IsReal);
        }