~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui/DisplayBindingService.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
using Mono.Addins;
33
33
 
34
34
using MonoDevelop.Ide.Codons;
 
35
using MonoDevelop.Core;
 
36
using MonoDevelop.Projects;
35
37
 
36
38
namespace MonoDevelop.Ide.Gui
37
39
{
38
40
        public static class DisplayBindingService
39
41
        {
40
 
                static List<DisplayBindingCodon> displayBindings = new List<DisplayBindingCodon> ();
41
 
                
42
 
                static DisplayBindingService ()
43
 
                {
44
 
                        //FIXME: this ignores node ordering
45
 
                        AddinManager.AddExtensionNodeHandler ("/MonoDevelop/Ide/DisplayBindings", delegate(object sender, ExtensionNodeEventArgs args) {
46
 
                                DisplayBindingCodon displayBindingCodon = (DisplayBindingCodon)args.ExtensionNode;
47
 
                                switch (args.Change) {
48
 
                                case ExtensionChange.Add:
49
 
                                        displayBindings.Add (displayBindingCodon);
50
 
                                        break;
51
 
                                case ExtensionChange.Remove:
52
 
                                        displayBindings.Remove (displayBindingCodon);
53
 
                                        break;
 
42
                static IEnumerable<T> GetBindings<T> ()
 
43
                {
 
44
                        return AddinManager.GetExtensionObjects ("/MonoDevelop/Ide/DisplayBindings")
 
45
                                .OfType<T> ();
 
46
                }
 
47
                
 
48
                internal static IEnumerable<IDisplayBinding> GetDisplayBindings (FilePath filePath, string mimeType, Project ownerProject)
 
49
                {
 
50
                        if (mimeType == null && !filePath.IsNullOrEmpty)
 
51
                                mimeType = DesktopService.GetMimeTypeForUri (filePath);
 
52
                        
 
53
                        foreach (var b in GetBindings<IDisplayBinding> ()) {
 
54
                                if (b.CanHandle (filePath, mimeType, ownerProject))
 
55
                                        yield return b;
 
56
                        }
 
57
                }
 
58
                
 
59
                public static IViewDisplayBinding GetDefaultViewBinding (FilePath filePath, string mimeType, Project ownerProject)
 
60
                {
 
61
                        return GetDisplayBindings (filePath, mimeType, ownerProject).OfType<IViewDisplayBinding> ()
 
62
                                .FirstOrDefault (d => d.CanUseAsDefault);
 
63
                }
 
64
                
 
65
                public static IDisplayBinding GetDefaultBinding (FilePath filePath, string mimeType, Project ownerProject)
 
66
                {
 
67
                        return GetDisplayBindings (filePath, mimeType, ownerProject).FirstOrDefault (d => d.CanUseAsDefault);
 
68
                }
 
69
                
 
70
                public static void AttachSubWindows (IWorkbenchWindow workbenchWindow)
 
71
                {
 
72
                        foreach (var b in GetBindings<IAttachableDisplayBinding> ()) {
 
73
                                if (b.CanAttachTo (workbenchWindow.ViewContent)) 
 
74
                                        workbenchWindow.AttachViewContent (b.CreateViewContent (workbenchWindow.ViewContent));
 
75
                        }
 
76
                }
 
77
 
 
78
                public static IEnumerable<FileViewer> GetFileViewers (FilePath filePath, Project ownerProject)
 
79
                {
 
80
                        string mimeType = DesktopService.GetMimeTypeForUri (filePath);
 
81
                        var viewerIds = new HashSet<string> ();
 
82
                        
 
83
                        foreach (var b in GetDisplayBindings (filePath, mimeType, ownerProject)) {
 
84
                                var vb = b as IViewDisplayBinding;
 
85
                                if (vb != null) {
 
86
                                        yield return new FileViewer (vb);
 
87
                                } else {
 
88
                                        var eb = (IExternalDisplayBinding) b;
 
89
                                        var app = eb.GetApplication (filePath, mimeType, ownerProject);
 
90
                                        if (viewerIds.Add (app.Id))
 
91
                                                yield return new FileViewer (app);
54
92
                                }
55
 
                        });
56
 
                }
57
 
                
58
 
                static IEnumerable<IDisplayBinding> RealDisplayBindings {
59
 
                        get {
60
 
                                return displayBindings.Select (d => d.DisplayBinding).OfType<IDisplayBinding> ();
61
 
                        }
62
 
                }
63
 
                
64
 
                public static IDisplayBinding GetDefaultBindingForUri (string uri)
65
 
                {
66
 
                        return GetDefaultBinding (uri, null);
67
 
                }
68
 
                
69
 
                public static IDisplayBinding GetDefaultBinding (string uri, string mimeType)
70
 
                {
71
 
                        return RealDisplayBindings.FirstOrDefault (binding =>
72
 
                                binding.CanUseAsDefault && (
73
 
                                        (uri != null && binding.CanCreateContentForUri (uri))
74
 
                                        || (mimeType != null && binding.CanCreateContentForMimeType (mimeType))));
75
 
                }
76
 
                
77
 
                public static IEnumerable<IDisplayBinding> GetBindingsForMimeType (string mimeType)
78
 
                {
79
 
                        return RealDisplayBindings.Where (b => b.CanCreateContentForMimeType (mimeType));
80
 
                }
81
 
                
82
 
                public static void AttachSubWindows (IWorkbenchWindow workbenchWindow)
83
 
                {
84
 
                        foreach (DisplayBindingCodon codon in displayBindings) {
85
 
                                IAttachableDisplayBinding binding = codon.DisplayBinding as IAttachableDisplayBinding;
86
 
                                if (binding != null && binding.CanAttachTo (workbenchWindow.ViewContent)) 
87
 
                                        workbenchWindow.AttachViewContent (binding.CreateViewContent (workbenchWindow.ViewContent));
88
 
                        }
89
 
                }
90
 
        }
91
 
}
 
93
                        }
 
94
 
 
95
                        foreach (var app in DesktopService.GetApplications (filePath))
 
96
                                if (viewerIds.Add (app.Id))
 
97
                                        yield return new FileViewer (app);
 
98
                }
 
99
        }
 
100
        
 
101
        //dummy binding, anchor point for extension tree
 
102
        class DefaultDisplayBinding : IViewDisplayBinding
 
103
        {
 
104
                public IViewContent CreateContent (FilePath fileName, string mimeType, Project ownerProject)
 
105
                {
 
106
                        throw new InvalidOperationException ();
 
107
                }
 
108
 
 
109
                public string Name {
 
110
                        get { return null; }
 
111
                }
 
112
 
 
113
                public bool CanHandle (FilePath fileName, string mimeType, Project ownerProject)
 
114
                {
 
115
                        return false;
 
116
                }
 
117
 
 
118
                public bool CanUseAsDefault {
 
119
                        get { return false; }
 
120
                }
 
121
        }
 
122
}
 
 
b'\\ No newline at end of file'