mirror of
https://github.com/chibicitiberiu/rainmeter-studio.git
synced 2024-02-24 04:33:31 +00:00
Work on document manager
This commit is contained in:
35
RainmeterEditor/Documents/Text/TextDocument.cs
Normal file
35
RainmeterEditor/Documents/Text/TextDocument.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
27
RainmeterEditor/Documents/Text/TextEditor.cs
Normal file
27
RainmeterEditor/Documents/Text/TextEditor.cs
Normal 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; } }
|
||||
}
|
||||
}
|
11
RainmeterEditor/Documents/Text/TextEditorControl.xaml
Normal file
11
RainmeterEditor/Documents/Text/TextEditorControl.xaml
Normal 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>
|
32
RainmeterEditor/Documents/Text/TextEditorControl.xaml.cs
Normal file
32
RainmeterEditor/Documents/Text/TextEditorControl.xaml.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
59
RainmeterEditor/Documents/Text/TextEditorFactory.cs
Normal file
59
RainmeterEditor/Documents/Text/TextEditorFactory.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
64
RainmeterEditor/Documents/Text/TextStorage.cs
Normal file
64
RainmeterEditor/Documents/Text/TextStorage.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user