static ConnectionPoint ConnectionPointHitTest(UIElement element, Point hitPoint)
        {
            FreeFormPanel          panel            = VisualTreeUtils.FindVisualAncestor <FreeFormPanel>(element);
            List <ConnectionPoint> connectionPoints = StateContainerEditor.GetConnectionPoints(element);

            return(FreeFormPanel.ConnectionPointHitTest(hitPoint, connectionPoints, panel));
        }
        protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            if (e != null && !this.Disabled)
            {
                this.offset = e.GetPosition(this);
                this.CaptureMouse();
                if (this.scope != null)
                {
                    this.ParentStateContainerEditor.StoreShapeSizeWithUndoRecursively(this.ParentStateContainerEditor.ModelItem);
                }
                // Select the designer when it is being resized
                WorkflowViewElement designer = this.ParentStateContainerEditor.ModelItem.View as WorkflowViewElement;

                if (!designer.IsKeyboardFocusWithin)
                {
                    // Fix 185562 - if the designer has the keyboard focus (i.e. DisplayName being edited)
                    // then there is no need to refocus on the designer again.  That prevents the
                    // DisplayName editing to be group into the same EditingScope as resizing, and
                    // also the defaultDisplayNameReadOnlyControl from being Visible but not modified.
                    Keyboard.Focus(designer);
                }

                StateMachineDesigner stateMachineDesigner = VisualTreeUtils.FindVisualAncestor <StateMachineDesigner>(this.ParentStateContainerEditor);
                stateMachineDesigner.IsResizing = true;

                e.Handled = true;
            }
            base.OnPreviewMouseLeftButtonDown(e);
        }
示例#3
0
        private void OnListBoxPreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            ListBoxItem item = VisualTreeUtils.FindVisualAncestor <ListBoxItem>(e.OriginalSource as DependencyObject);

            if (item != null)
            {
                this.SelectedListItem = item.Content as ExpandableItemWrapper;
            }
        }
示例#4
0
        void OnModelItemPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Expression")
            {
                Update();
            }
            else if (e.PropertyName == "DefaultCaseDisplayName")
            {
                // To fix 218600 without losing PropertyGrid focus (Bug 210326), the only workaround is to
                // update the connector label manually, because FlowSwitchLink.ModelItem["DefaultCaseDisplayName"]
                // is a FakeModelPropertyImpl, and would not generate a Undo unit
                // (FakeModelNotifyPropertyChange.GetInverse() returns null).
                // However, there is a known issue with PropertyGrid bound to a fake ModelItem.  The workaround is
                // to shift the focus to the FlowchartDesigner IF the keyboard focus is on the connector when the user
                // calls Undo/Redo, to avoid the problem of PropertyGrid not refreshable.
                FlowchartDesigner flowchartDesigner = VisualTreeUtils.FindVisualAncestor <FlowchartDesigner>(this);
                Fx.Assert(null != flowchartDesigner, "flowchart designer cannot be null because FlowswitchDesigner must exist within the same visual tree ofthe parent Flowchart.");

                if (null != flowchartDesigner &&
                    null != this.ModelItem.Properties["Default"].Value &&
                    this.Context.Services.GetService <UndoEngine>().IsUndoRedoInProgress)
                {
                    // the designer is available
                    Connector connector = flowchartDesigner.GetLinkOnCanvas(this.ModelItem, this.ModelItem.Properties["Default"].Value, "Default");
                    Fx.Assert(null != connector, "Connector should not be null.");
                    ModelItem linkModelItem = FlowchartDesigner.GetLinkModelItem(connector);
                    Fx.Assert(linkModelItem is FakeModelItemImpl, "ModelItem of FlowSwitch link is fake.");
                    IFlowSwitchDefaultLink link = (IFlowSwitchDefaultLink)linkModelItem.GetCurrentValue();
                    string defaultDisplayName   =
                        (string)this.ModelItem.Properties[FlowSwitchLabelFeature.DefaultCaseDisplayNamePropertyName].Value.GetCurrentValue();

                    if (link.DefaultCaseDisplayName != defaultDisplayName)
                    {
                        // the purpose of re-setting the link value during Undo/Redo is to update the FlowSwitch label
                        using (ModelEditingScope scope = this.ModelItem.BeginEdit(SR.FlowSwitchDefaultCaseDisplayNameEditingScopeDesc))
                        {
                            linkModelItem.Properties[FlowSwitchLabelFeature.DefaultCaseDisplayNamePropertyName].SetValue(defaultDisplayName);
                            link.DefaultCaseDisplayName = defaultDisplayName;
                            scope.Complete();
                        }

                        if (Selection.IsSelection(linkModelItem))
                        {
                            // cause the connector to lose focus, because the PropertyGrid would not have focus.
                            // this scenario only happens if the user explicitly selects the FlowSwitch link after
                            // editing the DefaultDisplayName.  This behavior is only a workaround due to the fact
                            // that PropertyGrid does not receive update from change in a FakeModelPropertyImpl
                            // (i.e. FlowSwitchLink).
                            Keyboard.ClearFocus();
                            Selection.SelectOnly(this.Context, this.ModelItem);
                            linkModelItem.Highlight();
                        }
                    }
                }
            }
        }
 internal static List <ConnectionPoint> GetConnectionPoints(DependencyObject obj)
 {
     if (obj is StartSymbol)
     {
         return((List <ConnectionPoint>)obj.GetValue(StateContainerEditor.ConnectionPointsProperty));
     }
     if (!(obj is VirtualizedContainerService.VirtualizingContainer))
     {
         obj = VisualTreeUtils.FindVisualAncestor <VirtualizedContainerService.VirtualizingContainer>(obj);
     }
     return((List <ConnectionPoint>)obj.GetValue(StateContainerEditor.ConnectionPointsProperty));
 }
