#region License
// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
// This source code is made available under the terms of the GNU Lesser General Public License v3.0
// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
#endregion
using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using MiniSqlQuery.Core;
using WeifenLuo.WinFormsUI.Docking;
namespace MiniSqlQuery.PlugIns.TemplateViewer
{
/// The template view form.
public partial class TemplateViewForm : DockContent
{
/// The _model.
private readonly TemplateModel _model;
/// The _services.
private readonly IApplicationServices _services;
/// The _selected file.
private FileInfo _selectedFile;
/// Initializes a new instance of the class.
/// The services.
/// The model.
public TemplateViewForm(IApplicationServices services, TemplateModel model)
{
InitializeComponent();
_services = services;
_model = model;
}
/// The get value.
/// The name.
/// The get value.
private string GetValue(string name)
{
string val = Interaction.InputBox(string.Format("'{0}'的值", name), "提供一个值", name, -1, -1);
return val;
}
/// The populate tree.
private void PopulateTree()
{
tvTemplates.Nodes[0].Nodes.Clear();
tvTemplates.Nodes[0].Nodes.AddRange(_model.CreateNodes());
tvTemplates.Nodes[0].Expand();
}
/// The run template.
/// The fi.
private void RunTemplate(FileInfo fi)
{
TemplateResult templateResult = _model.ProcessTemplateFile(fi.FullName, GetValue);
// display in new window
IFileEditorResolver resolver = _services.Resolve();
IEditor editor = _services.Resolve(resolver.ResolveEditorNameByExtension(templateResult.Extension));
editor.AllText = templateResult.Text;
editor.SetSyntax(templateResult.SyntaxName);
_services.HostWindow.DisplayDockedForm(editor as DockContent);
}
/// The template view form_ load.
/// The sender.
/// The e.
private void TemplateViewForm_Load(object sender, EventArgs e)
{
PopulateTree();
templateFileWatcher.Path = _model.GetTemplatePath();
}
/// The template file watcher_ changed.
/// The sender.
/// The e.
private void templateFileWatcher_Changed(object sender, FileSystemEventArgs e)
{
PopulateTree();
}
/// The template file watcher_ renamed.
/// The sender.
/// The e.
private void templateFileWatcher_Renamed(object sender, RenamedEventArgs e)
{
// todo - scan and modify tree
PopulateTree();
}
/// The tool strip menu item edit_ click.
/// The sender.
/// The e.
private void toolStripMenuItemEdit_Click(object sender, EventArgs e)
{
if (_selectedFile != null)
{
IFileEditorResolver resolver = _services.Resolve();
var editor = resolver.ResolveEditorInstance(_selectedFile.FullName);
editor.FileName = _selectedFile.FullName;
editor.LoadFile();
_services.HostWindow.DisplayDockedForm(editor as DockContent);
}
}
/// The tool strip menu item run_ click.
/// The sender.
/// The e.
private void toolStripMenuItemRun_Click(object sender, EventArgs e)
{
if (_selectedFile != null)
{
RunTemplate(_selectedFile);
}
}
/// The tv templates_ node mouse click.
/// The sender.
/// The e.
private void tvTemplates_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
FileInfo fi = null;
if (e.Button == MouseButtons.Right && e.Node != null && e.Node.Tag is FileInfo)
{
fi = e.Node.Tag as FileInfo;
}
_selectedFile = fi;
treeMenuStrip.Enabled = _selectedFile != null;
}
/// The tv templates_ node mouse double click.
/// The sender.
/// The e.
private void tvTemplates_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e != null && e.Node != null)
{
FileInfo fi = e.Node.Tag as FileInfo;
if (fi != null)
{
RunTemplate(fi);
}
}
}
}
}