#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.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using ICSharpCode.TextEditor.Document;
using Microsoft.VisualBasic;
using MiniSqlQuery.Commands;
using MiniSqlQuery.Core;
using MiniSqlQuery.Core.Commands;
using MiniSqlQuery.Core.Template;
using WeifenLuo.WinFormsUI.Docking;
namespace MiniSqlQuery.PlugIns.TemplateViewer
{
/// The template editor form.
public partial class TemplateEditorForm : DockContent, IEditor, IFindReplaceProvider, INavigatableDocument, ITemplateEditor
{
/// The _host window.
private readonly IHostWindow _hostWindow;
/// The _services.
private readonly IApplicationServices _services;
/// The _file name.
private string _fileName;
/// The _highlighting provider loaded.
private bool _highlightingProviderLoaded;
/// The _is dirty.
private bool _isDirty;
/// The _text find service.
private ITextFindService _textFindService;
/// Initializes a new instance of the class.
/// The services.
/// The host window.
public TemplateEditorForm(IApplicationServices services, IHostWindow hostWindow)
{
InitializeComponent();
txtEdit.Document.DocumentChanged += DocumentDocumentChanged;
_services = services;
_hostWindow = hostWindow;
formContextMenuStrip.Items.Add(CommandControlBuilder.CreateToolStripMenuItem());
formContextMenuStrip.Items.Add(CommandControlBuilder.CreateToolStripMenuItemSeparator());
formContextMenuStrip.Items.Add(CommandControlBuilder.CreateToolStripMenuItem());
formContextMenuStrip.Items.Add(CommandControlBuilder.CreateToolStripMenuItem());
formContextMenuStrip.Items.Add(CommandControlBuilder.CreateToolStripMenuItem());
CommandControlBuilder.MonitorMenuItemsOpeningForEnabling(formContextMenuStrip);
}
/// Gets or sets AllText.
public string AllText
{
get { return txtEdit.Text; }
set { txtEdit.Text = value; }
}
/// Gets a value indicating whether CanReplaceText.
public bool CanReplaceText
{
get { return true; }
}
/// Gets or sets CursorColumn.
public int CursorColumn
{
get { return txtEdit.ActiveTextAreaControl.Caret.Column; }
set { txtEdit.ActiveTextAreaControl.Caret.Column = value; }
}
/// Gets or sets CursorLine.
public int CursorLine
{
get { return txtEdit.ActiveTextAreaControl.Caret.Line; }
set { txtEdit.ActiveTextAreaControl.Caret.Line = value; }
}
/// Gets CursorOffset.
public int CursorOffset
{
get { return txtEdit.ActiveTextAreaControl.Caret.Offset; }
}
/// Gets FileFilter.
public string FileFilter
{
get { return "Mini SQL Template Files (*.mt)|*.mt|All Files (*.*)|*.*"; }
}
/// Gets or sets FileName.
public string FileName
{
get { return _fileName; }
set
{
_fileName = value;
Text = FileName;
SetTabTextByFilename();
}
}
/// Gets a value indicating whether IsBusy.
public bool IsBusy
{
get { return false; }
}
/// Gets or sets a value indicating whether IsDirty.
public bool IsDirty
{
get { return _isDirty; }
set
{
if (_isDirty != value)
{
_isDirty = value;
SetTabTextByFilename();
}
}
}
/// Gets SelectedText.
public string SelectedText
{
get { return txtEdit.ActiveTextAreaControl.SelectionManager.SelectedText; }
}
/// Gets TextFindService.
public ITextFindService TextFindService
{
get
{
if (_textFindService == null)
{
_textFindService = _services.Resolve();
}
return _textFindService;
}
}
/// Gets TotalLines.
public int TotalLines
{
get { return txtEdit.Document.TotalNumberOfLines; }
}
/// The load highlighting provider.
public void LoadHighlightingProvider()
{
if (_highlightingProviderLoaded)
{
return;
}
// see: http://wiki.sharpdevelop.net/Syntax%20highlighting.ashx
string dir = Path.GetDirectoryName(GetType().Assembly.Location);
FileSyntaxModeProvider fsmProvider = new FileSyntaxModeProvider(dir);
HighlightingManager.Manager.AddSyntaxModeFileProvider(fsmProvider); // Attach to the text editor.
txtEdit.SetHighlighting("NVelocity");
_highlightingProviderLoaded = true;
}
/// The clear selection.
public void ClearSelection()
{
txtEdit.ActiveTextAreaControl.SelectionManager.ClearSelection();
}
/// The highlight string.
/// The offset.
/// The length.
public void HighlightString(int offset, int length)
{
if (offset < 0 || length < 1)
{
return;
}
int endPos = offset + length;
txtEdit.ActiveTextAreaControl.SelectionManager.SetSelection(
txtEdit.Document.OffsetToPosition(offset),
txtEdit.Document.OffsetToPosition(endPos));
SetCursorByOffset(endPos);
}
/// The insert text.
/// The text.
public void InsertText(string text)
{
if (string.IsNullOrEmpty(text))
{
return;
}
int offset = txtEdit.ActiveTextAreaControl.Caret.Offset;
// if some text is selected we want to replace it
if (txtEdit.ActiveTextAreaControl.SelectionManager.IsSelected(offset))
{
offset = txtEdit.ActiveTextAreaControl.SelectionManager.SelectionCollection[0].Offset;
txtEdit.ActiveTextAreaControl.SelectionManager.RemoveSelectedText();
}
txtEdit.Document.Insert(offset, text);
int newOffset = offset + text.Length; // new offset at end of inserted text
// now reposition the caret if required to be after the inserted text
if (CursorOffset != newOffset)
{
SetCursorByOffset(newOffset);
}
txtEdit.Focus();
}
/// The load file.
public void LoadFile()
{
txtEdit.LoadFile(FileName);
IsDirty = false;
}
/// The save file.
public void SaveFile()
{
txtEdit.SaveFile(FileName);
IsDirty = false;
}
/// The set syntax.
/// The name.
public void SetSyntax(string name)
{
LoadHighlightingProvider();
txtEdit.SetHighlighting(name);
}
/// The find string.
/// The value.
/// The start index.
/// The comparison type.
/// The find string.
public int FindString(string value, int startIndex, StringComparison comparisonType)
{
if (string.IsNullOrEmpty(value) || startIndex < 0)
{
return -1;
}
string text = AllText;
int pos = text.IndexOf(value, startIndex, comparisonType);
if (pos > -1)
{
ClearSelection();
HighlightString(pos, value.Length);
}
return pos;
}
/// The replace string.
/// The value.
/// The start index.
/// The length.
/// The replace string.
public bool ReplaceString(string value, int startIndex, int length)
{
if (value == null || startIndex < 0 || length < 0)
{
return false;
}
if ((startIndex + length) > AllText.Length)
{
return false;
}
txtEdit.Document.Replace(startIndex, length, value);
return true;
}
/// The set text find service.
/// The text find service.
public void SetTextFindService(ITextFindService textFindService)
{
// accept nulls infering a reset
_textFindService = textFindService;
}
/// The set cursor by location.
/// The line.
/// The column.
/// The set cursor by location.
public bool SetCursorByLocation(int line, int column)
{
if (line > TotalLines)
{
return false;
}
txtEdit.ActiveTextAreaControl.Caret.Line = line;
txtEdit.ActiveTextAreaControl.Caret.Column = column;
return true;
}
/// The set cursor by offset.
/// The offset.
/// The set cursor by offset.
public bool SetCursorByOffset(int offset)
{
if (offset >= 0)
{
txtEdit.ActiveTextAreaControl.Caret.Position = txtEdit.Document.OffsetToPosition(offset);
return true;
}
return false;
}
/// The cancel task.
public void CancelTask()
{
// N/A
}
/// The execute task.
public void ExecuteTask()
{
RunTemplate();
}
/// The run template.
public void RunTemplate()
{
TemplateModel templateModel = _services.Resolve();
TemplateResult templateResult = null;
txtErrors.Clear();
try
{
string[] lines = AllText.Replace("\r", string.Empty).Split('\n');
string text;
Dictionary items = new Dictionary();
items[TemplateModel.Extension] = templateModel.InferExtensionFromFilename(FileName, items);
text = templateModel.PreProcessTemplate(lines, GetValue, items);
templateResult = templateModel.ProcessTemplate(text, items);
}
catch (TemplateException exp)
{
_hostWindow.DisplaySimpleMessageBox(this, exp.Message, "Template Error");
// todo - try to get the line number and move cursor?...
txtErrors.Text = exp.Message;
}
if (templateResult != null)
{
// display in new window
IFileEditorResolver resolver = _services.Resolve();
IEditor editor = _services.Resolve(resolver.ResolveEditorNameByExtension(templateResult.Extension));
editor.AllText = templateResult.Text;
editor.SetSyntax(templateResult.SyntaxName);
_hostWindow.DisplayDockedForm(editor as DockContent);
}
}
/// The document document changed.
/// The sender.
/// The e.
private void DocumentDocumentChanged(object sender, DocumentEventArgs e)
{
IsDirty = true;
}
/// The get value.
/// The name.
/// The get value.
private string GetValue(string name)
{
string val = Interaction.InputBox(string.Format("Value for '{0}'", name), "Supply a Value", name, -1, -1);
return val;
}
/// The set tab text by filename.
private void SetTabTextByFilename()
{
string dirty = string.Empty;
string text = "Untitled";
string tabtext;
if (_isDirty)
{
dirty = " *";
}
if (txtEdit.FileName != null)
{
text = FileName;
tabtext = Path.GetFileName(FileName);
}
else
{
text += _services.Settings.GetUntitledDocumentCounter();
tabtext = text;
}
TabText = tabtext + dirty;
ToolTipText = text + dirty;
}
/// The template editor form_ form closing.
/// The sender.
/// The e.
private void TemplateEditorForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (_isDirty)
{
DialogResult saveFile = _hostWindow.DisplayMessageBox(
this,
"Contents changed, do you want to save the file?\r\n" + TabText, "Save Changes?",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1,
0,
null,
null);
if (saveFile == DialogResult.Cancel)
{
e.Cancel = true;
}
else if (saveFile == DialogResult.Yes)
{
CommandManager.GetCommandInstance().Execute();
}
}
}
/// The template editor form_ load.
/// The sender.
/// The e.
private void TemplateEditorForm_Load(object sender, EventArgs e)
{
rtfHelp.Rtf = TemplateResources.TemplateHelp;
}
}
}