Browse Source

Move ViewModelBase.cs 8implementation of INotifyPropertyChanged)and RelayCommand.cs to ICSharpCode.SharpDevelop.Widgets.csproj

pull/23/head
PeterForstmeier 14 years ago
parent
commit
8f184b8643
  1. 10
      src/Main/Base/Project/Src/Gui/Dialogs/ReferenceDialog/AddServiceReferenceDialog.xaml
  2. 270
      src/Main/Base/Project/Src/Gui/Dialogs/ReferenceDialog/AddServiceReferenceViewModel.cs
  3. 2
      src/Main/ICSharpCode.SharpDevelop.Widgets/Project/ICSharpCode.SharpDevelop.Widgets.csproj
  4. 158
      src/Main/ICSharpCode.SharpDevelop.Widgets/Project/RelayCommand.cs
  5. 79
      src/Main/ICSharpCode.SharpDevelop.Widgets/Project/ViewModelBase.cs

10
src/Main/Base/Project/Src/Gui/Dialogs/ReferenceDialog/AddServiceReferenceDialog.xaml

@ -79,8 +79,8 @@ http://stackoverflow.com/questions/3373239/wpf-editable-combobox @@ -79,8 +79,8 @@ http://stackoverflow.com/questions/3373239/wpf-editable-combobox
VerticalAlignment="Top" >
<TreeViewItem Header="Cold Drinks">
<TreeViewItem Header="Service">
<!--
<TreeViewItem Header="Coke"></TreeViewItem>
<TreeViewItem Header="Pepsi"></TreeViewItem>
@ -92,14 +92,14 @@ http://stackoverflow.com/questions/3373239/wpf-editable-combobox @@ -92,14 +92,14 @@ http://stackoverflow.com/questions/3373239/wpf-editable-combobox
<TreeViewItem Header="Iced Tea"></TreeViewItem>
<TreeViewItem Header="Mango Shake"></TreeViewItem>
-->
</TreeViewItem>
</TreeView>
<GridSplitter Grid.Column="0" Width="2" />
<Label Background="Green" Grid.Column="1"/>
<GridSplitter Grid.Column="0" Width="2" Background="Black"/>
<Label Grid.Column="1"/>
</Grid>

270
src/Main/Base/Project/Src/Gui/Dialogs/ReferenceDialog/AddServiceReferenceViewModel.cs

