Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AnonymousCommand.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 
5 using SiliconStudio.Presentation.Services;
6 using SiliconStudio.Presentation.ViewModel;
7 
8 namespace SiliconStudio.Presentation.Commands
9 {
10  /// <summary>
11  /// An implementation of <see cref="CommandBase"/> that route <see cref="Execute"/> calls to a given anonymous method.
12  /// </summary>
13  /// <seealso cref="AnonymousCommand{T}"/>
15  {
16  private readonly Func<bool> canExecute;
17  private readonly Action<object> action;
18 
19  /// <summary>
20  /// Initializes a new instance of the <see cref="AnonymousCommand"/> class.
21  /// </summary>
22  /// <param name="serviceProvider">A service provider that can provide a <see cref="IDispatcherService"/> to use for this view model.</param>
23  /// <param name="action">An anonymous method that will be called each time the command is executed.</param>
24  public AnonymousCommand(IViewModelServiceProvider serviceProvider, Action action)
25  : base(serviceProvider)
26  {
27  if (action == null)
28  throw new ArgumentNullException("action");
29 
30  this.action = x => action();
31  }
32 
33  /// <summary>
34  /// Initializes a new instance of the <see cref="AnonymousCommand"/> class.
35  /// </summary>
36  /// <param name="serviceProvider">A service provider that can provide a <see cref="IDispatcherService"/> to use for this view model.</param>
37  /// <param name="action">An anonymous method that will be called each time the command is executed.</param>
38  /// <param name="canExecute">An anonymous method that will be called each time the command <see cref="CommandBase.CanExecute(object)"/> method is invoked.</param>
39  public AnonymousCommand(IViewModelServiceProvider serviceProvider, Action action, Func<bool> canExecute)
40  : base(serviceProvider)
41  {
42  this.canExecute = canExecute;
43  if (action == null)
44  throw new ArgumentNullException("action");
45 
46  this.action = x => action();
47  }
48 
49  /// <summary>
50  /// Initializes a new instance of the <see cref="AnonymousCommand"/> class.
51  /// </summary>
52  /// <param name="serviceProvider">A service provider that can provide a <see cref="IDispatcherService"/> to use for this view model.</param>
53  /// <param name="action">An anonymous method that will be called each time the command is executed.</param>
54  public AnonymousCommand(IViewModelServiceProvider serviceProvider, Action<object> action)
55  : base(serviceProvider)
56  {
57  if (action == null)
58  throw new ArgumentNullException("action");
59 
60  this.action = action;
61  }
62 
63  /// <summary>
64  /// Initializes a new instance of the <see cref="AnonymousCommand"/> class.
65  /// </summary>
66  /// <param name="serviceProvider">A service provider that can provide a <see cref="IDispatcherService"/> to use for this view model.</param>
67  /// <param name="action">An anonymous method that will be called each time the command is executed.</param>
68  /// <param name="canExecute">An anonymous method that will be called each time the command <see cref="CommandBase.CanExecute(object)"/> method is invoked.</param>
69  public AnonymousCommand(IViewModelServiceProvider serviceProvider, Action<object> action, Func<bool> canExecute)
70  : base(serviceProvider)
71  {
72  if (action == null)
73  throw new ArgumentNullException("action");
74 
75  this.action = action;
76  this.canExecute = canExecute;
77  }
78 
79  /// <summary>
80  /// Executes the command, and thus the anonymous method provided to the constructor.
81  /// </summary>
82  /// <param name="parameter">The command parameter.</param>
83  /// <seealso cref="AnonymousCommand{T}"/>
84  public override void Execute(object parameter)
85  {
86  action(parameter);
87  }
88 
89  /// <summary>
90  /// Indicates whether the command can be executed. Returns <c>true</c> if <see cref="CommandBase.IsEnabled"/> is <c>true</c> and either a <see cref="Func{Bool}"/>
91  /// was provided to the constructor, and this function returns <c>true</c>, or no <see cref="Func{Bool}"/> was provided (or a <c>null</c> function).
92  /// </summary>
93  /// <param name="parameter">The command parameter.</param>
94  /// <returns><c>true</c> if the command can be executed, <c>false</c> otherwise.</returns>
95  public override bool CanExecute(object parameter)
96  {
97  var result = base.CanExecute(parameter);
98  return result && canExecute != null ? canExecute() : result;
99  }
100  }
101 
102  /// <summary>
103  /// An implementation of <see cref="CommandBase"/> that route <see cref="Execute"/> calls to a given anonymous method with a typed parameter.
104  /// </summary>
105  /// <typeparam name="T">The type of parameter to use with the command.</typeparam>
106  /// <seealso cref="AnonymousCommand"/>
107  public class AnonymousCommand<T> : CommandBase
108  {
109  private readonly Action<T> action;
110  private readonly Func<bool> canExecute;
111 
112  /// <summary>
113  /// Initializes a new instance of the <see cref="AnonymousCommand{T}"/> class.
114  /// </summary>
115  /// <param name="serviceProvider">A service provider that can provide a <see cref="IDispatcherService"/> to use for this view model.</param>
116  /// <param name="action">An anonymous method with a typed parameter that will be called each time the command is executed.</param>
117  public AnonymousCommand(IViewModelServiceProvider serviceProvider, Action<T> action)
118  : base(serviceProvider)
119  {
120  this.action = action;
121  }
122 
123  /// <summary>
124  /// Initializes a new instance of the <see cref="AnonymousCommand{T}"/> class.
125  /// </summary>
126  /// <param name="serviceProvider">A service provider that can provide a <see cref="IDispatcherService"/> to use for this view model.</param>
127  /// <param name="action">An anonymous method with a typed parameter that will be called each time the command is executed.</param>
128  /// <param name="canExecute">An anonymous method that will be called each time the command <see cref="CommandBase.CanExecute(object)"/> method is invoked.</param>
129  public AnonymousCommand(IViewModelServiceProvider serviceProvider, Action<T> action, Func<bool> canExecute)
130  : base(serviceProvider)
131  {
132  this.action = action;
133  this.canExecute = canExecute;
134  }
135 
136  /// <summary>
137  /// Executes the command, and thus the anonymous method provided to the constructor.
138  /// </summary>
139  /// <seealso cref="AnonymousCommand"/>
140  public override void Execute(object parameter)
141  {
142  if ((typeof(T).IsValueType || parameter != null) && !(parameter is T))
143  throw new ArgumentException(@"Unexpected parameter type in the command.", "parameter");
144 
145  action((T)parameter);
146  }
147 
148  /// <summary>
149  /// Indicates whether the command can be executed. Returns <c>true</c> if <see cref="CommandBase.IsEnabled"/> is <c>true</c> and either a <see cref="Func{Bool}"/>
150  /// was provided to the constructor, and this function returns <c>true</c>, or no <see cref="Func{Bool}"/> was provided (or a <c>null</c> function).
151  /// </summary>
152  /// <param name="parameter">The command parameter.</param>
153  /// <returns><c>true</c> if the command can be executed, <c>false</c> otherwise.</returns>
154  public override bool CanExecute(object parameter)
155  {
156  var result = base.CanExecute(parameter);
157  return result && canExecute != null ? canExecute() : result;
158  }
159  }
160 }
AnonymousCommand(IViewModelServiceProvider serviceProvider, Action< object > action, Func< bool > canExecute)
Initializes a new instance of the AnonymousCommand class.
An implementation of CommandBase that route Execute calls to a given anonymous method.
override void Execute(object parameter)
Executes the command, and thus the anonymous method provided to the constructor.
AnonymousCommand(IViewModelServiceProvider serviceProvider, Action< T > action)
Initializes a new instance of the AnonymousCommand{T} class.
override bool CanExecute(object parameter)
Indicates whether the command can be executed. Returns true if CommandBase.IsEnabled is true and eith...
AnonymousCommand(IViewModelServiceProvider serviceProvider, Action< object > action)
Initializes a new instance of the AnonymousCommand class.
override bool CanExecute(object parameter)
Indicates whether the command can be executed. Returns true if CommandBase.IsEnabled is true and eith...
AnonymousCommand(IViewModelServiceProvider serviceProvider, Action action)
Initializes a new instance of the AnonymousCommand class.
AnonymousCommand(IViewModelServiceProvider serviceProvider, Action< T > action, Func< bool > canExecute)
Initializes a new instance of the AnonymousCommand{T} class.
override void Execute(object parameter)
Executes the command, and thus the anonymous method provided to the constructor.
An abstract class that is the base implementation of the ICommandBase interface.
Definition: CommandBase.cs:13
AnonymousCommand(IViewModelServiceProvider serviceProvider, Action action, Func< bool > canExecute)
Initializes a new instance of the AnonymousCommand class.