public static bool CanPaste(EventViewmodel destEventVm, TPasteLocation location)
 {
     if (destEventVm == null)
         return false;
     Event dest = destEventVm.Event;
     lock (_clipboard.SyncRoot)
     {
         var operation = Operation;
         using (var enumerator = _clipboard.GetEnumerator())
         {
             if (!enumerator.MoveNext())
                 return false;
             if (!_canPaste(enumerator.Current, dest, location, operation))
                 return false;
             dest = enumerator.Current;
             while (enumerator.MoveNext())
             {
                 if (!_canPaste(enumerator.Current, dest, TPasteLocation.After, operation))
                     return false;
                 dest = enumerator.Current;
             }
         }
     }
     return true;
 }
        public EngineViewmodel(Server.Engine engine)
        {
            _engine = engine;

            _engine.EngineTick += this.OnEngineTick;
            _engine.EngineOperation += this._engineOperation;
            _engine.ServerPropertyChanged += OnServerPropertyChanged;
            _engine.PropertyChanged += this.OnEnginePropertyChanged;
            _engine.VisibleEventsOperation += OnEnginePlayingEventsDictionaryOperation;
            _engine.LoadedNextEventsOperation += OnEngineLoadedEventsDictionaryOperation;
            _engine.RunningEventsOperation += OnEngineRunningEventsOperation;

            Debug.WriteLine(this, "Creating root EventViewmodel");
            _rootEventViewModel = new EventViewmodel(_engine, this);

            Debug.WriteLine(this, "Creating EventEditViewmodel");
            _eventEditViewmodel = new EventEditViewmodel(this);

            Debug.WriteLine(this, "Creating EventClipboard");

            Debug.WriteLine(this, "Creating PlayingEventViewmodel");
            _playingEventViewmodel = new PlayingEventViewmodel(_engine);
            _createCommands();

            Debug.WriteLine(this, "Creating EngineView");
            _engineView = new EngineView();
            _engineView.DataContext = this;

            _selectedEvents = new ObservableCollection<EventViewmodel>();
            _selectedEvents.CollectionChanged += _selectedEvents_CollectionChanged;
            EventClipboard.ClipboardChanged += EngineViewmodel_ClipboardChanged;
        }
 public EventViewmodel(Server.Engine engine, EngineViewmodel engineViewmodel)
 {
     _engine = engine;
     engine.EventSaved += _onEventSaved;
     _engineViewmodel = engineViewmodel;
     _root = this;
     _level = 0;
     _isExpanded = true;
     foreach (Event se in engine.RootEvents.ToList())
         _addRootEvent(se);
 }
 private EventViewmodel(Event aEvent, EventViewmodel parent)
 {
     if (aEvent == null) // dummy child
         return;
     _event = aEvent;
     _parent = parent;
     _root = parent._root;
     _engineViewmodel = parent._engineViewmodel;
     _level = (_parent == null) ? 0 : _parent._level + 1;
     if (aEvent.SubEvents.Count() > 0)
         _childrens.Add(DummyChild);
     _event.PropertyChanged += _onPropertyChanged;
     _event.Deleted += _eventDeleted;
     _event.SubEventChanged += _onSubeventChanged;
     _event.Relocated += _onRelocated;
     Media = aEvent.Media;
     _engine = _event.Engine;
     _createCommands();
 }
 private void _updateLocation()
 {
     if (_event != null)
     {
         Event prior = _event.Prior;
         Event parent = _event.Parent;
         Event next = _event.Next;
         Event visualParent = _event.VisualParent;
         if (prior != null)
         {
             int index = _parent._childrens.IndexOf(this);
             if (visualParent != _parent._event
                 || index <= 0
                 || _parent._childrens[index - 1]._event != prior)
             {
                 EventViewmodel priorVm = _root.Find(prior);
                 if (priorVm != null)
                 {
                     EventViewmodel newParent = priorVm._parent;
                     if (_parent == newParent)
                     {
                         int priorIndex = newParent._childrens.IndexOf(priorVm);
                         if (index >= priorIndex)
                             newParent._childrens.Move(index, priorIndex + 1);
                         else
                             newParent._childrens.Move(index, priorIndex);
                     }
                     else
                     {
                         _parent._childrens.Remove(this);
                         if (!newParent.HasDummyChild)
                             newParent._childrens.Insert(newParent._childrens.IndexOf(priorVm) + 1, this);
                         _parent = newParent;
                     }
                 }
             }
         }
         else
         if (parent == null && next != null)
         {
             int index = _parent._childrens.IndexOf(this);
             if (visualParent != _parent._event
                 || index <= 0
                 || _parent._childrens[index]._event != next)
             {
                 EventViewmodel nextVm = _root.Find(next);
                 if (nextVm != null)
                 {
                     EventViewmodel newParent = nextVm._parent;
                     if (_parent == newParent)
                     {
                         int nextIndex = newParent._childrens.IndexOf(nextVm);
                         if (index >= nextIndex)
                             newParent._childrens.Move(index, nextIndex);
                         else
                             newParent._childrens.Move(index, nextIndex -1);
                     }
                     else
                     {
                         _parent._childrens.Remove(this);
                         if (!newParent.HasDummyChild)
                             newParent._childrens.Insert(newParent._childrens.IndexOf(nextVm), this);
                         _parent = newParent;
                     }
                 }
             }
         }
         else
         if (parent == null)
         {
             _parent._childrens.Remove(this);
             _root._childrens.Add(this);
             _parent = _root;
         }
         else
         {
             EventViewmodel parentVm = _root.Find(parent);
             if (parentVm != null)
             {
                 if (parentVm == _parent)
                 {
                     if (prior == null)
                         parentVm.Childrens.Move(parentVm.Childrens.IndexOf(this), 0);
                 }
                 else
                     Parent = parentVm;
             }
         }
         this._bringIntoView();
     }
 }
 // when new event was created
 private void _onEventSaved(object o, EventArgs e)
 {
     Application.Current.Dispatcher.BeginInvoke((Action<bool>)delegate(bool onUIThread)
             {
                 var evm = _root.Find(o as Event);
                 EventViewmodel newVm = null;
                 if (evm == null)
                 {
                     var vp = (o as Event).VisualParent;
                     if (vp != null)
                     {
                         var evm_vp = _root.Find(vp);
                         if (evm_vp != null)
                         {
                             if ((o as Event).EventType == TEventType.Movie || (o as Event).EventType == TEventType.Rundown || (o as Event).EventType == TEventType.Live
                                 || evm_vp.IsExpanded)
                             {
                                 evm_vp.IsExpanded = true;
                                 if (evm_vp.Find(o as Event) == null) // find again after expand
                                 {
                                     if ((o as Event).Parent == vp) // StartType = With
                                     {
                                         newVm = new EventViewmodel(o as Event, evm_vp);
                                         evm_vp._childrens.Insert(0, newVm);
                                     }
                                     else // StartType == After
                                     {
                                         var prior = (o as Event).Prior;
                                         if (prior != null)
                                         {
                                             var evm_prior = evm_vp.Find(prior);
                                             if (evm_prior != null)
                                             {
                                                 var pos = evm_vp._childrens.IndexOf(evm_prior);
                                                 newVm = new EventViewmodel(o as Event, evm_vp);
                                                 evm_vp._childrens.Insert(pos + 1, newVm);
                                             }
                                         }
                                     }
                                 }
                             }
                             else
                             {
                                 if (!evm_vp.HasDummyChild)
                                     evm_vp.Childrens.Add(DummyChild);
                             }
                         }
                     }
                     else //vp == null
                     {
                         var prior = (o as Event).Prior;
                         if (prior != null)
                         {
                             var evm_prior = _root.Find(prior);
                             if (evm_prior != null)
                             {
                                 var pos = _root._childrens.IndexOf(evm_prior);
                                 newVm = new EventViewmodel(o as Event, _root);
                                 _root._childrens.Insert(pos + 1, newVm);
                             }
                         }
                         else
                             if ((o as Event).Parent == null)
                                 _addRootEvent(o as Event);
                     }
                 }
                 else //evm != null
                 {
                     evm.NotifyPropertyChanged("IsInvalidInSchedule");
                 }
                 if (onUIThread
                     && newVm != null
                     && !((o as Event).EventType == TEventType.AnimationFlash || (o as Event).EventType == TEventType.StillImage))
                 {
                     newVm.IsSelected = true;
                 }
             }, System.Windows.Threading.Dispatcher.FromThread(System.Threading.Thread.CurrentThread) != null); // current thread is uiThread
 }
 private void _addRootEvent(Event e)
 {
     if (!e.IsDeleted)
     {
         EngineViewmodel evm = _engineViewmodel;
         var newEvm = new EventViewmodel(e, _root);
         _childrens.Add(newEvm);
         Event ne = e.Next;
         while (ne != null)
         {
             _childrens.Add(new EventViewmodel(ne, _root));
             Debug.WriteLine(ne, "Reading next for");
             ne = ne.Next;
         }
     }
 }
 public static void Paste(EventViewmodel destination, TPasteLocation location)
 {
     Event dest = destination.Event;
     lock(_clipboard.SyncRoot)
     {
         if (CanPaste(destination, location))
         {
             var operation = Operation;
             using (var enumerator = _clipboard.GetEnumerator())
             {
                 if (!enumerator.MoveNext())
                     return;
                 dest = _paste(enumerator.Current, dest, location, operation);
                 while (enumerator.MoveNext())
                     dest = _paste(enumerator.Current, dest, TPasteLocation.After, operation);
             }
         }
         if (Operation == ClipboardOperation.Cut)
             _clipboard.Clear();
     }
 }