~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Main/Base/Project/Src/Services/DisplayBinding/DisplayBindingDescriptor.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.Text.RegularExpressions;
 
6
using ICSharpCode.Core;
 
7
 
 
8
namespace ICSharpCode.SharpDevelop
 
9
{
 
10
        public class DisplayBindingDescriptor
 
11
        {
 
12
                object binding;
 
13
                bool isSecondary;
 
14
                Codon codon;
 
15
                
 
16
                /// <summary>
 
17
                /// Gets the IDisplayBinding or ISecondaryDisplayBinding if it is already loaded,
 
18
                /// otherwise returns null.
 
19
                /// </summary>
 
20
                internal object GetLoadedBinding()
 
21
                {
 
22
                        return binding;
 
23
                }
 
24
                
 
25
                public IDisplayBinding Binding {
 
26
                        get {
 
27
                                if (codon != null && binding == null) {
 
28
                                        binding = codon.AddIn.CreateObject(codon.Properties["class"]);
 
29
                                }
 
30
                                return binding as IDisplayBinding;
 
31
                        }
 
32
                }
 
33
                
 
34
                public ISecondaryDisplayBinding SecondaryBinding {
 
35
                        get {
 
36
                                if (codon != null && binding == null) {
 
37
                                        binding = codon.AddIn.CreateObject(codon.Properties["class"]);
 
38
                                }
 
39
                                return binding as ISecondaryDisplayBinding;
 
40
                        }
 
41
                }
 
42
                
 
43
                public bool IsSecondary {
 
44
                        get { return isSecondary; }
 
45
                }
 
46
                
 
47
                public string Id { get; set; }
 
48
                public string Title { get; set; }
 
49
                public string FileNameRegex { get; set; }
 
50
                
 
51
                public DisplayBindingDescriptor(Codon codon)
 
52
                {
 
53
                        if (codon == null)
 
54
                                throw new ArgumentNullException("codon");
 
55
                        
 
56
                        isSecondary = codon.Properties["type"] == "Secondary";
 
57
                        if (!isSecondary && codon.Properties["type"] != "" && codon.Properties["type"] != "Primary")
 
58
                                MessageService.ShowWarning("Unknown display binding type: " + codon.Properties["type"]);
 
59
                        
 
60
                        this.codon = codon;
 
61
                        this.Id = codon.Id;
 
62
                        
 
63
                        string title = codon.Properties["title"];
 
64
                        if (string.IsNullOrEmpty(title))
 
65
                                this.Title = codon.Id;
 
66
                        else
 
67
                                this.Title = title;
 
68
                        
 
69
                        this.FileNameRegex = codon.Properties["fileNamePattern"];
 
70
                }
 
71
                
 
72
                public DisplayBindingDescriptor(IDisplayBinding binding)
 
73
                {
 
74
                        if (binding == null)
 
75
                                throw new ArgumentNullException("binding");
 
76
                        
 
77
                        this.isSecondary = false;
 
78
                        this.binding = binding;
 
79
                }
 
80
                
 
81
                public DisplayBindingDescriptor(ISecondaryDisplayBinding binding)
 
82
                {
 
83
                        if (binding == null)
 
84
                                throw new ArgumentNullException("binding");
 
85
                        
 
86
                        this.isSecondary = true;
 
87
                        this.binding = binding;
 
88
                }
 
89
                
 
90
                /// <summary>
 
91
                /// Gets if the display binding can possibly open the file.
 
92
                /// If this method returns false, it cannot open it; if the method returns
 
93
                /// true, it *might* open it.
 
94
                /// Call Binding.CanCreateContentForFile() to know for sure if the binding
 
95
                /// will open the file.
 
96
                /// </summary>
 
97
                /// <remarks>
 
98
                /// This method is used to skip loading addins like the ResourceEditor which cannot
 
99
                /// attach to a certain file name for sure.
 
100
                /// </remarks>
 
101
                public bool CanOpenFile(string fileName)
 
102
                {
 
103
                        string fileNameRegex = StringParser.Parse(this.FileNameRegex);
 
104
                        if (fileNameRegex == null || fileNameRegex.Length == 0) // no regex specified
 
105
                                return true;
 
106
                        if (fileName == null) // regex specified but file has no name
 
107
                                return false;
 
108
                        return Regex.IsMatch(fileName, fileNameRegex, RegexOptions.IgnoreCase);
 
109
                }
 
110
                
 
111
                public override string ToString()
 
112
                {
 
113
                        return string.Format("[DisplayBindingDescriptor Id={1} Binding={0}]", binding, Id);
 
114
                }
 
115
 
 
116
        }
 
117
}