Skip to content

Commit

Permalink
Fixed import and export
Browse files Browse the repository at this point in the history
  • Loading branch information
vanBassum committed Nov 7, 2020
1 parent 7527646 commit 4556c7f
Show file tree
Hide file tree
Showing 10 changed files with 605 additions and 54 deletions.
52 changes: 13 additions & 39 deletions StuffDatabase/StuffDatabase/CTRL_Component.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

144 changes: 138 additions & 6 deletions StuffDatabase/StuffDatabase/CTRL_Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.IO;
using System.Collections.ObjectModel;
using StuffDatabase.Components;
using System.Reflection;

namespace StuffDatabase
{
Expand All @@ -23,6 +24,7 @@ public partial class CTRL_Component : UserControl
string TemplateFolder { get { return CreatePathIfNotExist(Path.Combine(Settings.ComponentData, ComponentType.Name, "Templates")); } }
string DatasheetFolder { get { return CreatePathIfNotExist(Path.Combine(Settings.ComponentData, ComponentType.Name, "Datasheets")); } }

public bool ChangePending { get; set; } = false;

string CreatePathIfNotExist(string path)
{
Expand Down Expand Up @@ -52,6 +54,7 @@ private void CTRL_Components_Load(object sender, EventArgs e)

private void Components_ListChanged(object sender, ListChangedEventArgs e)
{
ChangePending = true;
switch (e.ListChangedType)
{
case ListChangedType.ItemAdded:
Expand Down Expand Up @@ -117,7 +120,6 @@ void StopEditComponent(bool saveChanges)
EditCompOrigional.Populate(EditCompNew);
if(!Components.Contains(EditCompOrigional))
Components.Add(EditCompOrigional);
Components.Save();
}
SetUIEditMode(false);
Render(EditCompOrigional);
Expand Down Expand Up @@ -205,7 +207,6 @@ private void btn_Delete_Click(object sender, EventArgs e)
if (selectedComponent != null)
{
Components.Remove(selectedComponent);
Components.Save();
}
}

Expand All @@ -219,17 +220,148 @@ private void btn_Print_Click(object sender, EventArgs e)
}
}

private void textBox1_TextChanged(object sender, EventArgs e)

public void Search(string searchString)
{
if(textBox1.Text == "")
if (searchString == "")
Filter = (a) => a.GetType() == ComponentType;
else
Filter = (a) => a.GetType() == ComponentType && a.Name.ToLower().Contains(textBox1.Text.ToLower());
Filter = (a) => a.GetType() == ComponentType && a.Name.ToLower().Contains(searchString.ToLower());

RePopulateList(true);
}
}

public void Export(string file)
{

using (StreamWriter writer = new StreamWriter(file))
{
PropertyInfo[] pis = ComponentType.GetProperties();
foreach (PropertyInfo pi in pis)
{
writer.Write($"\"{pi.Name}\"" + ((pi == pis.Last())?"\r\n":","));
}

foreach (BaseComponent component in Components)
{
if (Filter(component))
{
foreach (PropertyInfo pi in pis)
{
object val = pi.GetValue(component);
switch (val)
{
case double v:
writer.Write($"\"{v.ToString("E")}\"" + ((pi == pis.Last()) ? "\r\n" : ","));
break;

default:
writer.Write($"\"{val}\"" + ((pi == pis.Last()) ? "\r\n" : ","));
break;
}
}
}
}
}
}

public void Import(string file)
{
List<PropertyInfo> props = new List<PropertyInfo>();
SaveableBindingList<BaseComponent> comps = new SaveableBindingList<BaseComponent>();

using (StreamReader reader = new StreamReader(file))
{
//Read header

string header = reader.ReadLine();

foreach(string propName in header.Split(','))
{
PropertyInfo pi = ComponentType.GetProperty(propName.Trim('"'));
if (pi == null)
throw new Exception("Property not found.");
props.Add(pi);
}

while(!reader.EndOfStream)
{
List<string> values = new List<string>();
string line = reader.ReadLine();

string val = "";
bool b = false;
foreach(char c in line)
{
if (c == '"')
b = !b;
else
{
if (b)
val += c;
else
{
if (c == ',')
{
values.Add(val);
val = "";
}

else
val += c;
}
}

}



BaseComponent comp = (BaseComponent)Activator.CreateInstance(ComponentType);

for (int i = 0; i < values.Count; i++)
{
if(props[i].PropertyType == typeof(double))
{
props[i].SetValue(comp, double.Parse(values[i]));
}
else
{
props[i].SetValue(comp, values[i]);
}


}
comps.Add(comp);
}

}

Frm_Import frm = new Frm_Import(comps, ComponentType);

if (frm.ShowDialog() == DialogResult.OK)
{
bool overrideExisting = frm.OverrideExisting;
foreach(BaseComponent comp in comps)
{
int ind = Components.IndexOf(Components.FirstOrDefault(c => c.Name == comp.Name && c.Function == comp.Function));
if(ind == -1)
{
Components.Add(comp); //Add new component
}
else
{
if(overrideExisting)
{
Components.RemoveAt(ind); //Override existing
Components.Add(comp);
}
else
{
//TODO ask???
}
}
}
}
}
}
}
3 changes: 0 additions & 3 deletions StuffDatabase/StuffDatabase/Components/BaseComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public abstract class BaseComponent : PropertySensitive
[Browsable(false)]
public string Datasheet { get { return GetPar(""); } set { SetPar(value); } }



private static int MaxNameLength { get; set; }
public override string ToString()
{
Expand Down Expand Up @@ -56,5 +54,4 @@ public void Populate(BaseComponent comp)
pi.SetValue(this, pi.GetValue(comp));
}
}

}
Loading

0 comments on commit 4556c7f

Please sign in to comment.