8 changed files with 343 additions and 5 deletions
@ -0,0 +1,60 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.ComponentModel; |
||||||
|
using System.Collections.Generic; |
||||||
|
|
||||||
|
namespace ICSharpCode.SharpDevelop.Project.SavedData |
||||||
|
{ |
||||||
|
public enum ProjectSavedDataType |
||||||
|
{ |
||||||
|
WatchVariables |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Interface for storing project specific data.
|
||||||
|
/// When implementing this, one should be carefull when and how
|
||||||
|
/// the SavedDataManager is used in order to not alter the other data.
|
||||||
|
/// </summary>
|
||||||
|
[TypeConverter(typeof(ProjectSavedDataConverter))] |
||||||
|
public interface IProjectSavedData |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Saved data type.
|
||||||
|
/// </summary>
|
||||||
|
ProjectSavedDataType SavedDataType { get; } |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Saved data.
|
||||||
|
/// <remarks>The format is: "ProjectName"|{0}|"ProjectSavedDataType"|{1}|(specific data splited by '|').</remarks>
|
||||||
|
/// </summary>
|
||||||
|
string SavedString { get; set; } |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the project name.
|
||||||
|
/// </summary>
|
||||||
|
string ProjectName { get; } |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Dummy data. Used to map the saved data and exposed to addins where project specific data can exist.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class DummyProjectSavedData : IProjectSavedData |
||||||
|
{ |
||||||
|
public ProjectSavedDataType SavedDataType { get; set; } |
||||||
|
|
||||||
|
public string SavedString { get; set; } |
||||||
|
|
||||||
|
public string ProjectName { |
||||||
|
get { |
||||||
|
if (string.IsNullOrEmpty(SavedString)) |
||||||
|
return string.Empty; |
||||||
|
|
||||||
|
string[] v = SavedString.Split('|'); |
||||||
|
|
||||||
|
return v[1]; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,54 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.ComponentModel; |
||||||
|
using System.Globalization; |
||||||
|
using System.Text; |
||||||
|
|
||||||
|
namespace ICSharpCode.SharpDevelop.Project.SavedData |
||||||
|
{ |
||||||
|
public sealed class ProjectSavedDataConverter : TypeConverter |
||||||
|
{ |
||||||
|
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) |
||||||
|
{ |
||||||
|
if (sourceType == typeof(string)) { |
||||||
|
return true; |
||||||
|
} else { |
||||||
|
return base.CanConvertFrom(context, sourceType); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) |
||||||
|
{ |
||||||
|
// convert from saved data(string) to objects
|
||||||
|
if (value is string) { |
||||||
|
string[] v = ((string)value).Split('|'); |
||||||
|
ProjectSavedDataType type = (ProjectSavedDataType)Enum.Parse(typeof(ProjectSavedDataType), v[3]); |
||||||
|
var data = new DummyProjectSavedData { |
||||||
|
SavedString = (string)value, |
||||||
|
SavedDataType = type |
||||||
|
}; |
||||||
|
return data; |
||||||
|
} else { |
||||||
|
return base.ConvertFrom(context, culture, value); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) |
||||||
|
{ |
||||||
|
// convert objects to saved data(string)
|
||||||
|
var data = value as IProjectSavedData; |
||||||
|
if (destinationType == typeof(string) && data != null) { |
||||||
|
switch (data.SavedDataType) { |
||||||
|
case ProjectSavedDataType.WatchVariables: |
||||||
|
return data.SavedString; |
||||||
|
default: |
||||||
|
throw new Exception("Invalid value for ProjectSavedDataType"); |
||||||
|
} |
||||||
|
} else { |
||||||
|
return base.ConvertTo(context, culture, value, destinationType); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,81 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Concurrent; |
||||||
|
using System.Collections.Generic; |
||||||
|
|
||||||
|
namespace ICSharpCode.SharpDevelop.Project.SavedData |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Stores the project specific data.
|
||||||
|
/// </summary>
|
||||||
|
public static class SavedDataManager |
||||||
|
{ |
||||||
|
static readonly List<IProjectSavedData> list; |
||||||
|
static object _syncObject = new object(); |
||||||
|
|
||||||
|
static SavedDataManager() |
||||||
|
{ |
||||||
|
list = new List<IProjectSavedData>(); |
||||||
|
ProjectService.SolutionClosed += delegate { list.Clear(); }; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets all saved data.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static List<IProjectSavedData> GetSavedData() |
||||||
|
{ |
||||||
|
return list; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a new data.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
public static void Add(IProjectSavedData data) |
||||||
|
{ |
||||||
|
if (data == null) |
||||||
|
throw new ArgumentNullException("data"); |
||||||
|
|
||||||
|
if (!list.Contains(data)) { |
||||||
|
lock (_syncObject) { |
||||||
|
if (!list.Contains(data)) |
||||||
|
list.Add(data); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes data.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
public static void Remove(IProjectSavedData data) |
||||||
|
{ |
||||||
|
if (data == null) |
||||||
|
throw new ArgumentNullException("data"); |
||||||
|
|
||||||
|
if (list.Contains(data)) { |
||||||
|
lock (_syncObject) { |
||||||
|
if (list.Contains(data)) |
||||||
|
list.Remove(data); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes all data that satisfies a predicate.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="match"></param>
|
||||||
|
public static void RemoveAll(Predicate<IProjectSavedData> match) |
||||||
|
{ |
||||||
|
if (match == null) |
||||||
|
throw new ArgumentNullException("match"); |
||||||
|
|
||||||
|
lock (_syncObject) { |
||||||
|
list.RemoveAll(match); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue