Work on project editor

This commit is contained in:
Tiberiu Chibici 2014-09-12 13:19:01 +03:00
parent b6d8730783
commit 51e0b0d612
16 changed files with 354 additions and 127 deletions

View File

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
@ -12,8 +14,13 @@ namespace RainmeterStudio.Core.Model
/// <summary>
/// Defines a Rainmeter Studio project
/// </summary>
public class Project
public class Project : INotifyPropertyChanged
{
private string _author;
private Reference _autoLoadFile;
private Version _version, _minimumWindows, _minimumRainmeter;
private Reference _root;
#region Properties
/// <summary>
@ -29,6 +36,9 @@ namespace RainmeterStudio.Core.Model
set
{
Root.Name = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
@ -45,6 +55,9 @@ namespace RainmeterStudio.Core.Model
set
{
Root.StoragePath = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
@ -52,19 +65,58 @@ namespace RainmeterStudio.Core.Model
/// Gets or sets the author of the project
/// </summary>
[XmlElement(ElementName = "author", Order = 2)]
public string Author { get; set; }
public string Author
{
get
{
return _author;
}
set
{
_author = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Author"));
}
}
/// <summary>
/// Gets or sets the version of the project
/// </summary>
[XmlElement(ElementName = "version", Order = 3)]
public Version Version { get; set; }
public Version Version
{
get
{
return _version;
}
set
{
_version = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Version"));
}
}
/// <summary>
/// Gets or sets the reference to the file that automatically loads at package installation
/// </summary>
[XmlIgnore]
public Reference AutoLoadFile { get; set; }
public Reference AutoLoadFile
{
get
{
return _autoLoadFile;
}
set
{
_autoLoadFile = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("AutoLoadFile"));
}
}
/// <summary>
/// Gets or sets the qualified name of the auto load file
@ -86,7 +138,7 @@ namespace RainmeterStudio.Core.Model
/// Gets or sets the list of variable files
/// </summary>
[XmlIgnore]
public List<Reference> VariableFiles { get; set; }
public ObservableCollection<Reference> VariableFiles { get; private set; }
/// <summary>
/// Gets or sets the list of variable files qualified names
@ -101,7 +153,7 @@ namespace RainmeterStudio.Core.Model
set
{
VariableFiles.Clear();
VariableFiles.AddRange(value.Select(x => Root.GetReference(x)));
value.Select(x => Root.GetReference(x)).ForEach(VariableFiles.Add);
}
}
@ -109,19 +161,58 @@ namespace RainmeterStudio.Core.Model
/// Gets or sets the minimum rainmeter version
/// </summary>
[XmlElement(ElementName = "minimumRainmeter", Order = 4)]
public Version MinimumRainmeter { get; set; }
public Version MinimumRainmeter
{
get
{
return _minimumRainmeter;
}
set
{
_minimumRainmeter = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("MinimumRainmeter"));
}
}
/// <summary>
/// Gets or sets the minimum Windows version
/// </summary>
[XmlElement(ElementName = "minimumWindows", Order = 5)]
public Version MinimumWindows { get; set; }
public Version MinimumWindows
{
get
{
return _minimumWindows;
}
set
{
_minimumWindows = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("MinimumWindows"));
}
}
/// <summary>
/// Gets or sets the root node
/// </summary>
[XmlElement(ElementName = "root", Order = 6)]
public Reference Root { get; set; }
public Reference Root
{
get
{
return _root;
}
set
{
_root = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Root"));
}
}
#endregion
@ -133,7 +224,7 @@ namespace RainmeterStudio.Core.Model
public Project()
{
Root = new Reference(String.Empty);
VariableFiles = new List<Reference>();
VariableFiles = new ObservableCollection<Reference>();
Version = new Version();
MinimumRainmeter = new Version("3.1");
MinimumWindows = new Version("5.1");
@ -155,5 +246,10 @@ namespace RainmeterStudio.Core.Model
}
#endregion
/// <summary>
/// Triggered when a property changes
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
}
}

View File

