Example #1
0
        /// <summary>
        /// This command merges the last created merge script onto the active editor.  It better match the formats since we are
        /// not checking.
        /// </summary>
        public static void OnMergeCapturedMergeScript(ICommandParameter parameter, ICompositionHost host, DocumentEditSite site)
        {
            if (string.IsNullOrEmpty(_lastMergeScript))
            {
                return;
            }
            // The root element is the root of what ever the active editor is editing.  This would be things like the BlockDiagram
            // or FrontPanel of a VI.
            var rootElement = site.ActiveDocumentEditor?.EditorInfo?.RootElement;

            if (rootElement != null)
            {
                // Always perform modifications from within a transaction
                // Here we are creating a user transaction which means it is undoable
                using (var transaction = rootElement.TransactionManager.BeginTransaction("Merge", TransactionPurpose.User))
                {
                    var mergeScript = MergeScript.FromString(_lastMergeScript, host);
                    var resolver    = new MergeScriptResolver(mergeScript, host);
                    resolver.Merge(rootElement);
                    // Don't forget to commit the transaction
                    transaction.Commit();
                }
            }
        }
Example #2
0
 private static void HandleExecuteBorrowMutableCommand(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
 {
     selection.GetBorrowTunnels <BorrowTunnel>().SetBorrowTunnelsMode(BorrowMode.Mutable);
 }
Example #3
0
        private static void HandleAddUnwrapOptionTunnel(object parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
        {
            var flatSequenceStructureViewModels = selection.OfType <StructureViewModel>().WhereNotNull();

            if (flatSequenceStructureViewModels.Any())
            {
                using (var transaction = flatSequenceStructureViewModels.First().TransactionManager.BeginTransaction("Add Unwrap Option Tunnels", TransactionPurpose.User))
                {
                    foreach (var structureViewModel in flatSequenceStructureViewModels)
                    {
                        Structure model = (Structure)structureViewModel.Model;
                        SMRect    leftRect, rightRect;
                        BorderNodeViewModelHelpers.FindBorderNodePositions(structureViewModel, out leftRect, out rightRect);
                        UnwrapOptionTunnel unwrapOptionTunnel = model.MakeTunnel <UnwrapOptionTunnel>(model.AncestorDiagram, model.NestedDiagrams.First());
                        unwrapOptionTunnel.Top  = leftRect.Y;
                        unwrapOptionTunnel.Left = leftRect.X;
                    }
                    transaction.Commit();
                }
            }
        }
Example #4
0
        private static void HandleAddLockTunnel(object parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
        {
            var structureViewModels = selection.OfType <StructureViewModel>().WhereNotNull();

            if (structureViewModels.Any())
            {
                using (var transaction = structureViewModels.First().TransactionManager.BeginTransaction("Add Lock Tunnels", TransactionPurpose.User))
                {
                    foreach (var structureViewModel in structureViewModels)
                    {
                        SMRect leftRect, rightRect;
                        BorderNodeViewModelHelpers.FindBorderNodePositions(structureViewModel, out leftRect, out rightRect);
                        Structure model = (Structure)structureViewModel.Model;

                        LockTunnel lockTunnel = model.MakeTunnel <LockTunnel>(model.PrimaryNestedDiagram, model.NestedDiagrams.First());
                        FlatSequenceTerminateLifetimeTunnel flatSequenceTerminateLifetimeTunnel = model.MakeTunnel <FlatSequenceTerminateLifetimeTunnel>(model.AncestorDiagram, model.NestedDiagrams.First());
                        lockTunnel.TerminateLifetimeTunnel = flatSequenceTerminateLifetimeTunnel;
                        flatSequenceTerminateLifetimeTunnel.BeginLifetimeTunnel = lockTunnel;
                        // Set both as rules were not consistently picking right one to adjust to other.
                        lockTunnel.Top  = leftRect.Y;
                        lockTunnel.Left = leftRect.X;
                        flatSequenceTerminateLifetimeTunnel.Top  = lockTunnel.Top;
                        flatSequenceTerminateLifetimeTunnel.Left = rightRect.X;
                    }
                    transaction.Commit();
                }
            }
        }
Example #5
0
        public Refactorings([NotNull] ICompositionHost compositionHost)
        {
            Contract.Requires(compositionHost != null);

            _compositionHost = compositionHost;
        }
Example #6
0
 public static void TraceError([NotNull] this ICompositionHost exportProvider, [Localizable(false)][NotNull] string format, [NotNull][ItemNotNull] params object[] args)
 {
     exportProvider.GetExportedValue <ITracer>().TraceError(string.Format(CultureInfo.CurrentCulture, format, args));
 }
 /// <summary>
 /// This is the command enabler for the multiple by 10 command.  Command enablers are called when the state of the editor changes
 /// The enable can update the state of the command based on the current state of the editor (what is selected, is something running, ...)
 /// </summary>
 public static bool CanMultiplyBy10(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
 {
     // This command is always enabled
     return(true);
 }
Example #8
0
        /// <summary>
        /// Command handler which adds a new member VI to the gtype that is currently being edited
        /// </summary>
        public static void OnAddNewMemberVI(ICommandParameter parameter, ICompositionHost host, DocumentEditSite site)
        {
            // Check to see that the user is currently editing a type and if not do nothing.
            var gTypeEnvoy = site?.ActiveDocument?.Envoy;
            var gType      = (GTypeDefinition)gTypeEnvoy?.ReferenceDefinition;

            if (gType == null)
            {
                return;
            }

            var               project     = host.GetSharedExportedValue <IDocumentManager>().ActiveProject;
            var               createInfo  = EnvoyCreateInfo.CreateForNew(VirtualInstrument.VIModelDefinitionType, "New Member VI.gvi");
            Envoy             createdItem = null;
            ILockedSourceFile createdFile = null;

            // Always perform modifications from within a transaction
            // Here we are creating a user transaction which means it is undoable
            using (var transaction = gType.TransactionManager.BeginTransaction("Add A VI", TransactionPurpose.User))
            {
                createdFile = project.CreateNewFile(gType.Scope, createInfo);
                createdFile.Envoy.UpdateStoragePath("Members\\New Member VI.gvi");
                createdItem = createdFile.Envoy;
                transaction.Commit();
            }

            // Lets add a terminal to the member data
            // First we query for the merge script provider from our gtype.  Merge scripts are snippets of code that can be
            // merged into diagrams / panels / ...
            // The gtype will provide a merge script that can be used to add data item (control / terminal) to a VI and
            // many other things
            string mergeScriptText = string.Empty;
            var    dataProviders   = gTypeEnvoy.QueryService <IProvideMergeScriptData>();

            foreach (var dataProvider in dataProviders)
            {
                foreach (var script in dataProvider.MergeScriptData)
                {
                    if (script.ClipboardDataFormat == VIDiagramControl.ClipboardDataFormat)
                    {
                        // Found the merge script for a VI diagram, we are done
                        mergeScriptText = script.MergeText;
                        break;
                    }
                }
            }
            // Now merge the script onto the diagram of the VI we just created
            if (!string.IsNullOrEmpty(mergeScriptText))
            {
                var vi = (VirtualInstrument)createdItem.ReferenceDefinition;
                // Always perform modifications from within a transaction
                // We are making this transaction a non user so that it cannot be undone it is just the initial state of the VI
                using (var transaction = vi.TransactionManager.BeginTransaction("Add A VI", TransactionPurpose.NonUser))
                {
                    var mergeScript = MergeScript.FromString(mergeScriptText, host);
                    var resolver    = new MergeScriptResolver(mergeScript, host);
                    resolver.Merge(vi.BlockDiagram);
                    // Don't forget to commit the transaction or it will cancel.
                    transaction.Commit();
                }
            }
            // Now edit the memeber VI that was just created, and this time let's edit starting on the diagram
            EditDocumentInfo editInfo = new EditDocumentInfo();

            editInfo.EditorType = typeof(VIDiagramControl);
            createdItem?.Edit(editInfo);

            // After editing dispose our lock in the file
            createdFile?.Dispose();
        }
        public static T GetExportedValueOrDefault <T>([NotNull] this ICompositionHost compositionHost) where T : class
        {
            Contract.Requires(compositionHost != null);

            return(compositionHost.Container.GetExportedValueOrDefault <T>());
        }
Example #10
0
 private static void HandleExecuteVariantCommand(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
 {
     SetTypeDiagramSelfMode(selection, SelfTypeMode.Variant);
 }
Example #11
0
 private static bool HandleCanExecuteVariantCommand(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
 {
     return(UpdateParameterFromModeValue(parameter, selection, SelfTypeMode.Variant));
 }
        /// <summary>
        /// Here we handle the user changing the given command.
        /// Our job is to take the current value in the parameter and update our model
        /// </summary>
        /// <param name="parameter">command parameter - this is where the current 'value' should be held</param>
        /// <param name="selection">user selection, for commands based on selection</param>
        /// <param name="host">composition host</param>
        /// <param name="site">for communicating with user interface APIs</param>
        private static void HandleExecuteCommand(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
        {
            var viewModel        = selection.OfType <ElementViewModel>().First();
            var booleanParameter = parameter as ICheckableCommandParameter;
            var choiceParameter  = parameter as IChoiceCommandParameter;
            var numericParameter = parameter as IValueCommandParameter;
            var textParameter    = parameter as ITextCommandParameter;
            var colorParameter   = parameter as ColorCommandParameter;

            // Update the model based on current state. In general, changes to the model should
            // be transacted so undo redo works.
            using (var transaction = viewModel.Element.TransactionManager.BeginTransaction("update", NationalInstruments.SourceModel.TransactionPurpose.User))
            {
                // update state of the model based on type of parameter.
                // viewModel.Element.newState = parameter.newValue;
                transaction.Commit();
            }
        }
        /// <summary>
        /// A generic CanExecute handler.
        /// The role of this handler is to take the current state in the model and transfer it to the parameter,
        /// which is used by the view to display the current value.
        /// In general, you would likely write a different one for each command that did the appropriate
        /// cast of the parameter to get/set the value.
        /// </summary>
        /// <param name="parameter">command parameter - this is where the current 'value' should be held</param>
        /// <param name="selection">user selection, for commands based on selection</param>
        /// <param name="host">composition host</param>
        /// <param name="site">for communicating with user interface APIs</param>
        /// <returns>true if it can handle</returns>
        public static bool HandleCanExecuteCommand(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
        {
            // probably cast/OfType to your derived view model
            var viewModel        = selection.OfType <ElementViewModel>().First();
            var booleanParameter = parameter as ICheckableCommandParameter;
            var choiceParameter  = parameter as IChoiceCommandParameter;
            var numericParameter = parameter as IValueCommandParameter;
            var textParameter    = parameter as ITextCommandParameter;
            var colorParameter   = parameter as ColorCommandParameter;

            if (booleanParameter != null)
            {
                booleanParameter.IsChecked = true;
            }
            else if (choiceParameter != null)
            {
                if (choiceParameter.Choices == null)
                {
                    choiceParameter.Choices = new object[] { "one", "two", "three" }.Select(item => new ChoiceCommandParameterChoice(item, null, item.ToString())).ToArray();
                    choiceParameter.Choices.ElementAt(1).IsEnabled = false;
                }
            }
            else if (textParameter != null)
            {
                // This is for textbox and Path controls
                textParameter.Text = viewModel.Name;
            }
            else if (colorParameter != null)
            {
                // color and others are also value parameters, so we check them first.
                colorParameter.Value = SMBrush.FromBrush(Brushes.Red);
            }
            else if (numericParameter != null)
            {
                numericParameter.Value = 3.14;
            }

            return(true); // or false to disable the command
        }
Example #14
0
 public RuntimeExecutionTarget(ICompositionHost host)
 {
     Host = host;
 }
Example #15
0
        public MapView(ICompositionHost compositionHost)
        {
            this.SetExportProvider(compositionHost.Container);

            InitializeComponent();
        }
        private static void SelectChannels(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
        {
            IEnumerable <string> frequencyChannels = selection.Select(item => item.Model)
                                                     .OfType <PulseWidthModulationControlModel>()
                                                     .Select(model => model.FrequencyChannel)
                                                     .Where(channel => !string.IsNullOrEmpty(channel))
                                                     .ToList();
            IEnumerable <string> dutyCycleChannels = selection.Select(item => item.Model)
                                                     .OfType <PulseWidthModulationControlModel>()
                                                     .Select(model => model.DutyCycleChannel)
                                                     .Where(channel => !string.IsNullOrEmpty(channel))
                                                     .ToList();
            var systemDefinitionPalette = SystemDefinitionPaletteControl.Activate(site);

            systemDefinitionPalette.SelectNodes(dutyCycleChannels.Concat(frequencyChannels));
        }
        /// <summary>
        /// Creates a part from the specified value and composes it in the specified composition container. See <see cref="AttributedModelServices.ComposeExportedValue{T}(CompositionContainer, T)"/>.
        /// </summary>
        /// <typeparam name="T">The type of the new part.</typeparam>
        /// <param name="compositionHost">The composition host.</param>
        /// <param name="exportedValue">The value to compose.</param>
        public static void ComposeExportedValue <T>([NotNull] this ICompositionHost compositionHost, [CanBeNull] T exportedValue)
        {
            Contract.Requires(compositionHost != null);

            compositionHost.Container.ComposeExportedValue(exportedValue);
        }
Example #18
0
        /// <summary>
        /// This command create a merge script string from the selection of the active editor.  Merge scripts are what are used
        /// for the clipboard, palette entries, and drag drop.  Merge scripts are basically mini versions of our persisted source
        /// file format.  Merge scripts are also useful for scripting use cases where merge scripts can be pased onto a diagram,
        /// wired together and modified.  Using merge scripts as templates is easier than writing a bunch on scripting code by hand.
        /// </summary>
        public static void OnCreateMergeScriptFromSelection(ICommandParameter parameter, ICompositionHost host, DocumentEditSite site)
        {
            var selection = site.ActiveSelection.OfType <IElementViewModel>();

            if (!selection.Any())
            {
                return;
            }
            // Create an Element selection is which a selection model for the selected view models
            var elementSelection = SelectionToolViewModel.CreateElementSelection(selection, true);

            // Get the text from the merge script
            _lastMergeScript = elementSelection.CopyMergeScript(host, true);
            Clipboard.SetText(_lastMergeScript);
        }
Example #19
0
        private static void HandleAddIterateTunnel(object parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
        {
            var structureViewModels = selection.OfType <LoopViewModel>().WhereNotNull();

            if (structureViewModels.Any())
            {
                using (var transaction = structureViewModels.First().TransactionManager.BeginTransaction("Add Iterate Tunnels", TransactionPurpose.User))
                {
                    foreach (var structureViewModel in structureViewModels)
                    {
                        SMRect leftRect, rightRect;
                        BorderNodeViewModelHelpers.FindBorderNodePositions(structureViewModel, out leftRect, out rightRect);
                        var model = (Structure)structureViewModel.Model;

                        LoopIterateTunnel           iterateTunnel = model.MakeBorderNode <LoopIterateTunnel>();
                        LoopTerminateLifetimeTunnel loopTerminateLifetimeTunnel = model.MakeBorderNode <LoopTerminateLifetimeTunnel>();
                        iterateTunnel.TerminateLifetimeTunnel           = loopTerminateLifetimeTunnel;
                        loopTerminateLifetimeTunnel.BeginLifetimeTunnel = iterateTunnel;
                        // Set both as rules were not consistently picking right one to adjust to other.
                        iterateTunnel.Top  = leftRect.Y;
                        iterateTunnel.Left = leftRect.X;
                        loopTerminateLifetimeTunnel.Top  = iterateTunnel.Top;
                        loopTerminateLifetimeTunnel.Left = rightRect.X;
                    }
                    transaction.Commit();
                }
            }
        }
        /// <summary>
        /// This is the command handler that will add the code to multiple the output of the random number primitive by 10
        /// This is called when the user presses the Multiply By 10 button
        /// </summary>
        /// <param name="parameter">The command parameter associated with instance of the command</param>
        /// <param name="selection">The current selection</param>
        /// <param name="host">The Composition host for the session</param>
        /// <param name="site">The document edit site which is managing the edit session</param>
        public static void OnMultipleBy10(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
        {
            // The selected item will be the random number node since we only add this command for random numbers
            var node = selection.First().Model as Node;

            if (node == null)
            {
                return;
            }

            // Start a transaction and add content to multiply the output by 10
            using (var transaction = node.TransactionManager.BeginTransaction("Multiple By 10", TransactionPurpose.User))
            {
                // Create a multiple node, position it nicely to the right ot the random number, and add it to the same diagram
                var multiply = Multiply.Create(ElementCreateInfo.ForNew);
                multiply.TopLeft = new SMPoint(node.Bounds.Right + StockDiagramGeometries.StandardNodeWidth, node.Top + (2 * StockDiagramGeometries.GridSize));
                node.Diagram.AddNode(multiply);

                // Wire the random number output to the first input on the multiple node
                node.Diagram.WireWithin((NodeTerminal)node.OutputTerminals.First(), (NodeTerminal)multiply.InputTerminals.First());

                // Create a Double Numeric Constant with an initial value of 10.0
                var literalBuilder = LiteralBuilder.GetLiteralBuilder(host);
                var context        = new CreateLiteralContext(PFTypes.Double, 10.0);
                var literal        = literalBuilder.CreateLiteral(context);

                // Position the constant nicely and add it to the diagram
                literal.TopLeft = new SMPoint(node.Left + StockDiagramGeometries.TinyNodeWidth, node.Bounds.Bottom + (2 * StockDiagramGeometries.GridSize));
                node.Diagram.AddNode(literal);

                // Wire the constant to the multiply node
                node.Diagram.WireWithin(literal.OutputTerminal, (NodeTerminal)multiply.InputTerminals.ElementAt(1));

                // Commit the transaction to finish the operation
                transaction.Commit();
            }
        }
Example #21
0
        public static bool UpdateIsActive(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
        {
            var checkableParameter = parameter as ICheckableCommandParameter;

            if (checkableParameter != null)
            {
                checkableParameter.IsChecked = CommandHelpers.GetCheckedState(selection.OfType <InteractiveNodeViewModel>(), vm => vm.IsActive);
            }
            return(true);
        }
Example #22
0
 public static void TraceError([NotNull] this ICompositionHost exportProvider, [Localizable(false)][NotNull] string message)
 {
     exportProvider.GetExportedValue <ITracer>().TraceError(message);
 }
Example #23
0
        /// <summary>
        /// This command handler is used in the right rail to change the state of the IsActive property.
        /// </summary>
        public static void OnIsActive(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
        {
            // Get the selected interactive node view models
            var selected           = selection.OfType <InteractiveNodeViewModel>();
            var checkableParameter = (ICheckableCommandParameter)parameter;

            if (selected.Any())
            {
                // Create a transaction around the setting on all view models.  This is make it a single undoable action
                using (var transaction = selected.First().TransactionManager.BeginTransaction("Set IsActive", TransactionPurpose.User))
                {
                    // Set the state of the IsActive property based on the state of the checkbox
                    foreach (var selectedItem in selected)
                    {
                        selectedItem.IsActive = (bool)checkableParameter.IsChecked;
                    }
                    // don't forget to commit the transaction
                    transaction.Commit();
                }
            }
        }
Example #24
0
 private static bool HandleCanAddLockTunnel(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
 {
     return(selection.OfType <StructureViewModel>().Any());
 }
 private static void LaunchFrequencyChannelBrowser(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
 {
     _settingFrequency = true;
     LaunchChannelBrowser(parameter, selection, host, site);
 }
Example #26
0
 private static bool HandleCanAddUnwrapOptionTunnel(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
 {
     return(selection.OfType <FlatSequenceEditor>().Any());
 }
 /// <summary>
 /// This can execute method is run periodically by the command to determine whether it should be enabled or disabled.
 /// </summary>
 private static bool CanLaunchChannelBrowser(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
 {
     return(selection.All(s => s.Model is PulseWidthModulationControlModel));
 }
Example #28
0
 private static bool HandleCanExecuteBorrowMutableCommand(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
 {
     selection.CheckAllBorrowModesMatch <BorrowTunnel>(parameter, BorrowMode.Mutable);
     return(true);
 }
        private static void LaunchChannelBrowser(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
        {
            _uiSdfBrowsePopup = new ChannelPopup
            {
                ShowWaveforms = false,
                Name          = "UI_SDFBrowsePopup",
                IsOpen        = false,
                Placement     = Placement.BelowCenter
            };
            // Register the property changed callback for the popup window
            WeakEventManager <ChannelPopup, PropertyChangedEventArgs> .AddHandler(_uiSdfBrowsePopup, "PropertyChanged", ChannelNamePropertyChanged);

            // Show Popup window with Channels.
            _currentSelection = selection.ToList();
            _uiSdfBrowsePopup.PlacementTarget = (UIElement)parameter.AssociatedVisual;
            _uiSdfBrowsePopup.Channel         = _settingFrequency ? ((PulseWidthModulationControlModel)_currentSelection.First().Model).FrequencyChannel : ((PulseWidthModulationControlModel)_currentSelection.First().Model).DutyCycleChannel;
            _uiSdfBrowsePopup.ShowSdfBrowser(host, true, false);
        }
Example #30
0
 public static bool CanHistory(ICommandParameter parameter, ICompositionHost host, DocumentEditSite site)
 {
     return(true);
 }
 private static bool CanSelectChannel(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site)
 {
     return(host.GetSharedExportedValue <VeriStandHelper>().IsSystemDefinitionValid);
 }