public void Drop(IDropInfo dropInfo) { ProgramToStart sourceItem = dropInfo.Data as ProgramToStart; //dragged item ProgramToStart targetItem = dropInfo.TargetItem as ProgramToStart; //item on with user drops the sourceitem RelativeInsertPosition positionOfItem = dropInfo.InsertPosition; //position (before or after targetItem) int insertIndex = dropInfo.InsertIndex; //positon in ProgramsToStartList where item was dropped //Checking if item was dropped before or after targetItem and moving into correct position in ProgramsToStartList if (positionOfItem == (RelativeInsertPosition.BeforeTargetItem | RelativeInsertPosition.TargetItemCenter)) { ProgramsToStartList.Move(sourceItem.StartingOrder - 1, insertIndex); } else { //Checking if new position is the last in the list - if it is last, put item in the last position //To prevent ArgumentOutOfRange Exception for ProgramsToStartList.Move() if (insertIndex == ProgramsToStartList.Count) { ProgramsToStartList.Move(sourceItem.StartingOrder - 1, insertIndex - 1); } else { ProgramsToStartList.Move(sourceItem.StartingOrder - 1, insertIndex); } } //After source Item was moved to the new position we need to update StartingOrder property of all ProgramsToStart UpdateStartingOrdersInProgramsToStartListView(); //Then we need to refresh the ListView RefreshProgramsToStartListView(); }
/// <summary> /// Tries to remove ProgramToStart at the given index from the dictionary /// Recalculates keys for all other programs /// </summary> /// <param name="index">Index from which program should be removed (order)</param> /// <returns>True if program was succesfuly removed, false if there were errors</returns> public bool TryRemoveProgramToStart(int index) { if (index <= 0) { Logger.DoErrorLogKV("TryRemoveProgramToStart called with index below or equal 0!", "Index", index.ToString()); ProgramsToStartCollectionChanged?.Invoke(OperationType.Removed, false, string.Empty); return(false); } if (index.IsGreaterThan(ProgramsToStart.Count)) { Logger.DoErrorLogKV("TryRemoveProgramToStart called with index greater then ProgramsToStart.Count!", "Index", index.ToString(), "ProgramsToStart.Count", ProgramsToStart.Count.ToString()); ProgramsToStartCollectionChanged?.Invoke(OperationType.Removed, false, string.Empty); return(false); } ProgramToStart program = ProgramsToStart[index]; try { ProgramsToStart.RemoveProgramToStart(index); ProgramsToStartCollectionChanged?.Invoke(OperationType.Removed, true, program.Name); return(true); } catch (Exception ex) { Logger.DoErrorLogKV("Error while trying to remove program from ProgramsToStart dictionary: ", "Index", index.ToString(), "Error", ex.Message); ProgramsToStartCollectionChanged?.Invoke(OperationType.Removed, false, program.Name); } return(false); }
/// <summary> /// Tries to move ProgramToStart at the given index in the dictionary /// Recalculates keys for all other programs /// </summary> /// <param name="oldIndex">Index from which program should be moved (order)</param> /// <param name="newIndex">Index to which program should be moved (order)</param> /// <returns>True if program was succesfuly moved, false if there were errors</returns> public bool TryChangeProgramToStartIndex(int oldIndex, int newIndex) { if (newIndex <= 0) { Logger.DoErrorLogKV("TryChangeProgramToStartIndex called with newIndex below or equal 0!", "newIndex", newIndex.ToString(), "Program", string.Empty); ProgramsToStartCollectionChanged?.Invoke(OperationType.Moved, false, string.Empty); return(false); } if (oldIndex <= 0) { Logger.DoErrorLogKV("TryChangeProgramToStartIndex called with oldIndex below or equal 0!", "oldIndex", oldIndex.ToString(), "Program", string.Empty); ProgramsToStartCollectionChanged?.Invoke(OperationType.Moved, false, string.Empty); return(false); } ProgramToStart program = ProgramsToStart[oldIndex]; try { ProgramsToStart.ChangeProgramToStartIndex(oldIndex, newIndex); ProgramsToStartCollectionChanged?.Invoke(OperationType.Moved, true, program.Name); return(true); } catch (Exception ex) { ProgramsToStartCollectionChanged?.Invoke(OperationType.Moved, false, program.Name); Logger.DoErrorLogKV("Error while trying to change items index in ProgramsToStart dictionary: ", "Program", program.ToString(), "Error", ex.Message); } return(false); }
/// <summary> /// This method is reading all programs from xml file and returns them as List /// </summary> /// <returns></returns> public List <ProgramToStart> ReadProgramsToStartList() { List <ProgramToStart> programsList = new List <ProgramToStart>(); XmlDocument doc = new XmlDocument(); int temp = 0; if (File.Exists(XMLPath)) { doc.Load(XMLPath); XmlNodeList programsToStartNodes = doc.DocumentElement.SelectNodes("/ProgramStarter/ProgramsToStart/Program"); foreach (XmlNode programNode in programsToStartNodes) { ProgramToStart _program = new ProgramToStart(programNode.Attributes["name"].Value, programNode.Attributes["path"].Value); if (int.TryParse(programNode.Attributes["order"].Value, out temp)) { _program.StartingOrder = int.Parse(programNode.Attributes["order"].Value); } else { throw new Exception("Starting Order could not be parsed into int for: " + _program.ProgramName); } programsList.Add(_program); } } else { throw new Exception("XML file not found at path: " + XMLPath); } return(programsList); }
private void AddProgramToCollectionCommand() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.CheckFileExists = true; openFileDialog.CheckPathExists = true; openFileDialog.Multiselect = false; openFileDialog.Title = "Select Programs to add to Programs Starter"; openFileDialog.ValidateNames = true; if (openFileDialog.ShowDialog() == true) { ProgramToStart program = new ProgramToStart(openFileDialog.SafeFileName, openFileDialog.FileName); //check if user clicked on program or on some empty field in ListView //if user clicked on program then insert new program before the selected item if (SelectedItem != null) { HandlersManager.StartingProgramsHandler.TryInsertProgramToStart(program, SelectedItem.Order); } else //if user clicked on some empty field then add new program at the end of the list { HandlersManager.StartingProgramsHandler.TryAddProgramToStart(program); } } }
public static void ChangeProgramToStartIndex(this Dictionary <int, ProgramToStart> dict, int oldIndex, int newIndex) { if (dict == null) { throw new ArgumentNullException(); } if (oldIndex <= 0) { throw new ArgumentException("oldIndex below or equal 0!"); } if (oldIndex > dict.Count) { throw new ArgumentOutOfRangeException("oldIndex is bigger then number of objects in dictionary!"); } if (newIndex <= 0) { throw new ArgumentException("newIndex below or equal 0!"); } if (newIndex > dict.Count) { throw new ArgumentOutOfRangeException("newIndex is bigger then number of objects in dictionary!"); } ProgramToStart movedProgram = dict[oldIndex]; dict.RemoveProgramToStart(oldIndex); dict.InsertProgramToStart(movedProgram, newIndex); }
/// <summary> /// Tries to insert ProgramToStart at the given index in the dictionary /// </summary> /// <param name="program">Program to add</param> /// <param name="index">Index at which program should be added (order)</param> /// <returns>True if program was succesfuly inserted, false if there were some errors</returns> public bool TryInsertProgramToStart(ProgramToStart program, int index) { if (program == null || string.IsNullOrWhiteSpace(program.Name) || string.IsNullOrWhiteSpace(program.Path)) { Logger.DoErrorLog("TryInsertProgramToStart called with null parameter!"); ProgramsToStartCollectionChanged?.Invoke(OperationType.Added, false, string.Empty); return(false); } if (index <= 0) { Logger.DoErrorLogKV("TryInsertProgramToStart called with index below or equal 0!", "Index", index.ToString(), "Program", program.ToString()); ProgramsToStartCollectionChanged?.Invoke(OperationType.Added, false, program.Name); return(false); } if (index >= GetNewIndexForProgramToStart()) { return(TryAddProgramToStart(program)); } try { ProgramsToStart.InsertProgramToStart(program, index); ProgramsToStartCollectionChanged?.Invoke(OperationType.Added, true, program.Name); return(true); } catch (Exception ex) { ProgramsToStartCollectionChanged?.Invoke(OperationType.Added, false, program.Name); Logger.DoErrorLogKV("Error while trying to insert new program to ProgramsToStart dictionary: ", "Program", program.ToString(), "Error", ex.Message); } return(false); }
/// <summary> /// Tries to add ProgramToStart at the end of the dictionary /// </summary> /// <param name="program">Program to add</param> /// <returns>True if program was succesfuly added, false if there were some errors</returns> public bool TryAddProgramToStart(ProgramToStart program) { if (program == null || string.IsNullOrWhiteSpace(program.Name) || string.IsNullOrWhiteSpace(program.Path)) { Logger.DoErrorLog("TryAddProgramToStart called with null parameter!"); ProgramsToStartCollectionChanged?.Invoke(OperationType.Added, false, string.Empty); return(false); } int i = GetNewIndexForProgramToStart(); try { ProgramsToStart.Add(i, program); ProgramsToStartCollectionChanged?.Invoke(OperationType.Added, true, program.Name); return(true); } catch (Exception ex) { ProgramsToStartCollectionChanged?.Invoke(OperationType.Added, false, program.Name); Logger.DoErrorLogKV("Error while trying to add new program to ProgramsToStart dictionary: ", "Program", program.ToString(), "Error", ex.Message); } return(false); }
/// <summary> /// This method is reading all programs from xml file and returns them as Dictionary /// </summary> /// <returns>Dictionary with programs to start, empty if no programs found</returns> public Dictionary <int, ProgramToStart> ReadProgramsToStartFromConfig() { Dictionary <int, ProgramToStart> programsDict = new Dictionary <int, ProgramToStart>(); XmlDocument doc = new XmlDocument(); int temp = 0; if (File.Exists(XMLPath)) { try { doc.Load(XMLPath); XmlNodeList programsToStartNodes = doc.DocumentElement.SelectNodes(PROGRAMS_TO_START_XML_PATH); if (programsToStartNodes != null && programsToStartNodes.Count > 0) { foreach (XmlNode programNode in programsToStartNodes) { ProgramToStart _program = new ProgramToStart(programNode.Attributes["name"].Value, programNode.Attributes["path"].Value); if (int.TryParse(programNode.Attributes["order"].Value, out temp)) { programsDict.Add(temp, _program); } else { Logger.DoErrorLogKV("Starting Order could not be parsed into int: ", "Program", _program.Name); } } } else { Logger.DoWarningLog("No programs to start found in xml file"); NoProgramsToStartFound?.Invoke(); } } catch (Exception ex) { Logger.DoErrorLogKV("Error while reading ProgramsToStart from xml: ", "XMLPath", XMLPath, "Error", ex.Message); } } else { Logger.DoErrorLog("Error while reading ProgramsToStart from xml - XML file not found at path: " + XMLPath); } return(programsDict); }
/// <summary> /// This method is reading all programs from xml file and return them as ObservableCollection /// </summary> /// <returns></returns> public ObservableCollection <ProgramToStart> ReadProgramsToStartCollection() { ObservableCollection <ProgramToStart> programsList = new ObservableCollection <ProgramToStart>(); XmlDocument doc = new XmlDocument(); int temp = 0; //if XMLPath is null throw exception if (XMLPath == null) { throw new Exception("XMLPath is null"); } if (File.Exists(XMLPath)) { doc.Load(XMLPath); XmlNodeList programsToStartNodes = doc.DocumentElement.SelectNodes("/ProgramStarter/ProgramsToStart/Program"); foreach (XmlNode programNode in programsToStartNodes) { if (programNode.Attributes["name"] != null && programNode.Attributes["path"] != null && programNode.Attributes["order"] != null) { ProgramToStart _program = new ProgramToStart(programNode.Attributes["name"].Value, programNode.Attributes["path"].Value); if (int.TryParse(programNode.Attributes["order"].Value, out temp)) { _program.StartingOrder = int.Parse(programNode.Attributes["order"].Value); } else { throw new Exception("Starting Order could not be parsed into int for: " + _program.ProgramName); } programsList.Add(_program); } else { throw new Exception("One of Program To Start in configuration.xml doesn't have name, path or order (Null Exception)"); } } } else { throw new Exception("XML file not found at path: " + XMLPath); } return(programsList); }
/// <summary> /// This method is starting given program /// </summary> /// <param name="program">Program which needs to be started</param> private void StartProgram(ProgramToStart program) { // Check if given program is valid if (program == null || string.IsNullOrWhiteSpace(program.Name) || string.IsNullOrWhiteSpace(program.Path)) { Logger.DoErrorLog("StartProgram called with null parameter!"); ProgramsToStartCollectionChanged?.Invoke(OperationType.Started, false, string.Empty); return; } // Check if file exist if (!File.Exists(program.Path)) { Logger.DoErrorLogKV("Program does not exist at given path", "Program", program.ToString(), "Path", program.Path); program.SetProgramStatus(ProgramStatus.Error); ProgramsToStartCollectionChanged?.Invoke(OperationType.Started, false, program.Name); return; } try { ProcessStartInfo processInfo = new ProcessStartInfo(); //Check if file is an Excel sheet and Excel is installed then use default process if (FileIsExcelSheet(program.Path) && ExcelIsInstalled()) { processInfo.FileName = "Excel"; processInfo.Arguments = program.Path; Process process = Process.Start(processInfo); } else { //Start the program from it's path Process process = Process.Start(program.Path); } program.SetProgramStatus(ProgramStatus.Starting); ProgramsToStartCollectionChanged?.Invoke(OperationType.Started, true, program.Name); } catch (Exception ex) { Logger.DoErrorLogKV("Error while starting program!", "Program", program.ToString(), "Error", ex.Message); program.SetProgramStatus(ProgramStatus.Error); ProgramsToStartCollectionChanged?.Invoke(OperationType.Started, false, program.Name); } }
public void RemoveProgramToStartTest1() { Dictionary <int, ProgramToStart> testDict = new Dictionary <int, ProgramToStart>(); ProgramToStart program1 = new ProgramToStart("test1", "D:\test1.txt"); testDict.Add(1, program1); ProgramToStart program2 = new ProgramToStart("test2", "D:\test2.txt"); testDict.Add(2, program2); ProgramToStart program3 = new ProgramToStart("test3", "D:\test3.txt"); testDict.Add(3, program3); testDict.RemoveProgramToStart(2); Assert.AreEqual(2, testDict.Count); Assert.AreEqual(program1, testDict[1]); Assert.AreEqual(program3, testDict[2]); }
public void ChangeProgramToStartIndexTest2() { Dictionary <int, ProgramToStart> testDict = new Dictionary <int, ProgramToStart>(); ProgramToStart program1 = new ProgramToStart("test1", "D:\test1.txt"); testDict.Add(1, program1); ProgramToStart program2 = new ProgramToStart("test2", "D:\test2.txt"); testDict.Add(2, program2); ProgramToStart program3 = new ProgramToStart("test3", "D:\test3.txt"); testDict.Add(3, program3); testDict.ChangeProgramToStartIndex(3, 1); Assert.AreEqual(3, testDict.Count); Assert.AreEqual(program1, testDict[2]); Assert.AreEqual(program3, testDict[1]); Assert.AreEqual(program2, testDict[3]); }
public void RefreshProgramsToStartListView() { // //TODO: This is a temporary solution, need to figure out how to refresh ListView without clearing whole list ObservableCollection <ProgramToStart> _temp = new ObservableCollection <ProgramToStart>(); foreach (ProgramToStart item in ProgramsToStartList) { ProgramToStart insert = new ProgramToStart(item.ProgramName, item.Path); insert.StartingOrder = item.StartingOrder; _temp.Add(insert); } ProgramsToStartList.Clear(); foreach (ProgramToStart item in _temp) { ProgramToStart insert = new ProgramToStart(item.ProgramName, item.Path); insert.StartingOrder = item.StartingOrder; ProgramsToStartList.Add(insert); } }
public void InsertProgramToStartTest1() { Dictionary <int, ProgramToStart> testDict = new Dictionary <int, ProgramToStart>(); ProgramToStart program1 = new ProgramToStart("test1", "D:\test1.txt"); testDict.Add(1, program1); ProgramToStart program2 = new ProgramToStart("test2", "D:\test2.txt"); testDict.Add(2, program2); ProgramToStart program3 = new ProgramToStart("test3", "D:\test3.txt"); testDict.Add(3, program3); ProgramToStart testProgram = new ProgramToStart("testProgram", "D:\testProgram.txt"); testDict.InsertProgramToStart(testProgram, 2); Assert.AreEqual(4, testDict.Count); Assert.AreEqual(testProgram, testDict[2]); Assert.AreEqual(program2, testDict[3]); Assert.AreEqual(program3, testDict[4]); }
/// <summary> /// This method is starting next program in the list, calculating PercentOfStartedPrograms and incrementing CurrentProgramStartingOrder /// </summary> private void StartNextProgram() { //Start program ProgramToStart program = ProgramsToStartList.Where(p => p.StartingOrder == CurrentProgramStartingOrder).FirstOrDefault(); string path = ProgramsToStartList.Where(p => p.StartingOrder == CurrentProgramStartingOrder).Select(x => x.Path).FirstOrDefault().ToString(); try { StartProgram(path); } catch (Exception ex) { HasErrors = true; ErrorLog log = new ErrorLog(DateTime.Now, program.ProgramName, program.Path, ex.ToString()); ErrorsList.Add(log); } //Calculate percentage of started programs PercentOfStartedPrograms = ((float)CurrentProgramStartingOrder / ProgramsToStartList.Count()) * 100; //Increment CurrentProgramStartingOrder CurrentProgramStartingOrder++; }
private void AddProgramContextMenuItemClicked(object obj) { //TODO: This is breaking the MVVM concept, need to figure out how to this in MVVM style OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == true) { ProgramToStart program = new ProgramToStart(openFileDialog.SafeFileName, openFileDialog.FileName); //check if user clicked on program or on some empty field in ListView //if user clicked on program then insert new program before the selected item if (SelectedProgramOnProgramsToStartListView != null) { ProgramsToStartList.Insert(SelectedProgramOnProgramsToStartListView.StartingOrder - 1, program); } else //if user clicked on some empty field then add new program at the end of the list { ProgramsToStartList.Add(program); } UpdateStartingOrdersInProgramsToStartListView(); RefreshProgramsToStartListView(); } }
/// <summary> /// Inserts ProgramToStart with given index as Key, increments all key which are equal or greater /// </summary> /// <param name="dict">Dictionary with programs to start</param> /// <param name="program">New program to be insterted into dictionary</param> /// <param name="index">index on which new program should be inserted</param> public static void InsertProgramToStart(this Dictionary <int, ProgramToStart> dict, ProgramToStart program, int index) { if (program == null || dict == null) { throw new ArgumentNullException(); } if (index <= 0) { throw new ArgumentException("Index below or equal 0!"); } List <KeyValuePair <int, ProgramToStart> > tempList = new List <KeyValuePair <int, ProgramToStart> >(); foreach (var item in dict.OrderBy(x => x.Key)) { if (item.Key >= index) { tempList.Add(item); dict.Remove(item.Key); } } dict.Add(index, program); foreach (var item in tempList.OrderBy(x => x.Key)) { dict.Add(item.Key + 1, item.Value); } tempList.Clear(); }