Browse Source

create List<T> for data binding from service to TreeView

Treeview DataBinding in AddServiceReferenceDialog.xaml
pull/23/head
PeterForstmeier 14 years ago
parent
commit
2e44388c33
  1. 59
      src/Main/Base/Project/Src/Gui/Dialogs/ReferenceDialog/AddServiceReferenceDialog.xaml
  2. 12
      src/Main/Base/Project/Src/Gui/Dialogs/ReferenceDialog/AddServiceReferenceDialog.xaml.cs
  3. 203
      src/Main/Base/Project/Src/Gui/Dialogs/ReferenceDialog/AddServiceReferenceViewModel.cs

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

@ -6,12 +6,32 @@ @@ -6,12 +6,32 @@
xmlns:widgets="http://icsharpcode.net/sharpdevelop/widgets"
xmlns:core="http://icsharpcode.net/sharpdevelop/core"
xmlns:src="clr-namespace:Gui.Dialogs.ReferenceDialog"
xmlns:web="clr-namespace:System.Web.Services.Description;assembly=System.Web.Services"
WindowStartupLocation="CenterOwner"
Style="{x:Static core:GlobalStyles.DialogWindowStyle}"
Height="425"
Width="500"
Title="{Binding Title}">
<!-- http://blogs.msdn.com/b/chkoenig/archive/2008/05/24/hierarchical-databinding-in-wpf.aspx
http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx?msg=3663052
-->
<Window.Resources>
<!--
<DataTemplate x:Key="ServiceTemplate">
<TextBlock Text="{Binding Path=Name}" ></TextBlock>
</DataTemplate>
ItemTemplate="{StaticResource ServiceTemplate}">-->
<HierarchicalDataTemplate x:Key="HeaderTemplate"
ItemsSource="{Binding SubItems}">
<TextBlock Grid.Row="0" Text="{Binding Path=Name}" />
</HierarchicalDataTemplate>
</Window.Resources>
<Grid Name="grid" ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition
@ -43,10 +63,6 @@ @@ -43,10 +63,6 @@
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<!-- editable combo
http://stackoverflow.com/questions/582232/wpf-editable-combobox
http://stackoverflow.com/questions/3373239/wpf-editable-combobox
-->
<ComboBox Grid.Column="0" Margin="0,4,12,12"
IsEditable="True"
ItemsSource="{Binding MruServices}"
@ -74,29 +90,26 @@ http://stackoverflow.com/questions/3373239/wpf-editable-combobox @@ -74,29 +90,26 @@ http://stackoverflow.com/questions/3373239/wpf-editable-combobox
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!-- http://www.c-sharpcorner.com/UploadFile/mahesh/WPFTreeView08202008231544PM/WPFTreeView.aspx-->
<TreeView HorizontalAlignment="Left"
VerticalAlignment="Top" >
<TreeViewItem Header="Service">
<!--
<TreeViewItem Header="Coke"></TreeViewItem>
<TreeViewItem Header="Pepsi"></TreeViewItem>
<TreeViewItem Header="Orange Juice"></TreeViewItem>
<TreeViewItem Header="Milk"></TreeViewItem>
<TreeViewItem Header="Iced Tea"></TreeViewItem>
<TreeViewItem Header="Mango Shake"></TreeViewItem>
-->
</TreeViewItem>
<TreeView Name="tree" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
ItemsSource="{Binding Path=ServiceDescriptionCollection}"
ItemTemplate="{StaticResource HeaderTemplate}"
PreviewMouseDoubleClick="TreeView_PreviewMouseDoubleClick">
</TreeView>
-->
<TreeView Name="tree" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
ItemsSource="{Binding Path=Items}"
ItemTemplate="{StaticResource HeaderTemplate}"
SelectedItemChanged="Tree_SelectedItemChanged">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}" >
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
<GridSplitter Grid.Column="0" Width="2" Background="Black"/>
<Label Grid.Column="1"/>

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

