/// <summary> /// Handles the <see cref="INotifyCollectionChanged.CollectionChanged"/> event of the view model's /// installed plugin list. /// </summary> /// <remarks> /// This updates the list of plugins to refelct changes to the installed plugin list. /// </remarks> /// <param name="sender">The object that raised the event.</param> /// <param name="e">A <see cref="NotifyCollectionChangedEventArgs"/> describing the event arguments.</param> private void ManagedPlugins_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (rlvPlugins.InvokeRequired) { rlvPlugins.Invoke((Action <object, NotifyCollectionChangedEventArgs>)ManagedPlugins_CollectionChanged, sender, e); return; } switch (e.Action) { case NotifyCollectionChangedAction.Add: case NotifyCollectionChangedAction.Replace: Int32 intIndex = e.NewStartingIndex; foreach (Plugin plgAdded in e.NewItems) { InsertPluginToList(intIndex++, plgAdded); } break; case NotifyCollectionChangedAction.Remove: case NotifyCollectionChangedAction.Reset: foreach (Plugin plgRemoved in e.OldItems) { rlvPlugins.Items.RemoveByKey(plgRemoved.Filename.ToLowerInvariant()); } RefreshPluginIndices(); break; case NotifyCollectionChangedAction.Move: Int32 intNewIndex = e.NewStartingIndex; Int32 intOldIndex = e.OldStartingIndex; PluginComparer pcpComparer = PluginComparer.Filename; foreach (Plugin plgMoved in e.NewItems) { //if the item at the new index equals the moved item, // then the item has already been moved in the UI, // so do nothing... if (!pcpComparer.Equals(plgMoved, (Plugin)rlvPlugins.Items[intNewIndex].Tag)) { //...otherwise, move the item ListViewItem lviMoved = rlvPlugins.Items[intOldIndex]; rlvPlugins.Items.RemoveAt(intOldIndex); rlvPlugins.Items.Insert(intNewIndex, lviMoved); } intNewIndex++; intOldIndex++; } if (rlvPlugins.SelectedItems.Count > 0) { rlvPlugins.SelectedItems[0].Focused = true; } SetCommandExecutableStatus(); RefreshPluginIndices(); break; } }
public StatusBar(App app, PreferencesWindowFactory preferencesWindowFactory) { _app = app; _preferencesWindowFactory = preferencesWindowFactory; var comparer = new PluginComparer(); _subscription = _app .Select(plugins => plugins.Where(p => p.IsActive)) .DistinctUntilChanged() .Select(plugins => plugins.ToImmutableList()) .Subscribe(plugins => OnUpdateMenu(plugins)); }
/// <summary> /// Commits the changes to the <see cref="PluginOrderLog"/>. /// </summary> public void Commit() { PluginComparer pcpComparer = PluginComparer.Filename; Dictionary <Plugin, Plugin> dicPredecessors = new Dictionary <Plugin, Plugin>(); ThreadSafeObservableList <Plugin> oclUnorderedList = EnlistedPluginOrderLog.m_oclOrderedPlugins; // Removed faulty lock, it should no longer be necessary anyway //lock (EnlistedPluginOrderLog.m_oclOrderedPlugins) //{ EnlistedPluginOrderLog.m_oclOrderedPlugins.CollectionChanged -= MasterOrderedPlugins_CollectionChanged; IList <Plugin> lstOrderedList = m_oclOrderedPlugins; //sort the items whose order has been specified for (Int32 i = 0; i < lstOrderedList.Count; i++) { Int32 intOldIndex = 0; for (intOldIndex = 0; intOldIndex < oclUnorderedList.Count; intOldIndex++) { if (pcpComparer.Equals(oclUnorderedList[intOldIndex], lstOrderedList[i])) { break; } } if (intOldIndex == oclUnorderedList.Count) { oclUnorderedList.Insert(i, lstOrderedList[i]); continue; } if (intOldIndex != i) { oclUnorderedList.Move(intOldIndex, i); } } //as the transacted order list has been kept in sync with the master list // the transacted list is canonical (it contains all of the plugins, // and no plugins that shouldn't be present), so // if a plugin is not in the transacted list it means the plugin was removed, // and should be removed form the master list for (Int32 i = oclUnorderedList.Count - 1; i >= lstOrderedList.Count; i--) { oclUnorderedList.RemoveAt(i); } EnlistedPluginOrderLog.OrderValidator.CorrectOrder(oclUnorderedList); //} EnlistedPluginOrderLog.SavePluginLog(); m_booEnlisted = false; m_oclOrderedPlugins.Clear(); }
/// <summary> /// Sets the order of the plugins to the given order. /// </summary> /// <remarks> /// If the given list does not include all registered plugins, then the plugins are ordered in a manner /// so as to not displace the positions of the plugins whose order was not specified. /// </remarks> /// <param name="p_lstOrderedPlugins">The list indicating the desired order of the plugins.</param> public void SetPluginOrder(IList <Plugin> p_lstOrderedPlugins) { PluginComparer pcpComparer = PluginComparer.Filename; Dictionary <Plugin, Plugin> dicPredecessors = new Dictionary <Plugin, Plugin>(); ThreadSafeObservableList <Plugin> oclUnorderedList = m_oclOrderedPlugins; IList <Plugin> lstOrderedList = p_lstOrderedPlugins; string strError = String.Empty; try { strError = "Setup"; for (Int32 i = 0; i < oclUnorderedList.Count; i++) { if (!lstOrderedList.Contains(oclUnorderedList[i], pcpComparer)) { dicPredecessors[oclUnorderedList[i]] = (i > 0) ? oclUnorderedList[i - 1] : null; } } strError = "Specified"; //sort the items whose order has been specified for (Int32 i = 0; i < lstOrderedList.Count; i++) { strError = "intOldIndex"; Int32 intOldIndex = 0; for (intOldIndex = 0; intOldIndex < oclUnorderedList.Count; intOldIndex++) { if (pcpComparer.Equals(oclUnorderedList[intOldIndex], lstOrderedList[i])) { break; } } if (intOldIndex >= oclUnorderedList.Count) { strError = "Insert"; oclUnorderedList.Insert(i, lstOrderedList[i]); continue; } if (intOldIndex != i) { strError = "Move intOldIndex"; oclUnorderedList.Move(intOldIndex, i); } } strError = "Predecessor"; //sort the items whose order has not been specified // if an item's order hasn't been specified, it is placed after the // item it followed in the original, unordered, list for (Int32 i = lstOrderedList.Count; i < oclUnorderedList.Count; i++) { Plugin plgPredecessor = null; Int32 intNewIndex = -1; if (dicPredecessors.ContainsKey(oclUnorderedList[i])) { plgPredecessor = dicPredecessors[oclUnorderedList[i]]; } //if the predecessor is null, then the item was at the beginning of the list if (plgPredecessor != null) { for (intNewIndex = 0; intNewIndex < oclUnorderedList.Count; intNewIndex++) { if (pcpComparer.Equals(oclUnorderedList[intNewIndex], plgPredecessor)) { break; } } } strError = "intNewIndex + 1"; if (intNewIndex + 1 != i) { oclUnorderedList.Move(i, intNewIndex + 1); } } EnlistedPluginOrderLog.OrderValidator.CorrectOrder(oclUnorderedList); if (CurrentTransaction == null) { Commit(); } else { Enlist(); } } catch (Exception e) { throw new Exception(strError, e); } }