Work on document manager

This commit is contained in:
2014-07-23 23:27:14 +03:00
parent 1cf00049d2
commit 50e6e27cd6
33 changed files with 1204 additions and 6 deletions

View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using RainmeterEditor.Model;
namespace RainmeterEditor.Documents.Text
{
public class TextDocument : IDocument
{
public string Name
{
get
{
return Path.GetFileName(FilePath);
}
}
public string FilePath
{
get; set;
}
public string Text
{
get; set;
}
public TextDocument()
{
Text = String.Empty;
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using RainmeterEditor.Model;
namespace RainmeterEditor.Documents.Text
{
public class TextEditor : IDocumentEditor
{
private TextDocument _document;
private TextEditorControl _control;
public TextEditor(TextDocument document)
{
_document = document;
_control = new TextEditorControl(document);
}
public override IDocument Document { get { return _document; } }
public override string Title { get { return _document.Name; } }
public override System.Windows.UIElement EditorUI { get { return _control; } }
}
}

View File

@ -0,0 +1,11 @@
<UserControl x:Class="RainmeterEditor.Documents.Text.TextEditorControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox Name="text" AcceptsReturn="True" />
</Grid>
</UserControl>

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace RainmeterEditor.Documents.Text
{
/// <summary>
/// Interaction logic for TextEditorControl.xaml
/// </summary>
public partial class TextEditorControl : UserControl
{
private TextDocument _document;
public TextEditorControl(TextDocument document)
{
InitializeComponent();
_document = document;
text.Text = document.Text;
}
}
}

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using RainmeterEditor.Business;
using RainmeterEditor.Model;
namespace RainmeterEditor.Documents.Text
{
public class TextEditorFactory : IDocumentEditorFactory
{
private TextStorage _storage = new TextStorage();
/// <inheritdoc />
public string EditorName
{
get { return Resources.Strings.DocumentEditor_Text_Name; }
}
/// <inheritdoc />
public IEnumerable<DocumentFormat> CreateDocumentFormats
{
get
{
yield return new DocumentFormat()
{
Name = Resources.Strings.DocumentFormat_TextFile_Name,
Category = Resources.Strings.Category_Utility,
DefaultExtension = ".txt",
Description = Resources.Strings.DocumentFormat_TextFile_Description,
Icon = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Resources/Icons/text-x-generic-32.png", UriKind.RelativeOrAbsolute)),
Factory = this
};
}
}
public IDocumentEditor CreateEditor(IDocument document)
{
TextDocument textDocument = document as TextDocument;
if (textDocument == null)
throw new ArgumentException("Cannot edit provided document.");
return new TextEditor(textDocument);
}
public IDocumentStorage Storage { get { return _storage; } }
public IDocument CreateDocument(DocumentFormat format, string path)
{
var document = new TextDocument();
document.FilePath = path;
return document;
}
}
}

View File

@ -0,0 +1,64 @@
using System;
using System.IO;
using RainmeterEditor.Model;
namespace RainmeterEditor.Documents.Text
{
/// <summary>
/// Storage for text files
/// </summary>
public class TextStorage : IDocumentStorage
{
/// <inheritdoc />
IDocument IDocumentStorage.Read(string path)
{
TextDocument document = new TextDocument();
document.FilePath = path;
document.Text = File.ReadAllText(path);
return document;
}
/// <inheritdoc />
public void Write(string path, IDocument document)
{
TextDocument textDocument = document as TextDocument;
if (textDocument == null)
throw new ArgumentException("Provided document is not supported by this storage.");
File.WriteAllText(path, textDocument.Text);
}
/// <inheritdoc />
public bool CanRead(string path)
{
// Open the file
FileStream file = File.OpenRead(path);
// Read a small chunk (1kb should be enough)
byte[] buffer = new byte[1024];
int read = file.Read(buffer, 0, buffer.Length);
// Close file
file.Close();
// Find 4 consecutive zero bytes
int cnt = 0;
for (int i = 0; i < read; i++)
{
// Count zero bytes
if (buffer[i] == 0)
++cnt;
else cnt = 0;
// Found > 4? Most likely binary file
if (cnt >= 4)
return false;
}
// Not found, probably text file
return true;
}
}
}