Made the library provider much more robust can handle sql data and local file data.

Made a top level Library Provider Selector
This commit is contained in:
Lars Brubaker 2015-06-15 18:31:43 -07:00
parent c38b4de0ba
commit 0626981766
10 changed files with 439 additions and 75 deletions

View file

@ -82,7 +82,7 @@ namespace MatterHackers.MatterControl.PrintLibrary
{
if (SelectedIndex >= 0)
{
return LibraryProvider.CurrentProvider.GetPrintItemWrapper(SelectedIndex);
return LibraryProvider.Instance.GetPrintItemWrapper(SelectedIndex);
}
else
{
@ -208,23 +208,23 @@ namespace MatterHackers.MatterControl.PrintLibrary
{
topToBottomItemList.RemoveAllChildren();
PrintItemCollection parent = LibraryProvider.CurrentProvider.GetParentCollectionItem();
PrintItemCollection parent = LibraryProvider.Instance.GetParentCollectionItem();
if (parent != null)
{
LibraryRowItem queueItem = new LibraryRowItemCollection(parent, this, false);
AddListItemToTopToBottom(queueItem);
}
for (int i = 0; i < LibraryProvider.CurrentProvider.CollectionCount; i++)
for (int i = 0; i < LibraryProvider.Instance.CollectionCount; i++)
{
PrintItemCollection item = LibraryProvider.CurrentProvider.GetCollectionItem(i);
PrintItemCollection item = LibraryProvider.Instance.GetCollectionItem(i);
LibraryRowItem queueItem = new LibraryRowItemCollection(item, this);
AddListItemToTopToBottom(queueItem);
}
for (int i = 0; i < LibraryProvider.CurrentProvider.ItemCount; i++)
for (int i = 0; i < LibraryProvider.Instance.ItemCount; i++)
{
PrintItemWrapper item = LibraryProvider.CurrentProvider.GetPrintItemWrapper(i);
PrintItemWrapper item = LibraryProvider.Instance.GetPrintItemWrapper(i);
LibraryRowItem queueItem = new LibraryRowItemPart(item, this);
AddListItemToTopToBottom(queueItem);
}
@ -248,7 +248,7 @@ namespace MatterHackers.MatterControl.PrintLibrary
private void ItemAddedToLibrary(object sender, EventArgs e)
{
IndexArgs addedIndexArgs = e as IndexArgs;
PrintItemWrapper item = LibraryProvider.CurrentProvider.GetPrintItemWrapper(addedIndexArgs.Index);
PrintItemWrapper item = LibraryProvider.Instance.GetPrintItemWrapper(addedIndexArgs.Index);
LibraryRowItem libraryItem = new LibraryRowItemPart(item, this);
AddListItemToTopToBottom(libraryItem, addedIndexArgs.Index);
}
@ -258,7 +258,7 @@ namespace MatterHackers.MatterControl.PrintLibrary
IndexArgs removeIndexArgs = e as IndexArgs;
topToBottomItemList.RemoveChild(removeIndexArgs.Index);
if (LibraryProvider.CurrentProvider.ItemCount > 0)
if (LibraryProvider.Instance.ItemCount > 0)
{
SelectedIndex = Math.Max(SelectedIndex - 1, 0);
}

View file

@ -124,11 +124,11 @@ namespace MatterHackers.MatterControl.PrintLibrary
{
if (isSubdirector)
{
LibraryProvider.CurrentProvider.SetCollectionBase(collection);
LibraryProvider.Instance.SetCollectionBase(collection);
}
else
{
LibraryProvider.CurrentProvider.SetCollectionBase(LibraryProvider.CurrentProvider.GetParentCollectionItem());
LibraryProvider.Instance.SetCollectionBase(LibraryProvider.Instance.GetParentCollectionItem());
}
UiThread.RunOnIdle(libraryDataView.RebuildView);

View file

@ -165,7 +165,7 @@ namespace MatterHackers.MatterControl.PrintLibrary
public override void RemoveFromParentCollection()
{
LibraryProvider.CurrentProvider.RemoveItem(printItemWrapper);
LibraryProvider.Instance.RemoveItem(printItemWrapper);
}
public override void Export()
@ -180,7 +180,7 @@ namespace MatterHackers.MatterControl.PrintLibrary
public override void RemoveFromCollection()
{
LibraryProvider.CurrentProvider.RemoveItem(printItemWrapper);
LibraryProvider.Instance.RemoveItem(printItemWrapper);
}
public override void AddToQueue()
@ -245,7 +245,7 @@ namespace MatterHackers.MatterControl.PrintLibrary
protected override void RemoveThisFromPrintLibrary()
{
LibraryProvider.CurrentProvider.RemoveItem(this.printItemWrapper);
LibraryProvider.Instance.RemoveItem(this.printItemWrapper);
}
private void onRemoveLinkClick(object sender, EventArgs e)

View file

@ -237,7 +237,7 @@ namespace MatterHackers.MatterControl.PrintLibrary
private void searchButtonClick(object sender, EventArgs mouseEvent)
{
string searchText = searchInput.Text.Trim();
LibraryProvider.CurrentProvider.KeywordFilter = searchText;
LibraryProvider.Instance.KeywordFilter = searchText;
libraryDataView.ClearSelectedItems();
}
@ -356,7 +356,7 @@ namespace MatterHackers.MatterControl.PrintLibrary
public override void OnDragDrop(FileDropEventArgs fileDropEventArgs)
{
LibraryProvider.CurrentProvider.AddFilesToLibrary(fileDropEventArgs.DroppedFiles);
LibraryProvider.Instance.AddFilesToLibrary(fileDropEventArgs.DroppedFiles);
base.OnDragDrop(fileDropEventArgs);
}
@ -376,7 +376,7 @@ namespace MatterHackers.MatterControl.PrintLibrary
{
if (openParams.FileNames != null)
{
LibraryProvider.CurrentProvider.AddFilesToLibrary(openParams.FileNames);
LibraryProvider.Instance.AddFilesToLibrary(openParams.FileNames);
}
}
}

View file

@ -33,7 +33,6 @@ using MatterHackers.MatterControl.PrintQueue;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
namespace MatterHackers.MatterControl.PrintLibrary.Provider
{
@ -43,26 +42,29 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
public static RootedObjectEventHandler ItemAdded = new RootedObjectEventHandler();
public static RootedObjectEventHandler ItemRemoved = new RootedObjectEventHandler();
private static LibraryProvider currentProvider;
private static LibraryProvider instance;
public static LibraryProvider CurrentProvider
public static LibraryProvider Instance
{
get
{
if (currentProvider == null)
if (instance == null)
{
// hack for the moment
currentProvider = new LibraryProviderSQLite();
//PrintItemCollection collectionBase = new PrintItemCollection("Downloads", Path.Combine("C:\\", "Users", "LarsBrubaker", "Downloads"));
//currentProvider = new LibraryProviderFileSystem(collectionBase);
instance = new LibraryProviderSQLite(null);
//instance = new LibraryProviderSelector();
}
return currentProvider;
return instance;
}
}
#region AbstractMethods
#region Abstract Methods
public abstract void SetCollectionBase(PrintItemCollection collectionBase);
public abstract bool HasParent { get; }
public abstract string Key { get; }
public abstract string Name { get; }
public abstract int CollectionCount { get; }
@ -74,36 +76,51 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
public abstract void AddFilesToLibrary(IList<string> files, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null);
public abstract PrintItemCollection GetParentCollectionItem();
public abstract PrintItemCollection GetCollectionItem(int collectionIndex);
public abstract PrintItemCollection GetParentCollectionItem();
public abstract PrintItemWrapper GetPrintItemWrapper(int itemIndex);
public abstract void RemoveCollection(string collectionName);
public abstract void RemoveItem(PrintItemWrapper printItemWrapper);
#endregion AbstractMethods
public abstract void SetCollectionBase(PrintItemCollection collectionBase);
#endregion Abstract Methods
#region Static Methods
public static void OnDataReloaded(EventArgs eventArgs)
{
DataReloaded.CallEvents(CurrentProvider, eventArgs);
DataReloaded.CallEvents(Instance, eventArgs);
}
public static void OnItemAdded(EventArgs eventArgs)
{
ItemAdded.CallEvents(CurrentProvider, eventArgs);
ItemAdded.CallEvents(Instance, eventArgs);
}
public static void OnItemRemoved(EventArgs eventArgs)
{
ItemRemoved.CallEvents(CurrentProvider, eventArgs);
IndexArgs removeIndexArgs = eventArgs as IndexArgs;
if (removeIndexArgs != null)
{
int numIndicesToSkip = Instance.CollectionCount;
if (Instance.HasParent)
{
numIndicesToSkip++;
}
ItemRemoved.CallEvents(Instance, new IndexArgs(removeIndexArgs.Index + numIndicesToSkip));
}
}
public void SetCurrent(LibraryProvider current)
public static void SetCurrent(LibraryProvider current)
{
LibraryProvider.currentProvider = current;
LibraryProvider.instance = current;
}
#endregion Static Methods
}
}

View file

@ -39,46 +39,58 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
{
public class LibraryProviderFileSystem : LibraryProvider
{
public string key;
private static int keyCount = 0;
private string currentDirectory = ".";
private List<string> currentDirectoryFiles = new List<string>();
private List<string> currentDirectoryDirectories = new List<string>();
private List<string> currentDirectoryFiles = new List<string>();
private string description;
private string keywordFilter = string.Empty;
private string parentKey = null;
private string rootPath;
public LibraryProviderFileSystem(string rootPath)
public LibraryProviderFileSystem(string rootPath, string description)
{
this.description = description;
this.rootPath = rootPath;
key = keyCount.ToString();
keyCount++;
GetFilesInCurrentDirectory();
}
public LibraryProviderFileSystem(PrintItemCollection collectionBase)
public LibraryProviderFileSystem(PrintItemCollection collectionBase, string description, string parentKey)
{
this.parentKey = parentKey;
this.description = description;
this.rootPath = collectionBase.Key;
GetFilesInCurrentDirectory();
}
public override void SetCollectionBase(PrintItemCollection collectionBase)
{
string collectionPath = collectionBase.Key;
int startOfCurrentDir = collectionPath.IndexOf('.');
if (startOfCurrentDir != -1)
{
this.currentDirectory = collectionPath.Substring(startOfCurrentDir);
}
key = keyCount.ToString();
keyCount++;
GetFilesInCurrentDirectory();
}
public override int CollectionCount
{
get
get
{
return currentDirectoryDirectories.Count;
}
}
public override bool HasParent
{
get
{
if (parentKey != null)
{
return true;
}
return false;
}
}
public override int ItemCount
{
get
@ -87,6 +99,14 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public override string Key
{
get
{
return "LibraryProvider_" + key.ToString();
}
}
public override string KeywordFilter
{
get
@ -105,6 +125,8 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public override string Name { get { return description; } }
public override void AddCollectionToLibrary(string collectionName)
{
throw new NotImplementedException();
@ -115,23 +137,32 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
throw new NotImplementedException();
}
public override PrintItemCollection GetParentCollectionItem()
{
if (currentDirectory != ".")
{
string parentDirectory = Path.GetDirectoryName(currentDirectory);
return new PrintItemCollection("..", parentDirectory);
}
return null;
}
public override PrintItemCollection GetCollectionItem(int collectionIndex)
{
string directoryName = currentDirectoryDirectories[collectionIndex];
return new PrintItemCollection(Path.GetFileNameWithoutExtension(directoryName), directoryName);
}
public override PrintItemCollection GetParentCollectionItem()
{
if (currentDirectory == ".")
{
if (parentKey != null)
{
return new PrintItemCollection("..", parentKey);
}
else
{
return null;
}
}
else
{
string parentDirectory = Path.GetDirectoryName(currentDirectory);
return new PrintItemCollection("..", parentDirectory);
}
}
public override PrintItemWrapper GetPrintItemWrapper(int itemIndex)
{
string fileName = currentDirectoryFiles[itemIndex];
@ -148,6 +179,18 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
throw new NotImplementedException();
}
public override void SetCollectionBase(PrintItemCollection collectionBase)
{
string collectionPath = collectionBase.Key;
int startOfCurrentDir = collectionPath.IndexOf('.');
if (startOfCurrentDir != -1)
{
this.currentDirectory = collectionPath.Substring(startOfCurrentDir);
}
GetFilesInCurrentDirectory();
}
private void GetFilesInCurrentDirectory()
{
currentDirectoryDirectories.Clear();

View file

@ -0,0 +1,259 @@
/*
Copyright (c) 2015, Lars Brubaker
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using MatterHackers.Agg;
using MatterHackers.MatterControl.DataStorage;
using MatterHackers.MatterControl.PrintQueue;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
namespace MatterHackers.MatterControl.PrintLibrary.Provider
{
public class LibraryProviderSelector : LibraryProvider
{
private List<LibraryProvider> libraryProviders = new List<LibraryProvider>();
int selectedLibraryProvider = -1;
public LibraryProviderSelector()
{
// put in the sqlite provider
LibraryProviderSQLite localStore = new LibraryProviderSQLite(Key);
libraryProviders.Add(localStore);
// and any directory providers (sd card provider, etc...)
PrintItemCollection collectionBase = new PrintItemCollection("Downloads", Path.Combine("C:\\", "Users", "LarsBrubaker", "Downloads"));
libraryProviders.Add(new LibraryProviderFileSystem(collectionBase, "Downloads", Key));
// Check for LibraryProvider factories and put them in the list too.
}
#region Overriden Abstract Methods
public override int CollectionCount
{
get
{
if (selectedLibraryProvider == -1)
{
return libraryProviders.Count;
}
else
{
return libraryProviders[selectedLibraryProvider].CollectionCount;
}
}
}
public override int ItemCount
{
get
{
if (selectedLibraryProvider == -1)
{
return 0;
}
else
{
return libraryProviders[selectedLibraryProvider].ItemCount;
}
}
}
public override string KeywordFilter
{
get
{
if (selectedLibraryProvider == -1)
{
return "";
}
else
{
return libraryProviders[selectedLibraryProvider].KeywordFilter;
}
}
set
{
if (selectedLibraryProvider == -1)
{
}
else
{
libraryProviders[selectedLibraryProvider].KeywordFilter = value;
}
}
}
public override void AddCollectionToLibrary(string collectionName)
{
if (selectedLibraryProvider == -1)
{
throw new NotImplementedException();
}
else
{
libraryProviders[selectedLibraryProvider].AddCollectionToLibrary(collectionName);
}
}
public override void AddFilesToLibrary(IList<string> files, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null)
{
if (selectedLibraryProvider == -1)
{
throw new NotImplementedException();
}
else
{
libraryProviders[selectedLibraryProvider].AddFilesToLibrary(files, reportProgress, callback);
}
}
public override PrintItemCollection GetCollectionItem(int collectionIndex)
{
if (selectedLibraryProvider == -1)
{
LibraryProvider provider = libraryProviders[collectionIndex];
return new PrintItemCollection(provider.Name, provider.Key);
}
else
{
return libraryProviders[selectedLibraryProvider].GetCollectionItem(collectionIndex);
}
}
public override string Key
{
get
{
return "LibraryProviderSelector";
}
}
public override string Name
{
get
{
return "Never visible";
}
}
public override PrintItemCollection GetParentCollectionItem()
{
if (selectedLibraryProvider == -1)
{
return null;
}
else
{
return libraryProviders[selectedLibraryProvider].GetParentCollectionItem();
}
}
public override PrintItemWrapper GetPrintItemWrapper(int itemIndex)
{
if (selectedLibraryProvider == -1)
{
throw new NotImplementedException();
}
else
{
return libraryProviders[selectedLibraryProvider].GetPrintItemWrapper(itemIndex);
}
}
public override bool HasParent
{
get
{
if (selectedLibraryProvider == -1)
{
return false;
}
else
{
return libraryProviders[selectedLibraryProvider].HasParent;
}
}
}
public override void RemoveCollection(string collectionName)
{
if (selectedLibraryProvider == -1)
{
throw new NotImplementedException();
}
else
{
libraryProviders[selectedLibraryProvider].RemoveCollection(collectionName);
}
}
public override void RemoveItem(PrintItemWrapper printItemWrapper)
{
if (selectedLibraryProvider == -1)
{
throw new NotImplementedException();
}
else
{
libraryProviders[selectedLibraryProvider].RemoveItem(printItemWrapper);
}
}
public override void SetCollectionBase(PrintItemCollection collectionBase)
{
if (collectionBase.Key == Key)
{
selectedLibraryProvider = -1;
return;
}
bool wasSet = false;
for (int i = 0; i < libraryProviders.Count; i++)
{
if (libraryProviders[i].Key == collectionBase.Key)
{
selectedLibraryProvider = i;
wasSet = true;
break;
}
}
if (!wasSet)
{
libraryProviders[selectedLibraryProvider].SetCollectionBase(collectionBase);
}
}
#endregion Overriden Abstract Methods
}
}

View file

@ -38,21 +38,32 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
{
public class LibraryProviderSQLite : LibraryProvider
{
private string parentKey = null;
public LibraryProviderSQLite(string parentKey)
{
this.parentKey = parentKey;
}
public override int CollectionCount
{
get
get
{
return 0;
}
}
public override PrintItemCollection GetParentCollectionItem()
public override bool HasParent
{
return null;
}
get
{
if (parentKey != null)
{
return true;
}
public override void SetCollectionBase(PrintItemCollection collectionBase)
{
return false;
}
}
public override int ItemCount
@ -63,6 +74,14 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public override string Key
{
get
{
return "LibraryProviderSqlite";
}
}
public override string KeywordFilter
{
get
@ -76,6 +95,14 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public override string Name
{
get
{
return "Local Library";
}
}
public override void AddCollectionToLibrary(string collectionName)
{
throw new NotImplementedException();
@ -86,16 +113,28 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
LibrarySQLiteData.Instance.LoadFilesIntoLibrary(files, reportProgress, callback);
}
public override PrintItemWrapper GetPrintItemWrapper(int itemIndex)
{
return LibrarySQLiteData.Instance.GetPrintItemWrapper(itemIndex);
}
public override PrintItemCollection GetCollectionItem(int collectionIndex)
{
throw new NotImplementedException();
}
public override PrintItemCollection GetParentCollectionItem()
{
if (parentKey != null)
{
return new PrintItemCollection("..", parentKey);
}
else
{
return null;
}
}
public override PrintItemWrapper GetPrintItemWrapper(int itemIndex)
{
return LibrarySQLiteData.Instance.GetPrintItemWrapper(itemIndex);
}
public override void RemoveCollection(string collectionName)
{
throw new NotImplementedException();
@ -105,5 +144,9 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
{
LibrarySQLiteData.Instance.RemoveItem(printItemWrapper);
}
public override void SetCollectionBase(PrintItemCollection collectionBase)
{
}
}
}