~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Services/ChooseClass.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.ComponentModel;
 
7
using System.Windows.Data;
 
8
using System.IO;
 
9
using System.Reflection;
 
10
 
 
11
namespace ICSharpCode.WpfDesign.Designer.Services
 
12
{
 
13
        public class ChooseClass : INotifyPropertyChanged
 
14
        {
 
15
                public ChooseClass(IEnumerable<Assembly> assemblies)
 
16
                {
 
17
                        foreach (var a in assemblies) {
 
18
                                foreach (var t in a.GetExportedTypes()) {
 
19
                                        if (t.IsClass) {
 
20
                                                if (t.IsAbstract) continue;
 
21
                                                if (t.IsNested) continue;
 
22
                                                if (t.IsGenericTypeDefinition) continue;
 
23
                                                if (t.GetConstructor(Type.EmptyTypes) == null) continue;
 
24
                                                projectClasses.Add(t);
 
25
                                        }
 
26
                                }
 
27
                        }
 
28
 
 
29
                        projectClasses.Sort((c1, c2) => c1.Name.CompareTo(c2.Name));
 
30
                        classes = new ListCollectionView(projectClasses);
 
31
                        classes.Filter = FilterPredicate;
 
32
                }
 
33
 
 
34
                List<Type> projectClasses = new List<Type>();           
 
35
 
 
36
                ListCollectionView classes;
 
37
 
 
38
                public ICollectionView Classes {
 
39
                        get { return classes; }
 
40
                }
 
41
 
 
42
                string filter;
 
43
 
 
44
                public string Filter {
 
45
                        get {
 
46
                                return filter;
 
47
                        }
 
48
                        set {
 
49
                                filter = value;
 
50
                                Classes.Refresh();
 
51
                                RaisePropertyChanged("Filter");
 
52
                        }
 
53
                }
 
54
 
 
55
                bool showSystemClasses;
 
56
 
 
57
                public bool ShowSystemClasses {
 
58
                        get {
 
59
                                return showSystemClasses;
 
60
                        }
 
61
                        set {
 
62
                                showSystemClasses = value;
 
63
                                Classes.Refresh();
 
64
                                RaisePropertyChanged("ShowSystemClasses");
 
65
                        }
 
66
                }
 
67
 
 
68
                public Type CurrentClass {
 
69
                        get { return Classes.CurrentItem as Type; }
 
70
                }
 
71
 
 
72
                bool FilterPredicate(object item)
 
73
                {
 
74
                        Type c = item as Type;
 
75
                        if (!ShowSystemClasses) {
 
76
                                if (c.Namespace.StartsWith("System") || c.Namespace.StartsWith("Microsoft")) {
 
77
                                        return false;
 
78
                                }
 
79
                        }
 
80
                        return Match(c.Name, Filter);
 
81
                }
 
82
 
 
83
                static bool Match(string className, string filter)
 
84
                {
 
85
                        if (string.IsNullOrEmpty(filter))
 
86
                                return true;
 
87
                        else
 
88
                                return className.StartsWith(filter, StringComparison.InvariantCultureIgnoreCase);
 
89
                }
 
90
 
 
91
                #region INotifyPropertyChanged Members
 
92
                public event PropertyChangedEventHandler PropertyChanged;
 
93
 
 
94
                void RaisePropertyChanged(string name)
 
95
                {
 
96
                        if (PropertyChanged != null) {
 
97
                                PropertyChanged(this, new PropertyChangedEventArgs(name));
 
98
                        }
 
99
                }
 
100
                #endregion
 
101
        }
 
102
}