Handle file operations Accessor-agnostically

This commit is contained in:
Michael Becker 2019-09-15 15:27:33 -04:00
parent 5b40e43db7
commit 16761780be
5 changed files with 61 additions and 6 deletions

View File

@ -19,7 +19,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System;
using System.Collections.Generic;
using System.Linq;
@ -36,6 +36,19 @@ namespace UniversalEditor
[DebuggerNonUserCode()]
public abstract class Accessor : References<AccessorReference>
{
protected bool Initialized { get; private set; } = false;
protected void Initialize()
{
if (Initialized)
return;
InitializeInternal();
Initialized = true;
}
protected virtual void InitializeInternal()
{
}
/// <summary>
/// Creates or returns an existing <see cref="ReferencedBy" /> object referring to this <see cref="References" /> object.
/// </summary>
@ -200,6 +213,13 @@ namespace UniversalEditor
{
get { return String.Empty; }
}
/// <summary>
/// Gets or sets the original URI.
/// </summary>
/// <value>The original URI.</value>
public Uri OriginalUri { get; set; }
/// <summary>
/// Gets the file name without path information for this <see cref="Accessor" />, if applicable.
/// </summary>

View File

@ -21,6 +21,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
@ -51,6 +52,8 @@ namespace UniversalEditor
private string mvarTitle = String.Empty;
public string Title { get { return mvarTitle; } set { mvarTitle = value; } }
public StringCollection Schemas { get; } = new StringCollection();
/// <summary>
/// Gets the detail fields that are shown in lists of this <see cref="ReferencedBy" /> object in details view.

View File

@ -494,6 +494,23 @@ namespace UniversalEditor.Common
if (mvarAvailableAccessors == null) Initialize();
return mvarAvailableAccessors;
}
public static AccessorReference[] GetAvailableAccessors(string fileName)
{
AccessorReference[] accrefs = GetAvailableAccessors();
List<AccessorReference> _list = new List<AccessorReference>();
foreach (AccessorReference ar in accrefs)
{
foreach (string schema in ar.Schemas)
{
if (fileName.StartsWith(schema + "://"))
{
_list.Add(ar);
break;
}
}
}
return _list.ToArray();
}
#endregion
#region Data Formats
private static DataFormatReference[] mvarAvailableDataFormats = null;

View File

@ -591,12 +591,23 @@ namespace UniversalEditor.UserInterface
/// <param name="fileNames">The file name(s) of the document(s) to load.</param>
public void OpenWindow(params string[] fileNames)
{
Document[] documents = new Document[fileNames.Length];
List<Document> loadedDocuments = new List<Document>();
for (int i = 0; i < fileNames.Length; i++)
{
documents[i] = new Document(null, null, new FileAccessor(fileNames[i]));
AccessorReference[] accs = UniversalEditor.Common.Reflection.GetAvailableAccessors(fileNames[i]);
if (accs.Length > 0)
{
Accessor fa = accs[0].Create();
fa.OriginalUri = new Uri(fileNames[i]);
Document document = new Document(fa);
loadedDocuments.Add(document);
}
else
{
Console.Error.WriteLine("ue: accessor not found for path " + fileNames[i]);
}
}
OpenWindow(documents);
OpenWindow(loadedDocuments.ToArray());
}
/// <summary>
/// Opens a new window, optionally loading the specified documents.

View File

@ -543,8 +543,12 @@ namespace UniversalEditor.UserInterface
Document[] documents = new Document[fileNames.Length];
for (int i = 0; i < documents.Length; i++)
{
FileAccessor fa = new FileAccessor(fileNames[i]);
documents[i] = new Document(fa);
AccessorReference[] accs = UniversalEditor.Common.Reflection.GetAvailableAccessors(fileNames[i]);
if (accs.Length > 0)
{
Accessor fa = accs[0].Create();
documents[i] = new Document(fa);
}
}
OpenFile(documents);
}