public void MoveToSource()
        {
            CollectionControl control = (CollectionControl)tabControl.SelectedContent;

            if (control != null)
            {
                int            index    = control.lvCollection.SelectedIndex;
                List <Article> selected = new List <Article>();
                foreach (Article article in control.lvCollection.SelectedItems)
                {
                    selected.Add(article);
                }
                if (selected.Count != 0)
                {
                    foreach (Article article in selected)
                    {
                        control.Collection.Articles.Remove(article);
                        sourceCollection.Articles.Add(article);
                    }
                    control.lvCollection.Items.Refresh();
                    lvSource.Items.Refresh();
                    control.lvCollection.SelectedIndex = index;
                    lblStatus.Text = "Article(s) removed from collection: " + control.Collection.Name;
                }
            }
        }
        public void InitializeCollections()
        {
            System.IO.Directory.CreateDirectory(@"C:\Articles\");
            System.IO.Directory.CreateDirectory(@"C:\Articles\exports\");
            System.IO.Directory.CreateDirectory(@"C:\Articles\collections\");
            string[] filePaths = Directory.GetFiles(@"C:\Articles\collections\", "*.xml");
            foreach (string fileName in filePaths)
            {
                ArticleCollection collection = DeSerializeCollection <ArticleCollection>(fileName);
                if (collection != null)
                {
                    TabItem tab = new TabItem {
                        Content = new CollectionControl(collection.Name, collection), Header = collection.Name
                    };
                    tabControl.Items.Add(tab);
                    tabControl.SelectedItem = tab;
                    CollectionControl control = (CollectionControl)tabControl.SelectedContent;
                    if (control != null)
                    {
                        control.lvCollection.Items.Refresh();
                    }
                }
            }
            ArticleCollection src = DeSerializeCollection <ArticleCollection>(@"C:\Articles\source.xml");

            if (src != null)
            {
                foreach (Article article in src.Articles)
                {
                    sourceCollection.Articles.Add(article);
                }
                lvSource.ItemsSource = sourceCollection.Articles;
                lvSource.Items.Refresh();
                if (lvSource.Items.Count != 0)
                {
                    lvSource.SelectedItem = lvSource.Items[0];
                }
            }

            if (sourceCollection.Articles.Count == 0)
            {
                sourceCollection     = new ArticleCollection();
                lvSource.ItemsSource = sourceCollection.Articles;
                lvSource.Items.Refresh();
                if (lvSource.Items.Count != 0)
                {
                    lvSource.SelectedItem = lvSource.Items[0];
                }
            }

            lvSource.ItemsSource = sourceCollection.Articles;
            lvSource.Items.Refresh();
        }
 public void SaveCollections()
 {
     foreach (TabItem item in tabControl.Items)
     {
         CollectionControl control = (CollectionControl)item.Content;
         if (control != null)
         {
             string fileName = @"C:\Articles\collections\" + control.Collection.Name + ".xml";
             SerializeCollection <ArticleCollection>(control.Collection, fileName);
         }
     }
     SerializeCollection <ArticleCollection>(sourceCollection, @"C:\Articles\source.xml");
 }
        public void RemoveCollection()
        {
            CollectionControl control = (CollectionControl)tabControl.SelectedContent;

            try {
                TabItem ti = tabControl.SelectedItem as TabItem;

                System.IO.File.Delete(@"C:\Articles\collections\" + ti.Header + ".xml");
            } catch (Exception e) {
                lblStatus.Text = e.Message;
            }
            tabControl.Items.Remove(tabControl.SelectedItem);
        }
        public void ExportCollections()
        {
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            try {
                if (xlApp != null)
                {
                    xlApp.Visible = true;
                    Workbook  wb  = xlApp.Workbooks.Add(XlSheetType.xlWorksheet);
                    Worksheet ws1 = wb.ActiveSheet as Worksheet;
                    ws1.Name        = "Source";
                    ws1.Cells[1, 1] = "Something";

                    foreach (TabItem item in tabControl.Items)
                    {
                        CollectionControl control = (CollectionControl)item.Content;
                        if (control != null)
                        {
                            Worksheet ws = wb.Sheets.Add(Type.Missing, Type.Missing, 1, Type.Missing) as Worksheet;
                            ws.Name = control.Collection.Name;

                            // Alle aanwezige jaartallen ophalen uit collectie
                            List <int> jaartallen = new List <int>();
                            foreach (Article a in control.Collection.Articles)
                            {
                                int jaartal = Int32.Parse(a.Date.ToString().Substring(a.Date.ToString().Length - 4, 4));
                                if (!jaartallen.Contains(jaartal))
                                {
                                    jaartallen.Add(jaartal);
                                }
                            }
                            jaartallen.Sort();
                            Dictionary <int, int> jaartal_indices = new Dictionary <int, int>();
                            for (int i = 0; i < jaartallen.Count; i++)
                            {
                                jaartal_indices[jaartallen[i]] = i;
                            }
                            for (int i = 0; i < jaartallen.Count; i++)
                            {
                                ws.Cells[1, i + 2] = jaartallen[i];
                            }
                            ws.Cells[1, jaartallen.Count + 2] = "Totaal";

                            // Alle data in collectie inlezen naar SortedDictionary container
                            SortedDictionary <string, SortedDictionary <int, int> > collection_data = new SortedDictionary <string, SortedDictionary <int, int> >();
                            foreach (Article a in control.Collection.Articles)
                            {
                                string publisher = a.Source;
                                int    jaartal   = Int32.Parse(a.Date.ToString().Substring(a.Date.ToString().Length - 4, 4));

                                if (!collection_data.ContainsKey(publisher))
                                {
                                    collection_data[publisher] = new SortedDictionary <int, int>();
                                }
                                if (!collection_data[publisher].ContainsKey(jaartal))
                                {
                                    collection_data[publisher][jaartal] = 0;
                                }
                                collection_data[publisher][jaartal] = collection_data[publisher][jaartal] + 1;
                            }

                            // Export data van in SortedDictionary naar Excel cells
                            int index = 0;
                            foreach (KeyValuePair <string, SortedDictionary <int, int> > krant in collection_data)
                            {
                                ws.Cells[index + 2, 1] = krant.Key;
                                int aantal_artikels_per_krant = 0;
                                for (int fill = 0; fill < jaartallen.Count; fill++)
                                {
                                    ws.Cells[index + 2, fill + 2] = 0;
                                }
                                foreach (KeyValuePair <int, int> jaartal in collection_data[krant.Key])
                                {
                                    ws.Cells[index + 2, jaartal_indices[jaartal.Key] + 2] = jaartal.Value;
                                    aantal_artikels_per_krant += jaartal.Value;
                                }
                                ws.Cells[index + 2, jaartal_indices.Count + 2] = aantal_artikels_per_krant;
                                index++;
                            }
                        }
                    }
                    wb.SaveAs(@"C:\Articles\exports\collections_" + DateTime.Now.ToString("dd-MM-yyyy-HH-mm-tt") + ".xlsx");
                    //wb.Close(Type.Missing, Type.Missing, Type.Missing);
                    //xlApp.UserControl = true;
                    //xlApp.Quit();
                    lblStatus.Text = "Collections successfully exported to Excel file.";
                }
            } catch (Exception e) {
                Console.Out.WriteLine(e.Message);
            } finally {
                Marshal.ReleaseComObject(xlApp);
            }
        }