~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.WebReferences/MonoDevelop.WebReferences.Dialogs/WCFConfigWidget.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// WCFConfigWidget.cs
 
3
//
 
4
// Author:
 
5
//       Martin Baulig <martin.baulig@xamarin.com>
 
6
//
 
7
// Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using System.Linq;
 
28
using System.Collections.Generic;
 
29
using MonoDevelop.WebReferences.WCF;
 
30
using System.ServiceModel.Description;
 
31
using Gtk;
 
32
 
 
33
namespace MonoDevelop.WebReferences.Dialogs
 
34
{
 
35
        [System.ComponentModel.ToolboxItem(true)]
 
36
        public partial class WCFConfigWidget : Gtk.Bin
 
37
        {
 
38
                public WCFConfigWidget (ClientOptions options)
 
39
                {
 
40
                        this.Options = options;
 
41
                        this.Build ();
 
42
                        
 
43
                        listTypes = new List<Type> (DefaultListTypes);
 
44
                        PopulateBox (listCollection, "List", listTypes);
 
45
                        
 
46
                        dictTypes = new List<Type> (DefaultDictionaryTypes);
 
47
                        PopulateBox (dictionaryCollection, "Dictionary", dictTypes);
 
48
                }
 
49
                
 
50
                public ClientOptions Options {
 
51
                        get; private set;
 
52
                }
 
53
                
 
54
                static readonly Type[] DefaultListTypes = {
 
55
                        typeof (Array),
 
56
                        typeof (System.Collections.Generic.LinkedList<>),
 
57
                        typeof (System.Collections.Generic.List<>),
 
58
                        typeof (System.Collections.ObjectModel.Collection<>),
 
59
                        typeof (System.Collections.ObjectModel.ObservableCollection<>),
 
60
                        typeof (System.ComponentModel.BindingList<>)
 
61
                };
 
62
                
 
63
                static readonly Type[] DefaultDictionaryTypes = {
 
64
                        typeof (System.Collections.Generic.Dictionary<,>),
 
65
                        typeof (System.Collections.Generic.SortedList<,>),
 
66
                        typeof (System.Collections.Generic.SortedDictionary<,>)
 
67
                };
 
68
                
 
69
                public bool Modified {
 
70
                        get;
 
71
                        private set;
 
72
                }
 
73
                
 
74
                List<Type> listTypes;
 
75
                List<Type> dictTypes;
 
76
                
 
77
                static bool? runtimeSupport;
 
78
                
 
79
                internal static bool IsSupported ()
 
80
                {
 
81
                        if (runtimeSupport != null)
 
82
                                return runtimeSupport.Value;
 
83
                        
 
84
                        try {
 
85
                                // Test runtime support.
 
86
                                var ms = new MetadataSet ();
 
87
                                var importer = new WsdlImporter (ms);
 
88
                                importer.State.GetType ();
 
89
                                runtimeSupport = true;
 
90
                                return true;
 
91
                        } catch {
 
92
                                runtimeSupport = false;
 
93
                                return false;
 
94
                        }
 
95
                }
 
96
                
 
97
                internal static bool IsSupported (WebReferenceItem item)
 
98
                {
 
99
                        if (!IsSupported ())
 
100
                                return false;
 
101
                        return item.MapFile.FilePath.Extension == ".svcmap";
 
102
                }
 
103
                
 
104
                internal static Type GetType (string name)
 
105
                {
 
106
                        var type = typeof (char).Assembly.GetType (name);
 
107
                        if (type != null)
 
108
                                return type;
 
109
                        type = typeof (System.Collections.Generic.LinkedList<>).Assembly.GetType (name);
 
110
                        if (type != null)
 
111
                                return type;
 
112
                        return null;
 
113
                }
 
114
                
 
115
                internal static string GetTypeName (Type type)
 
116
                {
 
117
                        var name = type.FullName;
 
118
                        var pos = name.IndexOf ("`");
 
119
                        if (pos < 0)
 
120
                                return name;
 
121
                        return name.Substring (0, pos);
 
122
                }
 
123
                
 
124
                void PopulateBox (ComboBox box, string category, List<Type> types)
 
125
                {
 
126
                        var mapping = Options.CollectionMappings.FirstOrDefault (m => m.Category == category);
 
127
                        
 
128
                        Type current = null;
 
129
                        if (mapping != null)
 
130
                                current = GetType (mapping.TypeName);
 
131
                        if (current == null)
 
132
                                current = types [0];
 
133
                        
 
134
                        if (!types.Contains (current))
 
135
                                types.Add (current);
 
136
                        
 
137
                        foreach (var type in types)
 
138
                                box.AppendText (GetTypeName (type));
 
139
                        
 
140
                        box.Active = types.IndexOf (current);
 
141
                }
 
142
                
 
143
                void UpdateBox (ComboBox box, string category, List<Type> types)
 
144
                {
 
145
                        var mapping = Options.CollectionMappings.FirstOrDefault (m => m.Category == category);
 
146
                        if (mapping == null) {
 
147
                                mapping = new CollectionMapping { Category = category };
 
148
                                Options.CollectionMappings.Add (mapping);
 
149
                                Modified = true;
 
150
                        }
 
151
                        
 
152
                        var current = types [box.Active];
 
153
                        if (mapping.TypeName != current.FullName) {
 
154
                                mapping.TypeName = current.FullName;
 
155
                                Modified = true;
 
156
                        }
 
157
                }
 
158
                
 
159
                internal void Update ()
 
160
                {
 
161
                        UpdateBox (listCollection, "List", listTypes);
 
162
                        UpdateBox (dictionaryCollection, "Dictionary", dictTypes);
 
163
                }
 
164
        }
 
165
}
 
166