private void SaveDocumentAs(DocumentWindow window)
        {
            string filename = BuildFileBrowser.BrowseForSave();
            if (filename != null)
            {
                NAntDocument document = _documents[window];

                try
                {
                    document.SaveAs(filename, window.Contents);
                    document.BuildFinished += _mainForm.SetStateStopped;

                    Settings.Default.SaveScriptInitialDir = document.Directory;
                    Settings.Default.Save();

                    RecentItems.Add(filename);
                    _mainForm.CreateRecentItemsMenu();
                    UpdateTitle();
                    UpdateDisplay();
                }
                catch (Exception ex)
                {
                    Errors.CouldNotSave(document.Name, ex.Message);
                }
            }
        }
        private void SetupWindow(DocumentWindow window, NAntDocument doc)
        {
            _documents.Add(window, doc);
            _mainForm.AddDocumentMenuItem(doc);

            window.Contents = doc.Contents;
            window.TabText = doc.Name;

            window.DocumentChanged += WindowDocumentChanged;
            window.FormClosing += CloseDocument;
            window.FormClosed += WindowFormClosed;
            window.CloseClicked += delegate { Close(); };
            window.CloseAllClicked += delegate { CloseAllDocuments(); };
            window.CloseAllButThisClicked += delegate { CloseAllButThisClicked(); };
            window.SaveClicked += delegate { SaveDocument(); };
            window.Leave += WindowLeave;
            window.Enter += WindowEnter;
            window.Show(_mainForm.DockPanel);
        }
 private bool IsDirty(DocumentWindow window)
 {
     return window.Contents != _documents[window].Contents;
 }
        private void SaveDocument(DocumentWindow window)
        {
            NAntDocument document = _documents[window];

            if (document.FileType == FileType.New)
            {
                SaveDocumentAs(window);
            }
            else if (IsDirty(window))
            {
                try
                {
                    document.Save(window.Contents, true);

                    if (window == ActiveWindow)
                    {
                        List<IBuildTarget> targets = _mainForm.SelectedTargets;
                        UpdateTitle();
                        UpdateDisplay();
                        _mainForm.SelectedTargets = targets;
                    }
                }
                catch (Exception ex)
                {
                    Errors.CouldNotSave(document.Name, ex.Message);
                }
            }
        }
 private void CreateNewProject(object sender, NewProjectEventArgs e)
 {
     NAntDocument doc = new NAntDocument(_outputWindow, _options);
     DocumentWindow window = new DocumentWindow(doc.FullName);
     SetupWindow(window, doc);
     window.Contents = Utils.GetNewDocumentContents(e.Info);
     ParseBuildFile(doc);
 }
        internal void ReloadWindow(DocumentWindow window)
        {
            if (!IsDirty(window) || Errors.ReloadUnsaved() == DialogResult.Yes)
            {
                NAntDocument document = _documents[window];

                try
                {
                    TextLocation position = window.CaretPosition;
                    document.Reload();
                    window.Contents = document.Contents;
                    window.MoveCaretTo(position.Line, position.Column);
                    UpdateDisplay();
                }
                catch (Exception ex)
                {
                    Errors.CouldNotLoadFile(document.FullName, ex.Message);
                }
            }
        }
 internal void NewBlankDocument()
 {
     NAntDocument doc = new NAntDocument(_outputWindow, _options);
     DocumentWindow window = new DocumentWindow(doc.FullName);
     SetupWindow(window, doc);
 }
        internal void LoadDocument(string filename)
        {
            DocumentWindow window = FindDocumentWindow(filename);

            if (window != null)
            {
                window.Select();
                ReloadWindow(window);
            }
            else if (!File.Exists(filename))
            {
                Errors.FileNotFound(filename);
            }
            else
            {
                NAntDocument doc = new NAntDocument(filename, _outputWindow, _options);
                doc.BuildFinished += _mainForm.SetStateStopped;

                Settings.Default.OpenScriptDir = doc.Directory;
                Settings.Default.Save();

                window = new DocumentWindow(doc.FullName);
                SetupWindow(window, doc);

                RecentItems.Add(doc.FullName);

                // Parse the file in the background
                _loader.RunWorkerAsync();
            }
        }
        internal DocumentWindow GetWindow(string filename)
        {
            DocumentWindow window = null;

            if (File.Exists(filename))
            {
                NAntDocument doc = new NAntDocument(filename, _outputWindow, _options);
                doc.BuildFinished += _mainForm.SetStateStopped;

                window = new DocumentWindow(doc.FullName);
                SetupWindow(window, doc);

                RecentItems.Add(doc.FullName);

                ParseBuildFile(doc);
            }
            else
            {
                Errors.FileNotFound(filename);
            }

            return window;
        }