@ -45,7 +45,6 @@ namespace Gui.Dialogs.ReferenceDialog @@ -45,7 +45,6 @@ namespace Gui.Dialogs.ReferenceDialog
void Cbo_LostFocus(object sender, RoutedEventArgs e)
{
// http://stackoverflow.com/questions/3373239/wpf-editable-combobox
var comboBox = (ComboBox) sender;
if(comboBox.SelectedItem != null)
return;
@ -56,5 +55,16 @@ namespace Gui.Dialogs.ReferenceDialog @@ -56,5 +55,16 @@ namespace Gui.Dialogs.ReferenceDialog
}
void TreeView_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var d = tree.DataContext;
var it = tree.ItemTemplate;
}
void Tree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
MyItem m = (MyItem)e.NewValue;
}
}
}

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

@ -7,21 +7,25 @@ @@ -7,21 +7,25 @@
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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.Description;
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;
using Microsoft.Win32;
namespace Gui.Dialogs.ReferenceDialog
{
@ -35,6 +39,7 @@ namespace Gui.Dialogs.ReferenceDialog @@ -35,6 +39,7 @@ namespace Gui.Dialogs.ReferenceDialog
string noUrl = "Please enter the address of the Service.";
// string discoverMenu ="Services in Solution";
Uri discoveryUri;
public AddServiceReferenceViewModel(IProject project)
{
Project = project;
@ -131,6 +136,8 @@ namespace Gui.Dialogs.ReferenceDialog @@ -131,6 +136,8 @@ namespace Gui.Dialogs.ReferenceDialog
base.RaisePropertyChanged(() =>SelectedService);}
}
#endregion
#region Go
@ -175,13 +182,14 @@ namespace Gui.Dialogs.ReferenceDialog @@ -175,13 +182,14 @@ namespace Gui.Dialogs.ReferenceDialog
CredentialCache credentialCache = new CredentialCache();
WebServiceDiscoveryClientProtocol discoveryClientProtocol;
WebReference webReference;
ICSharpCode.SharpDevelop.Gui.WebReference webReference;
string namespacePrefix = String.Empty;
delegate DiscoveryDocument DiscoverAnyAsync(string url);
delegate void DiscoveredWebServicesHandler(DiscoveryClientProtocol protocol);
delegate void AuthenticationHandler(Uri uri, string authenticationType);
void StartDiscovery(Uri uri, DiscoveryNetworkCredential credential)
{
// Abort previous discovery.
@ -199,6 +207,8 @@ namespace Gui.Dialogs.ReferenceDialog @@ -199,6 +207,8 @@ namespace Gui.Dialogs.ReferenceDialog
/// Called after an asynchronous web services search has
/// completed.
/// </summary>
///
void DiscoveryCompleted(IAsyncResult result)
{
AsyncDiscoveryState state = (AsyncDiscoveryState)result.AsyncState;
@ -214,19 +224,19 @@ namespace Gui.Dialogs.ReferenceDialog @@ -214,19 +224,19 @@ namespace Gui.Dialogs.ReferenceDialog
DiscoveredWebServicesHandler handler = new DiscoveredWebServicesHandler(DiscoveredWebServices);
try {
DiscoverAnyAsync asyncDelegate = (DiscoverAnyAsync)((AsyncResult)result).AsyncDelegate;
DiscoveryDocument doc = asyncDelegate.EndInvoke(result);
DiscoveryDocument handlerdoc = asyncDelegate.EndInvoke(result);
if (!state.Credential.IsDefaultAuthenticationType) {
AddCredential(state.Uri, state.Credential);
}
// Invoke(handler, new object[] {protocol});
handler (protocol);
} catch (Exception ex) {
if (protocol.IsAuthenticationRequired) {
HttpAuthenticationHeader authHeader = protocol.GetAuthenticationHeader();
AuthenticationHandler authHandler = new AuthenticationHandler(AuthenticateUser);
// Invoke(authHandler, new object[] {state.Uri, authHeader.AuthenticationType});
// trouble Invoke(authHandler, new object[] {state.Uri, authHeader.AuthenticationType});
} else {
LoggingService.Error("DiscoveryCompleted", ex);
// Invoke(handler, new object[] {null});
// trouble Invoke(handler, new object[] {null});
}
}
}
@ -279,20 +289,185 @@ namespace Gui.Dialogs.ReferenceDialog @@ -279,20 +289,185 @@ namespace Gui.Dialogs.ReferenceDialog
credentialCache.Add(uri, credential.AuthenticationType, credential);
}
void DiscoveredWebServices(DiscoveryClientProtocol protocol)
{
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 {
ServiceDescriptionCollection = GetServiceDescriptions(protocol);
FillItems (ServiceDescriptionCollection);
var defaultNameSpace = GetDefaultNamespace();
var referenceName = GetReferenceName(discoveryUri);
webReference = new ICSharpCode.SharpDevelop.Gui.WebReference(project,
discoveryUri.AbsoluteUri,
defaultNameSpace,
referenceName, protocol);
}
else
{
webReference = null;
// addButton.Enabled = false;
// webServicesView.Clear();
}
}
/// <summary>
/// Gets the namespace to be used with the generated web reference code.
/// </summary>
string GetDefaultNamespace()
{
if (namespacePrefix.Length > 0 && discoveryUri != null) {
return String.Concat(namespacePrefix, ".", discoveryUri.Host);
} else if (discoveryUri != null) {
return discoveryUri.Host;
}
return String.Empty;
}
static string GetReferenceName(Uri uri)
{
if (uri != null) {
return uri.Host;
}
return String.Empty;
/*
if (discoveryUri != null) {
return discoveryUri.Host;
}
return String.Empty;*/
}
ServiceDescriptionCollection GetServiceDescriptions(DiscoveryClientProtocol protocol)
{
ServiceDescriptionCollection services = new ServiceDescriptionCollection();
protocol.ResolveOneLevel();
foreach (DictionaryEntry entry in protocol.References) {
ContractReference contractRef = entry.Value as ContractReference;
if (contractRef != null) {
services.Add(contractRef.Contract);
}
}
return services;
}
ServiceDescriptionCollection serviceDescriptionCollection;
public ServiceDescriptionCollection ServiceDescriptionCollection
{
get {return serviceDescriptionCollection;}
set {
if (serviceDescriptionCollection == null) {
serviceDescriptionCollection = new ServiceDescriptionCollection();
}
serviceDescriptionCollection = value;
var s = serviceDescriptionCollection[0];
RaisePropertyChanged(() =>ServiceDescriptionCollection);
}
}
#endregion
#region new binding
List<MyItem> items = new List <MyItem>();
object isSelected;
public object IsSelected {
get { return isSelected; }
set { isSelected = value; }
}
public List <MyItem> Items {
get {return items; }
set {
items = value;
base.RaisePropertyChanged(() =>Items);
}
}
void FillItems (ServiceDescriptionCollection descriptions)
{
foreach (ServiceDescription element in descriptions)
{
Add (element);
}
}
void Add(ServiceDescription description)
{
List<MyItem> l = new List<MyItem>();
var rootNode = new MyItem(GetName(description));
rootNode.Tag = description;
l.Add(rootNode);
foreach(Service service in description.Services) {
var serviceNode = new MyItem(service.Name);
serviceNode.Tag = service;
rootNode.SubItems.Add(serviceNode);
foreach(Port port in service.Ports) {
var portNode = new MyItem(port.Name);
portNode.Tag = port;
serviceNode.SubItems.Add(portNode);
// Get the operations
System.Web.Services.Description.Binding binding = description.Bindings[port.Binding.Name];
if (binding != null) {
PortType portType = description.PortTypes[binding.Type.Name];
if (portType != null) {
foreach(Operation operation in portType.Operations) {
var operationNode = new MyItem(operation.Name);
operationNode.Tag = operation;
// operationNode.ImageIndex = OperationImageIndex;
// operationNode.SelectedImageIndex = OperationImageIndex;
portNode.SubItems.Add(operationNode);
}
}
}
}
}
Items = l;
}
string GetName(ServiceDescription description)
{
if (description.Name != null) {
return description.Name;
} else if (description.RetrievalUrl != null) {
Uri uri = new Uri(description.RetrievalUrl);
if (uri.Segments.Length > 0) {
return uri.Segments[uri.Segments.Length - 1];
} else {
return uri.Host;
}
}
return String.Empty;
}
#endregion
}
public class MyItem
{
public MyItem (string name)
{
this.Name = name;
SubItems = new List<MyItem>();
}
public string Name {get;set;}
public object Tag {get;set;}
public List<MyItem> SubItems {get;set;}
}
}

Loading…
Cancel
Save