/// <summary>
 ///
 /// </summary>
 /// <param name="doc"></param>
 public void CreateDoc(ucBaseDocument doc)
 {
     doc.DocChanged = false;
     this.DocumentHost.Items.Add(doc);
     doc.Activate();
     doc.LoadProperties(this.lucDocumentProperties);
 }
 /// <summary>
 /// Select File Node
 /// </summary>
 /// <param name="pni"></param>
 void lucSolutionExplorer_SelectFileNode(PropertyNodeItem pni)
 {
     if (this.DocumentHost.Items.Count > 0)
     {
         foreach (Control c in this.DocumentHost.Items)
         {
             if (((ucBaseDocument)c).DocPath != null && ((ucBaseDocument)c).DocPath.ToLower() == pni.Value.ToLower())
             {
                 ((ucBaseDocument)c).Activate();
                 return;
             }
         }
     }
     if (File.Exists(pni.Value))
     {
         FileInfo       finfo = new FileInfo(pni.Value);
         ucBaseDocument doc   = new ucBaseDocument();
         doc.DocExt     = finfo.Extension;
         doc.DocPath    = pni.Value;
         doc.DocType    = DocumentType.CodeDocument;
         doc.DocChanged = false;
         doc.Title      = finfo.Name;
         TextEditor tb = new TextEditor();
         tb.IsReadOnly         = true;
         tb.SyntaxHighlighting = tb.SyntaxHighlighting = HighlightingManager.Instance.GetDefinitionByExtension(finfo.Extension);
         tb.Load(pni.Value);
         doc.Content = tb;
         doc.LoadProperties(this.lucDocumentProperties);
         this.DocumentHost.Items.Add(doc);
         doc.Activate();
     }
 }
        /// <summary>
        /// Select Template Node
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileName"></param>
        void lucTemplatesExplorer_SelectTemplateNode(string filePath, string fileName)
        {
            if (this.DocumentHost.Items.Count > 0)
            {
                foreach (Control c in this.DocumentHost.Items)
                {
                    if (((ucBaseDocument)c).DocPath != null && ((ucBaseDocument)c).DocPath.ToLower() == filePath.ToLower())
                    {
                        ((ucBaseDocument)c).Activate();
                        return;
                    }
                }
            }

            if (filePath.IndexOf(".tt") > 0)
            {
                ucBaseDocument doc = new ucBaseDocument();
                doc.DocExt     = ".tt";
                doc.DocPath    = filePath;
                doc.DocType    = DocumentType.TemplateDocument;
                doc.DocChanged = false;
                doc.Title      = fileName;
                TextEditor tb = new TextEditor();
                tb.IsReadOnly = false;
                tb.Load(filePath);
                tb.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition(ToolHelper.GetHighlightingByExtension(GetOutputExtension(tb.Text).Replace(".", string.Empty)));
                doc.Content           = tb;
                doc.LoadProperties(this.lucDocumentProperties);
                this.DocumentHost.Items.Add(doc);
                doc.Activate();
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGenerateCode_Click(object sender, RoutedEventArgs e)
        {
            if (!this.DocumentHost.ContainsActiveDocument)
            {
                MessageBox.Show("Please open a template document.");
                return;
            }
            ucBaseDocument doc = ((ucBaseDocument)this.DocumentHost.SelectedItem);

            if (doc.DocType == DocumentType.CodeDocument)
            {
                MessageBox.Show("Please select a template document.");
                return;
            }

            TextTemplatingEngineHost host = new TextTemplatingEngineHost();
            Engine engine = new Engine();

            host.Session           = new TextTemplatingSession();
            host.TemplateFileValue = doc.DocPath == null ? string.Empty : doc.DocPath.ToString();

            TextEditor tbox          = (TextEditor)doc.Content;
            string     inputTemplate = tbox == null ?string.Empty:tbox.Text;//.Replace("System.Windows.Controls.TextBox: ",string.Empty);

            List <CustomProperty> cpList = doc.GetParamters();

            foreach (CustomProperty cp in cpList)
            {
                Parameter par = new Parameter()
                {
                    Text = cp.Name.Trim(), Value = cp.Value.ToString()
                };
                host.Session.Add(cp.Name.Trim(), par);
            }

            string         strCodes = engine.ProcessTemplate(inputTemplate, host);
            string         docExt   = "." + GetOutputExtension(inputTemplate);
            CustomProperty tncp     = doc.GetProperty("TableName");
            string         tname    = tncp == null ? string.Empty : tncp.Value.ToString();
            string         dtitle   = doc.Title.Substring(4, doc.Title.Length - 7);
            ucBaseDocument codedoc  = new ucBaseDocument()
            {
                Title = tname + dtitle == string.Empty ? string.Format("Doc_{0}", DateTime.Now.ToString("yyyyMMdd")) : tname + dtitle + docExt,

                Content = new TextEditor()
                {
                    IsReadOnly         = false,
                    SyntaxHighlighting = HighlightingManager.Instance.GetDefinition(ToolHelper.GetHighlightingByExtension(GetOutputExtension(inputTemplate).Trim())),
                    Text = strCodes
                }
            };

            codedoc.DocType    = DocumentType.CodeDocument;
            codedoc.DocChanged = false;
            codedoc.DocExt     = docExt;
            this.DocumentHost.Items.Add(codedoc);
            codedoc.Activate();
            codedoc.LoadProperties(this.lucDocumentProperties);
        }
        /// <summary>
        /// SelectionChanged in Document Host
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DocumentHost_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ucBaseDocument doc = ((ucBaseDocument)DocumentHost.SelectedItem);

            if (doc != null && this.lucDocumentProperties != null)
            {
                doc.LoadProperties(this.lucDocumentProperties);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenDoc_Click(object sender, RoutedEventArgs e)
        {
            if (openFileDialog == null)
            {
                this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
            }

            this.openFileDialog.ShowDialog();
            if (this.DocumentHost.Items.Count > 0)
            {
                foreach (Control c in this.DocumentHost.Items)
                {
                    if (((ucBaseDocument)c).DocPath != null && ((ucBaseDocument)c).DocPath.ToLower() == openFileDialog.FileName.ToLower())
                    {
                        ((ucBaseDocument)c).Activate();
                        return;
                    }
                }
            }

            if (openFileDialog.CheckFileExists)
            {
                FileInfo       finfo = new FileInfo(openFileDialog.FileName);
                ucBaseDocument doc   = new ucBaseDocument()
                {
                    Title   = finfo.Name,
                    Content = new TextEditor()
                    {
                        Text = File.ReadAllText(openFileDialog.FileName),
                        SyntaxHighlighting = HighlightingManager.Instance.GetDefinitionByExtension(finfo.Extension),
                        Encoding           = System.Text.UnicodeEncoding.UTF8
                    }
                };
                doc.Title   = finfo.Name;
                doc.DocExt  = finfo.Extension;
                doc.DocPath = finfo.FullName;
                if (doc.DocExt == ".tt")
                {
                    doc.DocType = DocumentType.TemplateDocument;
                }
                else
                {
                    doc.DocType = DocumentType.CodeDocument;
                }
                doc.DocChanged = false;
                doc.LoadProperties(this.lucDocumentProperties);

                this.DocumentHost.Items.Add(doc);
                doc.Activate();
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDocAddToProj_Click(object sender, RoutedEventArgs e)
        {
            if (this.DocumentHost.Items.Count == 0)
            {
                MessageBox.Show("Please new or open a code document add to current selected project.");
            }
            if (this.lucSolutionExplorer.CurProject == null)
            {
                MessageBox.Show("Please select target project or a file below target project.");
            }

            ucBaseDocument doc = this.DocumentHost.SelectedItem as ucBaseDocument;

            MessageBoxResult mbr = MessageBox.Show("Are you sure to add " + doc.Title + " to " + lucSolutionExplorer.CurProject.DisplayName + " ?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);

            if (mbr == MessageBoxResult.No)
            {
                return;
            }

            TextEditor txtEditor = doc.Content as TextEditor;
            FileInfo   finfo     = new FileInfo(lucSolutionExplorer.CurProject.Value);

            string     newPaht = finfo.Directory.FullName + @"\" + doc.Title;
            FileStream fs;

            if (!File.Exists(newPaht))
            {
                fs = new FileStream(newPaht, FileMode.Create);
            }
            else
            {
                fs = new FileStream(newPaht, FileMode.Open);
            }
            StreamWriter sw = new StreamWriter(fs);

            sw.WriteLine(txtEditor.Text);

            sw.Close();
            fs.Close();
            FileInfo newinfo = new FileInfo(newPaht);

            doc.DocPath = newPaht;
            doc.DocExt  = finfo.Extension;
            doc.LoadProperties(this.lucDocumentProperties);

            UpdateProjectFile(doc, lucSolutionExplorer.CurProject.Value);

            this.lucSolutionExplorer.Refresh();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNewDoc_Click(object sender, RoutedEventArgs e)
        {
            ucBaseDocument doc = new ucBaseDocument()
            {
                Title   = string.Format("Doc_{0}", DateTime.Now.ToString("yyyyMMdd")),
                Content = new TextEditor()
                {
                    IsReadOnly         = false,
                    SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("txt")
                }
            };

            doc.DocType    = DocumentType.CodeDocument;
            doc.DocChanged = false;
            this.DocumentHost.Items.Add(doc);
            doc.Activate();
            doc.LoadProperties(this.lucDocumentProperties);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveDoc_Click(object sender, RoutedEventArgs e)
        {
            if (this.DocumentHost.ContainsActiveContent)
            {
                ucBaseDocument doc = ((ucBaseDocument)this.DocumentHost.SelectedItem);

                if (File.Exists(doc.DocPath))
                {
                    this.saveFileDialog.FileName = doc.DocPath;
                }
                if (!string.IsNullOrEmpty(doc.DocExt))
                {
                    this.saveFileDialog.Filter = doc.DocExt + "|*." + doc.DocExt;
                }
                else
                {
                    this.saveFileDialog.Filter = string.Empty;
                }
                this.saveFileDialog.ShowDialog();

                if (!string.IsNullOrEmpty(this.saveFileDialog.FileName))
                {
                    FileStream fs;
                    if (!File.Exists(this.saveFileDialog.FileName))
                    {
                        fs = new FileStream(this.saveFileDialog.FileName, FileMode.Create);
                    }
                    else
                    {
                        fs = new FileStream(this.saveFileDialog.FileName, FileMode.Open);
                    }
                    StreamWriter sw = new StreamWriter(fs);

                    sw.WriteLine(doc.Content.ToString());

                    sw.Close();
                    fs.Close();
                    FileInfo finfo = new FileInfo(this.saveFileDialog.FileName);
                    doc.DocPath = this.saveFileDialog.FileName;
                    doc.DocExt  = finfo.Extension;
                    doc.LoadProperties(this.lucDocumentProperties);
                }
            }
        }