@ -13,27 +13,27 @@ namespace RainmeterStudio.Core.Storage
/// </summary>
/// <param name="path">Path to file</param>
/// <returns>Read document</returns>
IDocument Read(string path);
IDocument ReadDocument(string path);
/// <summary>
/// Writes a document to a file
/// </summary>
/// <param name="path">Path to file</param>
/// <param name="document">Document to write</param>
void Write(string path, IDocument document);
void WriteDocument(IDocument document, string path);
/// <summary>
/// Tests if the file can be read by this storage
/// </summary>
/// <param name="path">Path to file</param>
/// <returns>True if file can be read</returns>
bool CanRead(string path);
bool CanReadDocument(string path);
/// <summary>
/// Tests if the document can be written by this storage
/// </summary>
/// <param name="documentType">Document type</param>
/// <returns>True if the document can be written</returns>
bool CanWrite(Type documentType);
bool CanWriteDocument(Type documentType);
}
}

View File

@ -9,22 +9,22 @@ namespace RainmeterStudio.SkinDesignerPlugin
{
public class SkinStorage : IDocumentStorage
{
public IDocument Read(string path)
public IDocument ReadDocument(string path)
{
throw new NotImplementedException();
}
public void Write(string path, IDocument document)
public void WriteDocument(IDocument document, string path)
{
throw new NotImplementedException();
}
public bool CanRead(string path)
public bool CanReadDocument(string path)
{
throw new NotImplementedException();
}
public bool CanWrite(Type documentType)
public bool CanWriteDocument(Type documentType)
{
throw new NotImplementedException();
}

View File

@ -1,11 +1,8 @@
using System;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RainmeterStudio.Storage;
using System.IO;
using RainmeterStudio.Core.Model;
using RainmeterStudio.Editor.ProjectEditor;
using Version = RainmeterStudio.Core.Utils.Version;
namespace RainmeterStudio.Tests.Storage
@ -35,8 +32,8 @@ namespace RainmeterStudio.Tests.Storage
Project project = CreateProject();
// Save and load
ProjectStorage.Save(project, filename);
Project res = ProjectStorage.Load(filename);
ProjectStorage.Write(project, filename);
Project res = ProjectStorage.Read(filename);
// Verify results
Assert.IsNotNull(res);
@ -59,8 +56,8 @@ namespace RainmeterStudio.Tests.Storage
Project project = new Project();
// Save and load project
ProjectStorage.Save(project, filename);
Project res = ProjectStorage.Load(filename);
ProjectStorage.Write(project, filename);
Project res = ProjectStorage.Read(filename);
// Test results
Assert.IsNotNull(res);

View File

@ -13,7 +13,7 @@ namespace RainmeterStudio.TextEditorPlugin
public class TextStorage : IDocumentStorage
{
/// <inheritdoc />
IDocument IDocumentStorage.Read(string path)
public IDocument ReadDocument(string path)
{
TextDocument document = new TextDocument();
document.Reference = new Reference(Path.GetFileName(path), path);
@ -23,7 +23,7 @@ namespace RainmeterStudio.TextEditorPlugin
}
/// <inheritdoc />
public void Write(string path, IDocument document)
public void WriteDocument(IDocument document, string path)
{
TextDocument textDocument = document as TextDocument;
@ -34,7 +34,7 @@ namespace RainmeterStudio.TextEditorPlugin
}
/// <inheritdoc />
public bool CanRead(string path)
public bool CanReadDocument(string path)
{
// Open the file
FileStream file = File.OpenRead(path);
@ -64,7 +64,7 @@ namespace RainmeterStudio.TextEditorPlugin
return true;
}
public bool CanWrite(Type documentType)
public bool CanWriteDocument(Type documentType)
{
return documentType.Equals(typeof(TextDocument));
}

View File

@ -156,7 +156,7 @@ namespace RainmeterStudio.Business
throw new ArgumentException("Reference cannot be empty");
// Save
storage.Write(document.Reference.StoragePath, document);
storage.WriteDocument(document, document.Reference.StoragePath);
// Clear dirty flag
document.IsDirty = false;
@ -173,7 +173,7 @@ namespace RainmeterStudio.Business
var storage = FindStorage(document);
// Save
storage.Write(path, document);
storage.WriteDocument(document, path);
// Update reference
document.Reference = new Reference(Path.GetFileName(path), path);
@ -193,7 +193,7 @@ namespace RainmeterStudio.Business
var storage = FindStorage(document);
// Save
storage.Write(path, document);
storage.WriteDocument(document, path);
}
/// <summary>
@ -249,9 +249,9 @@ namespace RainmeterStudio.Business
IDocument document = null;
foreach (var storage in Storages)
if (storage.CanRead(path))
if (storage.CanReadDocument(path))
{
document = storage.Read(path);
document = storage.ReadDocument(path);
break;
}
@ -272,7 +272,7 @@ namespace RainmeterStudio.Business
IDocumentStorage storage = null;
foreach (var s in Storages)
if (s.CanWrite(document.GetType()))
if (s.CanWriteDocument(document.GetType()))
{
storage = s;
break;

View File

@ -5,7 +5,7 @@ using System.Linq;
using System.Text;
using RainmeterStudio.Core.Model;
using RainmeterStudio.Core.Storage;
using RainmeterStudio.Storage;
using RainmeterStudio.Editor.ProjectEditor;
namespace RainmeterStudio.Business
{
@ -88,7 +88,7 @@ namespace RainmeterStudio.Business
Close();
// Open using storage
ActiveProject = Storage.Load(path);
ActiveProject = Storage.Read(path);
ActiveProject.Path = path;
// Raise event
@ -106,7 +106,7 @@ namespace RainmeterStudio.Business
throw new InvalidOperationException("Cannot save a project that is not opened.");
// Save
Storage.Save(ActiveProject);
Storage.Write(ActiveProject);
}
/// <summary>

View File

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RainmeterStudio.Core.Model;
namespace RainmeterStudio.Editor.ProjectEditor
{
/// <summary>
/// A project document
/// </summary>
/// <remarks>Unlike the Project class, this class implements the 'IDocument'
/// interface. This is a proxy class for the actual project.</remarks>
public class ProjectDocument : IDocument
{
private Reference _reference;
private bool _isDirty = false;
/// <summary>
/// Gets or sets the reference of the document
/// </summary>
public Reference Reference
{
get
{
return _reference;
}
set
{
_reference = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Reference"));
}
}
/// <inheritdoc />
public bool IsDirty
{
get
{
return _isDirty;
}
set
{
_isDirty = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("IsDirty"));
}
}
/// <summary>
/// Gets the project this project document is linked to
/// </summary>
public Project Project { get; private set; }
/// <summary>
/// Event triggered when a property changes value
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Initializes this project document
/// </summary>
/// <param name="project">The actual project this document is linked to</param>
public ProjectDocument(Project project)
{
Project = project;
}
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RainmeterStudio.Core;
using RainmeterStudio.Core.Documents;
using RainmeterStudio.Core.Model;
namespace RainmeterStudio.Editor.ProjectEditor
{
/// <summary>
/// Project editor factory
/// </summary>
[PluginExport]
public class ProjectEditorFactory : IDocumentEditorFactory
{
/// <summary>
/// Creates a new project editor
/// </summary>
/// <param name="document">The project document</param>
/// <returns>Created editor</returns>
public IDocumentEditor CreateEditor(IDocument document)
{
return new ProjectEditorUI((ProjectDocument)document);
}
/// <summary>
/// Checks if this editor can edit documents of given type
/// </summary>
/// <param name="type">Type</param>
/// <returns>True if the project editor can edit that kind of document</returns>
public bool CanEdit(Type type)
{
return (type.Equals(typeof(ProjectDocument)));
}
}
}

View File

@ -0,0 +1,31 @@
<UserControl x:Class="RainmeterStudio.Editor.ProjectEditor.ProjectEditorUI"
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"
xmlns:r="clr-namespace:RainmeterStudio.Resources"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<ScrollViewer VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Disabled">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0">Name:</TextBlock>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Name, Mode=TwoWay}" />
<TextBlock Grid.Row="1" Grid.Column="0">Author:</TextBlock>
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Author, Mode=TwoWay}" />
</Grid>
</ScrollViewer>
</UserControl>

View File

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
using RainmeterStudio.Core.Documents;
using RainmeterStudio.Core.Model;
namespace RainmeterStudio.Editor.ProjectEditor
{
/// <summary>
/// Interaction logic for ProjectEditorUI.xaml
/// </summary>
public partial class ProjectEditorUI : UserControl, IDocumentEditor
{
/// <summary>
/// Gets the attached project document being edited
/// </summary>
public ProjectDocument Document { get; private set; }
/// <summary>
/// Gets the attached document
/// </summary>
public IDocument AttachedDocument
{
get { return Document; }
}
/// <summary>
/// Gets the UI element to be displayed in the document window
/// </summary>
public UIElement EditorUI
{
get { return this; }
}
/// <summary>
/// Initializes this project editor UI
/// </summary>
/// <param name="document"></param>
public ProjectEditorUI(ProjectDocument document)
{
InitializeComponent();
Document = document;
Document.Project.PropertyChanged += Project_PropertyChanged;
DataContext = Document.Project;
}
private void Project_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
Document.IsDirty = true;
}
}
}

View File

@ -8,7 +8,7 @@ using RainmeterStudio.Business;
using RainmeterStudio.Core.Documents;
using RainmeterStudio.Core.Model;
using RainmeterStudio.Core.Storage;
using RainmeterStudio.Storage;
using RainmeterStudio.Editor.ProjectEditor;
using RainmeterStudio.UI;
using RainmeterStudio.UI.Controller;

View File

@ -65,6 +65,10 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Page Include="Editor\ProjectEditor\ProjectEditorUI.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="UI\App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@ -73,6 +77,11 @@
<Compile Include="Business\PluginManager.cs" />
<Compile Include="Business\ProjectManager.cs" />
<Compile Include="Documents\EmptyProjectTemplate.cs" />
<Compile Include="Editor\ProjectEditor\ProjectDocument.cs" />
<Compile Include="Editor\ProjectEditor\ProjectEditorFactory.cs" />
<Compile Include="Editor\ProjectEditor\ProjectEditorUI.xaml.cs">
<DependentUpon>ProjectEditorUI.xaml</DependentUpon>
</Compile>
<Compile Include="MainClass.cs" />
<Compile Include="Interop\NativeLibrary.cs" />
<Compile Include="Rainmeter.cs" />
@ -87,7 +96,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Strings.resx</DependentUpon>
</Compile>
<Compile Include="Storage\ProjectStorage.cs" />
<Compile Include="Editor\ProjectEditor\ProjectStorage.cs" />
<Compile Include="UI\Command.cs" />
<Compile Include="UI\Controller\IconProvider.cs" />
<Compile Include="UI\Controller\ProjectController.cs" />
@ -248,6 +257,9 @@
<Resource Include="Resources\Icons\16\disk.png" />
<Resource Include="Resources\Icons\16\disk_multiple.png" />
</ItemGroup>
<ItemGroup>
<Folder Include="Storage\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -1,69 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using RainmeterStudio.Core.Model;
using RainmeterStudio.Core.Storage;
namespace RainmeterStudio.Storage
{
/// <summary>
/// Project storage, loads and saves project files
/// </summary>
public class ProjectStorage
{
/// <summary>
/// Loads a project from file
/// </summary>
/// <param name="path">Path to file to load</param>
/// <returns>Loaded project</returns>
public Project Load(string path)
{
// Open file
var file = File.OpenText(path);
// Deserialize file
var serializer = new XmlSerializer(typeof(Project), new XmlRootAttribute("project"));
Project project = serializer.Deserialize(file) as Project;
if (project != null)
{
project.Path = path;
}
// Clean up
file.Close();
return project;
}
/// <summary>
/// Saves a project to file
/// </summary>
/// <param name="project">Project to save</param>
/// <param name="path">File to save to</param>
public void Save(Project project, string path)
{
// Open file
var file = File.OpenWrite(path);
// Serialize file
var serializer = new XmlSerializer(typeof(Project), new XmlRootAttribute("project"));
serializer.Serialize(file, project);
// Clean up
file.Close();
project.Path = path;
}
/// <summary>
/// Saves a project
/// </summary>
/// <param name="project">Saves a project to the path specified in the 'Path' property</param>
public void Save(Project project)
{
Save(project, project.Path);
}
}
}

View File

@ -15,7 +15,6 @@ using RainmeterStudio.Business;
using RainmeterStudio.Core.Documents;
using RainmeterStudio.Core.Model;
using RainmeterStudio.Core.Model.Events;
using RainmeterStudio.Storage;
using RainmeterStudio.UI.Controller;
using RainmeterStudio.UI.Panels;
using Xceed.Wpf.AvalonDock.Layout;

View File

@ -1,23 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
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;
using RainmeterStudio.Core.Model;
using RainmeterStudio.Core.Utils;
using RainmeterStudio.Interop;
using RainmeterStudio.Storage;
using RainmeterStudio.UI.Controller;
using RainmeterStudio.UI.ViewModel;