示例#6
0
        private void OnExpandCollapseButtonClicked(object sender, RoutedEventArgs e)
        {
            ToggleButton          button  = (ToggleButton)sender;
            ListBox               listBox = VisualTreeUtils.FindVisualAncestor <ListBox>(button);
            ExpandableItemWrapper wrapper = (ExpandableItemWrapper)listBox.SelectedItem;

            wrapper.IsExpanded = button.IsChecked.Value;
            this.ViewStateService.StoreViewState(wrapper.Item, ExpandViewStateKey, button.IsChecked.Value);

            if ((wrapper.IsExpanded && this.Designer.ShouldCollapseAll) || !wrapper.IsExpanded && this.Designer.ShouldExpandAll)
            {
                // Pin the item so that it can still be expanded / collapsed when CollapseAll / ExpandAll is enabled
                wrapper.IsPinned = true;
            }
        }
示例#7
0
            void OnDialogWindowLoaded(object s, RoutedEventArgs e)
            {
                //get the containing window
                var parentWindow = VisualTreeUtils.FindVisualAncestor <Window>(this);

                //and handle KeyDown event - in case of Esc, we should close the dialog
                parentWindow.KeyDown += OnWindowKeyDown;

                //add Copy command support - when user presses Ctrl+C, copy content of the error into clipboard
                var copyBinding = new CommandBinding(ApplicationCommands.Copy);

                copyBinding.PreviewCanExecute += OnCopyCanExecute;
                copyBinding.Executed          += OnCopyExecuted;
                parentWindow.CommandBindings.Add(copyBinding);
            }
 protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e)
 {
     if (e != null && !this.Disabled && this.scope != null)
     {
         ModelItem        stateContainerModelItem = this.ParentStateContainerEditor.ModelItem;
         ViewStateService viewStateService        = this.ParentStateContainerEditor.Context.Services.GetService <ViewStateService>();
         viewStateService.StoreViewStateWithUndo(stateContainerModelItem, StateContainerEditor.StateContainerWidthViewStateKey, this.ParentStateContainerEditor.StateContainerWidth);
         viewStateService.StoreViewStateWithUndo(stateContainerModelItem, StateContainerEditor.StateContainerHeightViewStateKey, this.ParentStateContainerEditor.StateContainerHeight);
         Mouse.OverrideCursor = null;
         Mouse.Capture(null);
         StateMachineDesigner stateMachineDesigner = VisualTreeUtils.FindVisualAncestor <StateMachineDesigner>(this.ParentStateContainerEditor);
         stateMachineDesigner.IsResizing = false;
         e.Handled = true;
     }
     base.OnPreviewMouseLeftButtonUp(e);
 }
        private void StateDesignerToolTipOpening(object sender, ToolTipEventArgs e)
        {
            StateContainerEditor stateContainerEditor = (StateContainerEditor)sender;

            if (StateContainerEditor.CopiedTransitionDestinationState != null)
            {
                WorkflowViewElement view = VisualTreeUtils.FindVisualAncestor <WorkflowViewElement>(stateContainerEditor);
                if (view != null)
                {
                    StateContainerEditor container = (StateContainerEditor)DragDropHelper.GetCompositeView(view);
                    string errorMessage;
                    if (container != null && container.CanPasteTransition(StateContainerEditor.CopiedTransitionDestinationState, out errorMessage, view.ModelItem))
                    {
                        stateContainerEditor.ToolTip = SR.EditStateToolTip + Environment.NewLine + SR.PasteTransitionToolTip;
                        return;
                    }
                }
            }

            stateContainerEditor.ToolTip = SR.EditStateToolTip;
        }
        public object OnItemsCopied(List <ModelItem> itemsToCopy)
        {
            itemsToCopy.Remove(this.initialModelItem);

            // If the item copied is Transition, save its destination state guid to find the destination state when pasting.
            if (itemsToCopy.Count == 1)
            {
                ModelItem item = itemsToCopy.First();
                if (item != null && item.ItemType == typeof(Transition))
                {
                    ModelItem destinationState = item.Properties[TransitionDesigner.ToPropertyName].Value;

                    if (!modelItemToGuid.ContainsKey(destinationState))
                    {
                        modelItemToGuid.Add(destinationState, Guid.NewGuid().ToString());
                    }

                    CopiedTransitionDestinationState = destinationState;
                    return(modelItemToGuid[destinationState]);
                }
            }

            itemsToCopy.RemoveAll(item => item.ItemType == typeof(Transition));

            // Save the locations of copied items relative to the statemachine editor to the metadata.
            // The metadata will be used to translate the location view states of pasted items to the pasting target.
            PointCollection metaData = new PointCollection();

            foreach (ModelItem modelItem in itemsToCopy)
            {
                object viewState = this.ViewStateService.RetrieveViewState(modelItem, ShapeLocationViewStateKey);
                Point  location  = (Point)viewState;
                StateContainerEditor parentDesigner = VisualTreeUtils.FindVisualAncestor <StateContainerEditor>(GetStateView(modelItem));
                location = parentDesigner.panel.GetLocationRelativeToOutmostPanel(location);
                metaData.Add(location);
            }

            CopiedTransitionDestinationState = null;
            return(metaData);
        }
        private bool IsMouseOnDragHandle(MouseButtonEventArgs e)
        {
            HitTestResult result = VisualTreeHelper.HitTest(this.Designer.scrollableContent, e.GetPosition(this.Designer.scrollableContent));

            if (result != null)
            {
                WorkflowViewElement view = VisualTreeUtils.FindVisualAncestor <WorkflowViewElement>(result.VisualHit);
                if (view != null && view.DragHandle != null)
                {
                    GeneralTransform transform = view.DragHandle.TransformToAncestor(this.Designer);
                    Fx.Assert(transform != null, "transform should not be null");
                    Point topLeft        = transform.Transform(new Point(0, 0));
                    Point bottomRight    = transform.Transform(new Point(view.DragHandle.ActualWidth, view.DragHandle.ActualHeight));
                    Rect  dragHandleRect = new Rect(topLeft, bottomRight);
                    if (dragHandleRect.Contains(e.GetPosition(this.Designer)))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
        public FreeFormPanel GetStateMachineFreeFormPanel()
        {
            StateDesigner       current = this;
            WorkflowViewElement parent  = VisualTreeUtils.FindVisualAncestor <WorkflowViewElement>(current);

            while (parent != null)
            {
                if (parent is StateDesigner)
                {
                    current = (StateDesigner)parent;
                    parent  = VisualTreeUtils.FindVisualAncestor <WorkflowViewElement>(current);
                }
                else if (parent is StateMachineDesigner)
                {
                    return(VisualTreeUtils.FindVisualAncestor <FreeFormPanel>(current));
                }
                else
                {
                    return(null);
                }
            }

            return(null);
        }
        private bool IsMouseOverAdorner(MouseButtonEventArgs e)
        {
            HitTestResult result = VisualTreeHelper.HitTest(this.Designer.scrollViewer, e.GetPosition(this.Designer.scrollViewer));

            return(result != null && VisualTreeUtils.FindVisualAncestor <Adorner>(result.VisualHit) != null);
        }
        public void OnItemsPasted(List <object> itemsToPaste, List <object> metaData, Point pastePoint, WorkflowViewElement pastePointReference)
        {
            if (this.ModelItem.ItemType == typeof(State))
            {
                WorkflowViewElement view = VisualTreeUtils.FindVisualAncestor <WorkflowViewElement>(this);
                if (view != null)
                {
                    StateContainerEditor container = (StateContainerEditor)DragDropHelper.GetCompositeView(view);
                    container.OnItemsPasted(itemsToPaste, metaData, pastePoint, pastePointReference);
                }

                return;
            }

            if (itemsToPaste.Count == 1 && itemsToPaste.First() is Transition)
            {
                if (metaData == null || metaData.Count != 1 || !(metaData.First() is string))
                {
                    ShowMessageBox(SR.PasteTransitionWithoutDestinationState);
                    return;
                }

                ModelItem destinationState = FindState(metaData.First() as string);

                if (destinationState == null)
                {
                    ShowMessageBox(SR.PasteTransitionWithoutDestinationState);
                    return;
                }

                this.PopulateVirtualizingContainer(destinationState);

                ModelItem[] selectedItems = this.Context.Items.GetValue <Selection>().SelectedObjects.ToArray();
                string      errorMessage;
                if (!CanPasteTransition(destinationState, out errorMessage, selectedItems))
                {
                    ShowMessageBox(errorMessage);
                    return;
                }

                Transition pastedTransition = itemsToPaste.First() as Transition;
                Fx.Assert(pastedTransition != null, "Copied Transition should not be null.");

                using (EditingScope es = (EditingScope)this.ModelItem.BeginEdit(System.Activities.Presentation.SR.PropertyChangeEditingScopeDescription))
                {
                    string displayName = pastedTransition.DisplayName;
                    bool   isFirst     = true;
                    foreach (ModelItem selectedItem in selectedItems)
                    {
                        if (!isFirst)
                        {
                            StringReader reader = new StringReader(XamlServices.Save(pastedTransition));
                            pastedTransition = (Transition)XamlServices.Load(reader);
                        }

                        ModelItem transitionModelItem = this.Context.Services.GetRequiredService <ModelTreeManager>().WrapAsModelItem(pastedTransition);
                        ModelItem sourceState         = selectedItem;
                        sourceState.Properties[StateDesigner.TransitionsPropertyName].Collection.Add(transitionModelItem);
                        transitionModelItem.Properties[TransitionDesigner.ToPropertyName].SetValue(destinationState);

                        if (isFirst)
                        {
                            this.ViewStateService.RemoveViewState(transitionModelItem, ConnectorLocationViewStateKey);
                            this.ViewStateService.RemoveViewState(transitionModelItem, SrcConnectionPointIndexStateKey);
                            this.ViewStateService.RemoveViewState(transitionModelItem, DestConnectionPointIndexStateKey);
                            isFirst = false;
                        }
                    }

                    es.Complete();
                }
            }
            else
            {
                List <ModelItem> modelItemsPasted = new List <ModelItem>();
                List <State>     states           = new List <State>();

                foreach (object obj in itemsToPaste)
                {
                    State state;
                    if (obj is FinalState)
                    {
                        state = new State()
                        {
                            DisplayName = DefaultFinalStateDisplayName, IsFinal = true
                        };
                    }
                    else
                    {
                        state = (State)obj;
                        if (state.DisplayName == null)
                        {
                            state.DisplayName = DefaultStateDisplayName;
                        }
                    }
                    states.Add(state);
                }

                RemoveDanglingTransitions(states);

                using (EditingScope es = (EditingScope)this.ModelItem.BeginEdit(
                           System.Activities.Presentation.SR.CollectionAddEditingScopeDescription))
                {
                    // Fix 157591 by storing the height and width of the container "before" the new states are added to the
                    // panel, and group the insertion inside one editing scope - such that Undo will also restore the
                    // size of the StateMachineContainer to pre-insert size.
                    StoreShapeSizeWithUndoRecursively(this.ModelItem);

                    foreach (State state in states)
                    {
                        ModelItem stateModelItem =
                            (this.ModelItem.ItemType == typeof(StateMachine)) ?
                            this.ModelItem.Properties[StateMachineDesigner.StatesPropertyName].Collection.Add(state) :
                            GetStateMachineModelItem(this.ModelItem).Properties[StateMachineDesigner.StatesPropertyName].Collection.Add(state);
                        modelItemsPasted.Add(stateModelItem);
                    }

                    es.Complete();
                }

                if (modelItemsPasted.Count > 0)
                {
                    // translate location view states to be in the coordinate system of the pasting target
                    Fx.Assert(this.ModelItem.ItemType == typeof(StateMachine), "Only StateMachine contain the StateContainerEditor.");

                    this.UpdateLocationViewStatesByMetaData(modelItemsPasted, metaData, this);

                    if (pastePoint.X > 0 && pastePoint.Y > 0)
                    {
                        if (pastePointReference != null)
                        {
                            pastePoint   = pastePointReference.TranslatePoint(pastePoint, this.panel);
                            pastePoint.X = pastePoint.X < 0 ? 0 : pastePoint.X;
                            pastePoint.Y = pastePoint.Y < 0 ? 0 : pastePoint.Y;
                        }
                        this.UpdateLocationViewStatesByPoint(modelItemsPasted, pastePoint);
                    }
                    // If paste point is not available, paste the items to the top left corner.
                    else
                    {
                        this.UpdateLocationViewStatesToAvoidOverlap(modelItemsPasted);
                    }
                }

                this.Dispatcher.BeginInvoke(() =>
                {
                    if (modelItemsPasted.Count > 0 && modelItemsPasted[0] != null)
                    {
                        Keyboard.Focus(modelItemsPasted[0].View as IInputElement);
                    }
                    this.Context.Items.SetValue(new Selection(modelItemsPasted));
                },
                                            DispatcherPriority.ApplicationIdle
                                            );
            }
        }
        internal static ConnectionPoint ConnectionPointHitTest(Point hitPoint, ConnectionPointsAdorner adorner)
        {
            FreeFormPanel panel = VisualTreeUtils.FindVisualAncestor <FreeFormPanel>(adorner.AdornedElement);

            return(ConnectionPointHitTest(hitPoint, adorner.ConnectionPoints, panel));
        }
        internal static object GetDroppedObjectInstance(DependencyObject dropTarget, EditingContext context, Type type, IDataObject dataObject)
        {
            if (type != null)
            {
                //check if type is generic
                if (type.IsGenericTypeDefinition)
                {
                    type = ResolveGenericParameters(dropTarget, context, type);
                }
            }

            object droppedObject = null;

            if (null != type)
            {
                try
                {
                    droppedObject = Activator.CreateInstance(type);

                    if (type.IsActivityTemplateFactory() && type.IsClass)
                    {
                        //find parent WorkflowViewElement - in case of mouse drop, current drop target most likely is ISourceContainer
                        if (!(dropTarget is WorkflowViewElement))
                        {
                            dropTarget = VisualTreeUtils.FindVisualAncestor <WorkflowViewElement>(dropTarget);
                        }

                        Type templateFactoryInterface2 = type.GetInterface(typeof(IActivityTemplateFactory <>).FullName);
                        if (templateFactoryInterface2 != null)
                        {
                            droppedObject = templateFactoryInterface2.InvokeMember("Create", BindingFlags.InvokeMethod, null, droppedObject, new object[] { dropTarget, dataObject }, CultureInfo.InvariantCulture);
                        }
                        else if (droppedObject is IActivityTemplateFactory)
                        {
                            droppedObject = ((IActivityTemplateFactory)droppedObject).Create(dropTarget);
                        }
                    }

                    // SQM: Log activity usage count
                    ActivityUsageCounter.ReportUsage(context.Services.GetService <IVSSqmService>(), type);
                }
                catch (Exception ex)
                {
                    if (Fx.IsFatal(ex))
                    {
                        throw;
                    }

                    string details = ex.Message;
                    if (ex is TargetInvocationException && ex.InnerException != null)
                    {
                        details = ex.InnerException.Message;
                    }


                    ErrorReporting.ShowErrorMessage(string.Format(CultureInfo.CurrentUICulture, SR.CannotCreateInstance, TypeNameHelper.GetDisplayName(type, false)), details);
                }
            }

            return(droppedObject);
        }