@ -11,12 +11,17 @@ using System.Collections.Generic; @@ -11,12 +11,17 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Net;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using System.Web.Services.Discovery;
using System.Windows;
using System.Windows.Input;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Project;
using Microsoft.Win32;
using ICSharpCode.SharpDevelop.Widgets;
namespace Gui.Dialogs.ReferenceDialog
{
@ -27,11 +32,9 @@ namespace Gui.Dialogs.ReferenceDialog @@ -27,11 +32,9 @@ namespace Gui.Dialogs.ReferenceDialog
{
string header1 = "To see a list of available services on an specific Server, ";
string header2 = "enter a service URL and click Go. To browse for available services click Discover";
string noUrl = "Please enter he address of the Service.";
string discoverMenu ="Services in Solution";
string noUrl = "Please enter the address of the Service.";
// string discoverMenu ="Services in Solution";
Uri discoveryUri;
public AddServiceReferenceViewModel(IProject project)
{
Project = project;
@ -132,12 +135,15 @@ namespace Gui.Dialogs.ReferenceDialog @@ -132,12 +135,15 @@ namespace Gui.Dialogs.ReferenceDialog
#region Go
public ICommand GoCommand {get; private set;}
public System.Windows.Input.ICommand GoCommand {get; private set;}
private void ExecuteGo ()
{
string s = String.Format("<Go> with Service <{0}>",SelectedService);
MessageBox.Show (s);
if (String.IsNullOrEmpty(SelectedService)) {
MessageBox.Show (noUrl);
}
Uri uri = new Uri(SelectedService);
StartDiscovery(uri, new DiscoveryNetworkCredential(CredentialCache.DefaultNetworkCredentials, DiscoveryNetworkCredential.DefaultAuthenticationType));
}
private bool CanExecuteGo()
@ -149,7 +155,7 @@ namespace Gui.Dialogs.ReferenceDialog @@ -149,7 +155,7 @@ namespace Gui.Dialogs.ReferenceDialog
#region Discover
public ICommand DiscoverCommand {get;private set;}
public System.Windows.Input.ICommand DiscoverCommand {get;private set;}
private bool CanExecuteDiscover ()
{
@ -162,209 +168,131 @@ namespace Gui.Dialogs.ReferenceDialog @@ -162,209 +168,131 @@ namespace Gui.Dialogs.ReferenceDialog
}
#endregion
}
public class ViewModelBase:INotifyPropertyChanged
{
public ViewModelBase()
{
}
#region discover service Code from Matt
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, e);
}
}
CredentialCache credentialCache = new CredentialCache();
WebServiceDiscoveryClientProtocol discoveryClientProtocol;
WebReference webReference;
protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpresssion)
{
var propertyName = ExtractPropertyName(propertyExpresssion);
this.RaisePropertyChanged(propertyName);
}
delegate DiscoveryDocument DiscoverAnyAsync(string url);
delegate void DiscoveredWebServicesHandler(DiscoveryClientProtocol protocol);
delegate void AuthenticationHandler(Uri uri, string authenticationType);
protected void RaisePropertyChanged(String propertyName)
void StartDiscovery(Uri uri, DiscoveryNetworkCredential credential)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
// Abort previous discovery.
StopDiscovery();
private static String ExtractPropertyName<T>(Expression<Func<T>> propertyExpresssion)
{
if (propertyExpresssion == null)
{
throw new ArgumentNullException("propertyExpresssion");
// Start new discovery.
discoveryUri = uri;
DiscoverAnyAsync asyncDelegate = new DiscoverAnyAsync(discoveryClientProtocol.DiscoverAny);
AsyncCallback callback = new AsyncCallback(DiscoveryCompleted);
discoveryClientProtocol.Credentials = credential;
IAsyncResult result = asyncDelegate.BeginInvoke(uri.AbsoluteUri, callback, new AsyncDiscoveryState(discoveryClientProtocol, uri, credential));
}
var memberExpression = propertyExpresssion.Body as MemberExpression;
if (memberExpression == null)
/// <summary>
/// Called after an asynchronous web services search has
/// completed.
/// </summary>
void DiscoveryCompleted(IAsyncResult result)
{
throw new ArgumentException("The expression is not a member access expression.", "propertyExpresssion");
}
AsyncDiscoveryState state = (AsyncDiscoveryState)result.AsyncState;
WebServiceDiscoveryClientProtocol protocol = state.Protocol;
var property = memberExpression.Member as PropertyInfo;
if (property == null)
{
throw new ArgumentException("The member access expression does not access a property.", "propertyExpresssion");
// Check that we are still waiting for this particular callback.
bool wanted = false;
lock (this) {
wanted = Object.ReferenceEquals(discoveryClientProtocol, protocol);
}
var getMethod = property.GetGetMethod(true);
if (getMethod.IsStatic)
{
throw new ArgumentException("The referenced property is a static property.", "propertyExpresssion");
if (wanted) {
DiscoveredWebServicesHandler handler = new DiscoveredWebServicesHandler(DiscoveredWebServices);
try {
DiscoverAnyAsync asyncDelegate = (DiscoverAnyAsync)((AsyncResult)result).AsyncDelegate;
DiscoveryDocument doc = asyncDelegate.EndInvoke(result);
if (!state.Credential.IsDefaultAuthenticationType) {
AddCredential(state.Uri, state.Credential);
}
// Invoke(handler, new object[] {protocol});
} catch (Exception ex) {
if (protocol.IsAuthenticationRequired) {
HttpAuthenticationHeader authHeader = protocol.GetAuthenticationHeader();
AuthenticationHandler authHandler = new AuthenticationHandler(AuthenticateUser);
// Invoke(authHandler, new object[] {state.Uri, authHeader.AuthenticationType});
} else {
LoggingService.Error("DiscoveryCompleted", ex);
// Invoke(handler, new object[] {null});
}
return memberExpression.Member.Name;
}
}
public class RelayCommand<T> : ICommand
{
#region Declarations
readonly Predicate<T> _canExecute;
readonly Action<T> _execute;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand&lt;T&gt;"/> class and the command can always be executed.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action<T> execute)
: this(execute, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand&lt;T&gt;"/> class.
/// Stops any outstanding asynchronous discovery requests.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<T> execute, Predicate<T> canExecute)
void StopDiscovery()
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
lock (this) {
if (discoveryClientProtocol != null) {
try {
discoveryClientProtocol.Abort();
} catch (NotImplementedException) {
} catch (ObjectDisposedException) {
// Receive this error if the url pointed to a file.
// The discovery client will already have closed the file
// so the abort fails.
}
#endregion
#region ICommand Members
public event EventHandler CanExecuteChanged
{
add
{
if (_canExecute != null)
CommandManager.RequerySuggested += value;
discoveryClientProtocol.Dispose();
}
remove
{
if (_canExecute != null)
CommandManager.RequerySuggested -= value;
discoveryClientProtocol = new WebServiceDiscoveryClientProtocol();
}
}
[DebuggerStepThrough]
public Boolean CanExecute(Object parameter)
{
return _canExecute == null ? true : _canExecute((T)parameter);
}
public void Execute(Object parameter)
void AuthenticateUser(Uri uri, string authenticationType)
{
_execute((T)parameter);
DiscoveryNetworkCredential credential = (DiscoveryNetworkCredential)credentialCache.GetCredential(uri, authenticationType);
if (credential != null) {
StartDiscovery(uri, credential);
} else {
using (UserCredentialsDialog credentialsForm = new UserCredentialsDialog(uri.ToString(), authenticationType)) {
// if (DialogResult.OK == credentialsForm.ShowDialog(WorkbenchSingleton.MainWin32Window)) {
// StartDiscovery(uri, credentialsForm.Credential);
// }
}
#endregion
}
/// <summary>
/// A command whose sole purpose is to relay its functionality to other objects by invoking delegates. The default return value for the CanExecute method is 'true'.
/// </summary>
public class RelayCommand : ICommand
{
#region Declarations
readonly Func<Boolean> _canExecute;
readonly Action _execute;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand&lt;T&gt;"/> class and the command can always be executed.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action execute)
: this(execute, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand&lt;T&gt;"/> class.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action execute, Func<Boolean> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
public event EventHandler CanExecuteChanged
{
add
{
if (_canExecute != null)
CommandManager.RequerySuggested += value;
}
remove
void AddCredential(Uri uri, DiscoveryNetworkCredential credential)
{
if (_canExecute != null)
CommandManager.RequerySuggested -= value;
NetworkCredential matchedCredential = credentialCache.GetCredential(uri, credential.AuthenticationType);
if (matchedCredential != null) {
credentialCache.Remove(uri, credential.AuthenticationType);
}
credentialCache.Add(uri, credential.AuthenticationType, credential);
}
[DebuggerStepThrough]
public Boolean CanExecute(Object parameter)
void DiscoveredWebServices(DiscoveryClientProtocol protocol)
{
return _canExecute == null ? true : _canExecute();
if (protocol != null) {
// addButton.Enabled = true;
// namespaceTextBox.Text = GetDefaultNamespace();
// referenceNameTextBox.Text = GetReferenceName();
// webServicesView.Add(GetServiceDescriptions(protocol));
// webReference = new WebReference(project, discoveryUri.AbsoluteUri, referenceNameTextBox.Text, namespaceTextBox.Text, protocol);
} else {
webReference = null;
// addButton.Enabled = false;
// webServicesView.Clear();
}
public void Execute(Object parameter)
{
_execute();
}
#endregion
}
}

2
src/Main/ICSharpCode.SharpDevelop.Widgets/Project/ICSharpCode.SharpDevelop.Widgets.csproj

@ -83,6 +83,7 @@ @@ -83,6 +83,7 @@
</Compile>
<Compile Include="Picker.cs" />
<Compile Include="RadioButtonGroup.cs" />
<Compile Include="RelayCommand.cs" />
<Compile Include="Resources\BitmapResources.cs" />
<Compile Include="SideBar\SideBar.cs">
<SubType>UserControl</SubType>
@ -95,6 +96,7 @@ @@ -95,6 +96,7 @@
</Compile>
<Compile Include="StackPanelWithSpacing.cs" />
<Compile Include="UniformGridWithSpacing.cs" />
<Compile Include="ViewModelBase.cs" />
<Compile Include="ZoomButtons.cs">
<DependentUpon>ZoomScrollViewer.xaml</DependentUpon>
</Compile>

158
src/Main/ICSharpCode.SharpDevelop.Widgets/Project/RelayCommand.cs

@ -0,0 +1,158 @@ @@ -0,0 +1,158 @@
/*
* Created by SharpDevelop.
* User: Peter Forstmeier
* Date: 16.10.2011
* Time: 19:40
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Diagnostics;
using System.Windows.Input;
namespace ICSharpCode.SharpDevelop.Widgets
{
/// <summary>
/// Description of RelayCommand.
/// </summary>
public class RelayCommand<T> : System.Windows.Input.ICommand
{
#region Declarations
readonly Predicate<T> _canExecute;
readonly Action<T> _execute;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand&lt;T&gt;"/> class and the command can always be executed.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action<T> execute)
: this(execute, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand&lt;T&gt;"/> class.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<T> execute, Predicate<T> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
public event EventHandler CanExecuteChanged
{
add
{
if (_canExecute != null)
CommandManager.RequerySuggested += value;
}
remove
{
if (_canExecute != null)
CommandManager.RequerySuggested -= value;
}
}
[DebuggerStepThrough]
public Boolean CanExecute(Object parameter)
{
return _canExecute == null ? true : _canExecute((T)parameter);
}
public void Execute(Object parameter)
{
_execute((T)parameter);
}
#endregion
}
/// <summary>
/// A command whose sole purpose is to relay its functionality to other objects by invoking delegates. The default return value for the CanExecute method is 'true'.
/// </summary>
public class RelayCommand : System.Windows.Input.ICommand
{
#region Declarations
readonly Func<Boolean> _canExecute;
readonly Action _execute;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand&lt;T&gt;"/> class and the command can always be executed.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action execute)
: this(execute, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand&lt;T&gt;"/> class.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action execute, Func<Boolean> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
public event EventHandler CanExecuteChanged
{
add
{
if (_canExecute != null)
CommandManager.RequerySuggested += value;
}
remove
{
if (_canExecute != null)
CommandManager.RequerySuggested -= value;
}
}
[DebuggerStepThrough]
public Boolean CanExecute(Object parameter)
{
return _canExecute == null ? true : _canExecute();
}
public void Execute(Object parameter)
{
_execute();
}
#endregion
}
}

79
src/Main/ICSharpCode.SharpDevelop.Widgets/Project/ViewModelBase.cs

@ -0,0 +1,79 @@ @@ -0,0 +1,79 @@
/*
* Created by SharpDevelop.
* User: Peter Forstmeier
* Date: 16.10.2011
* Time: 19:35
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;
namespace ICSharpCode.SharpDevelop.Widgets
{
/// <summary>
/// Description of ViewModelBase.
/// </summary>
public class ViewModelBase:INotifyPropertyChanged
{
public ViewModelBase()
{
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, e);
}
}
protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpresssion)
{
var propertyName = ExtractPropertyName(propertyExpresssion);
this.RaisePropertyChanged(propertyName);
}
protected void RaisePropertyChanged(String propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
private static String ExtractPropertyName<T>(Expression<Func<T>> propertyExpresssion)
{
if (propertyExpresssion == null)
{
throw new ArgumentNullException("propertyExpresssion");
}
var memberExpression = propertyExpresssion.Body as MemberExpression;
if (memberExpression == null)
{
throw new ArgumentException("The expression is not a member access expression.", "propertyExpresssion");
}
var property = memberExpression.Member as PropertyInfo;
if (property == null)
{
throw new ArgumentException("The member access expression does not access a property.", "propertyExpresssion");
}
var getMethod = property.GetGetMethod(true);
if (getMethod.IsStatic)
{
throw new ArgumentException("The referenced property is a static property.", "propertyExpresssion");
}
return memberExpression.Member.Name;
}
}
}
Loading…
Cancel
Save